From 6c5702d40b342915fb212b579eb4f96038eed115 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Jul 2024 15:10:59 +0330 Subject: [PATCH 1/7] Fix document setting emptiness for preview action --- includes/documents/abstract-wcpdf-order-document.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index cc62e7110..057075a08 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -170,18 +170,18 @@ public function init_settings() { return; } - public function init_settings_data() { - // don't override/save settings on Preview requests - if ( isset( $_REQUEST['action'] ) && 'wpo_wcpdf_preview' === $_REQUEST['action'] ) { - return; - } - + public function init_settings_data(): void { // order $this->order_settings = $this->get_order_settings(); // pdf $this->settings = $this->get_settings(); $this->latest_settings = $this->get_settings( true ); + // don't override/save settings on Preview requests + if ( isset( $_REQUEST['action'] ) && 'wpo_wcpdf_preview' === $_REQUEST['action'] ) { + return; + } + // save settings $this->save_settings( $this->maybe_use_latest_settings() ); } From a021d54fea48c5c346b1fd4a6dc4dd90bfbb26d9 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Jul 2024 15:13:22 +0330 Subject: [PATCH 2/7] Define a new `initiate_date` method --- .../abstract-wcpdf-order-document-methods.php | 29 ++++++------------- .../abstract-wcpdf-order-document.php | 10 +++++++ includes/documents/class-wcpdf-invoice.php | 10 +------ 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document-methods.php b/includes/documents/abstract-wcpdf-order-document-methods.php index 5910af0ff..e09d5d782 100644 --- a/includes/documents/abstract-wcpdf-order-document-methods.php +++ b/includes/documents/abstract-wcpdf-order-document-methods.php @@ -1282,36 +1282,25 @@ public function document_notes(): void { } } - public function document_display_date() { + public function document_display_date(): string { $document_display_date = $this->get_display_date( $this->get_type() ); - //If display date data is not available in order meta (for older orders), get the display date information from document settings order meta. + // If display date data is not available in order meta (for older orders), get the display date information from document settings order meta. if ( empty( $document_display_date ) ) { - $document_settings = $this->settings; - if( isset( $document_settings['display_date'] ) ) { - $document_display_date = $document_settings['display_date']; - } - else { - $document_display_date = 'invoice_date'; - } + $document_settings = $this->settings; + $document_display_date = $document_settings['display_date'] ?? 'document_date'; } - $formatted_value = $this->get_display_date_label( $document_display_date ); - return $formatted_value; + return $this->get_display_date_label( $document_display_date ); } - public function get_display_date_label( $date_string ) { - + public function get_display_date_label( string $date_string ): string { $date_labels = array( - 'invoice_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ), - 'order_date' => __( 'Order Date' , 'woocommerce-pdf-invoices-packing-slips' ), + 'invoice_date' => __( 'Document Date', 'woocommerce-pdf-invoices-packing-slips' ), + 'order_date' => __( 'Order Date', 'woocommerce-pdf-invoices-packing-slips' ), ); - if( isset( $date_labels[$date_string] ) ) { - return $date_labels[ $date_string ]; - } else { - return ''; - } + return $date_labels[ $date_string ] ?? ''; } } diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 057075a08..4fd0ac894 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -262,6 +262,16 @@ public function save_settings( $latest = false ) { } } + public function initiate_date(): void { + if ( isset( $this->settings['display_date'] ) && $this->settings['display_date'] === 'order_date' && ! empty( $this->order ) ) { + $this->set_date( $this->order->get_date_created() ); + $this->set_display_date( 'order_date' ); + } elseif ( empty( $this->get_date() ) ) { + $this->set_date( current_time( 'timestamp', true ) ); + $this->set_display_date( 'document_date' ); + } + } + public function initiate_number( $force_new_number = false ) { $lock = new Semaphore( $this->lock_name, $this->lock_time, $this->lock_loggers, $this->lock_context ); $document_number = $this->exists() ? $this->get_data( 'number' ) : null; diff --git a/includes/documents/class-wcpdf-invoice.php b/includes/documents/class-wcpdf-invoice.php index d06cc780f..5ec3537a6 100644 --- a/includes/documents/class-wcpdf-invoice.php +++ b/includes/documents/class-wcpdf-invoice.php @@ -59,15 +59,7 @@ public function get_title() { public function init() { // save settings $this->save_settings(); - - if ( isset( $this->settings['display_date'] ) && $this->settings['display_date'] == 'order_date' && !empty( $this->order ) ) { - $this->set_date( $this->order->get_date_created() ); - $this->set_display_date( 'order_date' ); - } elseif( empty( $this->get_date() ) ) { - $this->set_date( current_time( 'timestamp', true ) ); - $this->set_display_date( 'invoice_date' ); - } - + $this->initiate_date(); $this->initiate_number(); do_action( 'wpo_wcpdf_init_document', $this ); From 633b6eef476bb7ac61be58ed1638468753ee91a2 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Fri, 12 Jul 2024 15:23:24 +0330 Subject: [PATCH 3/7] Define a new `generate_document_number` method, Fix updating number and date on preview --- includes/class-wcpdf-settings.php | 18 ++++- .../abstract-wcpdf-order-document.php | 76 ++++++++++++------- 2 files changed, 65 insertions(+), 29 deletions(-) diff --git a/includes/class-wcpdf-settings.php b/includes/class-wcpdf-settings.php index 07a6a1e5f..ea87da79b 100644 --- a/includes/class-wcpdf-settings.php +++ b/includes/class-wcpdf-settings.php @@ -320,11 +320,23 @@ public function ajax_preview() { $document->set_number( $number_store->get_next() ); } - // apply document number formatting - if ( $document_number = $document->get_number( $document->get_type() ) ) { + // Update document date. + $document->initiate_date(); + + // Update document number. + $document_number = $document->generate_document_number(); + + if ( ! empty( $document_number ) ) { + $document->set_number( $document_number ); + } + + $document_number = $document->get_number( $document->get_type() ); + + // Apply document number formatting. + if ( $document_number ) { if ( ! empty( $document->settings['number_format'] ) ) { foreach ( $document->settings['number_format'] as $key => $value ) { - $document_number->$key = $document->settings['number_format'][$key]; + $document_number->$key = $document->settings['number_format'][ $key ]; } } $document_number->apply_formatting( $document, $order ); diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 4fd0ac894..2b7de49d9 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -272,7 +272,14 @@ public function initiate_date(): void { } } - public function initiate_number( $force_new_number = false ) { + /** + * Initiate and set document number. + * + * @param bool $force_new_number + * + * @return mixed + */ + public function initiate_number( bool $force_new_number = false ) { $lock = new Semaphore( $this->lock_name, $this->lock_time, $this->lock_loggers, $this->lock_context ); $document_number = $this->exists() ? $this->get_data( 'number' ) : null; $document_number = ! empty( $document_number ) && $force_new_number ? null : $document_number; @@ -281,35 +288,12 @@ public function initiate_number( $force_new_number = false ) { $lock->log( "Lock acquired for the {$this->slug} number init.", 'info' ); try { - // If a third-party plugin claims to generate document numbers, trigger this instead - if ( apply_filters( "woocommerce_{$this->slug}_number_by_plugin", false ) || apply_filters( "wpo_wcpdf_external_{$this->slug}_number_enabled", false, $this ) ) { - $document_number = apply_filters( "woocommerce_generate_{$this->slug}_number", $document_number, $this->order ); // legacy (backwards compatibility) - $document_number = apply_filters( "woocommerce_{$this->slug}_number", $document_number, $this->order->get_id() ); // legacy (backwards compatibility) - $document_number = apply_filters( "wpo_wcpdf_external_{$this->slug}_number", $document_number, $this ); - } elseif ( isset( $this->settings['display_number'] ) && 'order_number' === $this->settings['display_number'] && ! empty( $this->order ) ) { - $document_number = $this->order->get_order_number(); - } - - if ( ! empty( $document_number ) ) { // overridden by plugin or set to order number - if ( ! is_numeric( $document_number ) && ! ( $document_number instanceof Document_Number ) ) { - // document number is not numeric, treat as formatted - // try to extract meaningful number data - $formatted_number = $document_number; - $number = (int) preg_replace( '/\D/', '', $document_number ); - $document_number = compact( 'number', 'formatted_number' ); - } - } else { - $number_store = $this->get_sequential_number_store(); - $document_number = $number_store->increment( intval( $this->order_id ), $this->get_date()->date_i18n( 'Y-m-d H:i:s' ) ); - } + $document_number = $this->generate_document_number( true ); if ( ! is_null( $document_number ) ) { $this->set_number( $document_number ); } - - } catch ( \Exception $e ) { - $lock->log( $e, 'critical' ); - } catch ( \Error $e ) { + } catch ( \Exception|\Error $e ) { $lock->log( $e, 'critical' ); } @@ -324,6 +308,46 @@ public function initiate_number( $force_new_number = false ) { return $document_number; } + /** + * Generate the document number. + * + * @param bool $increment_document_number + * + * @return mixed + */ + public function generate_document_number( bool $increment_document_number = false ) { + $document_number = null; + + // If a third-party plugin claims to generate document numbers, trigger this instead + if ( apply_filters( "woocommerce_{$this->slug}_number_by_plugin", false ) || apply_filters( "wpo_wcpdf_external_{$this->slug}_number_enabled", false, $this ) ) { + $document_number = apply_filters( "woocommerce_generate_{$this->slug}_number", $document_number, $this->order ); // legacy (backwards compatibility) + $document_number = apply_filters( "woocommerce_{$this->slug}_number", $document_number, $this->order->get_id() ); // legacy (backwards compatibility) + $document_number = apply_filters( "wpo_wcpdf_external_{$this->slug}_number", $document_number, $this ); + } elseif ( isset( $this->settings['display_number'] ) && 'order_number' === $this->settings['display_number'] && ! empty( $this->order ) ) { + $document_number = $this->order->get_order_number(); + } + + if ( ! empty( $document_number ) ) { // overridden by plugin or set to order number + if ( ! is_numeric( $document_number ) && ! ( $document_number instanceof Document_Number ) ) { + // document number is not numeric, treat as formatted + // try to extract meaningful number data + $formatted_number = $document_number; + $number = (int) preg_replace( '/\D/', '', $document_number ); + $document_number = compact( 'number', 'formatted_number' ); + } + } else { + $number_store = $this->get_sequential_number_store(); + + if ( $increment_document_number ) { + $document_number = $number_store->increment( intval( $this->order_id ), $this->get_date()->date_i18n( 'Y-m-d H:i:s' ) ); + } else { + $document_number = $number_store->get_next(); + } + } + + return $document_number; + } + public function maybe_use_latest_settings() { return ! $this->use_historical_settings(); } From 76e5ffade41aa281999555b094b4af44622f514f Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 13 Aug 2024 12:30:41 +0330 Subject: [PATCH 4/7] Improvements --- .../documents/abstract-wcpdf-order-document.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 297afadf8..2fe4c1941 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -262,8 +262,13 @@ public function save_settings( $latest = false ) { } } + /** + * Initiate and set document date and display_date. + * + * @return void + */ public function initiate_date(): void { - if ( isset( $this->settings['display_date'] ) && $this->settings['display_date'] === 'order_date' && ! empty( $this->order ) ) { + if ( isset( $this->settings['display_date'] ) && 'order_date' === $this->settings['display_date'] && ! empty( $this->order ) ) { $this->set_date( $this->order->get_date_created() ); $this->set_display_date( 'order_date' ); } elseif ( empty( $this->get_date() ) ) { @@ -311,11 +316,11 @@ public function initiate_number( bool $force_new_number = false ) { /** * Generate the document number. * - * @param bool $increment_document_number + * @param bool $increment * * @return mixed */ - public function generate_document_number( bool $increment_document_number = false ) { + public function generate_document_number( bool $increment = false ) { $document_number = null; // If a third-party plugin claims to generate document numbers, trigger this instead @@ -338,7 +343,7 @@ public function generate_document_number( bool $increment_document_number = fals } else { $number_store = $this->get_sequential_number_store(); - if ( $increment_document_number ) { + if ( $increment ) { $document_number = $number_store->increment( intval( $this->order_id ), $this->get_date()->date_i18n( 'Y-m-d H:i:s' ) ); } else { $document_number = $number_store->get_next(); @@ -854,7 +859,7 @@ public function has_header_logo() { * @return int */ public function get_header_logo_id(): int { - $header_logo_id = ! empty( $this->settings['header_logo'] ) ? $this->get_settings_text( 'header_logo', 0, false ) : 0; + $header_logo_id = ! empty( $this->settings['header_logo'] ) ? $this->get_settings_text( 'header_logo', 0, false ) : 0; $header_logo_id = apply_filters( 'wpo_wcpdf_header_logo_id', $header_logo_id, $this ); return $header_logo_id && is_numeric( $header_logo_id ) ? absint( $header_logo_id ) : 0; @@ -888,7 +893,7 @@ public function header_logo(): void { } $src = apply_filters( 'wpo_wcpdf_use_path', true ) ? $attachment_path : $attachment_src; - + // fix URLs using path if ( ! apply_filters( 'wpo_wcpdf_use_path', true ) && false !== strpos( $src, 'http' ) && false !== strpos( $src, WP_CONTENT_DIR ) ) { $path = preg_replace( '/^https?:\/\//', '', $src ); // removes http(s):// From 0749c90f402b94aa969fee193b924f05e6b582a0 Mon Sep 17 00:00:00 2001 From: Mohamad Nateqi Rostami Date: Tue, 13 Aug 2024 12:32:17 +0330 Subject: [PATCH 5/7] Change `invoice_date` to `document_date` --- .../documents/abstract-wcpdf-order-document-methods.php | 4 ++-- includes/documents/abstract-wcpdf-order-document.php | 2 +- includes/documents/class-wcpdf-invoice.php | 2 +- includes/views/setup-wizard/display-options.php | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/documents/abstract-wcpdf-order-document-methods.php b/includes/documents/abstract-wcpdf-order-document-methods.php index e09d5d782..ef0a73311 100644 --- a/includes/documents/abstract-wcpdf-order-document-methods.php +++ b/includes/documents/abstract-wcpdf-order-document-methods.php @@ -1296,8 +1296,8 @@ public function document_display_date(): string { public function get_display_date_label( string $date_string ): string { $date_labels = array( - 'invoice_date' => __( 'Document Date', 'woocommerce-pdf-invoices-packing-slips' ), - 'order_date' => __( 'Order Date', 'woocommerce-pdf-invoices-packing-slips' ), + 'document_date' => __( 'Document Date', 'woocommerce-pdf-invoices-packing-slips' ), + 'order_date' => __( 'Order Date', 'woocommerce-pdf-invoices-packing-slips' ), ); return $date_labels[ $date_string ] ?? ''; diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 2fe4c1941..7413f2e3b 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -253,7 +253,7 @@ public function save_settings( $latest = false ) { if ( isset( $settings['display_date'] ) && 'order_date' === $settings['display_date'] ) { $this->order->update_meta_data( "_wcpdf_{$this->slug}_display_date", 'order_date' ); } else { - $this->order->update_meta_data( "_wcpdf_{$this->slug}_display_date", 'invoice_date' ); + $this->order->update_meta_data( "_wcpdf_{$this->slug}_display_date", 'document_date' ); } } diff --git a/includes/documents/class-wcpdf-invoice.php b/includes/documents/class-wcpdf-invoice.php index 5ec3537a6..11be3f4c9 100644 --- a/includes/documents/class-wcpdf-invoice.php +++ b/includes/documents/class-wcpdf-invoice.php @@ -269,7 +269,7 @@ public function get_pdf_settings_fields( $option_name ) { 'id' => 'display_date', 'options' => array( '' => __( 'No' , 'woocommerce-pdf-invoices-packing-slips' ), - 'invoice_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ), + 'document_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ), 'order_date' => __( 'Order Date' , 'woocommerce-pdf-invoices-packing-slips' ), ), ) diff --git a/includes/views/setup-wizard/display-options.php b/includes/views/setup-wizard/display-options.php index 8aae2115b..7ff7fbbcd 100644 --- a/includes/views/setup-wizard/display-options.php +++ b/includes/views/setup-wizard/display-options.php @@ -56,9 +56,9 @@