@@ -350,41 +350,93 @@ protected function get_services_items() {
350
350
}
351
351
352
352
/**
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
354
354
*/
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 ;
359
359
$ data ->tail = array ();
360
360
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 ;
375
377
}
376
378
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 ;
382
386
}
383
387
}
384
388
389
+ if ( null !== $ data ->file ) {
390
+ $ complete_log = file ( $ data ->file );
391
+ $ data ->tail = array_slice ( $ complete_log , -10 );
392
+ }
393
+
385
394
return $ data ;
386
395
}
387
396
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 );
401
+
402
+ if ( $ line_count < 1 ) {
403
+
404
+ $ description = '' ;
405
+ $ log_tail = __ ( 'Log is empty ' , 'woocommerce-services ' );
406
+
407
+ } else {
408
+
409
+ $ url = add_query_arg (
410
+ array (
411
+ 'page ' => 'wc-status ' ,
412
+ 'tab ' => 'logs ' ,
413
+ 'log_file ' => $ log_data ->key
414
+ ),
415
+ admin_url ( 'admin.php ' )
416
+ );
417
+
418
+ $ description = sprintf (
419
+ wp_kses (
420
+ __ ( 'Last %d entries <a href="%s">Show full log</a> ' , 'woocommerce-services ' ),
421
+ array ( 'a ' => array ( 'href ' => array () ) ) ),
422
+ $ line_count ,
423
+ esc_url ( $ url )
424
+ );
425
+
426
+ $ log_tail = implode ( $ log_data ->tail , '' );
427
+
428
+ }
429
+
430
+ return (object ) array (
431
+ 'key ' => 'wcc_ ' . $ feature . '_log_tail ' ,
432
+ 'title ' => $ title ,
433
+ 'type ' => 'textarea ' ,
434
+ 'description ' => $ description ,
435
+ 'readonly ' => true ,
436
+ 'value ' => $ log_tail ,
437
+ );
438
+ }
439
+
388
440
protected function get_debug_items () {
389
441
$ debug_items = array ();
390
442
@@ -412,39 +464,9 @@ protected function get_debug_items() {
412
464
'save_on_toggle ' => true ,
413
465
);
414
466
415
- // add connect log tail
416
- $ log_data = $ this ->get_debug_log_data ();
417
- $ log_tail_line_count = count ( $ log_data ->tail );
418
- if ( $ log_tail_line_count < 1 ) {
419
- $ description = '' ;
420
- $ log_tail = __ ( 'Log is empty ' , 'woocommerce-services ' );
421
- } else {
422
- $ url = add_query_arg (
423
- array (
424
- 'page ' => 'wc-status ' ,
425
- 'tab ' => 'logs ' ,
426
- 'log_file ' => $ log_data ->key
427
- ),
428
- admin_url ( 'admin.php ' )
429
- );
430
- $ description = sprintf (
431
- wp_kses (
432
- __ ( 'Last %d entries <a href="%s">Show full log</a> ' , 'woocommerce-services ' ),
433
- array ( 'a ' => array ( 'href ' => array () ) ) ),
434
- $ log_tail_line_count ,
435
- esc_url ( $ url )
436
- );
437
- $ log_tail = implode ( $ log_data ->tail , '' );
438
- }
439
-
440
- $ debug_items [] = (object ) array (
441
- 'key ' => 'wcc_debug_log_tail ' ,
442
- 'title ' => __ ( 'Debug Log ' , 'woocommerce-services ' ),
443
- 'type ' => 'textarea ' ,
444
- 'description ' => $ description ,
445
- 'readonly ' => true ,
446
- 'value ' => $ log_tail
447
- );
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 ' ) );
448
470
449
471
return $ debug_items ;
450
472
}
0 commit comments