swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Log all things! |
| 8 | * |
| 9 | * @since 4.0.0 |
| 10 | * @version 4.0.0 |
| 11 | */ |
| 12 | class WC_Stripe_Logger { |
| 13 | |
| 14 | public static $logger; |
| 15 | const WC_LOG_FILENAME = 'woocommerce-gateway-stripe'; |
| 16 | |
| 17 | /** |
| 18 | * Utilize WC logger class |
| 19 | * |
| 20 | * @since 4.0.0 |
| 21 | * @version 4.0.0 |
| 22 | */ |
| 23 | public static function log( $message, $start_time = null, $end_time = null ) { |
| 24 | if ( ! class_exists( 'WC_Logger' ) ) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | if ( apply_filters( 'wc_stripe_logging', true, $message ) ) { |
| 29 | if ( empty( self::$logger ) ) { |
| 30 | self::$logger = wc_get_logger(); |
| 31 | } |
| 32 | |
| 33 | $settings = get_option( 'woocommerce_stripe_settings' ); |
| 34 | |
| 35 | if ( empty( $settings ) || isset( $settings['logging'] ) && 'yes' !== $settings['logging'] ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if ( ! is_null( $start_time ) ) { |
| 40 | |
| 41 | $formatted_start_time = date_i18n( get_option( 'date_format' ) . ' g:ia', $start_time ); |
| 42 | $end_time = is_null( $end_time ) ? current_time( 'timestamp' ) : $end_time; |
| 43 | $formatted_end_time = date_i18n( get_option( 'date_format' ) . ' g:ia', $end_time ); |
| 44 | $elapsed_time = round( abs( $end_time - $start_time ) / 60, 2 ); |
| 45 | |
| 46 | $log_entry = "\n" . '====Stripe Version: ' . WC_STRIPE_VERSION . '====' . "\n"; |
| 47 | $log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n"; |
| 48 | $log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n"; |
| 49 | |
| 50 | } else { |
| 51 | $log_entry = "\n" . '====Stripe Version: ' . WC_STRIPE_VERSION . '====' . "\n"; |
| 52 | $log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n"; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | self::$logger->debug( $log_entry, [ 'source' => self::WC_LOG_FILENAME ] ); |
| 57 | } |
| 58 | } |
| 59 | } |