first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
<?php
namespace WPO\WC\PDF_Invoices\Documents;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( '\\WPO\\WC\\PDF_Invoices\\Documents\\Bulk_Document' ) ) :
/**
* Bulk Document
*
* Wraps single documents in a bulk document
*/
class Bulk_Document {
/**
* Document type.
* @var String
*/
public $type;
/**
* Wrapper document - used for filename etc.
* @var String
*/
public $wrapper_document;
/**
* Order IDs.
* @var array
*/
public $order_ids;
public $is_bulk;
public $output_formats;
public function __construct( $document_type, $order_ids = array() ) {
$this->type = $document_type;
$this->order_ids = $order_ids;
$this->is_bulk = true;
// output formats (placed after parent construct to override the abstract default)
$this->output_formats = apply_filters( "wpo_wcpdf_{$this->type}_output_formats", array( 'pdf' ), $this );
}
public function exists() {
$exists = false;
foreach ( $this->order_ids as $order_id ) {
$document = wcpdf_get_document( $this->type, $order_id );
if ( $document && is_callable( array( $document, 'exists' ) ) && $document->exists() ) {
$exists = true;
break;
}
}
return $exists;
}
public function is_enabled( $output_format = 'pdf' ) {
if ( in_array( $output_format, $this->output_formats ) ) {
return true;
}
return false;
}
public function get_type() {
return $this->type;
}
public function get_pdf() {
do_action( 'wpo_wcpdf_before_pdf', $this->get_type(), $this );
// temporarily apply filters that need to be removed again after the pdf is generated
$pdf_filters = apply_filters( 'wpo_wcpdf_pdf_filters', array(), $this );
$this->add_filters( $pdf_filters );
$html = $this->get_html();
$pdf_settings = array(
'paper_size' => apply_filters( 'wpo_wcpdf_paper_format', $this->wrapper_document->get_setting( 'paper_size', 'A4' ), $this->get_type(), $this ),
'paper_orientation' => apply_filters( 'wpo_wcpdf_paper_orientation', 'portrait', $this->get_type(), $this ),
'font_subsetting' => $this->wrapper_document->get_setting( 'font_subsetting', false ),
);
$pdf_maker = wcpdf_get_pdf_maker( $html, $pdf_settings, $this );
$pdf = apply_filters( 'wpo_wcpdf_pdf_data', $pdf_maker->output(), $this );
do_action( 'wpo_wcpdf_after_pdf', $this->get_type(), $this );
// remove temporary filters
$this->remove_filters( $pdf_filters );
return $pdf;
}
public function get_html() {
// temporarily apply filters that need to be removed again after the html is generated
$html_filters = apply_filters( 'wpo_wcpdf_html_filters', array(), $this );
$this->add_filters( $html_filters );
do_action( 'wpo_wcpdf_before_html', $this->get_type(), $this );
$html_content = array();
foreach ( $this->order_ids as $key => $order_id ) {
do_action( 'wpo_wcpdf_process_template_order', $this->get_type(), $order_id );
$order = wc_get_order( $order_id );
if ( $document = wcpdf_get_document( $this->get_type(), $order, true ) ) {
$html_content[ $key ] = $document->get_html( array( 'wrap_html_content' => false ) );
}
}
// get wrapper document & insert body content
$this->wrapper_document = wcpdf_get_document( $this->get_type(), null );
$html = $this->wrapper_document->wrap_html_content( $this->merge_documents( $html_content ) );
do_action( 'wpo_wcpdf_after_html', $this->get_type(), $this );
// remove temporary filters
$this->remove_filters( $html_filters );
return $html;
}
public function merge_documents( $html_content ) {
// insert page breaks merge
$page_break = "\n<div style=\"page-break-before: always;\"></div>\n";
$html = implode( $page_break, $html_content );
return apply_filters( 'wpo_wcpdf_merged_bulk_document_content', $html, $html_content, $this );
}
public function output_pdf( $output_mode = 'download' ) {
$pdf = $this->get_pdf();
wcpdf_pdf_headers( $this->get_filename(), $output_mode, $pdf );
echo $pdf;
die();
}
public function output_html() {
echo $this->get_html();
die();
}
public function get_filename( $context = 'download', $args = array() ) {
if ( empty( $this->wrapper_document ) ) {
$this->wrapper_document = wcpdf_get_document( $this->get_type(), null );
}
$default_args = array(
'order_ids' => $this->order_ids,
);
$args = $args + $default_args;
$filename = $this->wrapper_document->get_filename( $context, $args );
return $filename;
}
protected function add_filters( $filters ) {
foreach ( $filters as $filter ) {
$filter = $this->normalize_filter_args( $filter );
add_filter( $filter['hook_name'], $filter['callback'], $filter['priority'], $filter['accepted_args'] );
}
}
protected function remove_filters( $filters ) {
foreach ( $filters as $filter ) {
$filter = $this->normalize_filter_args( $filter );
remove_filter( $filter['hook_name'], $filter['callback'], $filter['priority'] );
}
}
protected function normalize_filter_args( $filter ) {
$filter = array_values( $filter );
$hook_name = $filter[0];
$callback = $filter[1];
$priority = isset( $filter[2] ) ? $filter[2] : 10;
$accepted_args = isset( $filter[3] ) ? $filter[3] : 1;
return compact( 'hook_name', 'callback', 'priority', 'accepted_args' );
}
}
endif; // class_exists

View File

@@ -0,0 +1,195 @@
<?php
namespace WPO\WC\PDF_Invoices\Documents;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( '\\WPO\\WC\\PDF_Invoices\\Documents\\Document_Number' ) ) :
class Document_Number {
/**
* The raw, unformatted number
* @var int
*/
public $number;
/**
* Document number formatted for display
* @var String
*/
public $formatted_number;
/**
* Number prefix
* @var string
*/
public $prefix;
/**
* Number suffix
* @var string
*/
public $suffix;
/**
* Document Type
* @var string
*/
public $document_type;
/**
* Order ID
* @var int
*/
public $order_id;
/**
* Zeros padding (total number of digits including leading zeros)
* @var int
*/
public $padding;
public function __construct( $number, $settings = array(), $document = null, $order = null ) {
$number = apply_filters( 'wpo_wcpdf_raw_document_number', $number, $settings, $document, $order );
if ( !is_array( $number ) && !empty( $number ) ) {
// we're creating a new number with settings as passed
$this->number = $number;
foreach ($settings as $key => $value) {
$this->{$key} = $value;
}
if ( !isset( $this->formatted_number ) ) {
$this->apply_formatting( $document, ( !empty( $document->order ) ? $document->order : $order ) );
}
} elseif ( is_array( $number ) ) {
// loaded with full number data
foreach ($number as $key => $value) {
$this->{$key} = $value;
}
}
if (!empty($document)) {
$this->document_type = $document->get_type();
}
if (!empty($order)) {
$this->order_id = $order->get_id();
}
}
public function __toString() {
return (string) $this->get_formatted();
}
public function get_formatted() {
$formatted_number = isset( $this->formatted_number ) ? $this->formatted_number : '';
$formatted_number = apply_filters( 'wpo_wcpdf_formatted_document_number', $formatted_number, $this, $this->document_type, $this->order_id );
return $formatted_number;
}
public function get_plain() {
return $this->number;
}
public function apply_formatting( $document, $order ) {
if ( empty( $document ) || empty( $order ) ) {
$this->formatted_number = $this->number;
return;
}
// load plain number
$number = $this->number;
// get dates
$order_date = $order->get_date_created();
// order date can be empty when order is being saved, fallback to current time
if ( empty( $order_date ) && function_exists('wc_string_to_datetime') ) {
$order_date = wc_string_to_datetime( date_i18n('Y-m-d H:i:s') );
}
$document_date = $document->get_date();
// fallback to order date if no document date available
if (empty($document_date)) {
$document_date = $order_date;
}
// get format settings
$formats = array(
'prefix' => $this->prefix,
'suffix' => $this->suffix,
);
// load replacement values
$order_year = $order_date->date_i18n( 'Y' );
$order_month = $order_date->date_i18n( 'm' );
$order_day = $order_date->date_i18n( 'd' );
$document_year = $document_date->date_i18n( 'Y' );
$document_month = $document_date->date_i18n( 'm' );
$document_day = $document_date->date_i18n( 'd' );
$order_number = is_callable( array( $order, 'get_order_number' ) ) ? $order->get_order_number() : '';
// make replacements
foreach ( $formats as $key => $value ) {
if ( empty( $value ) ) {
continue;
}
$value = str_replace( '[order_year]', $order_year, $value );
$value = str_replace( '[order_month]', $order_month, $value );
$value = str_replace( '[order_day]', $order_day, $value );
$value = str_replace( "[{$document->slug}_year]", $document_year, $value );
$value = str_replace( "[{$document->slug}_month]", $document_month, $value );
$value = str_replace( "[{$document->slug}_day]", $document_day, $value );
$value = str_replace( '[order_number]', $order_number, $value );
// replace date tag in the form [invoice_date="{$date_format}"] or [order_date="{$date_format}"]
$date_types = array( 'order', $document->slug );
foreach ( $date_types as $date_type ) {
if ( false !== strpos( $value, "[{$date_type}_date=" ) ) {
preg_match_all( "/\[{$date_type}_date=\"(.*?)\"\]/", $value, $document_date_tags );
if ( ! empty( $document_date_tags[1] ) ) {
foreach ( $document_date_tags[1] as $match_id => $date_format ) {
if ( 'order' === $date_type ) {
$value = str_replace( $document_date_tags[0][$match_id], $order_date->date_i18n( $date_format ), $value );
} else {
$value = str_replace( $document_date_tags[0][$match_id], $document_date->date_i18n( $date_format ), $value );
}
}
}
}
}
$formats[$key] = $value;
}
// Padding
$padding_string = '';
if ( function_exists('ctype_digit') ) { // requires the Ctype extension
if ( ctype_digit( (string) $this->padding ) ) {
$padding_string = (string) $this->padding;
}
} elseif ( !empty( $this->padding ) ) {
$padding_string = (string) absint($this->padding);
}
if ( !empty( $padding_string ) ) {
$number = sprintf('%0'.$padding_string.'d', $number);
}
// Add prefix & suffix
$this->formatted_number = $formats['prefix'] . $number . $formats['suffix'] ;
// Apply filters and store
$this->formatted_number = apply_filters( 'wpo_wcpdf_format_document_number', $this->formatted_number, $this, $document, $order );
return $this->formatted_number;
}
public function to_array() {
return (array) $this;
}
}
endif; // class_exists

View File

@@ -0,0 +1,670 @@
<?php
namespace WPO\WC\PDF_Invoices\Documents;
use WPO\WC\PDF_Invoices\Updraft_Semaphore_3_0 as Semaphore;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( '\\WPO\\WC\\PDF_Invoices\\Documents\\Invoice' ) ) :
/**
* Invoice Document
*/
class Invoice extends Order_Document_Methods {
public $type;
public $title;
public $icon;
public $lock_name;
public $lock_context;
public $lock_time;
public $lock_retries;
public $output_formats;
/**
* Init/load the order object.
*
* @param int|object|WC_Order $order Order to init.
*/
public function __construct( $order = 0 ) {
// set properties
$this->type = 'invoice';
$this->title = __( 'Invoice', 'woocommerce-pdf-invoices-packing-slips' );
$this->icon = WPO_WCPDF()->plugin_url() . "/assets/images/invoice.svg";
// semaphore
$this->lock_name = "wpo_wcpdf_{$this->slug}_number_lock";
$this->lock_context = array( 'source' => "wpo-wcpdf-{$this->type}-semaphore" );
$this->lock_time = apply_filters( "wpo_wcpdf_{$this->type}_number_lock_time", 2 );
$this->lock_retries = apply_filters( "wpo_wcpdf_{$this->type}_number_lock_retries", 0 );
// call parent constructor
parent::__construct( $order );
// output formats (placed after parent construct to override the abstract default)
$this->output_formats = apply_filters( "wpo_wcpdf_{$this->type}_output_formats", array( 'pdf', 'ubl' ), $this );
}
public function use_historical_settings() {
$document_settings = get_option( 'wpo_wcpdf_documents_settings_'.$this->get_type() );
// this setting is inverted on the frontend so that it needs to be actively/purposely enabled to be used
if (!empty($document_settings) && isset($document_settings['use_latest_settings'])) {
$use_historical_settings = false;
} else {
$use_historical_settings = true;
}
return apply_filters( 'wpo_wcpdf_document_use_historical_settings', $use_historical_settings, $this );
}
public function storing_settings_enabled() {
return apply_filters( 'wpo_wcpdf_document_store_settings', true, $this );
}
public function get_title() {
// override/not using $this->title to allow for language switching!
return apply_filters( "wpo_wcpdf_{$this->slug}_title", __( 'Invoice', 'woocommerce-pdf-invoices-packing-slips' ), $this );
}
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->init_number();
do_action( 'wpo_wcpdf_init_document', $this );
}
public function exists() {
return !empty( $this->data['number'] );
}
public function init_number() {
$logger = isset( $this->settings['log_number_generation'] ) ? [ wc_get_logger() ] : [];
$lock = new Semaphore( $this->lock_name, $this->lock_time, $logger, $this->lock_context );
$invoice_number = null;
if ( $lock->lock( $this->lock_retries ) ) {
try {
// If a third-party plugin claims to generate invoice numbers, trigger this instead
if ( apply_filters( 'woocommerce_invoice_number_by_plugin', false ) || apply_filters( 'wpo_wcpdf_external_invoice_number_enabled', false, $this ) ) {
$invoice_number = apply_filters( 'woocommerce_generate_invoice_number', null, $this->order );
$invoice_number = apply_filters( 'wpo_wcpdf_external_invoice_number', $invoice_number, $this );
} elseif ( isset( $this->settings['display_number'] ) && $this->settings['display_number'] == 'order_number' && ! empty( $this->order ) ) {
$invoice_number = $this->order->get_order_number();
}
if ( ! empty( $invoice_number ) ) { // overridden by plugin or set to order number
if ( ! is_numeric( $invoice_number ) && ! ( $invoice_number instanceof Document_Number ) ) {
// invoice number is not numeric, treat as formatted
// try to extract meaningful number data
$formatted_number = $invoice_number;
$number = (int) preg_replace( '/\D/', '', $invoice_number );
$invoice_number = compact( 'number', 'formatted_number' );
}
} else {
$number_store = $this->get_sequential_number_store();
$invoice_number = $number_store->increment( intval( $this->order_id ), $this->get_date()->date_i18n( 'Y-m-d H:i:s' ) );
}
if ( ! is_null( $invoice_number ) ) {
$this->set_number( $invoice_number );
}
} catch ( \Exception $e ) {
$lock->log( $e, 'critical' );
} catch ( \Error $e ) {
$lock->log( $e, 'critical' );
}
$lock->release();
} else {
$lock->log( "Couldn't get the Invoice Number lock!", 'critical' );
}
return $invoice_number;
}
public function get_filename( $context = 'download', $args = array() ) {
$order_count = isset($args['order_ids']) ? count($args['order_ids']) : 1;
$name = _n( 'invoice', 'invoices', $order_count, 'woocommerce-pdf-invoices-packing-slips' );
if ( $order_count == 1 ) {
if ( isset( $this->settings['display_number'] ) && $this->settings['display_number'] == 'invoice_number' ) {
$suffix = (string) $this->get_number();
} else {
if ( empty( $this->order ) && isset( $args['order_ids'][0] ) ) {
$order = wc_get_order( $args['order_ids'][0] );
$suffix = is_callable( array( $order, 'get_order_number' ) ) ? $order->get_order_number() : '';
} else {
$suffix = is_callable( array( $this->order, 'get_order_number' ) ) ? $this->order->get_order_number() : '';
}
}
// ensure unique filename in case suffix was empty
if ( empty( $suffix ) ) {
if ( ! empty( $this->order_id ) ) {
$suffix = $this->order_id;
} elseif ( ! empty( $args['order_ids'] ) && is_array( $args['order_ids'] ) ) {
$suffix = reset( $args['order_ids'] );
} else {
$suffix = uniqid();
}
}
} else {
$suffix = date( 'Y-m-d' ); // 2020-11-11
}
// get filename
$output_format = ! empty( $args['output'] ) ? esc_attr( $args['output'] ) : 'pdf';
$filename = $name . '-' . $suffix . $this->get_output_format_extension( $output_format );
// Filter filename
$order_ids = isset( $args['order_ids'] ) ? $args['order_ids'] : array( $this->order_id );
$filename = apply_filters( 'wpo_wcpdf_filename', $filename, $this->get_type(), $order_ids, $context );
// sanitize filename (after filters to prevent human errors)!
return sanitize_file_name( $filename );
}
/**
* Initialise settings
*/
public function init_settings() {
do_action( "wpo_wcpdf_before_{$this->type}_init_settings", $this );
foreach ( $this->output_formats as $output_format ) {
$settings_fields = array();
switch ( $output_format ) {
default:
case 'pdf':
$page = $option_group = $option_name = "wpo_wcpdf_documents_settings_{$this->get_type()}";
$settings_fields = apply_filters( "wpo_wcpdf_settings_fields_documents_{$this->get_type()}", $this->get_pdf_settings_fields( $option_name ), $page, $option_group, $option_name ); // legacy filter
break;
case 'ubl':
$page = $option_group = $option_name = "wpo_wcpdf_documents_settings_{$this->get_type()}_{$output_format}";
$settings_fields = $this->get_ubl_settings_fields( $option_name );
break;
}
if ( ! empty( $settings_fields ) ) {
// allow plugins to alter settings fields
$settings_fields = apply_filters( "wpo_wcpdf_settings_fields_documents_{$this->type}_{$output_format}", $settings_fields, $page, $option_group, $option_name, $this );
WPO_WCPDF()->settings->add_settings_fields( $settings_fields, $page, $option_group, $option_name );
}
}
do_action( "wpo_wcpdf_after_{$this->type}_init_settings", $this );
}
/**
* PDF settings fields
*/
public function get_pdf_settings_fields( $option_name ) {
$settings_fields = array(
array(
'type' => 'section',
'id' => $this->type,
'title' => '',
'callback' => 'section',
),
array(
'type' => 'setting',
'id' => 'enabled',
'title' => __( 'Enable', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'enabled',
)
),
array(
'type' => 'setting',
'id' => 'attach_to_email_ids',
'title' => __( 'Attach to:', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'multiple_checkboxes',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'attach_to_email_ids',
'fields_callback' => array( $this, 'get_wc_emails' ),
/* translators: directory path */
'description' => !is_writable( WPO_WCPDF()->main->get_tmp_path( 'attachments' ) ) ? '<span class="wpo-warning">' . sprintf( __( 'It looks like the temp folder (<code>%s</code>) is not writable, check the permissions for this folder! Without having write access to this folder, the plugin will not be able to email invoices.', 'woocommerce-pdf-invoices-packing-slips' ), WPO_WCPDF()->main->get_tmp_path( 'attachments' ) ).'</span>':'',
)
),
array(
'type' => 'setting',
'id' => 'disable_for_statuses',
'title' => __( 'Disable for:', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'disable_for_statuses',
'options_callback' => 'wc_get_order_statuses',
'multiple' => true,
'enhanced_select' => true,
'placeholder' => __( 'Select one or more statuses', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'display_shipping_address',
'title' => __( 'Display shipping address', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'display_shipping_address',
'options' => array(
'' => __( 'No' , 'woocommerce-pdf-invoices-packing-slips' ),
'when_different'=> __( 'Only when different from billing address' , 'woocommerce-pdf-invoices-packing-slips' ),
'always' => __( 'Always' , 'woocommerce-pdf-invoices-packing-slips' ),
),
// 'description' => __( 'Display shipping address (in addition to the default billing address) if different from billing address', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'display_email',
'title' => __( 'Display email address', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'display_email',
)
),
array(
'type' => 'setting',
'id' => 'display_phone',
'title' => __( 'Display phone number', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'display_phone',
)
),
array(
'type' => 'setting',
'id' => 'display_customer_notes',
'title' => __( 'Display customer notes', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'display_customer_notes',
'store_unchecked' => true,
'default' => 1,
)
),
array(
'type' => 'setting',
'id' => 'display_date',
'title' => __( 'Display invoice date', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'display_date',
'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' ),
),
)
),
array(
'type' => 'setting',
'id' => 'due_date',
'title' => __( 'Display due date', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'due_date',
'options' => apply_filters( 'wpo_wcpdf_due_date_options', array(
'' => __( 'No', 'woocommerce-pdf-invoices-packing-slips' ),
'1' => __( '1 day', 'woocommerce-pdf-invoices-packing-slips' ),
'7' => __( '7 days', 'woocommerce-pdf-invoices-packing-slips' ),
'30' => __( '30 days', 'woocommerce-pdf-invoices-packing-slips' ),
), $this->type ),
'description' => __( 'Displays a due date below the order data.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'display_number',
'title' => __( 'Display invoice number', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'display_number',
'options' => array(
'' => __( 'No' , 'woocommerce-pdf-invoices-packing-slips' ),
'invoice_number' => __( 'Invoice Number' , 'woocommerce-pdf-invoices-packing-slips' ),
'order_number' => __( 'Order Number' , 'woocommerce-pdf-invoices-packing-slips' ),
),
'description' => sprintf(
'<strong>%s</strong> %s <a href="https://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/invoice-numbers-explained/#why-is-the-pdf-invoice-number-different-from-the-woocommerce-order-number">%s</a>',
__( 'Warning!', 'woocommerce-pdf-invoices-packing-slips' ),
__( 'Using the Order Number as invoice number is not recommended as this may lead to gaps in the invoice number sequence (even when order numbers are sequential).', 'woocommerce-pdf-invoices-packing-slips' ),
__( 'More information', 'woocommerce-pdf-invoices-packing-slips' )
),
)
),
array(
'type' => 'setting',
'id' => 'next_invoice_number',
'title' => __( 'Next invoice number (without prefix/suffix etc.)', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'next_number_edit',
'section' => $this->type,
'args' => array(
'store_callback' => array( $this, 'get_sequential_number_store' ),
'size' => '10',
'description' => __( 'This is the number that will be used for the next document. By default, numbering starts from 1 and increases for every new document. Note that if you override this and set it lower than the current/highest number, this could create duplicate numbers!', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'number_format',
'title' => __( 'Number format', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'multiple_text_input',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'number_format',
'fields' => array(
'prefix' => array(
'label' => __( 'Prefix' , 'woocommerce-pdf-invoices-packing-slips' ),
'size' => 20,
'description' => __( 'If set, this value will be used as number prefix.' , 'woocommerce-pdf-invoices-packing-slips' ) . ' ' . sprintf(
/* translators: 1. document type, 2-3 placeholders */
__( 'You can use the %1$s year and/or month with the %2$s or %3$s placeholders respectively.', 'woocommerce-pdf-invoices-packing-slips' ),
__( 'invoice', 'woocommerce-pdf-invoices-packing-slips' ), '<strong>[invoice_year]</strong>', '<strong>[invoice_month]</strong>'
) . ' ' . __( 'Check the Docs article below to see all the available placeholders for prefix/suffix.', 'woocommerce-pdf-invoices-packing-slips' ),
),
'suffix' => array(
'label' => __( 'Suffix' , 'woocommerce-pdf-invoices-packing-slips' ),
'size' => 20,
'description' => __( 'If set, this value will be used as number suffix.' , 'woocommerce-pdf-invoices-packing-slips' ) . ' ' . sprintf(
/* translators: 1. document type, 2-3 placeholders */
__( 'You can use the %1$s year and/or month with the %2$s or %3$s placeholders respectively.', 'woocommerce-pdf-invoices-packing-slips' ),
__( 'invoice', 'woocommerce-pdf-invoices-packing-slips' ), '<strong>[invoice_year]</strong>', '<strong>[invoice_month]</strong>'
) . ' ' . __( 'Check the Docs article below to see all the available placeholders for prefix/suffix.', 'woocommerce-pdf-invoices-packing-slips' ),
),
'padding' => array(
'label' => __( 'Padding' , 'woocommerce-pdf-invoices-packing-slips' ),
'size' => 20,
'type' => 'number',
/* translators: document type */
'description' => sprintf( __( 'Enter the number of digits you want to use as padding. For instance, enter <code>6</code> to display the %s number <code>123</code> as <code>000123</code>, filling it with zeros until the number set as padding is reached.' , 'woocommerce-pdf-invoices-packing-slips' ), __( 'invoice', 'woocommerce-pdf-invoices-packing-slips' ) ),
),
),
/* translators: document type */
'description' => __( 'For more information about setting up the number format and see the available placeholders for the prefix and suffix, check this article:', 'woocommerce-pdf-invoices-packing-slips' ) . sprintf( ' <a href="https://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/number-format-explained/" target="_blank">%s</a>', __( 'Number format explained', 'woocommerce-pdf-invoices-packing-slips') ) . '.<br><br>'. sprintf( __( '<strong>Note</strong>: Changes made to the number format will only be reflected on new orders. Also, if you have already created a custom %s number format with a filter, the above settings will be ignored.', 'woocommerce-pdf-invoices-packing-slips' ), __( 'invoice', 'woocommerce-pdf-invoices-packing-slips' ) ),
)
),
array(
'type' => 'setting',
'id' => 'reset_number_yearly',
'title' => __( 'Reset invoice number yearly', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'reset_number_yearly',
)
),
array(
'type' => 'setting',
'id' => 'log_number_generation',
'title' => __( 'Log invoice number generation', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'log_number_generation',
'description' => sprintf(
/* translators: here link */
__( 'Enables the invoice number generation logs %s. Helpful for debugging numbering issues.', 'woocommerce-pdf-invoices-packing-slips' ),
'<a href="'.esc_url( admin_url( 'admin.php?page=wc-status&tab=logs' ) ).'">'.__( 'here', 'woocommerce-pdf-invoices-packing-slips' ).'</a>'
),
)
),
array(
'type' => 'setting',
'id' => 'my_account_buttons',
'title' => __( 'Allow My Account invoice download', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'my_account_buttons',
'options' => array(
'available' => __( 'Only when an invoice is already created/emailed' , 'woocommerce-pdf-invoices-packing-slips' ),
'custom' => __( 'Only for specific order statuses (define below)' , 'woocommerce-pdf-invoices-packing-slips' ),
'always' => __( 'Always' , 'woocommerce-pdf-invoices-packing-slips' ),
'never' => __( 'Never' , 'woocommerce-pdf-invoices-packing-slips' ),
),
'custom' => array(
'type' => 'multiple_checkboxes',
'args' => array(
'option_name' => $option_name,
'id' => 'my_account_restrict',
'fields_callback' => array( $this, 'get_wc_order_status_list' ),
),
),
)
),
array(
'type' => 'setting',
'id' => 'invoice_number_column',
'title' => __( 'Enable invoice number column in the orders list', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'invoice_number_column',
)
),
array(
'type' => 'setting',
'id' => 'invoice_date_column',
'title' => __( 'Enable invoice date column in the orders list', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'invoice_date_column',
)
),
array(
'type' => 'setting',
'id' => 'invoice_number_search',
'title' => __( 'Enable invoice number search in the orders list', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'invoice_number_search',
'description' => __( 'Can potentially slow down the search process.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'disable_free',
'title' => __( 'Disable for free orders', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'disable_free',
/* translators: zero number */
'description' => sprintf(__( "Disable document when the order total is %s", 'woocommerce-pdf-invoices-packing-slips' ), function_exists('wc_price') ? wc_price( 0 ) : 0 ),
)
),
array(
'type' => 'setting',
'id' => 'mark_printed',
'title' => __( 'Mark as printed', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'mark_printed',
'options' => array_merge(
[
'manually' => __( 'Manually', 'woocommerce-pdf-invoices-packing-slips' ),
],
apply_filters( 'wpo_wcpdf_document_triggers', [
'single' => __( 'On single order action', 'woocommerce-pdf-invoices-packing-slips' ),
'bulk' => __( 'On bulk order action', 'woocommerce-pdf-invoices-packing-slips' ),
'my_account' => __( 'On my account', 'woocommerce-pdf-invoices-packing-slips' ),
'email_attachment' => __( 'On email attachment', 'woocommerce-pdf-invoices-packing-slips' ),
'document_data' => __( 'On order document data (number and/or date set manually)', 'woocommerce-pdf-invoices-packing-slips' ),
] )
),
'multiple' => true,
'enhanced_select' => true,
'description' => __( 'Allows you to mark the document as printed, manually (in the order page) or automatically (based on the document creation context you have selected).', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'unmark_printed',
'title' => __( 'Unmark as printed', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'unmark_printed',
'description' => __( 'Adds a link in the order page to allow to remove the printed mark.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'use_latest_settings',
'title' => __( 'Always use most current settings', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'use_latest_settings',
'description' => __( "When enabled, the document will always reflect the most current settings (such as footer text, document name, etc.) rather than using historical settings.", 'woocommerce-pdf-invoices-packing-slips' )
. "<br>"
. __( "<strong>Caution:</strong> enabling this will also mean that if you change your company name or address in the future, previously generated documents will also be affected.", 'woocommerce-pdf-invoices-packing-slips' ),
)
),
);
// remove/rename some fields when invoice number is controlled externally
if ( apply_filters( 'woocommerce_invoice_number_by_plugin', false ) ) {
$remove_settings = array( 'next_invoice_number', 'number_format', 'reset_number_yearly' );
foreach ( $settings_fields as $key => $settings_field ) {
if ( in_array( $settings_field['id'], $remove_settings ) ) {
unset( $settings_fields[$key] );
} elseif ( $settings_field['id'] == 'display_number' ) {
// alternate description for invoice number
$invoice_number_desc = __( 'Invoice numbers are created by a third-party extension.', 'woocommerce-pdf-invoices-packing-slips' );
if ( $config_link = apply_filters( 'woocommerce_invoice_number_configuration_link', null ) ) {
/* translators: link */
$invoice_number_desc .= ' '.sprintf(__( 'Configure it <a href="%s">here</a>.', 'woocommerce-pdf-invoices-packing-slips' ), esc_attr( $config_link ) );
}
$settings_fields[$key]['args']['description'] = '<i>'.$invoice_number_desc.'</i>';
}
}
}
return apply_filters( "wpo_wcpdf_{$this->type}_pdf_settings_fields", $settings_fields, $option_name, $this );
}
/**
* UBL settings fields
*/
public function get_ubl_settings_fields( $option_name ) {
$settings_fields = array(
array(
'type' => 'section',
'id' => $this->type.'_ubl',
'title' => '',
'callback' => 'section',
),
array(
'type' => 'setting',
'id' => 'enabled',
'title' => __( 'Enable', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type.'_ubl',
'args' => array(
'option_name' => $option_name,
'id' => 'enabled',
)
),
array(
'type' => 'setting',
'id' => 'attach_to_email_ids',
'title' => __( 'Attach to:', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'multiple_checkboxes',
'section' => $this->type.'_ubl',
'args' => array(
'option_name' => $option_name,
'id' => 'attach_to_email_ids',
'fields_callback' => array( $this, 'get_wc_emails' ),
/* translators: directory path */
'description' => ! is_writable( WPO_WCPDF()->main->get_tmp_path( 'attachments' ) ) ? '<span class="wpo-warning">' . sprintf( __( 'It looks like the temp folder (<code>%s</code>) is not writable, check the permissions for this folder! Without having write access to this folder, the plugin will not be able to email invoices.', 'woocommerce-pdf-invoices-packing-slips' ), WPO_WCPDF()->main->get_tmp_path( 'attachments' ) ).'</span>':'',
)
),
array(
'type' => 'setting',
'id' => 'include_encrypted_pdf',
'title' => __( 'Include encrypted PDF:', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => $this->type.'_ubl',
'args' => array(
'option_name' => $option_name,
'id' => 'include_encrypted_pdf',
'description' => __( 'Include the PDF Invoice file encrypted in the UBL file.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
);
return apply_filters( "wpo_wcpdf_{$this->type}_ubl_settings_fields", $settings_fields, $option_name, $this );
}
/**
* Document number title
*/
public function get_number_title() {
$number_title = __( 'Invoice Number:', 'woocommerce-pdf-invoices-packing-slips' );
return apply_filters( "wpo_wcpdf_{$this->slug}_number_title", $number_title, $this );
}
/**
* Document date title
*/
public function get_date_title() {
$date_title = __( 'Invoice Date:', 'woocommerce-pdf-invoices-packing-slips' );
return apply_filters( "wpo_wcpdf_{$this->slug}_date_title", $date_title, $this );
}
}
endif; // class_exists

View File

@@ -0,0 +1,198 @@
<?php
namespace WPO\WC\PDF_Invoices\Documents;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( '\\WPO\\WC\\PDF_Invoices\\Documents\\Packing_Slip' ) ) :
/**
* Packing Slip Document
*/
class Packing_Slip extends Order_Document_Methods {
/**
* Init/load the order object.
*
* @param int|object|WC_Order $order Order to init.
*/
public function __construct( $order = 0 ) {
// set properties
$this->type = 'packing-slip';
$this->title = __( 'Packing Slip', 'woocommerce-pdf-invoices-packing-slips' );
$this->icon = WPO_WCPDF()->plugin_url() . "/assets/images/packing-slip.svg";
// call parent constructor
parent::__construct( $order );
// output formats (placed after parent construct to override the abstract default)
$this->output_formats = apply_filters( "wpo_wcpdf_{$this->type}_output_formats", array( 'pdf' ), $this );
}
public function get_title() {
// override/not using $this->title to allow for language switching!
return apply_filters( "wpo_wcpdf_{$this->slug}_title", __( 'Packing Slip', 'woocommerce-pdf-invoices-packing-slips' ), $this );
}
public function get_filename( $context = 'download', $args = array() ) {
$order_count = isset($args['order_ids']) ? count($args['order_ids']) : 1;
$name = _n( 'packing-slip', 'packing-slips', $order_count, 'woocommerce-pdf-invoices-packing-slips' );
if ( $order_count == 1 ) {
if ( isset( $this->settings['display_number'] ) ) {
$suffix = (string) $this->get_number();
} else {
if ( empty( $this->order ) && isset( $args['order_ids'] ) ) {
$order = wc_get_order( $args['order_ids'][0] );
$suffix = is_callable( array( $order, 'get_order_number' ) ) ? $order->get_order_number() : '';
} else {
$suffix = is_callable( array( $this->order, 'get_order_number' ) ) ? $this->order->get_order_number() : '';
}
}
} else {
$suffix = date( 'Y-m-d' ); // 2020-11-11
}
// get filename
$output_format = ! empty( $args['output'] ) ? esc_attr( $args['output'] ) : 'pdf';
$filename = $name . '-' . $suffix . $this->get_output_format_extension( $output_format );
// Filter filename
$order_ids = isset( $args['order_ids'] ) ? $args['order_ids'] : array( $this->order_id );
$filename = apply_filters( 'wpo_wcpdf_filename', $filename, $this->get_type(), $order_ids, $context );
// sanitize filename (after filters to prevent human errors)!
return sanitize_file_name( $filename );
}
public function init_settings() {
// Register settings.
$page = $option_group = $option_name = 'wpo_wcpdf_documents_settings_packing-slip';
$settings_fields = array(
array(
'type' => 'section',
'id' => 'packing_slip',
'title' => '',
'callback' => 'section',
),
array(
'type' => 'setting',
'id' => 'enabled',
'title' => __( 'Enable', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => 'packing_slip',
'args' => array(
'option_name' => $option_name,
'id' => 'enabled',
)
),
array(
'type' => 'setting',
'id' => 'display_billing_address',
'title' => __( 'Display billing address', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => 'packing_slip',
'args' => array(
'option_name' => $option_name,
'id' => 'display_billing_address',
'options' => array(
'' => __( 'No' , 'woocommerce-pdf-invoices-packing-slips' ),
'when_different'=> __( 'Only when different from shipping address' , 'woocommerce-pdf-invoices-packing-slips' ),
'always' => __( 'Always' , 'woocommerce-pdf-invoices-packing-slips' ),
),
// 'description' => __( 'Display billing address (in addition to the default shipping address) if different from shipping address', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'display_email',
'title' => __( 'Display email address', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => 'packing_slip',
'args' => array(
'option_name' => $option_name,
'id' => 'display_email',
)
),
array(
'type' => 'setting',
'id' => 'display_phone',
'title' => __( 'Display phone number', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => 'packing_slip',
'args' => array(
'option_name' => $option_name,
'id' => 'display_phone',
)
),
array(
'type' => 'setting',
'id' => 'display_customer_notes',
'title' => __( 'Display customer notes', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'checkbox',
'section' => 'packing_slip',
'args' => array(
'option_name' => $option_name,
'id' => 'display_customer_notes',
'store_unchecked' => true,
'default' => 1,
)
),
);
if ( ! function_exists( 'WPO_WCPDF_Pro' ) ) {
ob_start();
?>
<div class="notice notice-info inline">
<p><a href="https://wpovernight.com/downloads/woocommerce-pdf-invoices-packing-slips-professional/" target="_blank"><?php _e( 'Upgrade to our Professional extension to attach packing slips to any email!', 'woocommerce-pdf-invoices-packing-slips' ); ?></a></p>
</div>
<?php
$html = ob_get_clean();
$pro_notice = array(
array(
'type' => 'setting',
'id' => 'attach_to_email_ids',
'title' => __( 'Attach to:', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'html_section',
'section' => 'packing_slip',
'args' => array(
'option_name' => $option_name,
'id' => 'attach_to_email_ids',
'html' => $html,
)
),
);
$settings_fields = WPO_WCPDF()->settings->move_setting_after_id( $settings_fields, $pro_notice, 'enabled' );
}
// allow plugins to alter settings fields
$settings_fields = apply_filters( 'wpo_wcpdf_settings_fields_documents_packing_slip', $settings_fields, $page, $option_group, $option_name );
WPO_WCPDF()->settings->add_settings_fields( $settings_fields, $page, $option_group, $option_name );
return;
}
/**
* Document number title
*/
public function get_number_title() {
$number_title = __( 'Packing Slip Number:', 'woocommerce-pdf-invoices-packing-slips' );
return apply_filters( "wpo_wcpdf_{$this->slug}_number_title", $number_title, $this );
}
/**
* Document date title
*/
public function get_date_title() {
$date_title = __( 'Packing Slip Date:', 'woocommerce-pdf-invoices-packing-slips' );
return apply_filters( "wpo_wcpdf_{$this->slug}_date_title", $date_title, $this );
}
}
endif; // class_exists

View File

@@ -0,0 +1,198 @@
<?php
namespace WPO\WC\PDF_Invoices\Documents;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( '\\WPO\\WC\\PDF_Invoices\\Documents\\Sequential_Number_Store' ) ) :
/**
* Class handling database interaction for sequential numbers
*/
class Sequential_Number_Store {
/**
* WordPress database object
* @var object
*/
private $wpdb;
/**
* Name of the number store (used for table_name)
* @var string
*/
public $store_name;
/**
* Number store method, either 'auto_increment' or 'calculate'
* @var string
*/
public $method;
/**
* Name of the table that stores the number sequence (including the wp_wcpdf_ table prefix)
* @var string
*/
public $table_name;
/**
* If table name not found in database, is new table
* @var bool
*/
public $is_new = false;
public function __construct( $store_name, $method = 'auto_increment' ) {
global $wpdb;
$this->wpdb = $wpdb;
$this->store_name = $store_name;
$this->method = $method;
$this->table_name = apply_filters( "wpo_wcpdf_number_store_table_name", "{$this->wpdb->prefix}wcpdf_{$this->store_name}", $this->store_name, $this->method ); // e.g. wp_wcpdf_invoice_number
$this->init();
}
public function init() {
// check if table exists
if( ! $this->store_name_exists() ) {
$this->is_new = true;
} else {
// check calculated_number column if using 'calculate' method
if ( $this->method == 'calculate' ) {
$column_exists = $this->wpdb->get_var("SHOW COLUMNS FROM `{$this->table_name}` LIKE 'calculated_number'");
if ( empty( $column_exists ) ) {
$this->wpdb->query("ALTER TABLE {$this->table_name} ADD calculated_number int (16)");
}
}
return; // no further business
}
// create table (in case of concurrent requests, this does no harm if it already exists)
$charset_collate = $this->wpdb->get_charset_collate();
// dbDelta is a sensitive kid, so we omit indentation
$sql = "CREATE TABLE {$this->table_name} (
id int(16) NOT NULL AUTO_INCREMENT,
order_id int(16),
date datetime DEFAULT '1000-01-01 00:00:00' NOT NULL,
calculated_number int (16),
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// catch mysql errors
wcpdf_catch_db_object_errors( $this->wpdb );
return;
}
/**
* Consume/create the next number and return it
* @param integer $order_id WooCommerce Order ID
* @param string $date Local date, formatted as Y-m-d H:i:s
* @return int Number that was consumed/created
*/
public function increment( $order_id = 0, $date = null ) {
if ( empty( $date ) ) {
$date = get_date_from_gmt( date( 'Y-m-d H:i:s' ) );
}
do_action( 'wpo_wcpdf_before_sequential_number_increment', $this, $order_id, $date );
$data = array(
'order_id' => absint( $order_id ),
'date' => $date,
);
if ( $this->method == 'auto_increment' ) {
$this->wpdb->insert( $this->table_name, $data );
$number = $this->wpdb->insert_id;
} elseif ( $this->method == 'calculate' ) {
$number = $data['calculated_number'] = $this->get_next();
$this->wpdb->insert( $this->table_name, $data );
}
// return generated number
return $number;
}
/**
* Get the number that will be used on the next increment
* @return int next number
*/
public function get_next() {
if ( $this->method == 'auto_increment' ) {
// clear cache first on mysql 8.0+
if ( $this->wpdb->get_var( "SHOW VARIABLES LIKE 'information_schema_stats_expiry'" ) ) {
$this->wpdb->query( "SET SESSION information_schema_stats_expiry = 0" );
}
// get next auto_increment value
$table_status = $this->wpdb->get_row("SHOW TABLE STATUS LIKE '{$this->table_name}'");
$next = $table_status->Auto_increment;
} elseif ( $this->method == 'calculate' ) {
$last_row = $this->wpdb->get_row( "SELECT * FROM {$this->table_name} WHERE id = ( SELECT MAX(id) from {$this->table_name} )" );
if ( empty( $last_row ) ) {
$next = 1;
} elseif ( ! empty( $last_row->calculated_number ) ) {
$next = (int) $last_row->calculated_number + 1;
} else {
$next = (int) $last_row->id + 1;
}
}
return $next;
}
/**
* Set the number that will be used on the next increment
*/
public function set_next( $number = 1 ) {
// delete all rows
$delete = $this->wpdb->query("TRUNCATE TABLE {$this->table_name}");
// set auto_increment
if ( $number > 1 ) {
// if AUTO_INCREMENT is not 1, we need to make sure we have a 'highest value' in case of server restarts
// https://serverfault.com/questions/228690/mysql-auto-increment-fields-resets-by-itself
$highest_number = (int) $number - 1;
$this->wpdb->query( $this->wpdb->prepare( "ALTER TABLE {$this->table_name} AUTO_INCREMENT=%d;", $highest_number ) );
$data = array(
'order_id' => 0,
'date' => get_date_from_gmt( date( 'Y-m-d H:i:s' ) ),
);
if ( $this->method == 'calculate' ) {
$data['calculated_number'] = $highest_number;
}
// after this insert, AUTO_INCREMENT will be equal to $number
$this->wpdb->insert( $this->table_name, $data );
} else {
// simple scenario, no need to insert any rows
$this->wpdb->query( $this->wpdb->prepare( "ALTER TABLE {$this->table_name} AUTO_INCREMENT=%d;", $number ) );
}
}
public function get_last_date( $format = 'Y-m-d H:i:s' ) {
$row = $this->wpdb->get_row( "SELECT * FROM {$this->table_name} WHERE id = ( SELECT MAX(id) from {$this->table_name} )" );
$date = isset( $row->date ) ? $row->date : 'now';
$formatted_date = date( $format, strtotime( $date ) );
return $formatted_date;
}
/**
* @return bool
*/
public function store_name_exists() {
// check if table exists
if ( $this->wpdb->get_var("SHOW TABLES LIKE '{$this->table_name}'") == $this->table_name ) {
return true;
} else {
return false;
}
}
}
endif; // class_exists