From 36230d6869a77ab6b98f5a32610af24ebaa81aed Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Tue, 17 May 2022 16:20:46 +0300 Subject: [PATCH 1/3] View Logs --- includes/classes/Settings.php | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/includes/classes/Settings.php b/includes/classes/Settings.php index 2ea6cea..3b155b3 100644 --- a/includes/classes/Settings.php +++ b/includes/classes/Settings.php @@ -25,6 +25,8 @@ public function __construct() { add_action( 'admin_init', array( $this, 'fields' ), 20 ); add_filter( 'sanitize_option_' . SETTINGS_GROUP, array( $this, 'sanitize' ), 10, 2 ); + + add_action( 'admin_menu', array( $this, 'logs_menu' ) ); } /** @@ -57,6 +59,66 @@ public function fields() { ); } + /** + * Add logs page under Tools + * + * @return void + */ + public function logs_menu() { + add_management_page( + __( 'Sophi Logs', 'debug-bar-for-sophi' ), + __( 'Sophi Logs', 'debug-bar-for-sophi' ), + 'manage_options', + 'sophi-logs', + array( $this, 'logs_page' ) + ); + } + + /** + * Render Sophi logs page + * + * @return void + */ + public function logs_page() { + $files = glob( trailingslashit( SOPHI_DEBUG_BAR_LOG_PATH ) . 'sophi-*.log' ); + $dates = array(); + + foreach ( $files as $file ) { + if ( preg_match( '/.*?sophi-([\d\-]+)\.log/', $file, $match ) ) { + $dates[] = $match[1]; + } + } + + $dates = array_reverse( $dates ); + + if ( isset( $_REQUEST['date'] ) ) { + check_admin_referer( 'sophi-logs' ); + $current = sanitize_file_name( $_REQUEST['date'] ); + } else { + $current = reset( $dates ); + } + + //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $log_content = file_get_contents( trailingslashit( SOPHI_DEBUG_BAR_LOG_PATH ) . "sophi-{$current}.log" ); + + ?> +
+

+
+ + + +
+ +
+
+ Date: Tue, 17 May 2022 16:25:02 +0300 Subject: [PATCH 2/3] Link to logs from settings --- includes/classes/Settings.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/classes/Settings.php b/includes/classes/Settings.php index 3b155b3..6f5d370 100644 --- a/includes/classes/Settings.php +++ b/includes/classes/Settings.php @@ -162,7 +162,7 @@ public function render_debug_log_field() { /> - + %s is not writable.', 'sophi-debug-log' ), + __( 'The logs directory %s is not writable.', 'debug-bar-for-sophi' ), esc_attr( SOPHI_DEBUG_BAR_LOG_PATH ) ) ); echo '

'; + } else { + echo '

'; + echo '' . esc_html__( 'View Logs', 'debug-bar-for-sophi' ) . ''; + echo '

'; } } @@ -196,7 +200,7 @@ public function render_disable_sophi_caching() { value="yes" /> - + Date: Tue, 17 May 2022 16:34:24 +0300 Subject: [PATCH 3/3] Show message if no log available --- includes/classes/Settings.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/includes/classes/Settings.php b/includes/classes/Settings.php index 6f5d370..e6b2f51 100644 --- a/includes/classes/Settings.php +++ b/includes/classes/Settings.php @@ -98,8 +98,15 @@ public function logs_page() { $current = reset( $dates ); } + $log_file = trailingslashit( SOPHI_DEBUG_BAR_LOG_PATH ) . "sophi-{$current}.log"; + + if ( ! file_exists( $log_file ) ) { + esc_html_e( 'There is no log from Sophi.io available yet.', 'debug-bar-for-sophi' ); + return; + } + //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents - $log_content = file_get_contents( trailingslashit( SOPHI_DEBUG_BAR_LOG_PATH ) . "sophi-{$current}.log" ); + $log_content = file_get_contents( $log_file ); ?>
@@ -111,7 +118,7 @@ public function logs_page() { - +