diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-09-03 23:29:30 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-09-03 23:29:30 +0200 |
commit | 76b92fd85f056b1dede81f8313080d3bd8e38aef (patch) | |
tree | 60721303c189c80e154b7a62594a0d3825c9377e | |
parent | bcedc09787d4af90742716b7a3dd0c80daf14450 (diff) | |
download | woocommerce-taler-76b92fd85f056b1dede81f8313080d3bd8e38aef.tar.gz woocommerce-taler-76b92fd85f056b1dede81f8313080d3bd8e38aef.tar.bz2 woocommerce-taler-76b92fd85f056b1dede81f8313080d3bd8e38aef.zip |
logging
-rw-r--r-- | plugin/GNU-Taler-Payment-Gateway/class-wc-gnutaler-gateway.php | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/plugin/GNU-Taler-Payment-Gateway/class-wc-gnutaler-gateway.php b/plugin/GNU-Taler-Payment-Gateway/class-wc-gnutaler-gateway.php index 094ebd9..784ea91 100644 --- a/plugin/GNU-Taler-Payment-Gateway/class-wc-gnutaler-gateway.php +++ b/plugin/GNU-Taler-Payment-Gateway/class-wc-gnutaler-gateway.php @@ -105,6 +105,13 @@ function gnutaler_init_gateway_class() */ class WC_GNUTaler_Gateway extends WC_Payment_Gateway { + + // plugin logs + private static $logger = false; + + // is logging enabled? + private static $log_enabled = false; + /** * Class constructor */ @@ -125,15 +132,8 @@ function gnutaler_init_gateway_class() ); // FIXME: I think this broke! - $label = 'Enable logging'; - if ( defined( 'WC_LOG_DIR' ) ) { - $log_url = add_query_arg( 'tab', 'logs', add_query_arg( 'page', 'wc-status', admin_url( 'admin.php' ) ) ); - $log_key = 'gnutaler-' . sanitize_file_name( wp_hash( 'gnutaler' ) ) . '-log'; - $log_url = add_query_arg( 'log_file', $log_key, $log_url ); - $label .= ' | ' . sprintf( wp_kses( __( '<a href="%s">View Log<a>', 'gnutaler' ), - array('a' => array ('href' => array() ) ) ), - esc_url( $log_url ) ); - } + $this->debug = 'yes' === $this->get_option( 'debug', 'no' ); + self::$log_enabled = $this->debug; // setup 'form_fields' $this->form_fields = array( @@ -175,10 +175,11 @@ function gnutaler_init_gateway_class() 'description' => __( 'Set the text the customer will see when confirming payment. %s will be substituted with the order number. (Example: MyShop #%s)', 'gnutaler' ), 'default' => 'WooTalerShop #%s', ), - 'wc_gnutaler_debug' => array( - 'title' => __( 'Debug Log', 'gnutaler' ), - 'label' => $log_label, - 'description' => __( 'Enable debug-level logging for the GNU Taler payment plugin', 'gnutaler' ), + 'debug' => array( + 'title' => __( 'Debug Log', 'woocommerce' ), + 'label' => __( 'Enable logging', 'woocommerce' ), + 'description' => sprintf( __( 'Log GNU Taler events inside %s.', 'gnutaler' ), + '<code>' . WC_Log_Handler_File::get_log_file_path( 'gnutaler' ) . '</code>' ), 'type' => 'checkbox', 'default' => 'no' ) @@ -260,6 +261,29 @@ function gnutaler_init_gateway_class() } } + /** + * Processes and saves options. + * If there is an error thrown, will continue to + * save and validate fields, but + * will leave the erroring field out. + * + * @return bool was anything saved? + */ + public function process_admin_options() { + $saved = parent::process_admin_options(); + + // Maybe clear logs. + if ( 'yes' !== $this->get_option( 'debug', 'no' ) ) { + if ( empty( self::$log ) ) { + self::$log = wc_get_logger(); + } + self::$log->clear( 'gnutaler' ); + } + + return $saved; + } + + public function payment_fields () { // We do not have any. } @@ -884,18 +908,23 @@ function gnutaler_init_gateway_class() function log ($level, $msg) { - if ( function_exists ('wp_get_current_user()') ) + if (! self::$log_enabled ) + return; + if ( function_exists ('wp_get_current_user()') ) { - $user_id = wp_get_current_user(); + $user_id = wp_get_current_user(); if (! isset( $user_id )) - $user_id = __( '<user ID not set>', 'gnutaler' ); + $user_id = __( '<user ID not set>', 'gnutaler' ); } else { $user_id = 'Guest'; } $order_id = $_GET['order_id']; - $this->logger->log( $level, $user_id . '-' . $order_id . ': ' . $msg ); + if ( empty( self::$logger ) ) { + self::$logger = wc_get_logger(); + } + self::$logger->log( $level, $user_id . '-' . $order_id . ': ' . $msg, array ('source' => 'gnutaler' ) ); $this->add_log_entry( $level, $user_id . '-' . $order_id . ': ' . $msg ); } |