Skip to content

Commit 856e19b

Browse files
authored
Merge pull request #1303 from Automattic/fix/1163-shipping-details-debug
Output shipping debug messages on the Cart and Checkout pages.
2 parents c729cb6 + d6a32d0 commit 856e19b

27 files changed

+284
-126
lines changed

classes/class-wc-connect-help-view.php

Lines changed: 85 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -350,90 +350,124 @@ protected function get_services_items() {
350350
}
351351

352352
/**
353-
* Gets the last 10 lines from the WooCommerce Services log, if it exists
353+
* Gets the last 10 lines from the WooCommerce Services log by feature, if it exists
354354
*/
355-
protected function get_debug_log_data() {
356-
$data = new stdClass;
357-
$data->key = '';
358-
$data->file = '';
355+
protected function get_debug_log_data( $feature = '' ) {
356+
$data = new stdClass;
357+
$data->key = '';
358+
$data->file = null;
359359
$data->tail = array();
360360

361-
if ( method_exists( 'WC_Admin_Status', 'scan_log_files' ) ) {
362-
$logs = WC_Admin_Status::scan_log_files();
363-
$latest_file_date = 0;
364-
$file = null;
365-
$key = null;
366-
367-
foreach ( $logs as $log_key => $log_file ) {
368-
$log_file = WC_LOG_DIR . $log_file;
369-
$file_date = filemtime( $log_file );
370-
if ( 'wc-services-' === substr( $log_key, 0, 12 ) && $latest_file_date < $file_date ) {
371-
$latest_file_date = $file_date;
372-
$file = $log_file;
373-
$key = $log_key;
374-
}
361+
if ( ! method_exists( 'WC_Admin_Status', 'scan_log_files' ) ) {
362+
return $data;
363+
}
364+
365+
$log_prefix = 'wc\-services';
366+
367+
if ( ! empty( $feature ) ) {
368+
$log_prefix .= '\-' . $feature;
369+
}
370+
371+
$logs = WC_Admin_Status::scan_log_files();
372+
$latest_file_date = 0;
373+
374+
foreach ( $logs as $log_key => $log_file ) {
375+
if ( ! preg_match( '/' . $log_prefix . '\-[0-9a-f]{32}\-log/', $log_key ) ) {
376+
continue;
375377
}
376378

377-
if ( null !== $file ) {
378-
$complete_log = file( $file );
379-
$data->key = $key;
380-
$data->file = $file;
381-
$data->tail = array_slice( $complete_log, -10 );
379+
$log_file_path = WC_LOG_DIR . $log_file;
380+
$file_date = filemtime( $log_file_path );
381+
382+
if ( $latest_file_date < $file_date ) {
383+
$latest_file_date = $file_date;
384+
$data->file = $log_file_path;
385+
$data->key = $log_key;
382386
}
383387
}
384388

389+
if ( null !== $data->file ) {
390+
$complete_log = file( $data->file );
391+
$data->tail = array_slice( $complete_log, -10 );
392+
}
393+
385394
return $data;
386395
}
387396

388-
protected function get_debug_items() {
389-
$debug_items = array();
397+
protected function add_log_view( $title, $feature = '' ) {
398+
// add connect log tail
399+
$log_data = $this->get_debug_log_data( $feature );
400+
$line_count = count( $log_data->tail );
390401

391-
// add debug on/off boolean
392-
$debug_items[] = (object) array(
393-
'key' => 'wcc_debug_on',
394-
'title' => 'Debug Logging',
395-
'type' => 'boolean',
396-
'true_text' => __( 'Enabled', 'woocommerce-services' ),
397-
'false_text' => __( 'Disabled', 'woocommerce-services' ),
398-
'description' => '',
399-
'value' => $this->logger->is_debug_enabled(),
400-
'save_on_toggle' => true
401-
);
402+
if ( $line_count < 1 ) {
402403

403-
// add connect log tail
404-
$log_data = $this->get_debug_log_data();
405-
$log_tail_line_count = count( $log_data->tail );
406-
if ( $log_tail_line_count < 1 ) {
407404
$description = '';
408-
$log_tail = __( 'Log is empty', 'woocommerce-services' );
405+
$log_tail = __( 'Log is empty', 'woocommerce-services' );
406+
409407
} else {
408+
410409
$url = add_query_arg(
411410
array(
412-
'page' => 'wc-status',
413-
'tab' => 'logs',
411+
'page' => 'wc-status',
412+
'tab' => 'logs',
414413
'log_file' => $log_data->key
415414
),
416415
admin_url( 'admin.php' )
417416
);
417+
418418
$description = sprintf(
419419
wp_kses(
420420
__( 'Last %d entries <a href="%s">Show full log</a>', 'woocommerce-services' ),
421421
array( 'a' => array( 'href' => array() ) ) ),
422-
$log_tail_line_count,
422+
$line_count,
423423
esc_url( $url )
424424
);
425+
425426
$log_tail = implode( $log_data->tail, '' );
427+
426428
}
427429

428-
$debug_items[] = (object) array(
429-
'key' => 'wcc_debug_log_tail',
430-
'title' => __( 'Debug Log', 'woocommerce-services' ),
431-
'type' => 'textarea',
430+
return (object) array(
431+
'key' => 'wcc_' . $feature . '_log_tail',
432+
'title' => $title,
433+
'type' => 'textarea',
432434
'description' => $description,
433-
'readonly' => true,
434-
'value' => $log_tail
435+
'readonly' => true,
436+
'value' => $log_tail,
437+
);
438+
}
439+
440+
protected function get_debug_items() {
441+
$debug_items = array();
442+
443+
// add logging on/off boolean
444+
$debug_items[] = (object) array(
445+
'key' => 'wcc_logging_on',
446+
'title' => 'Logging',
447+
'type' => 'boolean',
448+
'true_text' => __( 'Enabled', 'woocommerce-services' ),
449+
'false_text' => __( 'Disabled', 'woocommerce-services' ),
450+
'description' => __( 'Write diagnostic messages to log files. Helpful when contacting support.', 'woocommerce-services' ),
451+
'value' => $this->logger->is_logging_enabled(),
452+
'save_on_toggle' => true,
435453
);
436454

455+
// add debug on/off boolean
456+
$debug_items[] = (object) array(
457+
'key' => 'wcc_debug_on',
458+
'title' => 'Debug',
459+
'type' => 'boolean',
460+
'true_text' => __( 'Enabled', 'woocommerce-services' ),
461+
'false_text' => __( 'Disabled', 'woocommerce-services' ),
462+
'description' => __( 'Display troubleshooting information on the Cart and Checkout pages.', 'woocommerce-services' ),
463+
'value' => $this->logger->is_debug_enabled(),
464+
'save_on_toggle' => true,
465+
);
466+
467+
$debug_items[] = $this->add_log_view( __( 'Shipping Log', 'woocommerce-services' ), 'shipping' );
468+
$debug_items[] = $this->add_log_view( __( 'Taxes Log', 'woocommerce-services' ), 'taxes' );
469+
$debug_items[] = $this->add_log_view( __( 'Other Log', 'woocommerce-services' ) );
470+
437471
return $debug_items;
438472
}
439473

classes/class-wc-connect-logger.php

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ class WC_Connect_Logger {
1010
private $logger;
1111

1212
private $is_logging_enabled = false;
13+
private $is_debug_enabled = false;
1314

14-
public function __construct( WC_Logger $logger ) {
15+
private $feature;
1516

16-
$this->logger = $logger;
17+
public function __construct( WC_Logger $logger, $feature = '' ) {
18+
19+
$this->logger = $logger;
20+
$this->feature = strtolower( $feature );
1721

1822
$this->is_logging_enabled = WC_Connect_Options::get_option( 'debug_logging_enabled', false );
23+
$this->is_debug_enabled = WC_Connect_Options::get_option( 'debug_display_enabled', false );
1924

2025
}
2126

@@ -54,19 +59,35 @@ public function disable_logging() {
5459
$this->is_logging_enabled = false;
5560
}
5661

62+
public function enable_debug() {
63+
WC_Connect_Options::update_option( 'debug_display_enabled', true );
64+
$this->is_debug_enabled = true;
65+
$this->log( 'Debug enabled' );
66+
}
67+
68+
public function disable_debug() {
69+
$this->log( 'Debug disabled' );
70+
WC_Connect_Options::update_option( 'debug_display_enabled', false );
71+
$this->is_debug_enabled = false;
72+
}
73+
5774
public function is_debug_enabled() {
75+
return $this->is_debug_enabled;
76+
}
77+
78+
public function is_logging_enabled() {
5879
return $this->is_logging_enabled;
5980
}
6081

6182
/**
62-
* Logs messages only when debugging is enabled
83+
* Log debug by printing it as notice when debugging is enabled.
6384
*
64-
* @param string $message Message to log
65-
* @param string $context Optional context (e.g. a class or function name)
85+
* @param string $message Debug message.
86+
* @param string $type Notice type.
6687
*/
67-
public function debug( $message, $context = '' ) {
68-
if ( $this->is_debug_enabled() ) {
69-
$this->log( $message, $context );
88+
public function debug( $message, $type = 'notice' ) {
89+
if ( $this->is_debug_enabled() ) {
90+
wc_add_notice( $message, $type );
7091
}
7192
}
7293

@@ -81,12 +102,32 @@ public function error( $message, $context = '' ) {
81102
$this->log( $message, $context );
82103
}
83104

84-
private function log( $message, $context = '' ) {
105+
/**
106+
* Logs messages to file and error_log if WP_DEBUG
107+
*
108+
* @param string $message Message to log
109+
* @param string $context Optional context (e.g. a class or function name)
110+
*/
111+
public function log( $message, $context = '' ) {
85112
$log_message = $this->format_message( $message, $context );
86-
$this->logger->add( 'wc-services', $log_message );
113+
87114
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
88115
error_log( $log_message );
89116
}
117+
118+
if ( ! $this->is_logging_enabled() ) {
119+
return;
120+
}
121+
122+
$log_file = 'wc-services';
123+
124+
if ( ! empty( $this->feature ) ) {
125+
$log_file .= '-' . $this->feature;
126+
}
127+
128+
$this->logger->add( $log_file, $log_message );
129+
130+
90131
}
91132

92133
}

classes/class-wc-connect-options.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static function get_option_names( $type = 'compact' ) {
3939
'tos_accepted',
4040
'store_guid',
4141
'debug_logging_enabled',
42+
'debug_display_enabled',
4243
'payment_methods',
4344
'account_settings',
4445
'paper_size',

classes/class-wc-connect-payment-methods-store.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public function fetch_payment_methods_from_connect_server() {
3333
$response_body = $this->api_client->get_payment_methods();
3434

3535
if ( is_wp_error( $response_body ) ) {
36-
$this->logger->debug( $response_body, __FUNCTION__ );
36+
$this->logger->log( $response_body, __FUNCTION__ );
3737
return;
3838
}
3939

4040
$payment_methods = $this->get_payment_methods_from_response_body( $response_body );
4141
if ( is_wp_error( $payment_methods ) ) {
42-
$this->logger->debug( $payment_methods, __FUNCTION__ );
42+
$this->logger->log( $payment_methods, __FUNCTION__ );
4343
return;
4444
}
4545

classes/class-wc-connect-service-schemas-store.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function fetch_service_schemas_from_connect_server() {
2626
$response_body = $this->api_client->get_service_schemas();
2727

2828
if ( is_wp_error( $response_body ) ) {
29-
$this->logger->debug( $response_body, __FUNCTION__ );
29+
$this->logger->log( $response_body, __FUNCTION__ );
3030
return false;
3131
}
3232

33-
$this->logger->debug( 'Successfully loaded service schemas from server response.', __FUNCTION__ );
33+
$this->logger->log( 'Successfully loaded service schemas from server response.', __FUNCTION__ );
3434
$this->update_last_fetch_timestamp();
3535
$this->maybe_update_heartbeat();
3636

classes/class-wc-connect-service-settings-store.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function get_account_settings() {
7676
public function update_account_settings( $settings ) {
7777
// simple validation for now
7878
if ( ! is_array( $settings ) ) {
79-
$this->logger->debug( 'Array expected but not received', __FUNCTION__ );
79+
$this->logger->log( 'Array expected but not received', __FUNCTION__ );
8080
return false;
8181
}
8282

@@ -576,7 +576,7 @@ private function translate_unit( $value ) {
576576
case 'yd':
577577
return __('yd', 'woocommerce-services');
578578
default:
579-
$this->logger->debug( 'Unexpected measurement unit: ' . $value, __FUNCTION__ );
579+
$this->logger->log( 'Unexpected measurement unit: ' . $value, __FUNCTION__ );
580580
return $value;
581581
}
582582
}

0 commit comments

Comments
 (0)