Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix preview issue #833

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
18 changes: 15 additions & 3 deletions includes/class-wcpdf-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
29 changes: 9 additions & 20 deletions includes/documents/abstract-wcpdf-order-document-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
}

$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' ),
'document_date' => __( 'Document Date', 'woocommerce-pdf-invoices-packing-slips' ),
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
'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 ] ?? '';
}

}
Expand Down
109 changes: 74 additions & 35 deletions includes/documents/abstract-wcpdf-order-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
Expand Down Expand Up @@ -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' );
}
}

Expand All @@ -262,7 +262,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 {
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
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( $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;
Expand All @@ -271,35 +293,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' );
}

Expand All @@ -314,6 +313,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 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 = $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();
}
Expand Down Expand Up @@ -820,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;
Expand Down Expand Up @@ -854,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)://
Expand Down
12 changes: 2 additions & 10 deletions includes/documents/class-wcpdf-invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -277,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' ),
),
)
Expand Down
6 changes: 3 additions & 3 deletions includes/views/setup-wizard/display-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
<select id="display-date" name="wcpdf_settings[wpo_wcpdf_documents_settings_invoice][display_date]">
<?php
$options = array(
'' => __( 'No' , 'woocommerce-pdf-invoices-packing-slips' ),
'invoice_date' => __( 'Invoice Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'order_date' => __( 'Order Date' , 'woocommerce-pdf-invoices-packing-slips' ),
'' => __( 'No', 'woocommerce-pdf-invoices-packing-slips' ),
'document_date' => __( 'Invoice Date', 'woocommerce-pdf-invoices-packing-slips' ),
'order_date' => __( 'Order Date', 'woocommerce-pdf-invoices-packing-slips' ),
);
foreach ( $options as $slug => $name ) {
$selected = ( ! empty( $current_settings['display_date'] ) && $current_settings['display_date'] == $slug ) ? 'selected' : '';
Expand Down
Loading