Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for "PDF document data" metabox #757

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
72c030f
Move PDF document data markup to views
MohamadNateqi Mar 28, 2024
69e9b3d
WPCS
MohamadNateqi Mar 28, 2024
34ee8d1
Update and improvements for metabox markup
MohamadNateqi Mar 28, 2024
e39996f
Add auto update documents data using heartbeat
MohamadNateqi Mar 28, 2024
0058f6f
Generate minified JS and CSS files
MohamadNateqi Mar 28, 2024
ac6ec0d
Fix nonce validation
MohamadNateqi Apr 3, 2024
6ecfebe
Validate the document type
MohamadNateqi Apr 3, 2024
2251d16
Improve performance
MohamadNateqi Apr 3, 2024
00c299c
Better sanitize
MohamadNateqi Apr 3, 2024
ed6bc11
WPCS
MohamadNateqi Apr 3, 2024
7fd96b7
Add DocBlock
MohamadNateqi Apr 3, 2024
75c411b
Improvement
MohamadNateqi Apr 3, 2024
acc87cb
Fix bugs
MohamadNateqi Apr 3, 2024
2b66b21
Indentation fixes
alexmigf Apr 3, 2024
46ee270
Improve the meta box method to print all documents data,
MohamadNateqi Apr 9, 2024
bcbb368
Update version
MohamadNateqi Apr 9, 2024
969f928
Merge branch 'main' into document-data-metabox
alexmigf Apr 10, 2024
69e812e
Rename a parameter
MohamadNateqi Apr 10, 2024
146180c
Fix HPOS compatibility
MohamadNateqi Apr 10, 2024
0d02eda
Fix input width issue on Firefox
MohamadNateqi Apr 10, 2024
fedccf6
Merge branch 'main' into document-data-metabox
MohamadNateqi Apr 10, 2024
b407d25
Merge remote-tracking branch 'origin/document-data-metabox' into docu…
MohamadNateqi Apr 10, 2024
8fa23bf
Generate minified JS and CSS files
MohamadNateqi Apr 10, 2024
ab89e8c
Fix a bug
MohamadNateqi Apr 10, 2024
d19708a
Rename a parameter
MohamadNateqi Apr 12, 2024
bcccb26
Fix not removing dependent document
MohamadNateqi Apr 12, 2024
f9cbcca
Check if we have an order ID
MohamadNateqi Apr 12, 2024
8f7c72c
Better name for a hook
MohamadNateqi Apr 12, 2024
db3c341
Use another AS method
MohamadNateqi Apr 12, 2024
46e8aa9
Generate minified JS and CSS files
MohamadNateqi Apr 12, 2024
a13139e
Merge branch 'main' into document-data-metabox
MohamadNateqi Apr 12, 2024
813e746
Add document number to output
MohamadNateqi Apr 15, 2024
e424202
Revert outputting all documents in the meta box
MohamadNateqi Apr 15, 2024
721ef11
Merge branch 'main' into document-data-metabox
MohamadNateqi Apr 15, 2024
a58ff0f
Improvements for the default data labels
MohamadNateqi Apr 15, 2024
df58528
WPCS
MohamadNateqi Apr 16, 2024
0bbec3f
Improvement
MohamadNateqi Apr 16, 2024
55b47a8
Update the usage of a filter
MohamadNateqi Apr 16, 2024
a6c5927
Introducing a new propery in Order_Document class to store prerequisi…
MohamadNateqi Apr 16, 2024
48d0371
Check document prerequisites in `is_allowed()` method
MohamadNateqi Apr 17, 2024
2a9d4e9
Introduce the new `do_prerequisites_exist()` method
MohamadNateqi Apr 17, 2024
2393848
Remove an unnecessary filter
MohamadNateqi Apr 17, 2024
8c6ed82
Update `pdf_actions_meta_box()` method
MohamadNateqi Apr 17, 2024
c391c99
Updates
MohamadNateqi Apr 17, 2024
6e4349d
Merge branch 'main' into document-data-metabox
MohamadNateqi Apr 17, 2024
08cff80
Revert changes and improvements
MohamadNateqi Apr 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/js/order-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ jQuery( function( $ ) {

// fetch data for pending documents if documents are generated
$( document ).on( 'heartbeat-tick', function( e, data ) {
if (!data.wpo_ready_documents) {
if ( ! data.wpo_ready_documents ) {
return;
}

Expand Down
34 changes: 22 additions & 12 deletions includes/class-wcpdf-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1300,47 +1300,57 @@ public function handle_heartbeat_received( array $response, array $data ): array
return $response;
}

/**
* Updates documents data in the "PDF document data" meta box if the generation in the background is finished.
*
* @return void
*/
public function ajax_fetch_pdf_document_data(): void {
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
if ( isset( $_REQUEST['nonce'] ) && wp_verify_nonce( $_REQUEST['nonce'], 'generate_wpo_wcpdf' ) ) {
if ( ! isset( $_REQUEST['nonce'] ) || wp_verify_nonce( $_REQUEST['nonce'], 'generate_wpo_wcpdf' ) ) {
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
wp_send_json_error( array(
'message' => esc_html__( 'Nonce expired!', 'woocommerce-pdf-invoices-packing-slips' ),
'message' => esc_html__( 'Invalid or expired nonce!', 'woocommerce-pdf-invoices-packing-slips' ),
) );
}

if ( empty( $_REQUEST['document_types'] ) || empty( $_REQUEST['order_id'] ) ) {
if ( empty( $_REQUEST['document_types'] ) || empty( (int) $_REQUEST['order_id'] ) ) {
wp_send_json_error( array(
'message' => esc_html__( 'Incomplete request!', 'woocommerce-pdf-invoices-packing-slips' ),
'message' => esc_html__( 'Incomplete or incorrect request!', 'woocommerce-pdf-invoices-packing-slips' ),
) );
}

$document_types = $_REQUEST['document_types'];
$order_id = (int) $_REQUEST['order_id'];
$document_types = array_map( 'sanitize_text_field', $_REQUEST['document_types'] );
$data['invoice'] = array(
'number' => array( 'label' => __( 'Invoice number:', 'woocommerce-pdf-invoices-packing-slips' ) ),
'date' => array( 'label' => __( 'Invoice date:', 'woocommerce-pdf-invoices-packing-slips' ) ),
'display_date' => array( 'label' => __( 'Invoice display date:', 'woocommerce-pdf-invoices-packing-slips' ) ),
'creation_trigger' => array( 'label' => __( 'Invoice created via:', 'woocommerce-pdf-invoices-packing-slips' ) ),
'notes' => array( 'label' => __( 'Notes (printed in the invoice):', 'woocommerce-pdf-invoices-packing-slips' ) ),
);
$data = apply_filters( 'wpo_wcpdf_ajax_fetch_pdf_document_data_fields', $data, $_REQUEST['order_id'] );
$data = apply_filters( 'wpo_wcpdf_ajax_fetch_pdf_document_data_fields', $data, $order_id );

ob_start();
foreach ( $document_types as $document_type ) {
if ( ! in_array( $document_type, array_keys( $data ), true ) ) {
if ( ! wpo_wcpdf_is_document_type_valid( $document_type ) || ! in_array( $document_type, array_keys( $data ), true ) ) {
continue;
}

$document = wcpdf_get_document( $document_type, wc_get_order( $_REQUEST['order_id'] ) );
$document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) );
if ( $document && $document->exists() ) {
ob_start();
$this->output_number_date_edit_fields( $document, $data[ $document_type ] );
$documents_data[ $document_type ] = ob_get_clean();
$documents_data[ $document_type ] = ob_get_contents();
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
ob_clean();
}
}
ob_end_clean();

if ( ! empty ( $documents_data ) ) {
if ( ! empty( $documents_data ) ) {
wp_send_json_success( $documents_data );
}

wp_die();
wp_send_json_error( array(
'message' => esc_html__( 'Documents data is empty!', 'woocommerce-pdf-invoices-packing-slips' ),
) );
}

}
Expand Down
18 changes: 18 additions & 0 deletions includes/wcpdf-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,21 @@ function wpo_wcpdf_parse_document_date_for_wp_query( array $wp_query_args, mixed
return $wp_query_args;
}

/**
* Validates the given document type against the currently defined documents.
*
* @param string $document_type
*
* @return bool
*/
function wpo_wcpdf_is_document_type_valid( string $document_type ): bool {
$documents = WPO_WCPDF()->documents->get_documents();

foreach ( $documents as $document ) {
if ( $document_type === $document->get_type() ) {
return true;
}
}

return false;
}
Loading