Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/daniele/enrich debug page #748

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions assets/scss/admin/debug.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 41 additions & 3 deletions includes/class-fs-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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;
}

Expand All @@ -691,5 +695,39 @@ 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;

if ( ! $wp_filesystem->is_writable( dirname( $file_path ) ) ) {
return false;
}

$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
}
70 changes: 63 additions & 7 deletions templates/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
$is_multisite = is_multisite();

$auto_off_timestamp = wp_next_scheduled( 'fs_debug_turn_off_logging_hook' ) * 1000;

$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 );
?>
<h1><?php echo fs_text_inline( 'Freemius Debug' ) . ' - ' . fs_text_inline( 'SDK' ) . ' v.' . $fs_active_plugins->newest->version ?></h1>
<div>
Expand Down Expand Up @@ -261,6 +264,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(),
),
)
?>
<br>
Expand All @@ -284,7 +291,10 @@ function stopCountdownManually() {
<?php endforeach ?>
</tbody>
</table>
<h2><?php fs_esc_html_echo_x_inline( 'SDK Versions', 'as software development kit versions', 'sdk-versions' ) ?></h2>
<h2>
<?php echo $debug_table_toggle_button ?>
<?php fs_esc_html_echo_x_inline( 'SDK Versions', 'as software development kit versions', 'sdk-versions' ) ?>
</h2>
<table id="fs_sdks" class="widefat">
<thead>
<tr>
Expand Down Expand Up @@ -319,7 +329,11 @@ function stopCountdownManually() {
<?php foreach ( $module_types as $module_type ) : ?>
<?php $modules = fs_get_entities( $fs_options->get_option( $module_type . 's' ), FS_Plugin::get_class_name() ) ?>
<?php if ( is_array( $modules ) && count( $modules ) > 0 ) : ?>
<h2><?php echo esc_html( ( WP_FS__MODULE_TYPE_PLUGIN == $module_type ) ? fs_text_inline( 'Plugins', 'plugins' ) : fs_text_inline( 'Themes', 'themes' ) ) ?></h2>
<h2>
<?php echo $debug_table_toggle_button ?>
<?php echo esc_html( ( WP_FS__MODULE_TYPE_PLUGIN == $module_type ) ? fs_text_inline( 'Plugins',
'plugins' ) : fs_text_inline( 'Themes', 'themes' ) ) ?>
</h2>
<table id="fs_<?php echo $module_type ?>" class="widefat">
<thead>
<tr>
Expand Down Expand Up @@ -452,11 +466,14 @@ function stopCountdownManually() {
$all_plans = false;
?>
<?php if ( is_array( $sites_map ) && count( $sites_map ) > 0 ) : ?>
<h2><?php echo esc_html( sprintf(
<h2>
<?php echo $debug_table_toggle_button ?>
<?php echo esc_html( sprintf(
/* translators: %s: 'plugin' or 'theme' */
fs_text_inline( '%s Installs', 'module-installs' ),
( WP_FS__MODULE_TYPE_PLUGIN === $module_type ? fs_text_inline( 'Plugin', 'plugin' ) : fs_text_inline( 'Theme', 'theme' ) )
) ) ?> / <?php fs_esc_html_echo_x_inline( 'Sites', 'like websites', 'sites' ) ?></h2>
) ) ?> / <?php fs_esc_html_echo_x_inline( 'Sites', 'like websites', 'sites' ) ?>
</h2>
<table id="fs_<?php echo $module_type ?>_installs" class="widefat">
<thead>
<tr>
Expand Down Expand Up @@ -567,7 +584,10 @@ function stopCountdownManually() {
$addons = $VARS['addons'];
?>
<?php foreach ( $addons as $plugin_id => $plugin_addons ) : ?>
<h2><?php echo esc_html( sprintf( fs_text_inline( 'Add Ons of module %s', 'addons-of-x' ), $plugin_id ) ) ?></h2>
<h2>
<?php echo $debug_table_toggle_button ?>
<?php echo esc_html( sprintf( fs_text_inline( 'Add Ons of module %s', 'addons-of-x' ), $plugin_id ) ) ?>
</h2>
<table id="fs_addons" class="widefat">
<thead>
<tr>
Expand Down Expand Up @@ -626,7 +646,10 @@ function stopCountdownManually() {

?>
<?php if ( is_array( $users ) && 0 < count( $users ) ) : ?>
<h2><?php fs_esc_html_echo_inline( 'Users' ) ?></h2>
<h2>
<?php echo $debug_table_toggle_button ?>
<?php fs_esc_html_echo_inline( 'Users' ) ?>
</h2>
<table id="fs_users" class="widefat">
<thead>
<tr>
Expand Down Expand Up @@ -675,7 +698,10 @@ function stopCountdownManually() {
*/
$licenses = $VARS[ $module_type . '_licenses' ] ?>
<?php if ( is_array( $licenses ) && count( $licenses ) > 0 ) : ?>
<h2><?php echo esc_html( sprintf( fs_text_inline( '%s Licenses', 'module-licenses' ), ( WP_FS__MODULE_TYPE_PLUGIN === $module_type ? fs_text_inline( 'Plugin', 'plugin' ) : fs_text_inline( 'Theme', 'theme' ) ) ) ) ?></h2>
<h2>
<?php echo $debug_table_toggle_button ?>
<?php echo esc_html( sprintf( fs_text_inline( '%s Licenses', 'module-licenses' ), ( WP_FS__MODULE_TYPE_PLUGIN === $module_type ? fs_text_inline( 'Plugin', 'plugin' ) : fs_text_inline( 'Theme', 'theme' ) ) ) ) ?>
</h2>
<table id="fs_<?php echo $module_type ?>_licenses" class="widefat">
<thead>
<tr>
Expand Down Expand Up @@ -714,6 +740,10 @@ function stopCountdownManually() {
</table>
<?php endif ?>
<?php endforeach ?>
<?php
$page_params = array( 'title_tag' => 'h2' );
fs_require_template( 'debug/scheduled-crons.php', $page_params );
DanieleAlessandra marked this conversation as resolved.
Show resolved Hide resolved
?>
<?php if ( FS_Logger::is_storage_logging_on() ) : ?>

<h2><?php fs_esc_html_echo_inline( 'Debug Log', 'debug-log' ) ?></h2>
Expand Down Expand Up @@ -888,3 +918,29 @@ class="dashicons dashicons-download"></i> <?php fs_esc_html_echo_inline( 'Downlo
});
</script>
<?php endif ?>
<script type="text/javascript">
// JavaScript to toggle the visibility of the table body and change the caret icon
jQuery( document ).ready( function ( $ ) {
$( '.fs-debug-table-toggle-button' ).each( function () {
const button = $( this );
const table = button.closest( 'h2' ).next( 'table' );
table.css( 'overflow', 'hidden' );

const toggle = function ( isExpanded ) {
button.attr( 'aria-expanded', isExpanded );
button.find( '.fs-debug-table-toggle-icon' ).text( isExpanded ? '▼' : '▶' );
table.css( {
display : isExpanded ? 'table' : 'block',
borderBottomWidth: isExpanded ? '1px' : '0',
maxHeight : isExpanded ? 'auto' : '0',
} );
};

button.on( 'click', function () {
toggle( button.attr( 'aria-expanded' ) === 'false' );
} );

toggle( button.attr( 'aria-expanded' ) === 'true' );
} );
} );
</script>
21 changes: 21 additions & 0 deletions templates/debug/partials/toggle-button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2015, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 2.10.1
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* @var array $VARS
* @var bool $is_open
*/
$is_open = $VARS['is_open'];
?>
<button class="fs-debug-table-toggle-button" aria-expanded="<?php echo $is_open ? 'true' : 'false' ?>">
<span class="fs-debug-table-toggle-icon"><?php echo $is_open ? '▼' : '▶' ?></span>
</button>
13 changes: 12 additions & 1 deletion templates/debug/scheduled-crons.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "</$title_tag>";
$toggle_button = isset( $VARS['toggle_button'] ) ? $VARS['toggle_button'] : '';

$module_types = array(
WP_FS__MODULE_TYPE_PLUGIN,
WP_FS__MODULE_TYPE_THEME
Expand Down Expand Up @@ -73,7 +78,13 @@

$sec_text = fs_text_x_inline( 'sec', 'seconds' );
?>
<h1><?php fs_esc_html_echo_inline( 'Scheduled Crons' ) ?></h1>
<?php echo $title_tag_open; ?>
<?php
$debug_table_toggle_button_template_vars = array( 'is_open' => false );
$debug_table_toggle_button = fs_require_template( 'debug/partials/toggle-button.php', $debug_table_toggle_button_template_vars );
?>
<?php fs_esc_html_echo_inline('Scheduled Crons') ?>
<?php echo $title_tag_close; ?>
<table class="widefat">
<thead>
<tr>
Expand Down
Loading