Status > Logs). Off by default: nothing is written then. * * Other parts and add-ons log through Log::info() / Log::error(), or with * `do_action( 'aiolos_log', $message, $context, $level )`. */ final class Log { public const SOURCE = 'aiolos-checkout'; /** * Hooks. */ public static function init(): void { add_action( 'aiolos_log', array( self::class, 'from_action' ), 10, 3 ); // Why an order could not be placed: the codes and messages WooCommerce shows the customer. // The setting is read when they run, not here: reading the settings while the plugin starts // would cache them before every part has added its fields. add_action( 'woocommerce_after_checkout_validation', array( self::class, 'checkout_errors' ), PHP_INT_MAX, 2 ); add_action( 'woocommerce_checkout_order_processed', array( self::class, 'order_placed' ), 10, 2 ); } /** * Whether the log is on. */ public static function enabled(): bool { /** * Filters whether the debug log is written. * * @since 1.6.0 * * @param bool $enabled From the "Debug log" setting. */ return (bool) apply_filters( 'aiolos_debug_log_enabled', Settings::enabled( 'debug_log' ) ); } /** * Information. * * @param string $message Message. * @param array $context Context (printed as JSON). */ public static function info( string $message, array $context = array() ): void { self::write( 'info', $message, $context ); } /** * An error. * * @param string $message Message. * @param array $context Context. */ public static function error( string $message, array $context = array() ): void { self::write( 'error', $message, $context ); } /** * The `aiolos_log` action. * * @param mixed $message Message. * @param mixed $context Context. * @param mixed $level Level (debug, info, notice, warning, error). */ public static function from_action( $message, $context = array(), $level = 'info' ): void { if ( ! is_scalar( $message ) ) { return; } self::write( is_string( $level ) ? $level : 'info', (string) $message, is_array( $context ) ? $context : array() ); } /** * Writes one entry. * * @param string $level Level. * @param string $message Message. * @param array $context Context. */ private static function write( string $level, string $message, array $context ): void { if ( ! function_exists( 'wc_get_logger' ) || ! self::enabled() ) { return; } $levels = array( 'emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug' ); $level = in_array( $level, $levels, true ) ? $level : 'info'; if ( $context ) { $json = wp_json_encode( self::redact( $context ) ); $message .= is_string( $json ) ? ' ' . $json : ''; } wc_get_logger()->log( $level, $message, array( 'source' => self::SOURCE ) ); } /** * Context without passwords, keys and tokens. * * @param array $context Context. * @return array */ private static function redact( array $context ): array { foreach ( $context as $key => $value ) { if ( is_string( $key ) && Transfer::is_secret_key( $key ) ) { $context[ $key ] = '***'; } elseif ( is_array( $value ) ) { $context[ $key ] = self::redact( $value ); } } return $context; } /** * Logs the checkout validation errors. * * @param array $data Posted data. * @param \WP_Error $errors Errors. */ public static function checkout_errors( $data, $errors ): void { if ( ! self::enabled() || ! is_object( $errors ) || ! method_exists( $errors, 'get_error_codes' ) || ! $errors->get_error_codes() ) { return; } $list = array(); foreach ( $errors->get_error_codes() as $code ) { $list[ (string) $code ] = wp_strip_all_tags( (string) $errors->get_error_message( $code ) ); } self::info( 'Checkout not placed: validation errors.', array( 'errors' => $list, 'payment_method' => is_array( $data ) && is_scalar( $data['payment_method'] ?? null ) ? (string) $data['payment_method'] : '', 'shipping_method' => is_array( $data ) && is_array( $data['shipping_method'] ?? null ) ? array_values( array_filter( $data['shipping_method'], 'is_scalar' ) ) : array(), ) ); } /** * Logs a placed order (id, payment method, whether our checkout sent it). * * @param int $order_id Order id. * @param array $data Posted data. */ public static function order_placed( $order_id, $data = array() ): void { if ( ! self::enabled() ) { return; } self::info( 'Order placed.', array( 'order' => (int) $order_id, 'payment_method' => is_array( $data ) && is_scalar( $data['payment_method'] ?? null ) ? (string) $data['payment_method'] : '', 'aiolos' => class_exists( \AiolosCheckout\Checkout\Checkout::class ) && method_exists( \AiolosCheckout\Checkout\Checkout::class, 'from_our_form' ) && \AiolosCheckout\Checkout\Checkout::from_our_form(), ) ); } /** * The WooCommerce logs screen, filtered to this plugin. */ public static function url(): string { return admin_url( 'admin.php?page=wc-status&tab=logs&source=' . self::SOURCE ); } }