| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
/** |
| 6 |
* Responsible for writing to the WooCommerce log. |
| 7 |
* |
| 8 |
* @since 4.0.0 |
| 9 |
* @version 4.0.0 |
| 10 |
*/ |
| 11 |
class WC_Vipps_Recurring_Logger { |
| 12 |
|
| 13 |
public static $logger; |
| 14 |
const WC_LOG_FILENAME = 'woocommerce-gateway-vipps-mobilepay-recurring'; |
| 15 |
|
| 16 |
/** |
| 17 |
* @uses WC_Logger class |
| 18 |
* |
| 19 |
* @since 4.0.0 |
| 20 |
* @version 4.0.0 |
| 21 |
* |
| 22 |
* @param $message |
| 23 |
* @param null $start_time |
| 24 |
* @param null $end_time |
| 25 |
*/ |
| 26 |
public static function log( $message, $start_time = null, $end_time = null ) { |
| 27 |
if ( ! class_exists( 'WC_Logger' ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
if ( apply_filters( 'wc_vipps_recurring_logging', true, $message ) ) { |
| 32 |
if ( empty( self::$logger ) ) { |
| 33 |
self::$logger = WC_Vipps_Recurring_Helper::is_wc_lt( '3.0' ) |
| 34 |
? new WC_Logger() |
| 35 |
: wc_get_logger(); |
| 36 |
} |
| 37 |
|
| 38 |
$settings = get_option( 'woocommerce_vipps_recurring_settings' ); |
| 39 |
|
| 40 |
if ( empty( $settings ) || ( isset( $settings['logging'] ) && 'yes' !== $settings['logging'] ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if ( $start_time !== null ) { |
| 45 |
$formatted_start_time = date_i18n( get_option( 'date_format' ) . ' g:ia', $start_time ); |
| 46 |
$end_time = $end_time ?? current_time( 'timestamp' ); |
| 47 |
$formatted_end_time = date_i18n( get_option( 'date_format' ) . ' g:ia', $end_time ); |
| 48 |
$elapsed_time = round( abs( $end_time - $start_time ) / 60, 2 ); |
| 49 |
|
| 50 |
$log_entry = "\n" . '==== Vipps/MobilePay Recurring Version: ' . WC_VIPPS_RECURRING_VERSION . ' ====' . "\n"; |
| 51 |
$log_entry .= '# Start Log ' . $formatted_start_time . "\n" . $message . "\n"; |
| 52 |
$log_entry .= '# End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')' . "\n\n"; |
| 53 |
} else { |
| 54 |
$log_entry = "\n" . '==== Vipps/MobilePay Recurring Version: ' . WC_VIPPS_RECURRING_VERSION . ' ====' . "\n"; |
| 55 |
$log_entry .= $message . "\n\n"; |
| 56 |
} |
| 57 |
|
| 58 |
WC_Vipps_Recurring_Helper::is_wc_lt( '3.0' ) |
| 59 |
? self::$logger->add( self::WC_LOG_FILENAME, $log_entry ) |
| 60 |
: self::$logger->debug( $log_entry, [ 'source' => self::WC_LOG_FILENAME ] ); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|