From 2316d86ac402fa0bf46991a472ad59685b7029f0 Mon Sep 17 00:00:00 2001 From: Daniele Alessandra Date: Wed, 2 Oct 2024 18:36:42 +0200 Subject: [PATCH 1/2] Add: unfiltered_site_url; Add: Scheduled Crons; Add: Collapsible tables Fix: Export logs in CSV PHP compatibility Formatting [debug page] Fixed code style, Fixed duplicated header in exported file Changed toggle script from vanilla JS to jQuery Toggle button in a variable, class names Added toggle button factory function Added border width Moved css snippet to debug.scss --- assets/css/admin/debug.css | 2 +- assets/scss/admin/debug.scss | 7 +++ includes/class-fs-logger.php | 40 +++++++++++++-- templates/debug.php | 78 ++++++++++++++++++++++++++--- templates/debug/scheduled-crons.php | 10 +++- 5 files changed, 125 insertions(+), 12 deletions(-) diff --git a/assets/css/admin/debug.css b/assets/css/admin/debug.css index 84d9404fe..7561b1d07 100644 --- a/assets/css/admin/debug.css +++ b/assets/css/admin/debug.css @@ -1 +1 @@ -label.fs-tag,span.fs-tag{background:#ffba00;border-radius:3px;color:#fff;display:inline-block;font-size:11px;line-height:11px;padding:5px;vertical-align:baseline}label.fs-tag.fs-warn,span.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-info,span.fs-tag.fs-info{background:#00a0d2}label.fs-tag.fs-success,span.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error,span.fs-tag.fs-error{background:#dc3232}.fs-switch-label{font-size:20px;line-height:31px;margin:0 5px}#fs_log_book table{font-family:Consolas,Monaco,monospace;font-size:12px}#fs_log_book table th{color:#ccc}#fs_log_book table tr{background:#232525}#fs_log_book table tr.alternate{background:#2b2b2b}#fs_log_book table tr td.fs-col--logger{color:#5a7435}#fs_log_book table tr td.fs-col--type{color:#ffc861}#fs_log_book table tr td.fs-col--function{color:#a7b7b1;font-weight:700}#fs_log_book table tr td.fs-col--message,#fs_log_book table tr td.fs-col--message a{color:#9a73ac!important}#fs_log_book table tr td.fs-col--file{color:#d07922}#fs_log_book table tr td.fs-col--timestamp{color:#6596be} \ No newline at end of file +label.fs-tag,span.fs-tag{background:#ffba00;border-radius:3px;color:#fff;display:inline-block;font-size:11px;line-height:11px;padding:5px;vertical-align:baseline}label.fs-tag.fs-warn,span.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-info,span.fs-tag.fs-info{background:#00a0d2}label.fs-tag.fs-success,span.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error,span.fs-tag.fs-error{background:#dc3232}.fs-switch-label{font-size:20px;line-height:31px;margin:0 5px}.fs-debug-table-toggle-button{background:transparent;border:none;cursor:pointer;font-size:1.2em}#fs_log_book table{font-family:Consolas,Monaco,monospace;font-size:12px}#fs_log_book table th{color:#ccc}#fs_log_book table tr{background:#232525}#fs_log_book table tr.alternate{background:#2b2b2b}#fs_log_book table tr td.fs-col--logger{color:#5a7435}#fs_log_book table tr td.fs-col--type{color:#ffc861}#fs_log_book table tr td.fs-col--function{color:#a7b7b1;font-weight:700}#fs_log_book table tr td.fs-col--message,#fs_log_book table tr td.fs-col--message a{color:#9a73ac!important}#fs_log_book table tr td.fs-col--file{color:#d07922}#fs_log_book table tr td.fs-col--timestamp{color:#6596be} \ No newline at end of file diff --git a/assets/scss/admin/debug.scss b/assets/scss/admin/debug.scss index e2abdce53..bf8236bf2 100644 --- a/assets/scss/admin/debug.scss +++ b/assets/scss/admin/debug.scss @@ -8,6 +8,13 @@ margin: 0 5px; } +.fs-debug-table-toggle-button { + font-size: 1.2em; + cursor: pointer; + background: transparent; + border: none; +} + #fs_log_book { table { font-family: Consolas,Monaco,monospace; diff --git a/includes/class-fs-logger.php b/includes/class-fs-logger.php index c5f0c52a1..bee0e76da 100644 --- a/includes/class-fs-logger.php +++ b/includes/class-fs-logger.php @@ -652,8 +652,6 @@ public static function download_db_logs( } $filepath = rtrim( $upload_dir['path'], '/' ) . "/{$filename}"; - $query .= " INTO OUTFILE '{$filepath}' FIELDS TERMINATED BY '\t' ESCAPED BY '\\\\' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n'"; - $columns = ''; for ( $i = 0, $len = count( self::$_log_columns ); $i < $len; $i ++ ) { if ( $i > 0 ) { @@ -665,12 +663,18 @@ public static function download_db_logs( $query = "SELECT {$columns} UNION ALL " . $query; - $result = $wpdb->query( $query ); + $result = $wpdb->get_results( $query ); if ( false === $result ) { return false; } + $write_file = self::write_csv_to_filesystem( $filepath, $result ); + + if ( false === $write_file ) { + return false; + } + return rtrim( $upload_dir['url'], '/' ) . '/' . $filename; } @@ -691,5 +695,35 @@ public static function get_logs_download_url( $filename = '' ) { return rtrim( $upload_dir['url'], '/' ) . $filename; } + /** + * @param string $file_path + * @param array $query_results + * + * @return bool + */ + private static function write_csv_to_filesystem( $file_path, $query_results ) { + if ( empty( $query_results ) ) { + return false; + } + + WP_Filesystem(); + global $wp_filesystem; + + $content = ''; + + foreach ( $query_results as $row ) { + $row_data = array_map( function ( $value ) { + return str_replace( "\n", ' ', $value ); + }, (array) $row ); + $content .= implode( "\t", $row_data ) . "\n"; + } + + if ( ! $wp_filesystem->put_contents( $file_path, $content, FS_CHMOD_FILE ) ) { + return false; + } + + return true; + } + #endregion } diff --git a/templates/debug.php b/templates/debug.php index b45a6ff53..9dc392b13 100644 --- a/templates/debug.php +++ b/templates/debug.php @@ -25,6 +25,13 @@ $is_multisite = is_multisite(); $auto_off_timestamp = wp_next_scheduled( 'fs_debug_turn_off_logging_hook' ) * 1000; + + function fs_get_debug_table_toggle_button( $open = false ) { + return ' + '; + } ?>

newest->version ?>

@@ -261,6 +268,10 @@ function stopCountdownManually() { 'key' => 'wp_using_ext_object_cache()', 'val' => wp_using_ext_object_cache() ? 'true' : 'false', ), + array( + 'key' => 'Freemius::get_unfiltered_site_url()', + 'val' => Freemius::get_unfiltered_site_url(), + ), ) ?>
@@ -284,7 +295,10 @@ function stopCountdownManually() { -

+

+ + +

@@ -319,7 +333,11 @@ function stopCountdownManually() { get_option( $module_type . 's' ), FS_Plugin::get_class_name() ) ?> 0 ) : ?> -

+

+ + +

@@ -452,11 +470,14 @@ function stopCountdownManually() { $all_plans = false; ?> 0 ) : ?> -

+ + /

+ ) ) ?> / +
@@ -567,7 +588,10 @@ function stopCountdownManually() { $addons = $VARS['addons']; ?> $plugin_addons ) : ?> -

+

+ + +

@@ -626,7 +650,10 @@ function stopCountdownManually() { ?> -

+

+ + +

@@ -675,7 +702,10 @@ function stopCountdownManually() { */ $licenses = $VARS[ $module_type . '_licenses' ] ?> 0 ) : ?> -

+

+ + +

@@ -714,6 +744,14 @@ function stopCountdownManually() {
+ 'h2', + 'toggle_button' => fs_get_debug_table_toggle_button(), + ); + + fs_require_template( 'debug/scheduled-crons.php', $page_params ); +?>

@@ -888,3 +926,29 @@ class="dashicons dashicons-download"> + diff --git a/templates/debug/scheduled-crons.php b/templates/debug/scheduled-crons.php index 47a715ea4..4989bf3c6 100644 --- a/templates/debug/scheduled-crons.php +++ b/templates/debug/scheduled-crons.php @@ -13,6 +13,11 @@ $fs_options = FS_Options::instance( WP_FS__ACCOUNTS_OPTION_NAME, true ); $scheduled_crons = array(); + $title_tag = isset( $VARS['title_tag'] ) ? $VARS['title_tag'] : 'h1'; + $title_tag_open = "<$title_tag>"; + $title_tag_close = ""; + $toggle_button = isset( $VARS['toggle_button'] ) ? $VARS['toggle_button'] : ''; + $module_types = array( WP_FS__MODULE_TYPE_PLUGIN, WP_FS__MODULE_TYPE_THEME @@ -73,7 +78,10 @@ $sec_text = fs_text_x_inline( 'sec', 'seconds' ); ?> -

+ + + + From 65fc86c3aa666ecdd1cdf2517118a3534e949401 Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Thu, 5 Dec 2024 22:58:38 +0800 Subject: [PATCH 2/2] [debug-page] [code] Enhancements --- assets/css/admin/debug.css | 2 +- includes/class-fs-logger.php | 4 +++ templates/debug.php | 32 ++++++++-------------- templates/debug/partials/toggle-button.php | 21 ++++++++++++++ templates/debug/scheduled-crons.php | 5 +++- 5 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 templates/debug/partials/toggle-button.php diff --git a/assets/css/admin/debug.css b/assets/css/admin/debug.css index 7561b1d07..84d9404fe 100644 --- a/assets/css/admin/debug.css +++ b/assets/css/admin/debug.css @@ -1 +1 @@ -label.fs-tag,span.fs-tag{background:#ffba00;border-radius:3px;color:#fff;display:inline-block;font-size:11px;line-height:11px;padding:5px;vertical-align:baseline}label.fs-tag.fs-warn,span.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-info,span.fs-tag.fs-info{background:#00a0d2}label.fs-tag.fs-success,span.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error,span.fs-tag.fs-error{background:#dc3232}.fs-switch-label{font-size:20px;line-height:31px;margin:0 5px}.fs-debug-table-toggle-button{background:transparent;border:none;cursor:pointer;font-size:1.2em}#fs_log_book table{font-family:Consolas,Monaco,monospace;font-size:12px}#fs_log_book table th{color:#ccc}#fs_log_book table tr{background:#232525}#fs_log_book table tr.alternate{background:#2b2b2b}#fs_log_book table tr td.fs-col--logger{color:#5a7435}#fs_log_book table tr td.fs-col--type{color:#ffc861}#fs_log_book table tr td.fs-col--function{color:#a7b7b1;font-weight:700}#fs_log_book table tr td.fs-col--message,#fs_log_book table tr td.fs-col--message a{color:#9a73ac!important}#fs_log_book table tr td.fs-col--file{color:#d07922}#fs_log_book table tr td.fs-col--timestamp{color:#6596be} \ No newline at end of file +label.fs-tag,span.fs-tag{background:#ffba00;border-radius:3px;color:#fff;display:inline-block;font-size:11px;line-height:11px;padding:5px;vertical-align:baseline}label.fs-tag.fs-warn,span.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-info,span.fs-tag.fs-info{background:#00a0d2}label.fs-tag.fs-success,span.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error,span.fs-tag.fs-error{background:#dc3232}.fs-switch-label{font-size:20px;line-height:31px;margin:0 5px}#fs_log_book table{font-family:Consolas,Monaco,monospace;font-size:12px}#fs_log_book table th{color:#ccc}#fs_log_book table tr{background:#232525}#fs_log_book table tr.alternate{background:#2b2b2b}#fs_log_book table tr td.fs-col--logger{color:#5a7435}#fs_log_book table tr td.fs-col--type{color:#ffc861}#fs_log_book table tr td.fs-col--function{color:#a7b7b1;font-weight:700}#fs_log_book table tr td.fs-col--message,#fs_log_book table tr td.fs-col--message a{color:#9a73ac!important}#fs_log_book table tr td.fs-col--file{color:#d07922}#fs_log_book table tr td.fs-col--timestamp{color:#6596be} \ No newline at end of file diff --git a/includes/class-fs-logger.php b/includes/class-fs-logger.php index bee0e76da..c102142da 100644 --- a/includes/class-fs-logger.php +++ b/includes/class-fs-logger.php @@ -709,6 +709,10 @@ private static function write_csv_to_filesystem( $file_path, $query_results ) { WP_Filesystem(); global $wp_filesystem; + if ( ! $wp_filesystem->is_writable( dirname( $file_path ) ) ) { + return false; + } + $content = ''; foreach ( $query_results as $row ) { diff --git a/templates/debug.php b/templates/debug.php index 9dc392b13..ef92373ea 100644 --- a/templates/debug.php +++ b/templates/debug.php @@ -26,12 +26,8 @@ $auto_off_timestamp = wp_next_scheduled( 'fs_debug_turn_off_logging_hook' ) * 1000; - function fs_get_debug_table_toggle_button( $open = false ) { - return ' - '; - } + $debug_table_toggle_button_template_vars = array( 'is_open' => true ); + $debug_table_toggle_button = fs_get_template( 'debug/partials/toggle-button.php', $debug_table_toggle_button_template_vars ); ?>

newest->version ?>

@@ -296,7 +292,7 @@ function stopCountdownManually() {

- +

@@ -334,7 +330,7 @@ function stopCountdownManually() { get_option( $module_type . 's' ), FS_Plugin::get_class_name() ) ?> 0 ) : ?>

- +

@@ -471,7 +467,7 @@ function stopCountdownManually() { ?> 0 ) : ?>

- + $plugin_addons ) : ?>

- +

@@ -651,7 +647,7 @@ function stopCountdownManually() { ?>

- +

@@ -703,7 +699,7 @@ function stopCountdownManually() { $licenses = $VARS[ $module_type . '_licenses' ] ?> 0 ) : ?>

- +

@@ -745,11 +741,7 @@ function stopCountdownManually() { 'h2', - 'toggle_button' => fs_get_debug_table_toggle_button(), - ); - + $page_params = array( 'title_tag' => 'h2' ); fs_require_template( 'debug/scheduled-crons.php', $page_params ); ?> @@ -938,9 +930,9 @@ class="dashicons dashicons-download"> + \ No newline at end of file diff --git a/templates/debug/scheduled-crons.php b/templates/debug/scheduled-crons.php index 4989bf3c6..d4c983fdb 100644 --- a/templates/debug/scheduled-crons.php +++ b/templates/debug/scheduled-crons.php @@ -79,7 +79,10 @@ $sec_text = fs_text_x_inline( 'sec', 'seconds' ); ?> - + false ); + $debug_table_toggle_button = fs_require_template( 'debug/partials/toggle-button.php', $debug_table_toggle_button_template_vars ); +?>