diff --git a/includes/Documents/Invoice.php b/includes/Documents/Invoice.php index 0782d0a9a..5b71b5046 100644 --- a/includes/Documents/Invoice.php +++ b/includes/Documents/Invoice.php @@ -95,15 +95,7 @@ public function get_shipping_address_title(): string { 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 ); @@ -313,7 +305,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/Documents/OrderDocument.php b/includes/Documents/OrderDocument.php index 738d02275..e6f78518a 100644 --- a/includes/Documents/OrderDocument.php +++ b/includes/Documents/OrderDocument.php @@ -135,18 +135,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() ); } @@ -218,7 +218,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' ); } } @@ -227,7 +227,29 @@ public function save_settings( $latest = false ) { } } - public function initiate_number( $force_new_number = false ) { + /** + * Initiate and set document date and display_date. + * + * @return void + */ + public function initiate_date(): void { + 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() ) ) { + $this->set_date( current_time( 'timestamp', true ) ); + $this->set_display_date( 'document_date' ); + } + } + + /** + * Initiate and set document number. + * + * @param bool $force_new_number + * + * @return mixed + */ + public function initiate_number( bool $force_new_number = false ) { $lock = new Semaphore( "initiate_{$this->slug}_number" ); $document_number = $this->exists() ? $this->get_data( 'number' ) : null; $document_number = ! empty( $document_number ) && $force_new_number ? null : $document_number; @@ -236,35 +258,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 DocumentNumber ) ) { - // 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' ); } @@ -279,6 +278,46 @@ public function initiate_number( $force_new_number = false ) { return $document_number; } + /** + * Generate the document number. + * + * @param bool $increment + * + * @return mixed + */ + public function generate_document_number( bool $increment = 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 DocumentNumber ) ) { + // 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 = $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(); } diff --git a/includes/Documents/OrderDocumentMethods.php b/includes/Documents/OrderDocumentMethods.php index eddc6aff8..dfe896c6b 100644 --- a/includes/Documents/OrderDocumentMethods.php +++ b/includes/Documents/OrderDocumentMethods.php @@ -1282,36 +1282,34 @@ 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; - } + // Convert the old `invoice_date` slug to the new `document_date` slug. + if ( 'invoice_date' === $document_display_date ) { + $document_display_date = 'document_date'; + } - public function get_display_date_label( $date_string ) { + return $this->get_display_date_label( $document_display_date ); + } + 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' ), + 'document_date' => sprintf( + /* translators: Document title */ + __( '%s Date', 'woocommerce-pdf-invoices-packing-slips' ), + $this->title + ), + '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/Settings.php b/includes/Settings.php index bb9afac78..a7b50df2f 100644 --- a/includes/Settings.php +++ b/includes/Settings.php @@ -309,11 +309,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/views/setup-wizard/display-options.php b/views/setup-wizard/display-options.php index 8aae2115b..7ff7fbbcd 100644 --- a/views/setup-wizard/display-options.php +++ b/views/setup-wizard/display-options.php @@ -56,9 +56,9 @@