abstracts
1 month ago
admin
1 week ago
blocks
11 months ago
cli
1 month ago
customizer
4 months ago
data-stores
1 month ago
emails
1 month ago
export
1 year ago
gateways
1 month ago
import
3 months ago
integrations
2 months ago
interfaces
4 months ago
legacy
4 months ago
libraries
1 month ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 months ago
react-admin
1 month ago
rest-api
1 month ago
shipping
3 months ago
shortcodes
1 month ago
theme-support
2 years ago
tracks
1 month ago
traits
5 years ago
walkers
5 years ago
wccom-site
1 month ago
widgets
2 months ago
class-wc-ajax.php
1 month ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
9 months ago
class-wc-background-emailer.php
2 months ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
1 month ago
class-wc-breadcrumb.php
4 months ago
class-wc-cache-helper.php
1 month ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
3 months ago
class-wc-cart-totals.php
11 months ago
class-wc-cart.php
1 month ago
class-wc-checkout.php
1 month ago
class-wc-cli.php
10 months ago
class-wc-comments.php
4 months ago
class-wc-countries.php
2 months ago
class-wc-coupon.php
2 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
1 month ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 months ago
class-wc-discounts.php
11 months ago
class-wc-download-handler.php
1 month ago
class-wc-emails.php
1 month ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
4 months ago
class-wc-frontend-scripts.php
2 months ago
class-wc-geo-ip.php
9 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
2 months ago
class-wc-https.php
3 years ago
class-wc-install.php
1 week ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
4 months ago
class-wc-meta-data.php
1 month ago
class-wc-order-factory.php
2 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
5 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 month ago
class-wc-order-item-shipping.php
5 months ago
class-wc-order-item-tax.php
1 month ago
class-wc-order-item.php
1 month ago
class-wc-order-query.php
1 month ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 month ago
class-wc-payment-gateways.php
1 month ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
2 months ago
class-wc-post-types.php
1 month ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
10 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 months ago
class-wc-product-download.php
4 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
3 months ago
class-wc-product-grouped.php
3 months ago
class-wc-product-query.php
4 months ago
class-wc-product-simple.php
11 months ago
class-wc-product-variable.php
1 month ago
class-wc-product-variation.php
1 month ago
class-wc-query.php
1 month ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
2 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
7 months ago
class-wc-shipping.php
2 months ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
1 month ago
class-wc-tax.php
2 months ago
class-wc-template-loader.php
8 months ago
class-wc-tracker.php
1 month ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 month ago
class-woocommerce.php
1 week ago
wc-account-functions.php
1 month ago
wc-attribute-functions.php
1 month ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
1 month ago
wc-conditional-functions.php
11 months ago
wc-core-functions.php
2 months ago
wc-coupon-functions.php
5 months ago
wc-deprecated-functions.php
1 month ago
wc-formatting-functions.php
1 month ago
wc-interactivity-api-functions.php
2 months ago
wc-notice-functions.php
5 months ago
wc-order-functions.php
2 months ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
4 months ago
wc-page-functions.php
2 months ago
wc-product-functions.php
2 months ago
wc-rest-functions.php
7 months ago
wc-stock-functions.php
7 months ago
wc-template-functions.php
1 month ago
wc-template-hooks.php
10 months ago
wc-term-functions.php
2 months ago
wc-update-functions.php
1 week ago
wc-user-functions.php
1 month ago
wc-webhook-functions.php
1 month ago
wc-widget-functions.php
5 years ago
wc-order-step-logger-functions.php
167 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Order Step Logging Functions |
| 4 | * |
| 5 | * Tracks the steps of the checkout process for place order debugging. |
| 6 | * |
| 7 | * @package WooCommerce\Functions |
| 8 | * @version 9.7.0 |
| 9 | */ |
| 10 | |
| 11 | declare( strict_types=1 ); |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessingController; |
| 16 | use Automattic\WooCommerce\Internal\Logging\OrderLogsDeletionProcessor; |
| 17 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 18 | |
| 19 | /** |
| 20 | * Log an order-related message. This is not public API and should not be used by plugins or themes. |
| 21 | * |
| 22 | * @param string $message Message to log. |
| 23 | * @param array|null $context Optional. Additional information for log handlers. |
| 24 | * @param bool $final_step Optional. Whether this is the final step of the order logging, and should clear the log. |
| 25 | * @param bool $first_step Optional. Whether this is declared the first step in order to start a new log. |
| 26 | * |
| 27 | * @internal This function is intended for internal use only. |
| 28 | * @since 9.9.0 |
| 29 | */ |
| 30 | function wc_log_order_step( string $message, ?array $context = null, bool $final_step = false, bool $first_step = false ): void { |
| 31 | static $logging_active; |
| 32 | static $order; |
| 33 | |
| 34 | try { |
| 35 | if ( empty( $message ) ) { |
| 36 | return; // Nothing to log. |
| 37 | } |
| 38 | |
| 39 | if ( $first_step ) { |
| 40 | $logging_active = true; |
| 41 | } |
| 42 | |
| 43 | if ( ! $logging_active ) { |
| 44 | return; // Whenever the method is called without a started logging session, it will be ignored. |
| 45 | } |
| 46 | |
| 47 | static $order_uid, $order_uid_short, $store_url; |
| 48 | static $steps = array(); // Static array to store the messages and validate against unique messages before clearing the log. |
| 49 | |
| 50 | // Generate a static place order unique ID for logging purposes. When this is called multiple times in the same request, |
| 51 | // the same UID will be used, enabling us to track recursion and race-condition issues on order processing methods |
| 52 | // or other problems related to third-party plugins and filters. |
| 53 | $order_uid = $order_uid ? $order_uid : wp_generate_uuid4(); |
| 54 | $order_uid_short = $order_uid_short ? $order_uid_short : substr( $order_uid, 0, 8 ); |
| 55 | $store_url = $store_url ? $store_url : get_site_url(); |
| 56 | |
| 57 | $context['order_uid'] = $order_uid; |
| 58 | $context['source'] = 'place-order-debug-' . $order_uid_short; // Source is segmented per order unique id. |
| 59 | $context['store_url'] = $store_url; |
| 60 | |
| 61 | // Extract safe data from order object. |
| 62 | if ( ( $context['order_object'] ?? null ) instanceof WC_Order ) { |
| 63 | $order = $context['order_object']; |
| 64 | $context = array_merge( extract_order_safe_data( $order ), $context ); |
| 65 | unset( $context['order_object'] ); // This is super-important to avoid logging sensitive data. |
| 66 | $order->add_meta_data( '_debug_log_source', $context['source'], true ); |
| 67 | $order->save(); |
| 68 | } |
| 69 | |
| 70 | // Use the global logger instance which respects site's logging configuration. |
| 71 | $logger = wc_get_logger(); |
| 72 | |
| 73 | if ( ! is_null( error_get_last() ) ) { |
| 74 | $context['last_error'] = error_get_last(); |
| 75 | } |
| 76 | $context['backtrace'] = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 3 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 77 | $context['remote-logging'] = false; // forcing disable on remote logging. |
| 78 | |
| 79 | $steps[] = $message; |
| 80 | $logger->log( WC_Log_Levels::DEBUG, $message, $context ); |
| 81 | |
| 82 | if ( $final_step ) { |
| 83 | // Clears the log if instructed and all steps are unique. |
| 84 | // We'll schedule the deletion for later in order to speed up the checkout process |
| 85 | // unless a custom (non-Woo core) orders data store is in use, because in that case there's |
| 86 | // no reliable way to query orders by meta key. |
| 87 | if ( $order && ( count( array_unique( $steps ) ) === count( $steps ) ) ) { |
| 88 | $order->delete_meta_data( '_debug_log_source' ); |
| 89 | if ( OrderUtil::unknown_orders_data_store_in_use() ) { |
| 90 | if ( $logger instanceof WC_Logger ) { |
| 91 | $logger->clear( $context['source'] ); |
| 92 | } |
| 93 | $order->save(); |
| 94 | } else { |
| 95 | $order->add_meta_data( '_debug_log_source_pending_deletion', $context['source'], true ); |
| 96 | $order->save(); |
| 97 | wc_get_container()->get( BatchProcessingController::class )->enqueue_processor( OrderLogsDeletionProcessor::class ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Prevent further logging for this request. |
| 102 | $order = null; |
| 103 | $logging_active = false; |
| 104 | $steps = array(); |
| 105 | } |
| 106 | } catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 107 | // Since this runs in a critical path, we need to catch any exceptions and ignore them. |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Extract safe data from order object for debug temporary logging. |
| 113 | * |
| 114 | * @param WC_Order $order Order object. |
| 115 | * |
| 116 | * @internal This function is intended for internal use only. |
| 117 | * @since 9.9.0 |
| 118 | */ |
| 119 | function extract_order_safe_data( WC_Order $order ) { |
| 120 | $order_data = array( |
| 121 | 'order_id' => $order->get_id(), |
| 122 | 'payment_method' => $order->get_payment_method_title(), |
| 123 | 'billing' => array( |
| 124 | 'country' => $order->get_billing_country(), |
| 125 | 'state' => $order->get_billing_state(), |
| 126 | ), |
| 127 | 'shipping' => array( |
| 128 | 'country' => $order->get_shipping_country(), |
| 129 | 'state' => $order->get_shipping_state(), |
| 130 | ), |
| 131 | 'used_coupons' => $order->get_coupon_codes(), |
| 132 | 'totals' => array( |
| 133 | 'subtotal' => $order->get_subtotal(), |
| 134 | 'shipping' => $order->get_shipping_total(), |
| 135 | 'tax' => $order->get_total_tax(), |
| 136 | 'discount' => $order->get_discount_total(), |
| 137 | 'total' => $order->get_total(), |
| 138 | ), |
| 139 | |
| 140 | ); |
| 141 | |
| 142 | // Add tax breakdown. |
| 143 | foreach ( $order->get_tax_totals() as $tax ) { |
| 144 | $order_data['totals']['tax_breakdown'][ $tax->label ] = $tax->amount; |
| 145 | } |
| 146 | |
| 147 | // Add cart items and quantities. |
| 148 | foreach ( $order->get_items() as $item_id => $item ) { |
| 149 | $order_data['cart_items'][] = array( |
| 150 | 'id' => $item_id, |
| 151 | 'product_id' => $item->get_product_id(), |
| 152 | 'variation_id' => $item->get_variation_id(), |
| 153 | 'quantity' => $item->get_quantity(), |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | // Add Shipping methods used. |
| 158 | foreach ( $order->get_shipping_methods() as $method ) { |
| 159 | $order_data['shipping_methods'][] = array( |
| 160 | 'id' => $method->get_method_id(), |
| 161 | 'label' => $method->get_method_title(), |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | return $order_data; |
| 166 | } |
| 167 |