get_id(); if ( ! add_option( $lock, time(), '', false ) ) { $stale = (int) get_option( $lock ) < time() - 30; if ( ! $stale ) { $fresh = wc_get_order( $order->get_id() ); return $fresh ? self::get( $fresh ) : ''; } update_option( $lock, time(), false ); } try { $fresh = wc_get_order( $order->get_id() ); $existing = $fresh ? self::persist_canonical( $fresh, self::META_NUMBER, self::META_SEQ, self::META_DATE, 'format' ) : ''; if ( '' !== $existing ) { return $existing; } $seq = self::next_sequence(); $number = self::format( $seq ); $order->update_meta_data( self::META_NUMBER, $number ); $order->update_meta_data( self::META_SEQ, $seq ); $order->update_meta_data( self::META_DATE, time() ); $order->save(); return $number; } finally { delete_option( $lock ); } } /** * Format an invoice sequence number. * * @param int $seq Sequence. * @return string */ public static function format_invoice( $seq ) { $prefix = (string) VPRWC_Settings::get( 'vprwc_invoice_prefix' ); $padding = max( 1, (int) VPRWC_Settings::get( 'vprwc_padding' ) ); return apply_filters( 'vprwc_invoice_number_format', $prefix . str_pad( (string) (int) $seq, $padding, '0', STR_PAD_LEFT ), $seq ); } /** * Existing invoice number for an order, or '' if none. * * @param WC_Order $order Order. * @return string */ public static function get_invoice( $order ) { return self::canonical_number( $order, self::META_INVOICE_NUMBER, self::META_INVOICE_SEQ, 'format_invoice' ); } /** * Return the order's invoice number, assigning the next one if needed. * * @param WC_Order $order Order. * @return string */ public static function get_or_assign_invoice( $order ) { $existing = self::persist_canonical( $order, self::META_INVOICE_NUMBER, self::META_INVOICE_SEQ, self::META_INVOICE_DATE, 'format_invoice' ); if ( '' !== $existing ) { return $existing; } $lock = 'vprwc_inv_lock_' . $order->get_id(); if ( ! add_option( $lock, time(), '', false ) ) { $stale = (int) get_option( $lock ) < time() - 30; if ( ! $stale ) { $fresh = wc_get_order( $order->get_id() ); return $fresh ? self::get_invoice( $fresh ) : ''; } update_option( $lock, time(), false ); } try { $fresh = wc_get_order( $order->get_id() ); $existing = $fresh ? self::persist_canonical( $fresh, self::META_INVOICE_NUMBER, self::META_INVOICE_SEQ, self::META_INVOICE_DATE, 'format_invoice' ) : ''; if ( '' !== $existing ) { return $existing; } $seq = self::next_sequence( 'vprwc_invoice_next_number' ); $number = self::format_invoice( $seq ); $order->update_meta_data( self::META_INVOICE_NUMBER, $number ); $order->update_meta_data( self::META_INVOICE_SEQ, $seq ); $order->update_meta_data( self::META_INVOICE_DATE, time() ); $order->save(); return $number; } finally { delete_option( $lock ); } } /** * Atomically take the next sequence value. * * @param string $option Option name for the counter. * @return int */ private static function next_sequence( $option = 'vprwc_next_number' ) { global $wpdb; add_option( $option, 1, '', false ); $suppress = $wpdb->suppress_errors( true ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $updated = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = LAST_INSERT_ID(CAST(option_value AS UNSIGNED) + 1) WHERE option_name = %s", $option ) ); $seq = 0; if ( $updated ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery $lid = (int) $wpdb->get_var( 'SELECT LAST_INSERT_ID()' ); wp_cache_delete( $option, 'options' ); wp_cache_delete( 'notoptions', 'options' ); $current = (int) get_option( $option, 0 ); // Trust LAST_INSERT_ID only when it matches the value the UPDATE wrote. if ( $lid >= 2 && $lid === $current ) { $seq = $lid - 1; } elseif ( $current >= 2 ) { // The UPDATE already incremented. Do not increment again (that skipped numbers). $seq = $current - 1; } } $wpdb->suppress_errors( $suppress ); if ( $seq < 1 ) { // Fallback for databases without LAST_INSERT_ID(expr), such as SQLite. wp_cache_delete( $option, 'options' ); wp_cache_delete( 'notoptions', 'options' ); $seq = max( 1, (int) get_option( $option, 1 ) ); update_option( $option, $seq + 1, false ); } wp_cache_delete( $option, 'options' ); wp_cache_delete( 'notoptions', 'options' ); return $seq; } /** * Whether a stored value is already a printed document number (has a prefix), not a bare sequence. * * @param string $number Stored meta. * @return bool */ public static function is_formatted_number( $number ) { $number = trim( (string) $number ); return '' !== $number && ! preg_match( '/^\d+$/', $number ); } /** * Sequence implied by stored number and/or sequence meta. * * @param string $number Stored document number or bare digits. * @param mixed $seq Stored sequence meta. * @return int */ public static function sequence_from_stored( $number, $seq ) { $seq = (int) $seq; if ( $seq > 0 ) { return $seq; } if ( preg_match( '/(\d+)\s*$/', (string) $number, $m ) ) { return (int) $m[1]; } return 0; } /** * The printed number for an order, from stored meta only. Never increments the counter. * * A bare integer in the number meta (or a sequence with no formatted number) is formatted * with the current prefix and padding. An already-formatted number is left unchanged. * * @param WC_Order $order Order. * @param string $number_meta Number meta key. * @param string $seq_meta Sequence meta key. * @param string $format_method format or format_invoice. * @return string */ public static function canonical_number( $order, $number_meta, $seq_meta, $format_method ) { $raw = trim( (string) $order->get_meta( $number_meta ) ); $seq = (int) $order->get_meta( $seq_meta ); if ( self::is_formatted_number( $raw ) ) { return $raw; } $seq = self::sequence_from_stored( $raw, $seq ); if ( $seq < 1 ) { return ''; } return self::$format_method( $seq ); } /** * If a number or sequence is already stored, return the canonical printed number and * persist a formatted value when only a bare sequence was saved. Never remints. * * @param WC_Order $order Order. * @param string $number_meta Number meta key. * @param string $seq_meta Sequence meta key. * @param string $date_meta Issued-at meta key. * @param string $format_method format or format_invoice. * @return string */ private static function persist_canonical( $order, $number_meta, $seq_meta, $date_meta, $format_method ) { $raw = trim( (string) $order->get_meta( $number_meta ) ); $seq = (int) $order->get_meta( $seq_meta ); if ( '' === $raw && $seq < 1 ) { return ''; } $number = self::canonical_number( $order, $number_meta, $seq_meta, $format_method ); if ( '' === $number ) { return ''; } $seq_now = self::sequence_from_stored( $raw, $seq ); $changed = false; if ( $number !== $raw ) { $order->update_meta_data( $number_meta, $number ); $changed = true; } if ( $seq_now > 0 && $seq !== $seq_now ) { $order->update_meta_data( $seq_meta, $seq_now ); $changed = true; } if ( $changed ) { if ( ! $order->get_meta( $date_meta ) ) { $order->update_meta_data( $date_meta, time() ); } $order->save(); } return $number; } }