From 72c030f7e573b6c4ae7c625ea6c754a1c1eadf25 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Thu, 28 Mar 2024 17:44:16 +0330 Subject: [PATCH 01/40] Move PDF document data markup to views --- includes/class-wcpdf-admin.php | 144 +---------------------- includes/views/document-data-metabox.php | 137 +++++++++++++++++++++ 2 files changed, 143 insertions(+), 138 deletions(-) create mode 100644 includes/views/document-data-metabox.php diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index d0473044f..45f69cfe5 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -794,146 +794,14 @@ public function output_number_date_edit_fields( $document, $data ) { if ( empty( $document ) || empty( $data ) || empty( $document->order ) || ! is_callable( array( $document->order, 'get_id' ) ) ) { return; } - $data = $this->get_current_values_for_document( $document, $data ); - ?> -
-
- -

- get_title() ); ?> - exists() && ( isset( $data['number'] ) || isset( $data['date'] ) ) && $this->user_can_manage_document( $document->get_type() ) ) : ?> - - "> - - -

- - -
- exists() ) : ?> - -
-

-

- - -

-

-
- - -
-

-

- - -

-

-
- - - -
- - -
- - order ); ?> - - - user_can_manage_document( $document->get_type() ) ) { - printf( - '%s', - sprintf( - /* translators: document title */ - esc_html__( 'Set %s number & date', 'woocommerce-pdf-invoices-packing-slips' ), - wp_kses_post( $document->get_title() ) - ) - ); - } else { - printf( '

%s

', esc_html__( 'You do not have sufficient permissions to edit this document.', 'woocommerce-pdf-invoices-packing-slips' ) ); - } - ?> - - -
- - -
- -

- - () -

- - -

- - @: -

- -
- - - - order ); ?> + $data = $this->get_current_values_for_document( $document, $data ); + $in_process = as_has_scheduled_action( 'wpo_wcpdf_generate_document_on_order_status', array( + 'document_type' => $document->get_type(), + 'order_id' => $document->order->get_id() + ) ); - -
- - user_can_manage_document( $document->get_type() ) ) : ?> - - -

-
- -
-

- -

-

-
- - order ); ?> - - - - -
- - -
-
- " data-action="save"> - -
-
- -
- plugin_path() . '/includes/views/document-data-metabox.php'; } public function add_regenerate_document_button( $document ) { diff --git a/includes/views/document-data-metabox.php b/includes/views/document-data-metabox.php new file mode 100644 index 000000000..89988ddf0 --- /dev/null +++ b/includes/views/document-data-metabox.php @@ -0,0 +1,137 @@ +
+
+ +

+ get_title() ); ?> + exists() && ( isset( $data['number'] ) || isset( $data['date'] ) ) && $this->user_can_manage_document( $document->get_type() ) ) : ?> + + "> + + +

+ + +
+ exists() ) : ?> + +
+

+

+ + +

+

+
+ + +
+

+

+ + +

+

+
+ + + +
+ + +
+ + order ); ?> + + + user_can_manage_document( $document->get_type() ) ) { + printf( + '%s', + sprintf( + /* translators: document title */ + esc_html__( 'Set %s number & date', 'woocommerce-pdf-invoices-packing-slips' ), + wp_kses_post( $document->get_title() ) + ) + ); + } else { + printf( '

%s

', esc_html__( 'You do not have sufficient permissions to edit this document.', 'woocommerce-pdf-invoices-packing-slips' ) ); + } + ?> + + +
+ + +
+ +

+ + () +

+ + +

+ + @: +

+ +
+ + + + + order ); ?> + + +
+ + user_can_manage_document( $document->get_type() ) ) : ?> + + +

+
+ +
+

+ +

+

+
+ + order ); ?> + + + + +
+ + +
+
+ " data-action="save"> + +
+
+ +
\ No newline at end of file From 69e9b3d3b06ae004e8e94f5675d736b2d28bbf63 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Thu, 28 Mar 2024 18:10:53 +0330 Subject: [PATCH 02/40] WPCS --- includes/views/document-data-metabox.php | 166 +++++++++++------------ 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/includes/views/document-data-metabox.php b/includes/views/document-data-metabox.php index 89988ddf0..779e48811 100644 --- a/includes/views/document-data-metabox.php +++ b/includes/views/document-data-metabox.php @@ -1,65 +1,65 @@ -
-
- -

+
+
+ +

get_title() ); ?> exists() && ( isset( $data['number'] ) || isset( $data['date'] ) ) && $this->user_can_manage_document( $document->get_type() ) ) : ?> - - "> + + "> -

+

- -
+ +
exists() ) : ?> -
-

-

- - -

-

-
+
+

+

+ + +

+

+
-
-

-

- - -

-

-
+
+

+

+ + +

+

+
- -
- - -
+
+ + +
order ); ?> @@ -80,58 +80,58 @@ ?> -
+
- -
+ +
-

- - () -

+

+ + () +

-

- - @: -

+

+ + @: +

-
+
- + order ); ?> - -
- + +
+ user_can_manage_document( $document->get_type() ) ) : ?> - + -

-
- -
-

- -

-

-
+

+
+ +
+

+ +

+

+
order ); ?> - + -
+ - -
-
- " data-action="save"> - -
-
- + +
+
+ " data-action="save"> + +
+
+
\ No newline at end of file From 34ee8d1ef75ed4350a986d817595a48a79ee61a2 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Thu, 28 Mar 2024 21:05:20 +0330 Subject: [PATCH 03/40] Update and improvements for metabox markup --- includes/views/document-data-metabox.php | 76 +++++++++++++----------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/includes/views/document-data-metabox.php b/includes/views/document-data-metabox.php index 779e48811..0db3806c0 100644 --- a/includes/views/document-data-metabox.php +++ b/includes/views/document-data-metabox.php @@ -1,4 +1,6 @@ -
+ + +

@@ -12,24 +14,31 @@
- exists() ) : ?> + exists() && $in_process ) : ?> +

+ get_title() ); + ?> +

+ exists() ) : ?>

-

- - -

+

+ + +

-

- - -

+

+ + +

@@ -37,20 +46,20 @@

-

- - -

+

+ + +

-

- - -

+

+ + +

@@ -62,23 +71,20 @@
order ); ?> - - - user_can_manage_document( $document->get_type() ) ) { + user_can_manage_document( $document->get_type() ) ) : ?> + + %s', - sprintf( /* translators: document title */ - esc_html__( 'Set %s number & date', 'woocommerce-pdf-invoices-packing-slips' ), - wp_kses_post( $document->get_title() ) - ) + esc_html__( 'Set %s number & date', 'woocommerce-pdf-invoices-packing-slips' ), + wp_kses_post( $document->get_title() ) ); - } else { - printf( '

%s

', esc_html__( 'You do not have sufficient permissions to edit this document.', 'woocommerce-pdf-invoices-packing-slips' ) ); - } - ?> - + ?> + + +

+ +

@@ -87,7 +93,7 @@

- () + ()

@@ -99,8 +105,7 @@
- - + order ); ?> @@ -120,7 +125,6 @@ order ); ?> - From e39996f3223e31bf6563e822496823874f1e4db0 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Thu, 28 Mar 2024 22:21:20 +0330 Subject: [PATCH 04/40] Add auto update documents data using heartbeat --- assets/js/order-script.js | 42 +++++++++++++++++- includes/class-wcpdf-admin.php | 78 +++++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/assets/js/order-script.js b/assets/js/order-script.js index 47f3d5079..395c22870 100644 --- a/assets/js/order-script.js +++ b/assets/js/order-script.js @@ -187,5 +187,45 @@ jQuery( function( $ ) { $( '.view-more' ).show(); } } ); - + + // send pending documents with heartbeat to fetch data + $( document ).on( 'heartbeat-send', function( e, data ) { + let pending_documents = []; + $( '.wcpdf-data-fields' ).each( function () { + if ( 'yes' === $( this ).attr( 'data-is_pending' ) ) { + pending_documents.push( $( this ).data( 'document' ) ); + } + } ); + + if ( pending_documents.length > 0 ) { + data.wpo_pending_documents = pending_documents; + } + } ); + + // fetch data for pending documents if documents are generated + $( document ).on( 'heartbeat-tick', function( e, data ) { + if (!data.wpo_ready_documents) { + return; + } + + $.ajax( { + url: wpo_wcpdf_ajax.ajaxurl, + type: 'POST', + data: { + action: 'wpo_fetch_document_data', + security: wpo_wcpdf_ajax.nonce, + document_types: data.wpo_ready_documents.document_types, + order_id: data.wpo_ready_documents.order_id, + }, + success: function ( response ) { + $.each( response.data, function ( key, value ) { + $( '.wcpdf-data-fields[data-document="' + key + '"]' ).replaceWith( value ); + } ); + }, + error: function ( response ) { + console.log( response.message ); + } + } ); + } ); + } ); \ No newline at end of file diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 45f69cfe5..ebba32b21 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -75,6 +75,9 @@ public function __construct() { add_filter( 'woocommerce_rest_prepare_report_orders', array( $this, 'add_invoice_number_to_order_report' ) ); add_filter( 'woocommerce_report_orders_export_columns', array( $this, 'add_invoice_number_header_to_order_export' ) ); add_filter( 'woocommerce_report_orders_prepare_export_item', array( $this, 'add_invoice_number_value_to_order_export' ), 10, 2 ); + + add_filter( 'heartbeat_received', array( $this, 'handle_heartbeat_received' ), 10, 2 ); + add_action( 'wp_ajax_wpo_fetch_document_data', array( $this, 'ajax_fetch_pdf_document_data' ) ); } // display review admin notice after 100 pdf downloads @@ -1259,7 +1262,7 @@ public function sort_orders_by_numeric_invoice_number( $query ): void { * * @return bool */ - private function is_invoice_number_numeric() { + private function is_invoice_number_numeric(): bool { $invoice_settings = WPO_WCPDF()->settings->get_document_settings( 'invoice' ); $is_numeric = ( empty( $invoice_settings['number_format']['prefix'] ) || ctype_digit( $invoice_settings['number_format']['prefix'] ) ) && ( empty( $invoice_settings['number_format']['suffix'] ) || ctype_digit( $invoice_settings['number_format']['suffix'] ) ); @@ -1267,6 +1270,79 @@ private function is_invoice_number_numeric() { return apply_filters( 'wpo_wcpdf_invoice_number_is_numeric', $is_numeric ); } + /** + * Append ready documents to the heartbeat response. + * + * @param array $response + * @param array $data + * + * @return array + */ + public function handle_heartbeat_received( array $response, array $data ): array { + if ( empty( $data['wpo_pending_documents'] ) || empty( $data['wc-refresh-order-lock'] ) ) { + return $response; + } + + $order_id = $data['wc-refresh-order-lock']; + $pending_documents = $data['wpo_pending_documents']; + + foreach ( $pending_documents as $document_type ) { + $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); + if ( $document && $document->exists() ) { + $response['wpo_ready_documents']['document_types'][] = $document_type; + } + } + + if ( ! empty( $response['wpo_ready_documents'] ) ) { + $response['wpo_ready_documents']['order_id'] = $order_id; + } + + return $response; + } + + public function ajax_fetch_pdf_document_data(): void { + if ( isset( $_REQUEST['nonce'] ) && wp_verify_nonce( $_REQUEST['nonce'], 'generate_wpo_wcpdf' ) ) { + wp_send_json_error( array( + 'message' => esc_html__( 'Nonce expired!', 'woocommerce-pdf-invoices-packing-slips' ), + ) ); + } + + if ( empty( $_REQUEST['document_types'] ) || empty( $_REQUEST['order_id'] ) ) { + wp_send_json_error( array( + 'message' => esc_html__( 'Incomplete request!', 'woocommerce-pdf-invoices-packing-slips' ), + ) ); + } + + $document_types = $_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'] ); + + foreach ( $document_types as $document_type ) { + if ( ! in_array( $document_type, array_keys( $data ), true ) ) { + continue; + } + + $document = wcpdf_get_document( $document_type, wc_get_order( $_REQUEST['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(); + } + } + + if ( ! empty ( $documents_data ) ) { + wp_send_json_success( $documents_data ); + } + + wp_die(); + } + } endif; // class_exists From 0058f6fa903452b315f37d49c9ff36f2901da083 Mon Sep 17 00:00:00 2001 From: MohamadNateqi Date: Thu, 28 Mar 2024 18:52:03 +0000 Subject: [PATCH 05/40] Generate minified JS and CSS files --- assets/js/order-script.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/order-script.min.js b/assets/js/order-script.min.js index 0f9be7ef2..31f575a6a 100644 --- a/assets/js/order-script.min.js +++ b/assets/js/order-script.min.js @@ -1 +1 @@ -jQuery(function(a){function b(a,b=null){a.find(".read-only").is(":visible")?("notes"==b?a.find(".editable-notes :input").attr("disabled",!1):(a.find(".editable").show(),a.find(":input").attr("disabled",!1)),a.find(".read-only").hide(),a.find(".editable-notes").show(),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").show()):(a.find(".read-only").show(),a.find(".editable").hide(),a.find(".editable-notes").hide(),a.find(":input").attr("disabled",!0),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").hide())}a("#doaction, #doaction2").on("click",function(b){let c=a(this).attr("id").substr(2),d=a("select[name=\""+c+"\"]").val();if(-1!==a.inArray(d,wpo_wcpdf_ajax.bulk_actions)){b.preventDefault();let c=d,e=[],f=!1;if(-1!=d.indexOf("ubl")&&(c=c.replace("_ubl",""),f=!0),a("tbody th.check-column input[type=\"checkbox\"]:checked").each(function(){e.push(a(this).val())}),!e.length)return void alert(wpo_wcpdf_ajax.select_orders);let g="",h="";if(g=-1==wpo_wcpdf_ajax.ajaxurl.indexOf("?")?wpo_wcpdf_ajax.ajaxurl+"?action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce:wpo_wcpdf_ajax.ajaxurl+"&action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce,f)a.each(e,function(a,b){h=g+"&order_ids="+b+"&output=ubl",window.open(h,"_blank")});else{let a=e.join("x");h=g+"&order_ids="+a,window.open(h,"_blank")}}}),wpo_wcpdf_ajax.sticky_document_data_metabox&&a("#wpo_wcpdf-data-input-box").insertAfter("#woocommerce-order-data"),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-set-date-number, .wpo-wcpdf-edit-date-number, .wpo-wcpdf-edit-document-notes",function(){let c=a(this).closest(".wcpdf-data-fields-section");0==c.length&&(c=a(this).closest(".wcpdf-data-fields"));let d=a(this).data("edit");b(c,d)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-cancel",function(){let c=a(this).closest(".wcpdf-data-fields");b(c)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-save-document, .wpo-wcpdf-regenerate-document, .wpo-wcpdf-delete-document",function(c){c.preventDefault();let d=a(this).closest(".wcpdf-data-fields"),e=a(this).data("action"),f=a(this).data("nonce"),g=d.data(),h=d.find(":input:visible:not(:disabled)").serialize();if("regenerate"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_regenerate))return;d.find(".wpo-wcpdf-regenerate-document").addClass("wcpdf-regenerate-spin")}else if("delete"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_delete))return;d.find(".wpo-wcpdf-regenerate-document").hide()}d.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajax({url:wpo_wcpdf_ajax.ajaxurl,data:{action:"wpo_wcpdf_"+e+"_document",security:f,form_data:h,order_id:g.order_id,document_type:g.document,action_type:e,wpcdf_document_data_notice:e+"d"},type:"POST",context:d,success:function(c){b(d),d.closest("#wpo_wcpdf-data-input-box").load(document.URL+" #wpo_wcpdf-data-input-box .postbox-header, #wpo_wcpdf-data-input-box .inside",function(){let b;b=c.success?"success":"error",a(this).find(".wcpdf-data-fields[data-document='"+g.document+"'][data-order_id='"+g.order_id+"']").before("

"+c.data.message+"

")}),"regenerate"==e&&(d.find(".wpo-wcpdf-regenerate-document").removeClass("wcpdf-regenerate-spin"),b(d)),d.unblock()}})}),a("#wpo_wcpdf-data-input-box").on("click",".view-more, .hide-details",function(b){b.preventDefault(),a(this).hide(),a(".pdf-more-details").slideToggle("slow"),a(this).hasClass("view-more")?a(".hide-details").show():a(".view-more").show()})}); \ No newline at end of file +jQuery(function(a){function b(a,b=null){a.find(".read-only").is(":visible")?("notes"==b?a.find(".editable-notes :input").attr("disabled",!1):(a.find(".editable").show(),a.find(":input").attr("disabled",!1)),a.find(".read-only").hide(),a.find(".editable-notes").show(),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").show()):(a.find(".read-only").show(),a.find(".editable").hide(),a.find(".editable-notes").hide(),a.find(":input").attr("disabled",!0),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").hide())}a("#doaction, #doaction2").on("click",function(b){let c=a(this).attr("id").substr(2),d=a("select[name=\""+c+"\"]").val();if(-1!==a.inArray(d,wpo_wcpdf_ajax.bulk_actions)){b.preventDefault();let c=d,e=[],f=!1;if(-1!=d.indexOf("ubl")&&(c=c.replace("_ubl",""),f=!0),a("tbody th.check-column input[type=\"checkbox\"]:checked").each(function(){e.push(a(this).val())}),!e.length)return void alert(wpo_wcpdf_ajax.select_orders);let g="",h="";if(g=-1==wpo_wcpdf_ajax.ajaxurl.indexOf("?")?wpo_wcpdf_ajax.ajaxurl+"?action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce:wpo_wcpdf_ajax.ajaxurl+"&action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce,f)a.each(e,function(a,b){h=g+"&order_ids="+b+"&output=ubl",window.open(h,"_blank")});else{let a=e.join("x");h=g+"&order_ids="+a,window.open(h,"_blank")}}}),wpo_wcpdf_ajax.sticky_document_data_metabox&&a("#wpo_wcpdf-data-input-box").insertAfter("#woocommerce-order-data"),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-set-date-number, .wpo-wcpdf-edit-date-number, .wpo-wcpdf-edit-document-notes",function(){let c=a(this).closest(".wcpdf-data-fields-section");0==c.length&&(c=a(this).closest(".wcpdf-data-fields"));let d=a(this).data("edit");b(c,d)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-cancel",function(){let c=a(this).closest(".wcpdf-data-fields");b(c)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-save-document, .wpo-wcpdf-regenerate-document, .wpo-wcpdf-delete-document",function(c){c.preventDefault();let d=a(this).closest(".wcpdf-data-fields"),e=a(this).data("action"),f=a(this).data("nonce"),g=d.data(),h=d.find(":input:visible:not(:disabled)").serialize();if("regenerate"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_regenerate))return;d.find(".wpo-wcpdf-regenerate-document").addClass("wcpdf-regenerate-spin")}else if("delete"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_delete))return;d.find(".wpo-wcpdf-regenerate-document").hide()}d.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajax({url:wpo_wcpdf_ajax.ajaxurl,data:{action:"wpo_wcpdf_"+e+"_document",security:f,form_data:h,order_id:g.order_id,document_type:g.document,action_type:e,wpcdf_document_data_notice:e+"d"},type:"POST",context:d,success:function(c){b(d),d.closest("#wpo_wcpdf-data-input-box").load(document.URL+" #wpo_wcpdf-data-input-box .postbox-header, #wpo_wcpdf-data-input-box .inside",function(){let b;b=c.success?"success":"error",a(this).find(".wcpdf-data-fields[data-document='"+g.document+"'][data-order_id='"+g.order_id+"']").before("

"+c.data.message+"

")}),"regenerate"==e&&(d.find(".wpo-wcpdf-regenerate-document").removeClass("wcpdf-regenerate-spin"),b(d)),d.unblock()}})}),a("#wpo_wcpdf-data-input-box").on("click",".view-more, .hide-details",function(b){b.preventDefault(),a(this).hide(),a(".pdf-more-details").slideToggle("slow"),a(this).hasClass("view-more")?a(".hide-details").show():a(".view-more").show()}),a(document).on("heartbeat-send",function(b,c){let d=[];a(".wcpdf-data-fields").each(function(){"yes"===a(this).attr("data-is_pending")&&d.push(a(this).data("document"))}),0 Date: Wed, 3 Apr 2024 12:41:54 +0330 Subject: [PATCH 06/40] Fix nonce validation --- includes/class-wcpdf-admin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index ebba32b21..bef78da1d 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1301,9 +1301,9 @@ public function handle_heartbeat_received( array $response, array $data ): array } public function ajax_fetch_pdf_document_data(): void { - if ( isset( $_REQUEST['nonce'] ) && wp_verify_nonce( $_REQUEST['nonce'], 'generate_wpo_wcpdf' ) ) { + if ( ! isset( $_REQUEST['nonce'] ) || wp_verify_nonce( $_REQUEST['nonce'], 'generate_wpo_wcpdf' ) ) { 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' ), ) ); } From 6ecfebe8fea546b1a04eda9197226f5df0f8e1e4 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 3 Apr 2024 12:49:29 +0330 Subject: [PATCH 07/40] Validate the document type --- includes/class-wcpdf-admin.php | 4 ++-- includes/wcpdf-functions.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index bef78da1d..b9f3484a0 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1313,7 +1313,7 @@ public function ajax_fetch_pdf_document_data(): void { ) ); } - $document_types = $_REQUEST['document_types']; + $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' ) ), @@ -1324,7 +1324,7 @@ public function ajax_fetch_pdf_document_data(): void { $data = apply_filters( 'wpo_wcpdf_ajax_fetch_pdf_document_data_fields', $data, $_REQUEST['order_id'] ); 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; } diff --git a/includes/wcpdf-functions.php b/includes/wcpdf-functions.php index 84594dc15..bf19d270d 100644 --- a/includes/wcpdf-functions.php +++ b/includes/wcpdf-functions.php @@ -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; +} From 2251d1638e335c466de9dd2f79053fc7149e789c Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 3 Apr 2024 12:52:04 +0330 Subject: [PATCH 08/40] Improve performance --- includes/class-wcpdf-admin.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index b9f3484a0..d8324317c 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1323,6 +1323,7 @@ public function ajax_fetch_pdf_document_data(): void { ); $data = apply_filters( 'wpo_wcpdf_ajax_fetch_pdf_document_data_fields', $data, $_REQUEST['order_id'] ); + ob_start(); foreach ( $document_types as $document_type ) { if ( ! wpo_wcpdf_is_document_type_valid( $document_type ) || ! in_array( $document_type, array_keys( $data ), true ) ) { continue; @@ -1330,11 +1331,12 @@ public function ajax_fetch_pdf_document_data(): void { $document = wcpdf_get_document( $document_type, wc_get_order( $_REQUEST['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(); + ob_clean(); } } + ob_end_clean(); if ( ! empty ( $documents_data ) ) { wp_send_json_success( $documents_data ); From 00c299ca298342d1a83a4e23ebbfb3ce082ac0e7 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 3 Apr 2024 12:56:16 +0330 Subject: [PATCH 09/40] Better sanitize --- includes/class-wcpdf-admin.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index d8324317c..611baffd7 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1307,12 +1307,13 @@ public function ajax_fetch_pdf_document_data(): void { ) ); } - 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' ), ) ); } + $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' ) ), @@ -1321,7 +1322,7 @@ public function ajax_fetch_pdf_document_data(): void { '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 ) { @@ -1329,7 +1330,7 @@ public function ajax_fetch_pdf_document_data(): void { 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() ) { $this->output_number_date_edit_fields( $document, $data[ $document_type ] ); $documents_data[ $document_type ] = ob_get_contents(); @@ -1338,7 +1339,7 @@ public function ajax_fetch_pdf_document_data(): void { } ob_end_clean(); - if ( ! empty ( $documents_data ) ) { + if ( ! empty( $documents_data ) ) { wp_send_json_success( $documents_data ); } From ed6bc11dbf27f3292cfc895cbef9fbda37b7a6ed Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 3 Apr 2024 13:27:40 +0330 Subject: [PATCH 10/40] WPCS --- assets/js/order-script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/order-script.js b/assets/js/order-script.js index 395c22870..e4ef78fbe 100644 --- a/assets/js/order-script.js +++ b/assets/js/order-script.js @@ -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; } From 7fd96b7ac5fad59b92fb9d4247623e30c9796edc Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 3 Apr 2024 13:37:51 +0330 Subject: [PATCH 11/40] Add DocBlock --- includes/class-wcpdf-admin.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 611baffd7..3ed5d79f6 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1300,6 +1300,11 @@ 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 { if ( ! isset( $_REQUEST['nonce'] ) || wp_verify_nonce( $_REQUEST['nonce'], 'generate_wpo_wcpdf' ) ) { wp_send_json_error( array( From 75c411b3bf662082c895d5b50c64bc98835281e7 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 3 Apr 2024 13:42:03 +0330 Subject: [PATCH 12/40] Improvement --- includes/class-wcpdf-admin.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 3ed5d79f6..9a14ef45e 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1348,7 +1348,9 @@ public function ajax_fetch_pdf_document_data(): void { 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' ), + ) ); } } From acc87cb0ddd1de0aac3c2d489192ef09d5f49bdc Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 3 Apr 2024 14:34:45 +0330 Subject: [PATCH 13/40] Fix bugs --- includes/class-wcpdf-admin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 9a14ef45e..be397298a 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1306,7 +1306,7 @@ public function handle_heartbeat_received( array $response, array $data ): array * @return void */ public function ajax_fetch_pdf_document_data(): void { - if ( ! isset( $_REQUEST['nonce'] ) || wp_verify_nonce( $_REQUEST['nonce'], 'generate_wpo_wcpdf' ) ) { + if ( ! isset( $_REQUEST['security'] ) || ! wp_verify_nonce( $_REQUEST['security'], 'generate_wpo_wcpdf' ) ) { wp_send_json_error( array( 'message' => esc_html__( 'Invalid or expired nonce!', 'woocommerce-pdf-invoices-packing-slips' ), ) ); @@ -1328,6 +1328,7 @@ public function ajax_fetch_pdf_document_data(): void { '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, $order_id ); + $documents_data = array(); ob_start(); foreach ( $document_types as $document_type ) { From 2b66b21e82164a3b2e73137aa090bc7f2dc8557b Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Wed, 3 Apr 2024 12:07:51 +0100 Subject: [PATCH 14/40] Indentation fixes --- includes/class-wcpdf-admin.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index be397298a..910998813 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -71,7 +71,7 @@ public function __construct() { // document actions add_action( 'wpo_wcpdf_document_actions', array( $this, 'add_regenerate_document_button' ) ); - // add "invoice number" column to WooCommerce Analytic - Orders + // add "invoice number" column to WooCommerce Analytic - Orders add_filter( 'woocommerce_rest_prepare_report_orders', array( $this, 'add_invoice_number_to_order_report' ) ); add_filter( 'woocommerce_report_orders_export_columns', array( $this, 'add_invoice_number_header_to_order_export' ) ); add_filter( 'woocommerce_report_orders_prepare_export_item', array( $this, 'add_invoice_number_value_to_order_export' ), 10, 2 ); @@ -1257,15 +1257,15 @@ public function sort_orders_by_numeric_invoice_number( $query ): void { } /** - * Determines if the invoice number is numeric. - * It evaluates the presence of non-numeric characters in the prefix and suffix of the invoice number. - * + * Determines if the invoice number is numeric. + * It evaluates the presence of non-numeric characters in the prefix and suffix of the invoice number. + * * @return bool */ private function is_invoice_number_numeric(): bool { $invoice_settings = WPO_WCPDF()->settings->get_document_settings( 'invoice' ); $is_numeric = ( empty( $invoice_settings['number_format']['prefix'] ) || ctype_digit( $invoice_settings['number_format']['prefix'] ) ) && - ( empty( $invoice_settings['number_format']['suffix'] ) || ctype_digit( $invoice_settings['number_format']['suffix'] ) ); + ( empty( $invoice_settings['number_format']['suffix'] ) || ctype_digit( $invoice_settings['number_format']['suffix'] ) ); return apply_filters( 'wpo_wcpdf_invoice_number_is_numeric', $is_numeric ); } @@ -1301,8 +1301,8 @@ public function handle_heartbeat_received( array $response, array $data ): array } /** - * Updates documents data in the "PDF document data" meta box if the generation in the background is finished. - * + * 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 { From 46ee270091059281a8a738bde938ef4f951c8cc5 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 9 Apr 2024 18:02:34 +0330 Subject: [PATCH 15/40] Improve the meta box method to print all documents data, Improve data output method for document data meta box --- includes/class-wcpdf-admin.php | 98 +++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 38 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 910998813..fbf64c872 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -713,33 +713,33 @@ public function data_input_box_content( $post_or_order_object ) { $this->disable_storing_document_settings(); - $invoice = wcpdf_get_document( 'invoice', $order ); - do_action( 'wpo_wcpdf_meta_box_start', $order, $this ); - if ( $invoice ) { - // data - $data = 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' ), - ), + $documents = WPO_WCPDF()->documents->get_documents(); - ); - // output - $this->output_number_date_edit_fields( $invoice, $data ); + foreach ( $documents as $document ) { + // Allow getting documents without invoice. + add_filter( 'wpo_wcpdf_allow_document_without_invoice', '__return_true' ); + $document = wcpdf_get_document( $document->get_type(), $order ); + + if ( ! $document ) { + continue; + } + + // Bulk documents + if ( ! empty( $document->order_ids ) ) { + foreach ( $document->order_ids as $order_id ) { + $sub_order = wc_get_order( $order_id ); + $sub_document = wcpdf_get_document( $document->get_type(), $sub_order ); + + if ( $sub_document ) { + $this->output_number_date_edit_fields( $sub_document ); + } + } + } else { + $this->output_number_date_edit_fields( $document ); + } } do_action( 'wpo_wcpdf_meta_box_end', $order, $this ); @@ -793,17 +793,47 @@ public function get_current_values_for_document( $document, $data ) { return apply_filters( 'wpo_wcpdf_current_values_for_document', $data, $document ); } - public function output_number_date_edit_fields( $document, $data ) { - if ( empty( $document ) || empty( $data ) || empty( $document->order ) || ! is_callable( array( $document->order, 'get_id' ) ) ) { + public function output_number_date_edit_fields( object $document, $data = array() ): void { + if ( empty( $document->order ) || ! is_callable( array( $document->order, 'get_id' ) ) ) { + return; + } + + $independent_documents = apply_filters( 'wpo_wcpdf_document_data_meta_box_independent_documents', array( 'invoice', 'packing-slip' ) ); + + // Prevent displaying setup for documents that are not independent and don't exist, + // like documents that require an invoice first and don't exist already. + if ( ! $document->exists() && ! in_array( $document->get_type(), $independent_documents, true ) ) { return; } + // Go for default data. + if ( empty( $data ) ) { + $data = array( + 'number' => array( 'label' => __( 'Number', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + 'date' => array( 'label' => __( 'Date', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + ); + + if ( 'invoice' === $document->get_type() ) { + $data = array_merge( $data, array( + 'display_date' => array( 'label' => __( 'Display date', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + 'creation_trigger' => array( 'label' => __( '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_document_data_meta_box_document_data_fields', $data, $document ); $data = $this->get_current_values_for_document( $document, $data ); $in_process = as_has_scheduled_action( 'wpo_wcpdf_generate_document_on_order_status', array( 'document_type' => $document->get_type(), 'order_id' => $document->order->get_id() ) ); + // Allow preventing document output. + if ( apply_filters( 'wpo_wcpdf_document_data_meta_box_allow_document_output', false, $data, $in_process, $document ) ) { + return; + } + include WPO_WCPDF()->plugin_path() . '/includes/views/document-data-metabox.php'; } @@ -1318,27 +1348,19 @@ public function ajax_fetch_pdf_document_data(): void { ) ); } - $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, $order_id ); - $documents_data = array(); + $order_id = (int) $_REQUEST['order_id']; + $document_types = array_map( 'sanitize_text_field', $_REQUEST['document_types'] ); + $documents_data = array(); ob_start(); foreach ( $document_types as $document_type ) { - if ( ! wpo_wcpdf_is_document_type_valid( $document_type ) || ! in_array( $document_type, array_keys( $data ), true ) ) { + if ( ! wpo_wcpdf_is_document_type_valid( $document_type ) ) { continue; } $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); if ( $document && $document->exists() ) { - $this->output_number_date_edit_fields( $document, $data[ $document_type ] ); + $this->output_number_date_edit_fields( $document ); $documents_data[ $document_type ] = ob_get_contents(); ob_clean(); } From bcbb36890a5e11c4f5feee41d9b997dbc29b7d25 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 9 Apr 2024 18:32:42 +0330 Subject: [PATCH 16/40] Update version --- woocommerce-pdf-invoices-packingslips.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/woocommerce-pdf-invoices-packingslips.php b/woocommerce-pdf-invoices-packingslips.php index a8d91f8ba..d6975c3ab 100644 --- a/woocommerce-pdf-invoices-packingslips.php +++ b/woocommerce-pdf-invoices-packingslips.php @@ -3,7 +3,7 @@ * Plugin Name: PDF Invoices & Packing Slips for WooCommerce * Plugin URI: https://wpovernight.com/downloads/woocommerce-pdf-invoices-packing-slips-bundle/ * Description: Create, print & email PDF or UBL Invoices & PDF Packing Slips for WooCommerce orders. - * Version: 3.8.0 + * Version: 3.9.0-beta-1 * Author: WP Overnight * Author URI: https://www.wpovernight.com * License: GPLv2 or later @@ -21,7 +21,7 @@ class WPO_WCPDF { - public $version = '3.8.0'; + public $version = '3.9.0-beta-1'; public $version_php = '7.2'; public $version_woo = '3.3'; public $version_wp = '4.4'; From 69e812e886783614fdf3d7130fde1941c188ed72 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 10 Apr 2024 21:00:24 +0330 Subject: [PATCH 17/40] Rename a parameter --- assets/js/order-script.js | 2 +- includes/class-wcpdf-admin.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/js/order-script.js b/assets/js/order-script.js index e4ef78fbe..8495d35a4 100644 --- a/assets/js/order-script.js +++ b/assets/js/order-script.js @@ -198,7 +198,7 @@ jQuery( function( $ ) { } ); if ( pending_documents.length > 0 ) { - data.wpo_pending_documents = pending_documents; + data.wpo_wcpdf_pending_documents = pending_documents; } } ); diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index fbf64c872..fa0e10303 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1309,12 +1309,12 @@ private function is_invoice_number_numeric(): bool { * @return array */ public function handle_heartbeat_received( array $response, array $data ): array { - if ( empty( $data['wpo_pending_documents'] ) || empty( $data['wc-refresh-order-lock'] ) ) { + if ( empty( $data['wpo_wcpdf_pending_documents'] ) || empty( $data['wc-refresh-order-lock'] ) ) { return $response; } $order_id = $data['wc-refresh-order-lock']; - $pending_documents = $data['wpo_pending_documents']; + $pending_documents = $data['wpo_wcpdf_pending_documents']; foreach ( $pending_documents as $document_type ) { $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); From 146180c0f1337747eb5d80b92f3cba616cbf335c Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 10 Apr 2024 21:11:06 +0330 Subject: [PATCH 18/40] Fix HPOS compatibility --- includes/class-wcpdf-admin.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index fa0e10303..d5d21faf3 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1309,11 +1309,14 @@ private function is_invoice_number_numeric(): bool { * @return array */ public function handle_heartbeat_received( array $response, array $data ): array { - if ( empty( $data['wpo_wcpdf_pending_documents'] ) || empty( $data['wc-refresh-order-lock'] ) ) { + if ( + empty( $data['wpo_wcpdf_pending_documents'] ) || + ( empty( $data['wc-refresh-order-lock'] ) && empty( $data['wp-refresh-post-lock']['post_id'] ) ) + ) { return $response; } - $order_id = $data['wc-refresh-order-lock']; + $order_id = $data['wc-refresh-order-lock'] ?? $data['wp-refresh-post-lock']['post_id']; $pending_documents = $data['wpo_wcpdf_pending_documents']; foreach ( $pending_documents as $document_type ) { From 0d02eda0a60a1a6891a3f362c5c2141a23563575 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 10 Apr 2024 22:10:43 +0330 Subject: [PATCH 19/40] Fix input width issue on Firefox --- assets/css/order-styles.css | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/css/order-styles.css b/assets/css/order-styles.css index 6fbcfae16..71fb4cedc 100644 --- a/assets/css/order-styles.css +++ b/assets/css/order-styles.css @@ -107,6 +107,7 @@ #wpo_wcpdf-data-input-box .form-field input { width: auto; + min-width: 5em; } .wcpdf-data-fields { From 8fa23bf14be8d48f1cfb3fb269570a4ed54cba9b Mon Sep 17 00:00:00 2001 From: MohamadNateqi Date: Wed, 10 Apr 2024 18:49:13 +0000 Subject: [PATCH 20/40] Generate minified JS and CSS files --- assets/css/order-styles.min.css | 2 +- assets/js/order-script.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/css/order-styles.min.css b/assets/css/order-styles.min.css index d48311ddc..8701f5ea8 100644 --- a/assets/css/order-styles.min.css +++ b/assets/css/order-styles.min.css @@ -1 +1 @@ -.type-shop_order .column-wc_actions a.button.wpo_wcpdf,.type-shop_order .column-wc_actions a.button.wpo_wcpdf:hover,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf:hover{background-repeat:no-repeat;background-position:center;text-align:center;background-size:16px 16px}.type-shop_order .column-wc_actions a.button.wpo_wcpdf>svg,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf>svg{display:block;position:absolute;width:10px;height:10px;bottom:1px;background:#f6f7f7}.type-shop_order .column-wc_actions a.button.wpo_wcpdf.exists>svg.icon-exists,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf.exists>svg.icon-exists{left:auto;right:2px;fill:#2aad2a}.type-shop_order .column-wc_actions a.button.wpo_wcpdf.ubl>svg.icon-ubl,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf.ubl>svg.icon-ubl{left:auto;right:2px;fill:#000}.type-shop_order .column-wc_actions a.button.wpo_wcpdf.printed>svg.icon-printed,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf.printed>svg.icon-printed{left:2px;right:auto;fill:#51266b}.wpo_wcpdf-actions a.button.exists>svg,.wpo_wcpdf-ubl-actions a.button.exists>svg{width:14px;height:14px;line-height:30px;margin-left:4px;margin-bottom:3px;fill:#2aad2a;vertical-align:middle}.wcpdf-data-fields .printed-data,.wpo_wcpdf-actions .printed-data{font-size:8pt;color:#999;margin-left:5px;vertical-align:middle;margin-top:3px!important}.wcpdf-data-fields .printed-data>svg,.wpo_wcpdf-actions .printed-data>svg{width:14px;height:14px;background:#fff;fill:#999;vertical-align:middle;margin-right:3px}.wcpdf-data-fields a.unmark_printed,.wpo_wcpdf-actions a.unmark_printed{margin-left:4px;color:#a00}.widefat .column-pdf_invoice_number{width:110px}@media screen and (max-width:782px){.wp-list-table .column-pdf_invoice_number,.wp-list-table tr:not(.inline-edit-row):not(.no-items) td.column-pdf_invoice_number:not(.check-column){display:none}.wp-list-table tr:not(.inline-edit-row):not(.no-items) td.column-pdf_invoice_number{padding:3px 8px 3px 35%}}#wpo_wcpdf-data-input-box>.inside{margin:0;padding:10px 0;background-color:#f8f8f8}#wpo_wcpdf-data-input-box h4{font-size:14px;line-height:1.4;margin:0}#wpo_wcpdf-data-input-box p{margin:.5em 0}#wpo_wcpdf-data-input-box .form-field input{width:auto}.wcpdf-data-fields{margin-left:10px;margin-right:10px;margin-bottom:10px;background-color:#fff;border:1px solid #c3c4c7;box-shadow:0 1px 1px rgba(0,0,0,.04)}.wcpdf-data-fields:last-child{margin-bottom:0}.wcpdf-data-fields .read-only{height:auto;display:block;padding:10px}.wcpdf-data-fields .editable,.wcpdf-data-fields .editable-notes{height:auto;display:none;padding:10px}.wcpdf-data-fields h4{padding:10px;border-bottom:1px solid #dfdfdf}.wcpdf-data-fields h4 .dashicons:first-of-type{margin-left:20px!important}.wcpdf-data-fields .wpo-wcpdf-delete-document,.wcpdf-data-fields .wpo-wcpdf-edit-date-number,.wcpdf-data-fields .wpo-wcpdf-edit-document-notes,.wcpdf-data-fields .wpo-wcpdf-regenerate-document{opacity:.5}.wcpdf-data-fields .wpo-wcpdf-delete-document:hover,.wcpdf-data-fields .wpo-wcpdf-edit-date-number:hover,.wcpdf-data-fields .wpo-wcpdf-edit-document-notes:hover,.wcpdf-data-fields .wpo-wcpdf-regenerate-document:hover{opacity:1;cursor:pointer}.wcpdf-data-fields .wpo-wcpdf-document-buttons{display:none;padding:10px;border-top:1px solid #dfdfdf}.wcpdf-data-fields h4 .wcpdf-regenerate-spin{opacity:1;-webkit-animation:spin 1.6s linear infinite;animation:spin 1.6s linear infinite;pointer-events:none}@-webkit-keyframes wcpdf-regenerate-spin{0%{-webkit-transform:rotate(0)}100%{-webkit-transform:rotate(-360deg)}}@keyframes wcpdf-regenerate-spin{0%{transform:rotate(0)}100%{transform:rotate(-360deg)}} \ No newline at end of file +.type-shop_order .column-wc_actions a.button.wpo_wcpdf,.type-shop_order .column-wc_actions a.button.wpo_wcpdf:hover,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf:hover{background-repeat:no-repeat;background-position:center;text-align:center;background-size:16px 16px}.type-shop_order .column-wc_actions a.button.wpo_wcpdf>svg,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf>svg{display:block;position:absolute;width:10px;height:10px;bottom:1px;background:#f6f7f7}.type-shop_order .column-wc_actions a.button.wpo_wcpdf.exists>svg.icon-exists,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf.exists>svg.icon-exists{left:auto;right:2px;fill:#2aad2a}.type-shop_order .column-wc_actions a.button.wpo_wcpdf.ubl>svg.icon-ubl,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf.ubl>svg.icon-ubl{left:auto;right:2px;fill:#000}.type-shop_order .column-wc_actions a.button.wpo_wcpdf.printed>svg.icon-printed,.woocommerce_page_wc-orders .column-wc_actions a.button.wpo_wcpdf.printed>svg.icon-printed{left:2px;right:auto;fill:#51266b}.wpo_wcpdf-actions a.button.exists>svg,.wpo_wcpdf-ubl-actions a.button.exists>svg{width:14px;height:14px;line-height:30px;margin-left:4px;margin-bottom:3px;fill:#2aad2a;vertical-align:middle}.wcpdf-data-fields .printed-data,.wpo_wcpdf-actions .printed-data{font-size:8pt;color:#999;margin-left:5px;vertical-align:middle;margin-top:3px!important}.wcpdf-data-fields .printed-data>svg,.wpo_wcpdf-actions .printed-data>svg{width:14px;height:14px;background:#fff;fill:#999;vertical-align:middle;margin-right:3px}.wcpdf-data-fields a.unmark_printed,.wpo_wcpdf-actions a.unmark_printed{margin-left:4px;color:#a00}.widefat .column-pdf_invoice_number{width:110px}@media screen and (max-width:782px){.wp-list-table .column-pdf_invoice_number,.wp-list-table tr:not(.inline-edit-row):not(.no-items) td.column-pdf_invoice_number:not(.check-column){display:none}.wp-list-table tr:not(.inline-edit-row):not(.no-items) td.column-pdf_invoice_number{padding:3px 8px 3px 35%}}#wpo_wcpdf-data-input-box>.inside{margin:0;padding:10px 0;background-color:#f8f8f8}#wpo_wcpdf-data-input-box h4{font-size:14px;line-height:1.4;margin:0}#wpo_wcpdf-data-input-box p{margin:.5em 0}#wpo_wcpdf-data-input-box .form-field input{width:auto;min-width:5em}.wcpdf-data-fields{margin-left:10px;margin-right:10px;margin-bottom:10px;background-color:#fff;border:1px solid #c3c4c7;box-shadow:0 1px 1px rgba(0,0,0,.04)}.wcpdf-data-fields:last-child{margin-bottom:0}.wcpdf-data-fields .read-only{height:auto;display:block;padding:10px}.wcpdf-data-fields .editable,.wcpdf-data-fields .editable-notes{height:auto;display:none;padding:10px}.wcpdf-data-fields h4{padding:10px;border-bottom:1px solid #dfdfdf}.wcpdf-data-fields h4 .dashicons:first-of-type{margin-left:20px!important}.wcpdf-data-fields .wpo-wcpdf-delete-document,.wcpdf-data-fields .wpo-wcpdf-edit-date-number,.wcpdf-data-fields .wpo-wcpdf-edit-document-notes,.wcpdf-data-fields .wpo-wcpdf-regenerate-document{opacity:.5}.wcpdf-data-fields .wpo-wcpdf-delete-document:hover,.wcpdf-data-fields .wpo-wcpdf-edit-date-number:hover,.wcpdf-data-fields .wpo-wcpdf-edit-document-notes:hover,.wcpdf-data-fields .wpo-wcpdf-regenerate-document:hover{opacity:1;cursor:pointer}.wcpdf-data-fields .wpo-wcpdf-document-buttons{display:none;padding:10px;border-top:1px solid #dfdfdf}.wcpdf-data-fields h4 .wcpdf-regenerate-spin{opacity:1;-webkit-animation:spin 1.6s linear infinite;animation:spin 1.6s linear infinite;pointer-events:none}@-webkit-keyframes wcpdf-regenerate-spin{0%{-webkit-transform:rotate(0)}100%{-webkit-transform:rotate(-360deg)}}@keyframes wcpdf-regenerate-spin{0%{transform:rotate(0)}100%{transform:rotate(-360deg)}} \ No newline at end of file diff --git a/assets/js/order-script.min.js b/assets/js/order-script.min.js index 31f575a6a..180a29436 100644 --- a/assets/js/order-script.min.js +++ b/assets/js/order-script.min.js @@ -1 +1 @@ -jQuery(function(a){function b(a,b=null){a.find(".read-only").is(":visible")?("notes"==b?a.find(".editable-notes :input").attr("disabled",!1):(a.find(".editable").show(),a.find(":input").attr("disabled",!1)),a.find(".read-only").hide(),a.find(".editable-notes").show(),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").show()):(a.find(".read-only").show(),a.find(".editable").hide(),a.find(".editable-notes").hide(),a.find(":input").attr("disabled",!0),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").hide())}a("#doaction, #doaction2").on("click",function(b){let c=a(this).attr("id").substr(2),d=a("select[name=\""+c+"\"]").val();if(-1!==a.inArray(d,wpo_wcpdf_ajax.bulk_actions)){b.preventDefault();let c=d,e=[],f=!1;if(-1!=d.indexOf("ubl")&&(c=c.replace("_ubl",""),f=!0),a("tbody th.check-column input[type=\"checkbox\"]:checked").each(function(){e.push(a(this).val())}),!e.length)return void alert(wpo_wcpdf_ajax.select_orders);let g="",h="";if(g=-1==wpo_wcpdf_ajax.ajaxurl.indexOf("?")?wpo_wcpdf_ajax.ajaxurl+"?action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce:wpo_wcpdf_ajax.ajaxurl+"&action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce,f)a.each(e,function(a,b){h=g+"&order_ids="+b+"&output=ubl",window.open(h,"_blank")});else{let a=e.join("x");h=g+"&order_ids="+a,window.open(h,"_blank")}}}),wpo_wcpdf_ajax.sticky_document_data_metabox&&a("#wpo_wcpdf-data-input-box").insertAfter("#woocommerce-order-data"),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-set-date-number, .wpo-wcpdf-edit-date-number, .wpo-wcpdf-edit-document-notes",function(){let c=a(this).closest(".wcpdf-data-fields-section");0==c.length&&(c=a(this).closest(".wcpdf-data-fields"));let d=a(this).data("edit");b(c,d)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-cancel",function(){let c=a(this).closest(".wcpdf-data-fields");b(c)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-save-document, .wpo-wcpdf-regenerate-document, .wpo-wcpdf-delete-document",function(c){c.preventDefault();let d=a(this).closest(".wcpdf-data-fields"),e=a(this).data("action"),f=a(this).data("nonce"),g=d.data(),h=d.find(":input:visible:not(:disabled)").serialize();if("regenerate"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_regenerate))return;d.find(".wpo-wcpdf-regenerate-document").addClass("wcpdf-regenerate-spin")}else if("delete"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_delete))return;d.find(".wpo-wcpdf-regenerate-document").hide()}d.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajax({url:wpo_wcpdf_ajax.ajaxurl,data:{action:"wpo_wcpdf_"+e+"_document",security:f,form_data:h,order_id:g.order_id,document_type:g.document,action_type:e,wpcdf_document_data_notice:e+"d"},type:"POST",context:d,success:function(c){b(d),d.closest("#wpo_wcpdf-data-input-box").load(document.URL+" #wpo_wcpdf-data-input-box .postbox-header, #wpo_wcpdf-data-input-box .inside",function(){let b;b=c.success?"success":"error",a(this).find(".wcpdf-data-fields[data-document='"+g.document+"'][data-order_id='"+g.order_id+"']").before("

"+c.data.message+"

")}),"regenerate"==e&&(d.find(".wpo-wcpdf-regenerate-document").removeClass("wcpdf-regenerate-spin"),b(d)),d.unblock()}})}),a("#wpo_wcpdf-data-input-box").on("click",".view-more, .hide-details",function(b){b.preventDefault(),a(this).hide(),a(".pdf-more-details").slideToggle("slow"),a(this).hasClass("view-more")?a(".hide-details").show():a(".view-more").show()}),a(document).on("heartbeat-send",function(b,c){let d=[];a(".wcpdf-data-fields").each(function(){"yes"===a(this).attr("data-is_pending")&&d.push(a(this).data("document"))}),0

"+c.data.message+"

")}),"regenerate"==e&&(d.find(".wpo-wcpdf-regenerate-document").removeClass("wcpdf-regenerate-spin"),b(d)),d.unblock()}})}),a("#wpo_wcpdf-data-input-box").on("click",".view-more, .hide-details",function(b){b.preventDefault(),a(this).hide(),a(".pdf-more-details").slideToggle("slow"),a(this).hasClass("view-more")?a(".hide-details").show():a(".view-more").show()}),a(document).on("heartbeat-send",function(b,c){let d=[];a(".wcpdf-data-fields").each(function(){"yes"===a(this).attr("data-is_pending")&&d.push(a(this).data("document"))}),0 Date: Wed, 10 Apr 2024 22:34:12 +0330 Subject: [PATCH 21/40] Fix a bug --- includes/class-wcpdf-admin.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index d5d21faf3..436e84b27 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -798,14 +798,6 @@ public function output_number_date_edit_fields( object $document, $data = array( return; } - $independent_documents = apply_filters( 'wpo_wcpdf_document_data_meta_box_independent_documents', array( 'invoice', 'packing-slip' ) ); - - // Prevent displaying setup for documents that are not independent and don't exist, - // like documents that require an invoice first and don't exist already. - if ( ! $document->exists() && ! in_array( $document->get_type(), $independent_documents, true ) ) { - return; - } - // Go for default data. if ( empty( $data ) ) { $data = array( @@ -822,13 +814,20 @@ public function output_number_date_edit_fields( object $document, $data = array( } } - $data = apply_filters( 'wpo_wcpdf_document_data_meta_box_document_data_fields', $data, $document ); - $data = $this->get_current_values_for_document( $document, $data ); - $in_process = as_has_scheduled_action( 'wpo_wcpdf_generate_document_on_order_status', array( + $independent_documents = apply_filters( 'wpo_wcpdf_document_data_meta_box_independent_documents', array( 'invoice', 'packing-slip' ) ); + $data = apply_filters( 'wpo_wcpdf_document_data_meta_box_document_data_fields', $data, $document ); + $data = $this->get_current_values_for_document( $document, $data ); + $in_process = as_has_scheduled_action( 'wpo_wcpdf_generate_document_on_order_status', array( 'document_type' => $document->get_type(), 'order_id' => $document->order->get_id() ) ); + // Prevent displaying setup for documents that are not independent, and don't exist, or are not in process, + // like documents that require an invoice first and don't exist already or are not in process. + if ( ! $in_process && ! $document->exists() && ! in_array( $document->get_type(), $independent_documents, true ) ) { + return; + } + // Allow preventing document output. if ( apply_filters( 'wpo_wcpdf_document_data_meta_box_allow_document_output', false, $data, $in_process, $document ) ) { return; From d19708ad7b46e60fa2dcc3118970c8dfca4a5234 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Apr 2024 12:34:11 +0330 Subject: [PATCH 22/40] Rename a parameter --- assets/js/order-script.js | 6 +++--- includes/class-wcpdf-admin.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/assets/js/order-script.js b/assets/js/order-script.js index 8495d35a4..3153da695 100644 --- a/assets/js/order-script.js +++ b/assets/js/order-script.js @@ -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_wcpdf_ready_documents ) { return; } @@ -214,8 +214,8 @@ jQuery( function( $ ) { data: { action: 'wpo_fetch_document_data', security: wpo_wcpdf_ajax.nonce, - document_types: data.wpo_ready_documents.document_types, - order_id: data.wpo_ready_documents.order_id, + document_types: data.wpo_wcpdf_ready_documents.document_types, + order_id: data.wpo_wcpdf_ready_documents.order_id, }, success: function ( response ) { $.each( response.data, function ( key, value ) { diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 436e84b27..e197541a7 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1321,12 +1321,12 @@ public function handle_heartbeat_received( array $response, array $data ): array foreach ( $pending_documents as $document_type ) { $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); if ( $document && $document->exists() ) { - $response['wpo_ready_documents']['document_types'][] = $document_type; + $response['wpo_wcpdf_ready_documents']['document_types'][] = $document_type; } } - if ( ! empty( $response['wpo_ready_documents'] ) ) { - $response['wpo_ready_documents']['order_id'] = $order_id; + if ( ! empty( $response['wpo_wcpdf_ready_documents'] ) ) { + $response['wpo_wcpdf_ready_documents']['order_id'] = $order_id; } return $response; From bcccb26ad0b88c55181937c2901c718003f9729a Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Apr 2024 13:23:46 +0330 Subject: [PATCH 23/40] Fix not removing dependent document --- includes/class-wcpdf-admin.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index e197541a7..9a6ad7d1c 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1054,6 +1054,9 @@ public function ajax_crud_document() { ); try { + // Allow getting documents without invoice. + add_filter( 'wpo_wcpdf_allow_document_without_invoice', '__return_true' ); + $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); if( ! empty( $document ) ) { From f9cbccadc8f96d3d3c405b56de31c2a621b625d2 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Apr 2024 13:28:22 +0330 Subject: [PATCH 24/40] Check if we have an order ID --- includes/class-wcpdf-admin.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 9a6ad7d1c..82c92ca80 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1318,7 +1318,12 @@ public function handle_heartbeat_received( array $response, array $data ): array return $response; } - $order_id = $data['wc-refresh-order-lock'] ?? $data['wp-refresh-post-lock']['post_id']; + $order_id = $data['wc-refresh-order-lock'] ?? $data['wp-refresh-post-lock']['post_id']; + + if ( empty( $order_id ) ) { + return $response; + } + $pending_documents = $data['wpo_wcpdf_pending_documents']; foreach ( $pending_documents as $document_type ) { From 8f7c72c079748dcf85fbd38130bc3a7a9462ed01 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Apr 2024 14:06:09 +0330 Subject: [PATCH 25/40] Better name for a hook --- includes/class-wcpdf-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 82c92ca80..08f2c5043 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -829,7 +829,7 @@ public function output_number_date_edit_fields( object $document, $data = array( } // Allow preventing document output. - if ( apply_filters( 'wpo_wcpdf_document_data_meta_box_allow_document_output', false, $data, $in_process, $document ) ) { + if ( apply_filters( 'wpo_wcpdf_document_data_meta_box_prevent_document_output', false, $data, $in_process, $document ) ) { return; } From db3c341eda03f46a4fffabe8a63464c628837211 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Apr 2024 14:22:28 +0330 Subject: [PATCH 26/40] Use another AS method --- includes/class-wcpdf-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 08f2c5043..3e0ae4477 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -817,7 +817,7 @@ public function output_number_date_edit_fields( object $document, $data = array( $independent_documents = apply_filters( 'wpo_wcpdf_document_data_meta_box_independent_documents', array( 'invoice', 'packing-slip' ) ); $data = apply_filters( 'wpo_wcpdf_document_data_meta_box_document_data_fields', $data, $document ); $data = $this->get_current_values_for_document( $document, $data ); - $in_process = as_has_scheduled_action( 'wpo_wcpdf_generate_document_on_order_status', array( + $in_process = as_next_scheduled_action( 'wpo_wcpdf_generate_document_on_order_status', array( 'document_type' => $document->get_type(), 'order_id' => $document->order->get_id() ) ); From 46e8aa909a55b8d7077ed8b94f1cdb281332e672 Mon Sep 17 00:00:00 2001 From: MohamadNateqi Date: Fri, 12 Apr 2024 10:53:19 +0000 Subject: [PATCH 27/40] Generate minified JS and CSS files --- assets/js/order-script.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/order-script.min.js b/assets/js/order-script.min.js index 180a29436..dafccf74f 100644 --- a/assets/js/order-script.min.js +++ b/assets/js/order-script.min.js @@ -1 +1 @@ -jQuery(function(a){function b(a,b=null){a.find(".read-only").is(":visible")?("notes"==b?a.find(".editable-notes :input").attr("disabled",!1):(a.find(".editable").show(),a.find(":input").attr("disabled",!1)),a.find(".read-only").hide(),a.find(".editable-notes").show(),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").show()):(a.find(".read-only").show(),a.find(".editable").hide(),a.find(".editable-notes").hide(),a.find(":input").attr("disabled",!0),a.closest(".wcpdf-data-fields").find(".wpo-wcpdf-document-buttons").hide())}a("#doaction, #doaction2").on("click",function(b){let c=a(this).attr("id").substr(2),d=a("select[name=\""+c+"\"]").val();if(-1!==a.inArray(d,wpo_wcpdf_ajax.bulk_actions)){b.preventDefault();let c=d,e=[],f=!1;if(-1!=d.indexOf("ubl")&&(c=c.replace("_ubl",""),f=!0),a("tbody th.check-column input[type=\"checkbox\"]:checked").each(function(){e.push(a(this).val())}),!e.length)return void alert(wpo_wcpdf_ajax.select_orders);let g="",h="";if(g=-1==wpo_wcpdf_ajax.ajaxurl.indexOf("?")?wpo_wcpdf_ajax.ajaxurl+"?action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce:wpo_wcpdf_ajax.ajaxurl+"&action=generate_wpo_wcpdf&document_type="+c+"&bulk&_wpnonce="+wpo_wcpdf_ajax.nonce,f)a.each(e,function(a,b){h=g+"&order_ids="+b+"&output=ubl",window.open(h,"_blank")});else{let a=e.join("x");h=g+"&order_ids="+a,window.open(h,"_blank")}}}),wpo_wcpdf_ajax.sticky_document_data_metabox&&a("#wpo_wcpdf-data-input-box").insertAfter("#woocommerce-order-data"),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-set-date-number, .wpo-wcpdf-edit-date-number, .wpo-wcpdf-edit-document-notes",function(){let c=a(this).closest(".wcpdf-data-fields-section");0==c.length&&(c=a(this).closest(".wcpdf-data-fields"));let d=a(this).data("edit");b(c,d)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-cancel",function(){let c=a(this).closest(".wcpdf-data-fields");b(c)}),a("#wpo_wcpdf-data-input-box").on("click",".wpo-wcpdf-save-document, .wpo-wcpdf-regenerate-document, .wpo-wcpdf-delete-document",function(c){c.preventDefault();let d=a(this).closest(".wcpdf-data-fields"),e=a(this).data("action"),f=a(this).data("nonce"),g=d.data(),h=d.find(":input:visible:not(:disabled)").serialize();if("regenerate"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_regenerate))return;d.find(".wpo-wcpdf-regenerate-document").addClass("wcpdf-regenerate-spin")}else if("delete"==e){if(!1===window.confirm(wpo_wcpdf_ajax.confirm_delete))return;d.find(".wpo-wcpdf-regenerate-document").hide()}d.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a.ajax({url:wpo_wcpdf_ajax.ajaxurl,data:{action:"wpo_wcpdf_"+e+"_document",security:f,form_data:h,order_id:g.order_id,document_type:g.document,action_type:e,wpcdf_document_data_notice:e+"d"},type:"POST",context:d,success:function(c){b(d),d.closest("#wpo_wcpdf-data-input-box").load(document.URL+" #wpo_wcpdf-data-input-box .postbox-header, #wpo_wcpdf-data-input-box .inside",function(){let b;b=c.success?"success":"error",a(this).find(".wcpdf-data-fields[data-document='"+g.document+"'][data-order_id='"+g.order_id+"']").before("

"+c.data.message+"

")}),"regenerate"==e&&(d.find(".wpo-wcpdf-regenerate-document").removeClass("wcpdf-regenerate-spin"),b(d)),d.unblock()}})}),a("#wpo_wcpdf-data-input-box").on("click",".view-more, .hide-details",function(b){b.preventDefault(),a(this).hide(),a(".pdf-more-details").slideToggle("slow"),a(this).hasClass("view-more")?a(".hide-details").show():a(".view-more").show()}),a(document).on("heartbeat-send",function(b,c){let d=[];a(".wcpdf-data-fields").each(function(){"yes"===a(this).attr("data-is_pending")&&d.push(a(this).data("document"))}),0

"+c.data.message+"

")}),"regenerate"==e&&(d.find(".wpo-wcpdf-regenerate-document").removeClass("wcpdf-regenerate-spin"),b(d)),d.unblock()}})}),a("#wpo_wcpdf-data-input-box").on("click",".view-more, .hide-details",function(b){b.preventDefault(),a(this).hide(),a(".pdf-more-details").slideToggle("slow"),a(this).hasClass("view-more")?a(".hide-details").show():a(".view-more").show()}),a(document).on("heartbeat-send",function(b,c){let d=[];a(".wcpdf-data-fields").each(function(){"yes"===a(this).attr("data-is_pending")&&d.push(a(this).data("document"))}),0 Date: Mon, 15 Apr 2024 11:10:55 +0330 Subject: [PATCH 28/40] Add document number to output --- includes/views/document-data-metabox.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/views/document-data-metabox.php b/includes/views/document-data-metabox.php index 0db3806c0..63114da1d 100644 --- a/includes/views/document-data-metabox.php +++ b/includes/views/document-data-metabox.php @@ -1,6 +1,6 @@ -
+

From e424202186a3ec0014a7601aca41380b2089d3a7 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Mon, 15 Apr 2024 11:12:02 +0330 Subject: [PATCH 29/40] Revert outputting all documents in the meta box --- includes/class-wcpdf-admin.php | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 3e0ae4477..73a99deef 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -715,31 +715,15 @@ public function data_input_box_content( $post_or_order_object ) { do_action( 'wpo_wcpdf_meta_box_start', $order, $this ); - $documents = WPO_WCPDF()->documents->get_documents(); + $invoice = wcpdf_get_document( 'invoice', $order ); + $packing_slip = wcpdf_get_document( 'packing-slip', $order ); - foreach ( $documents as $document ) { - // Allow getting documents without invoice. - add_filter( 'wpo_wcpdf_allow_document_without_invoice', '__return_true' ); - - $document = wcpdf_get_document( $document->get_type(), $order ); - - if ( ! $document ) { - continue; - } - - // Bulk documents - if ( ! empty( $document->order_ids ) ) { - foreach ( $document->order_ids as $order_id ) { - $sub_order = wc_get_order( $order_id ); - $sub_document = wcpdf_get_document( $document->get_type(), $sub_order ); + if ( $invoice ) { + $this->output_number_date_edit_fields( $invoice ); + } - if ( $sub_document ) { - $this->output_number_date_edit_fields( $sub_document ); - } - } - } else { - $this->output_number_date_edit_fields( $document ); - } + if ( $packing_slip ) { + $this->output_number_date_edit_fields( $packing_slip ); } do_action( 'wpo_wcpdf_meta_box_end', $order, $this ); From a58ff0f4280b5e2ed8cbebc8dda54bdd21476ac7 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Mon, 15 Apr 2024 19:46:46 +0330 Subject: [PATCH 30/40] Improvements for the default data labels --- includes/class-wcpdf-admin.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 73a99deef..1b864ec6c 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -784,16 +784,18 @@ public function output_number_date_edit_fields( object $document, $data = array( // Go for default data. if ( empty( $data ) ) { + $document_title = $document->get_title(); + $data = array( - 'number' => array( 'label' => __( 'Number', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), - 'date' => array( 'label' => __( 'Date', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + 'number' => array( 'label' => $document_title . ' ' . __( 'Number', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + 'date' => array( 'label' => $document_title . ' ' . __( 'Date', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), ); if ( 'invoice' === $document->get_type() ) { $data = array_merge( $data, array( - 'display_date' => array( 'label' => __( 'Display date', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), - 'creation_trigger' => array( 'label' => __( 'Created via', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), - 'notes' => array( 'label' => __( 'Notes (printed in the invoice)', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + 'display_date' => array( 'label' => $document_title . ' ' . __( 'Display date', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + 'creation_trigger' => array( 'label' => $document_title . ' ' . __( 'Created via', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), + 'notes' => array( 'label' => $document_title . ' ' . __( 'Notes (printed in the invoice)', 'woocommerce-pdf-invoices-packing-slips' ) . ':' ), ) ); } } From df5852818bd01daa7d82c32a32b7a6c81b5e0eb5 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 16 Apr 2024 14:42:09 +0330 Subject: [PATCH 31/40] WPCS --- includes/class-wcpdf-admin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 1b864ec6c..e267bee4f 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1045,7 +1045,7 @@ public function ajax_crud_document() { $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); - if( ! empty( $document ) ) { + if ( ! empty( $document ) ) { // perform legacy date fields replacements check if( isset( $form_data["_wcpdf_{$document->slug}_date"] ) && ! is_array( $form_data["_wcpdf_{$document->slug}_date"] ) ) { @@ -1304,7 +1304,7 @@ public function handle_heartbeat_received( array $response, array $data ): array return $response; } - $order_id = $data['wc-refresh-order-lock'] ?? $data['wp-refresh-post-lock']['post_id']; + $order_id = (int) ( $data['wc-refresh-order-lock'] ?? $data['wp-refresh-post-lock']['post_id'] ); if ( empty( $order_id ) ) { return $response; From 0bbec3f8f885a3297a007ecd299da3fc0cda529f Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 16 Apr 2024 14:42:17 +0330 Subject: [PATCH 32/40] Improvement --- includes/class-wcpdf-admin.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index e267bee4f..384bbd400 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -715,15 +715,13 @@ public function data_input_box_content( $post_or_order_object ) { do_action( 'wpo_wcpdf_meta_box_start', $order, $this ); - $invoice = wcpdf_get_document( 'invoice', $order ); - $packing_slip = wcpdf_get_document( 'packing-slip', $order ); + $document_types = apply_filters( 'wpo_wcpdf_meta_box_documents', array( 'invoice', 'packing-slip' ), $order ); - if ( $invoice ) { - $this->output_number_date_edit_fields( $invoice ); - } - - if ( $packing_slip ) { - $this->output_number_date_edit_fields( $packing_slip ); + foreach ( $document_types as $document_type ) { + $document = wcpdf_get_document( $document_type, $order ); + if ( $document ) { + $this->output_number_date_edit_fields( $document ); + } } do_action( 'wpo_wcpdf_meta_box_end', $order, $this ); From 55b47a8df4af86bbad9bd30a21d01e3e1a458768 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 16 Apr 2024 17:43:17 +0330 Subject: [PATCH 33/40] Update the usage of a filter --- includes/class-wcpdf-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 384bbd400..6de58ea77 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1039,7 +1039,7 @@ public function ajax_crud_document() { try { // Allow getting documents without invoice. - add_filter( 'wpo_wcpdf_allow_document_without_invoice', '__return_true' ); + add_filter( 'wpo_wcpdf_require_invoice_documents', '__return_empty_array()' ); $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); From a6c59279b703c8ab239f0f297dfa727037e53ba8 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 16 Apr 2024 21:26:54 +0330 Subject: [PATCH 34/40] Introducing a new propery in Order_Document class to store prerequisite documents and a getter for it --- includes/class-wcpdf-admin.php | 4 ++-- includes/documents/abstract-wcpdf-order-document.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 6de58ea77..b94ae68e1 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1038,8 +1038,8 @@ public function ajax_crud_document() { ); try { - // Allow getting documents without invoice. - add_filter( 'wpo_wcpdf_require_invoice_documents', '__return_empty_array()' ); + // Allow getting documents even if their prerequisite documents doesn't exist. + add_filter( 'wpo_wcpdf_prerequisite_documents', '__return_empty_array()' ); $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index bb6daeff0..d8815bccf 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -128,6 +128,12 @@ abstract class Order_Document { */ protected $data = array(); + /** + * Documents prerequisite + * @var string[] + */ + protected $prerequisite_documents = array(); + /** * Init/load the order object. * @@ -1528,6 +1534,10 @@ public function get_due_date(): int { return apply_filters( 'wpo_wcpdf_due_date', $due_date_datetime->getTimestamp() ?? 0, $this->type, $this ); } + public function get_prerequisite_documents(): array { + return apply_filters( 'wpo_wcpdf_prerequisite_documents', $this->prerequisite_documents, $this->type, $this ); + } + protected function add_filters( $filters ) { foreach ( $filters as $filter ) { $filter = $this->normalize_filter_args( $filter ); From 48d03715bf5e586a79098d22eb181d9045ef5c95 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 17 Apr 2024 11:29:28 +0330 Subject: [PATCH 35/40] Check document prerequisites in `is_allowed()` method --- .../documents/abstract-wcpdf-order-document.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index d8815bccf..bc6e2e5fb 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -516,7 +516,7 @@ public function regenerate( $order = null, $data = null ) { do_action( 'wpo_wcpdf_regenerate_document', $this ); } - public function is_allowed() { + public function is_allowed(): bool { $allowed = true; // Check if document is enabled if ( ! $this->is_enabled() ) { @@ -533,7 +533,17 @@ public function is_allowed() { if ( in_array( $status, $disabled_statuses ) ) { $allowed = false; } - } + } + + // Check document prerequisites + foreach ( $this->get_prerequisite_documents() as $prerequisite_document_type ) { + $prerequisite_document = wcpdf_get_document( $prerequisite_document_type, $this->order ); + + if ( $prerequisite_document && ! $prerequisite_document->exists() ) { + $allowed = false; + } + } + return apply_filters( 'wpo_wcpdf_document_is_allowed', $allowed, $this ); } From 2a9d4e9c9fe17949159cf39325438fbd05149dd7 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 17 Apr 2024 14:25:28 +0330 Subject: [PATCH 36/40] Introduce the new `do_prerequisites_exist()` method --- includes/class-wcpdf-frontend.php | 2 +- includes/documents/abstract-wcpdf-order-document.php | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/includes/class-wcpdf-frontend.php b/includes/class-wcpdf-frontend.php index 31cf76e37..108cfbb55 100644 --- a/includes/class-wcpdf-frontend.php +++ b/includes/class-wcpdf-frontend.php @@ -167,7 +167,7 @@ public function generate_document_shortcode( $atts, $content = null, $shortcode_ $document = wcpdf_get_document( $values['document_type'], $order ); - if ( ! $document || ! $document->is_allowed() ) { + if ( ! $document || ! $document->is_allowed() || ! $document->do_prerequisites_exist() ) { return; } diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index bc6e2e5fb..707a106c8 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -535,16 +535,22 @@ public function is_allowed(): bool { } } - // Check document prerequisites + return apply_filters( 'wpo_wcpdf_document_is_allowed', $allowed, $this ); + } + + public function do_prerequisites_exist(): bool { + $prerequisites_exist = true; + foreach ( $this->get_prerequisite_documents() as $prerequisite_document_type ) { $prerequisite_document = wcpdf_get_document( $prerequisite_document_type, $this->order ); if ( $prerequisite_document && ! $prerequisite_document->exists() ) { - $allowed = false; + $prerequisites_exist = false; + break; } } - return apply_filters( 'wpo_wcpdf_document_is_allowed', $allowed, $this ); + return apply_filters( 'wpo_wcpdf_document_do_prerequisites_exist', $prerequisites_exist, $this ); } public function exists() { From 239384893aca8deea8c5f8cf1873a24d69bc0e56 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 17 Apr 2024 14:26:37 +0330 Subject: [PATCH 37/40] Remove an unnecessary filter --- includes/class-wcpdf-admin.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index b94ae68e1..d9cea1168 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -1038,9 +1038,6 @@ public function ajax_crud_document() { ); try { - // Allow getting documents even if their prerequisite documents doesn't exist. - add_filter( 'wpo_wcpdf_prerequisite_documents', '__return_empty_array()' ); - $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); if ( ! empty( $document ) ) { From 8c6ed824e586f1eb04f682d24808800a6bf75a7a Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 17 Apr 2024 14:42:55 +0330 Subject: [PATCH 38/40] Update `pdf_actions_meta_box()` method --- includes/class-wcpdf-admin.php | 70 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index d9cea1168..464a3b584 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -564,41 +564,43 @@ public function pdf_actions_meta_box( $post_or_order_object ) { $documents = WPO_WCPDF()->documents->get_documents(); foreach ( $documents as $document ) { - $document_title = $document->get_title(); - $document = wcpdf_get_document( $document->get_type(), $order ); - - if ( $document ) { - $document_url = WPO_WCPDF()->endpoint->get_document_link( $order, $document->get_type() ); - $document_title = is_callable( array( $document, 'get_title' ) ) ? $document->get_title() : $document_title; - $document_exists = is_callable( array( $document, 'exists' ) ) ? $document->exists() : false; - $document_printed = $document_exists && is_callable( array( $document, 'printed' ) ) ? $document->printed() : false; - $document_printed_data = $document_exists && $document_printed && is_callable( array( $document, 'get_printed_data' ) ) ? $document->get_printed_data() : []; - $document_settings = get_option( 'wpo_wcpdf_documents_settings_'.$document->get_type() ); // $document-settings might be not updated with the last settings - $unmark_printed_url = ! empty( $document_printed_data ) && isset( $document_settings['unmark_printed'] ) ? WPO_WCPDF()->endpoint->get_document_printed_link( 'unmark', $order, $document->get_type() ) : false; - $manually_mark_printed = WPO_WCPDF()->main->document_can_be_manually_marked_printed( $document ); - $mark_printed_url = $manually_mark_printed ? WPO_WCPDF()->endpoint->get_document_printed_link( 'mark', $order, $document->get_type() ) : false; - $class = [ $document->get_type() ]; - - if ( $document_exists ) { - $class[] = 'exists'; - } - if ( $document_printed ) { - $class[] = 'printed'; - } - - $meta_box_actions[$document->get_type()] = array( - 'url' => esc_url( $document_url ), - 'alt' => "PDF " . $document_title, - 'title' => "PDF " . $document_title, - 'exists' => $document_exists, - 'printed' => $document_printed, - 'printed_data' => $document_printed_data, - 'unmark_printed_url' => $unmark_printed_url, - 'manually_mark_printed' => $manually_mark_printed, - 'mark_printed_url' => $mark_printed_url, - 'class' => apply_filters( 'wpo_wcpdf_action_button_class', implode( ' ', $class ), $document ), - ); + $document = wcpdf_get_document( $document->get_type(), $order ); + + if ( ! $document || ! $document->do_prerequisites_exist() ) { + continue; + } + + $document_title = $document->get_title(); + $document_url = WPO_WCPDF()->endpoint->get_document_link( $order, $document->get_type() ); + $document_title = is_callable( array( $document, 'get_title' ) ) ? $document->get_title() : $document_title; + $document_exists = is_callable( array( $document, 'exists' ) ) ? $document->exists() : false; + $document_printed = $document_exists && is_callable( array( $document, 'printed' ) ) ? $document->printed() : false; + $document_printed_data = $document_exists && $document_printed && is_callable( array( $document, 'get_printed_data' ) ) ? $document->get_printed_data() : []; + $document_settings = get_option( 'wpo_wcpdf_documents_settings_'.$document->get_type() ); // $document-settings might be not updated with the last settings + $unmark_printed_url = ! empty( $document_printed_data ) && isset( $document_settings['unmark_printed'] ) ? WPO_WCPDF()->endpoint->get_document_printed_link( 'unmark', $order, $document->get_type() ) : false; + $manually_mark_printed = WPO_WCPDF()->main->document_can_be_manually_marked_printed( $document ); + $mark_printed_url = $manually_mark_printed ? WPO_WCPDF()->endpoint->get_document_printed_link( 'mark', $order, $document->get_type() ) : false; + $class = [ $document->get_type() ]; + + if ( $document_exists ) { + $class[] = 'exists'; } + if ( $document_printed ) { + $class[] = 'printed'; + } + + $meta_box_actions[ $document->get_type() ] = array( + 'url' => esc_url( $document_url ), + 'alt' => "PDF " . $document_title, + 'title' => "PDF " . $document_title, + 'exists' => $document_exists, + 'printed' => $document_printed, + 'printed_data' => $document_printed_data, + 'unmark_printed_url' => $unmark_printed_url, + 'manually_mark_printed' => $manually_mark_printed, + 'mark_printed_url' => $mark_printed_url, + 'class' => apply_filters( 'wpo_wcpdf_action_button_class', implode( ' ', $class ), $document ), + ); } $meta_box_actions = apply_filters( 'wpo_wcpdf_meta_box_actions', $meta_box_actions, $order->get_id() ); From c391c99feb69851b5fe08f1ff78d100b7fa60f81 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Wed, 17 Apr 2024 16:06:55 +0330 Subject: [PATCH 39/40] Updates --- includes/class-wcpdf-admin.php | 8 +++++++- includes/wcpdf-functions.php | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 464a3b584..94a5a08fe 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -566,7 +566,7 @@ public function pdf_actions_meta_box( $post_or_order_object ) { foreach ( $documents as $document ) { $document = wcpdf_get_document( $document->get_type(), $order ); - if ( ! $document || ! $document->do_prerequisites_exist() ) { + if ( ! $document || ( ! $document->exists() && ! $document->do_prerequisites_exist() ) ) { continue; } @@ -719,6 +719,9 @@ public function data_input_box_content( $post_or_order_object ) { $document_types = apply_filters( 'wpo_wcpdf_meta_box_documents', array( 'invoice', 'packing-slip' ), $order ); + // Allow getting documents even if their prerequisite documents doesn't exist. + add_filter( 'wpo_wcpdf_document_do_prerequisites_exist', '__return_true' ); + foreach ( $document_types as $document_type ) { $document = wcpdf_get_document( $document_type, $order ); if ( $document ) { @@ -1040,6 +1043,9 @@ public function ajax_crud_document() { ); try { + // Allow getting documents even if their prerequisite documents doesn't exist. + add_filter( 'wpo_wcpdf_document_do_prerequisites_exist', '__return_true' ); + $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); if ( ! empty( $document ) ) { diff --git a/includes/wcpdf-functions.php b/includes/wcpdf-functions.php index b74815dce..98b8f136b 100644 --- a/includes/wcpdf-functions.php +++ b/includes/wcpdf-functions.php @@ -46,7 +46,7 @@ function wcpdf_get_document( $document_type, $order, $init = false ) { do_action( 'wpo_wcpdf_process_template_order', $document_type, $order->get_id() ); $document = WPO_WCPDF()->documents->get_document( $document_type, $order ); - if ( ! $document || ! is_callable( array( $document, 'is_allowed' ) ) || ! $document->is_allowed() ) { + if ( ! $document || ! $document->is_allowed() || ! $document->do_prerequisites_exist() ) { return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init ); } @@ -79,7 +79,7 @@ function wcpdf_get_document( $document_type, $order, $init = false ) { $document = WPO_WCPDF()->documents->get_document( $document_type, $order ); - if ( ! $document->is_allowed() ) { + if ( ! $document || ! $document->is_allowed() || ! $document->do_prerequisites_exist() ) { return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init ); } From 08cff804ddb50a6a542f62f868a7a76c5fc59680 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Thu, 18 Apr 2024 10:37:52 +0330 Subject: [PATCH 40/40] Revert changes and improvements --- includes/class-wcpdf-admin.php | 4 ++-- .../abstract-wcpdf-order-document.php | 22 ++++++++----------- includes/wcpdf-functions.php | 4 ++-- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/includes/class-wcpdf-admin.php b/includes/class-wcpdf-admin.php index 94a5a08fe..47d571dfd 100644 --- a/includes/class-wcpdf-admin.php +++ b/includes/class-wcpdf-admin.php @@ -566,7 +566,7 @@ public function pdf_actions_meta_box( $post_or_order_object ) { foreach ( $documents as $document ) { $document = wcpdf_get_document( $document->get_type(), $order ); - if ( ! $document || ( ! $document->exists() && ! $document->do_prerequisites_exist() ) ) { + if ( ! $document ) { continue; } @@ -1044,7 +1044,7 @@ public function ajax_crud_document() { try { // Allow getting documents even if their prerequisite documents doesn't exist. - add_filter( 'wpo_wcpdf_document_do_prerequisites_exist', '__return_true' ); + add_filter( 'wpo_wcpdf_bypass_prerequisites_restriction', '__return_true' ); $document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) ); diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 707a106c8..4a905c1b9 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -516,7 +516,7 @@ public function regenerate( $order = null, $data = null ) { do_action( 'wpo_wcpdf_regenerate_document', $this ); } - public function is_allowed(): bool { + public function is_allowed( $context = ''): bool { $allowed = true; // Check if document is enabled if ( ! $this->is_enabled() ) { @@ -535,22 +535,18 @@ public function is_allowed(): bool { } } - return apply_filters( 'wpo_wcpdf_document_is_allowed', $allowed, $this ); - } - - public function do_prerequisites_exist(): bool { - $prerequisites_exist = true; + // Check document prerequisites + if ( false === apply_filters( 'wpo_wcpdf_bypass_prerequisites_restriction', false, $context, $this ) && ! empty( $this->get_prerequisite_documents() ) ) { + foreach ( $this->get_prerequisite_documents() as $prerequisite_document_type ) { + $prerequisite_document = wcpdf_get_document( $prerequisite_document_type, $this->order ); - foreach ( $this->get_prerequisite_documents() as $prerequisite_document_type ) { - $prerequisite_document = wcpdf_get_document( $prerequisite_document_type, $this->order ); - - if ( $prerequisite_document && ! $prerequisite_document->exists() ) { - $prerequisites_exist = false; - break; + if ( $prerequisite_document && ! $prerequisite_document->exists() ) { + $allowed = false; + } } } - return apply_filters( 'wpo_wcpdf_document_do_prerequisites_exist', $prerequisites_exist, $this ); + return apply_filters( 'wpo_wcpdf_document_is_allowed', $allowed, $this ); } public function exists() { diff --git a/includes/wcpdf-functions.php b/includes/wcpdf-functions.php index 98b8f136b..2ebd0a1de 100644 --- a/includes/wcpdf-functions.php +++ b/includes/wcpdf-functions.php @@ -46,7 +46,7 @@ function wcpdf_get_document( $document_type, $order, $init = false ) { do_action( 'wpo_wcpdf_process_template_order', $document_type, $order->get_id() ); $document = WPO_WCPDF()->documents->get_document( $document_type, $order ); - if ( ! $document || ! $document->is_allowed() || ! $document->do_prerequisites_exist() ) { + if ( ! $document || ! $document->is_allowed() ) { return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init ); } @@ -79,7 +79,7 @@ function wcpdf_get_document( $document_type, $order, $init = false ) { $document = WPO_WCPDF()->documents->get_document( $document_type, $order ); - if ( ! $document || ! $document->is_allowed() || ! $document->do_prerequisites_exist() ) { + if ( ! $document || ! $document->is_allowed() ) { return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init ); }