From 9449711842d472cab91811e33e35b8f58256106a Mon Sep 17 00:00:00 2001 From: Daniele Alessandra Date: Fri, 15 Mar 2024 18:37:39 +0100 Subject: [PATCH 1/2] =?UTF-8?q?[debug]=20[cron]=C2=A0Added=20Site=20URL,?= =?UTF-8?q?=20Added=20Cron=20Table,=20Separated=20tables=20in=20files,=20F?= =?UTF-8?q?ixed=20Log=20Download?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-fs-logger.php | 46 +- includes/debug/class-fs-debug-bar-panel.php | 4 - templates/debug.php | 858 +------------------- templates/debug/debug-action-buttons.php | 120 +++ templates/debug/debug-addons.php | 35 + templates/debug/debug-defined-variables.php | 52 ++ templates/debug/debug-licenses.php | 47 ++ templates/debug/debug-logger-switch.php | 31 + templates/debug/debug-logger.php | 177 ++++ templates/debug/debug-module-installs.php | 77 ++ templates/debug/debug-modules.php | 108 +++ templates/debug/debug-scheduled-crons.php | 89 ++ templates/debug/debug-sdk-versions.php | 44 + templates/debug/debug-users.php | 47 ++ templates/debug/logger.php | 66 -- templates/debug/scheduled-crons.php | 136 ---- templates/generic-table.php | 217 +++++ 17 files changed, 1131 insertions(+), 1023 deletions(-) create mode 100644 templates/debug/debug-action-buttons.php create mode 100644 templates/debug/debug-addons.php create mode 100644 templates/debug/debug-defined-variables.php create mode 100644 templates/debug/debug-licenses.php create mode 100644 templates/debug/debug-logger-switch.php create mode 100644 templates/debug/debug-logger.php create mode 100644 templates/debug/debug-module-installs.php create mode 100644 templates/debug/debug-modules.php create mode 100644 templates/debug/debug-scheduled-crons.php create mode 100644 templates/debug/debug-sdk-versions.php create mode 100644 templates/debug/debug-users.php delete mode 100644 templates/debug/logger.php delete mode 100644 templates/debug/scheduled-crons.php create mode 100644 templates/generic-table.php diff --git a/includes/class-fs-logger.php b/includes/class-fs-logger.php index 6dde8aa30..e4226a18f 100644 --- a/includes/class-fs-logger.php +++ b/includes/class-fs-logger.php @@ -649,8 +649,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 ) { @@ -662,22 +660,29 @@ 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; } /** * @author Vova Feldman (@svovaf) - * @since 1.2.1.6 * * @param string $filename * * @return string + * @since 1.2.1.6 + * */ public static function get_logs_download_url( $filename = '' ) { $upload_dir = wp_upload_dir(); @@ -689,4 +694,37 @@ public static function get_logs_download_url( $filename = '' ) { } #endregion + + /** + * @param $file_path + * @param $query_results + * + * @return bool + */ + public static function write_csv_to_filesystem( $file_path, $query_results ) { + require_once( ABSPATH . 'wp-admin/includes/file.php' ); + WP_Filesystem(); + global $wp_filesystem; + + if ( ! empty( $query_results ) ) { + $headers = array_keys( (array) $query_results[0] ); + } else { + return false; + } + + $content = implode( "\t", $headers ) . "\n"; + + 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; + } } diff --git a/includes/debug/class-fs-debug-bar-panel.php b/includes/debug/class-fs-debug-bar-panel.php index c3255612a..6bc0636c7 100644 --- a/includes/debug/class-fs-debug-bar-panel.php +++ b/includes/debug/class-fs-debug-bar-panel.php @@ -56,11 +56,7 @@ public function render() {

- -
-
-
-

newest->version ?>

-
- - - - -
-
-
- -
-

- - - - - - - - - - - get_option( 'ms_migration_complete', false, true ) ) : ?> - - - - - - - -
- -
- - - -
-
- -
- - -
-
- -
- - - -
-
- -
- - - -
-
- -
- - -
-
- -
- - - -
-
- - - - - - Resolve Clone(s) -
- - 'WP_FS__REMOTE_ADDR', - 'val' => WP_FS__REMOTE_ADDR, - ), - array( - 'key' => 'WP_FS__ADDRESS_PRODUCTION', - 'val' => WP_FS__ADDRESS_PRODUCTION, - ), - array( - 'key' => 'FS_API__ADDRESS', - 'val' => FS_API__ADDRESS, - ), - array( - 'key' => 'FS_API__SANDBOX_ADDRESS', - 'val' => FS_API__SANDBOX_ADDRESS, - ), - array( - 'key' => 'WP_FS__DIR', - 'val' => WP_FS__DIR, - ), - array( - 'key' => 'wp_using_ext_object_cache()', - 'val' => wp_using_ext_object_cache() ? 'true' : 'false', - ), - ) + /** + * @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 1.1.1 + */ + + if ( ! defined( 'ABSPATH' ) ) { + exit; + } + + global $fs_active_plugins; + + require_once 'generic-table.php'; + + $off_text = fs_text_x_inline( 'Off', 'as turned off' ); + $on_text = fs_text_x_inline( 'On', 'as turned on' ); + $page_title = fs_text_inline( 'Freemius Debug' ); + $sdk_text = fs_text_inline( 'SDK' ); + $version_text = 'v.' . $fs_active_plugins->newest->version; + + $has_any_active_clone = false; + + $common_params = array_merge( + $VARS, + array( + 'module_types' => array( + WP_FS__MODULE_TYPE_PLUGIN, + WP_FS__MODULE_TYPE_THEME, + ), + 'is_multisite' => is_multisite(), + 'fs_options' => FS_Options::instance( WP_FS__ACCOUNTS_OPTION_NAME, true ), + ) ); ?> -
- - - - - - - - - - > - - - - - - -
-

- - - - - - - - - - - plugins as $sdk_path => $data ) : ?> - version ) ?> - > - - - - - - - -
version ?>plugin_path ?>
- +

- - - get_option( $module_type . 's' ), FS_Plugin::get_class_name() ) ?> - 0 ) : ?> -

- - - - - - - - - - - - - - - - - - - - $data ) : ?> - file ); - } else { - $current_theme = wp_get_theme(); - $is_active = ( $current_theme->stylesheet === $data->file ); - - if ( ! $is_active && is_child_theme() ) { - $parent_theme = $current_theme->parent(); - - $is_active = ( ( $parent_theme instanceof WP_Theme ) && $parent_theme->stylesheet === $data->file ); - } - } - ?> - id ); - - $active_modules_by_id[ $data->id ] = true; - } - ?> - has_api_connectivity(); - - if ( true === $has_api_connectivity && $fs->is_on() ) { - echo ' style="background: #E6FFE6; font-weight: bold"'; - } else { - echo ' style="background: #ffd0d0; font-weight: bold"'; - } - } ?>> - - - - - > - is_on() ) { - echo ' style="color: red; text-transform: uppercase;"'; - } ?>>is_on() ? - $on_text : - $off_text - ); - } ?> - - - - get_network_install_blog_id(); - $network_user = $fs->get_network_user(); - } - ?> - - - - - - - -
id ?>version ?>title ?>file ?>public_key ?>email; - } ?> - - has_trial_plan() ) : ?> -
- - - - - -
- - is_registered() ) : ?> - - - is_network_upgrade_mode() ) : ?> -
- - - - - -
- - -
- - - - - 0 ) : ?> -

/

- - - - - - - - - - - - - - - - - - - - $sites ) : ?> - - blog_id : - null; - - if ( is_null( $site_url ) || $is_multisite ) { - $site_url = Freemius::get_unfiltered_site_url( - $blog_id, - true, - true - ); - } + fs_require_template( 'debug/debug-logger-switch.php' ); - $is_active_clone = ( $site->is_clone( $site_url ) && isset( $active_modules_by_id[ $site->plugin_id ] ) ); + fs_require_template( 'debug/debug-action-buttons.php' ); - if ( $is_active_clone ) { - $has_any_active_clone = true; - } - ?> - - - - - - - - - - - - - - - - - -
- id ?> - - - - url ) ?>user_id ?>license_id) ? $site->license_id : '' ?>plan_id ) ) { - if ( false === $all_plans ) { - $option_name = 'plans'; - if ( WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) { - $option_name = $module_type . '_' . $option_name; - } + fs_require_template( 'debug/debug-defined-variables.php' ); - $all_plans = fs_get_entities( $fs_options->get_option( $option_name, array() ), FS_Plugin_Plan::get_class_name() ); - } + fs_require_template( 'debug/debug-sdk-versions.php' ); - foreach ( $all_plans[ $slug ] as $plan ) { - $plan_id = Freemius::_decrypt( $plan->id ); + fs_require_template( 'debug/debug-modules.php', $common_params ); - if ( $site->plan_id == $plan_id ) { - $plan_name = Freemius::_decrypt( $plan->name ); - break; - } - } - } + fs_require_template( 'debug/debug-module-installs.php', $common_params ); - echo $plan_name; - ?>public_key ?>is_whitelabeled ? - FS_Plugin_License::mask_secret_key_for_html( $site->secret_key ) : - esc_html( $site->secret_key ); - ?> -
- - - - - - - - - -
-
- - - - $plugin_addons ) : ?> -

- - - - - - - - - - - - - - - - - - - - - - - -
id ?>title ?>slug ?>version ?>public_key ?>secret_key ) ?>
- -id ] = true; - } - } + fs_require_template( 'debug/debug-licenses.php', $common_params ); - foreach ( $module_types as $module_type ) { - /** - * @var FS_Plugin_License[] $licenses - */ - $licenses = $VARS[ $module_type . '_licenses' ]; + fs_require_template( 'debug/debug-scheduled-crons.php', $common_params ); - foreach ( $licenses as $license ) { - if ( $license->is_whitelabeled ) { - $users_with_developer_license_by_id[ $license->user_id ] = true; - } - } - } + fs_require_template( 'debug/debug-logger.php', $common_params ); ?> - -

- - - - - - - - - - - - - - $user ) : ?> - - - - - - - - - - - - -
id ?>get_name() ?> - - email ?> - - is_verified ) ?>public_key ?>secret_key) : esc_html( $user->secret_key ) ?> - -
- - - - -
- -
- - - - 0 ) : ?> -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
id ?>plugin_id ?>user_id ?>plan_id ?>is_unlimited() ? 'Unlimited' : ( $license->is_single_site() ? 'Single Site' : $license->quota ) ?>activated ?>is_block_features ? 'Blocking' : 'Flexible' ?>is_whitelabeled ? 'Whitelabeled' : 'Normal' ?>is_whitelabeled || ! isset( $user_ids_map[ $license->user_id ] ) ) ? - $license->get_html_escaped_masked_secret_key() : - esc_html( $license->secret_key ); - ?>expiration ?>
- - - - -

- -
- - - - - - - -
- - -
- - -
- -
-
-
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - -
#
{$log.log_order}.{$log.type}{$log.logger}{$log.function} - - {$log.message_short} - -
{$log.message}
-
{$log.file}:{$log.line}{$log.created}
-
- - diff --git a/templates/debug/debug-action-buttons.php b/templates/debug/debug-action-buttons.php new file mode 100644 index 000000000..1a8113ce2 --- /dev/null +++ b/templates/debug/debug-action-buttons.php @@ -0,0 +1,120 @@ +

+ + + + + + + + + + + get_option( 'ms_migration_complete', + false, + true ) ) : ?> + + + + + + + +
+ +
+ + + +
+
+ +
+ + +
+
+ +
+ + + +
+
+ +
+ + + +
+
+ +
+ + +
+
+ +
+ + + +
+
+ + + + + + Resolve Clone(s) +
+ diff --git a/templates/debug/debug-addons.php b/templates/debug/debug-addons.php new file mode 100644 index 000000000..bd29c7efd --- /dev/null +++ b/templates/debug/debug-addons.php @@ -0,0 +1,35 @@ + $plugin_addons ) { + $headers = [ + [ 'key' => 'id', 'val' => fs_esc_html_inline( 'ID', 'id' ) ], + [ 'key' => 'title', 'val' => fs_esc_html_inline( 'Title' ) ], + [ 'key' => 'slug', 'val' => fs_esc_html_inline( 'Slug' ) ], + [ 'key' => 'version', 'val' => fs_esc_html_x_inline( 'Version', 'product version' ) ], + [ 'key' => 'public_key', 'val' => fs_esc_html_inline( 'Public Key' ) ], + [ 'key' => 'secret_key', 'val' => fs_esc_html_inline( 'Secret Key' ) ], + ]; + + $data = []; + foreach ( $plugin_addons as $addon ) { + $data[] = [ + 'id' => $addon->id, + 'title' => esc_html( $addon->title ), + 'slug' => esc_html( $addon->slug ), + 'version' => esc_html( $addon->version ), + 'public_key' => esc_html( $addon->public_key ), + 'secret_key' => esc_html( $addon->secret_key ), + ]; + } + + $tableData = [ + 'attributes' => [ 'id' => 'fs_addons_' . esc_attr( $plugin_id ), 'class' => 'widefat' ], + 'headers' => $headers, + 'data' => $data, + ]; + + $title = '

' . esc_html( sprintf( fs_text_inline( 'Add Ons of module %s', 'addons-of-x' ), $plugin_id ) ) . '

'; + + fs_debug_generate_table( $tableData, array('title' => $title) ); + } diff --git a/templates/debug/debug-defined-variables.php b/templates/debug/debug-defined-variables.php new file mode 100644 index 000000000..b3c3e2cef --- /dev/null +++ b/templates/debug/debug-defined-variables.php @@ -0,0 +1,52 @@ + array( + array( + 'key' => 'key', + 'val' => fs_esc_html_inline( 'Key', 'key' ), + ), + array( + 'key' => 'val', + 'val' => fs_esc_html_inline( 'Value', 'value' ), + ), + ), + 'data' => array( + array( + 'key' => 'WP_FS__REMOTE_ADDR', + 'val' => WP_FS__REMOTE_ADDR, + ), + array( + 'key' => 'WP_FS__ADDRESS_PRODUCTION', + 'val' => WP_FS__ADDRESS_PRODUCTION, + ), + array( + 'key' => 'FS_API__ADDRESS', + 'val' => FS_API__ADDRESS, + ), + array( + 'key' => 'FS_API__SANDBOX_ADDRESS', + 'val' => FS_API__SANDBOX_ADDRESS, + ), + array( + 'key' => 'WP_FS__DIR', + 'val' => WP_FS__DIR, + ), + array( + '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(), + ), + ), + ), array('title' => $title) ); diff --git a/templates/debug/debug-licenses.php b/templates/debug/debug-licenses.php new file mode 100644 index 000000000..f77668eca --- /dev/null +++ b/templates/debug/debug-licenses.php @@ -0,0 +1,47 @@ + 0 ) { + $data = [ + 'attributes' => [ + 'id' => 'fs_' . $module_type . '_licenses', + 'class' => 'widefat', + ], + 'headers' => [ + [ 'key' => 'id', 'val' => fs_esc_html_inline( 'ID', 'id' ) ], + [ 'key' => 'plugin_id', 'val' => fs_esc_html_inline( 'Plugin ID' ) ], + [ 'key' => 'user_id', 'val' => fs_esc_html_inline( 'User ID' ) ], + [ 'key' => 'plan_id', 'val' => fs_esc_html_inline( 'Plan ID' ) ], + [ 'key' => 'quota', 'val' => fs_esc_html_inline( 'Quota' ) ], + [ 'key' => 'activated', 'val' => fs_esc_html_inline( 'Activated' ) ], + [ 'key' => 'blocking', 'val' => fs_esc_html_inline( 'Blocking' ) ], + [ 'key' => 'type', 'val' => fs_esc_html_inline( 'Type' ) ], + [ 'key' => 'license_key', 'val' => fs_esc_html_inline( 'License Key' ) ], + [ 'key' => 'expiration', 'val' => fs_esc_html_x_inline( 'Expiration', 'as expiration date' ) ], + ], + 'data' => [], + ]; + + foreach ( $licenses as $license ) { + $data['data'][] = [ + 'id' => $license->id, + 'plugin_id' => $license->plugin_id, + 'user_id' => $license->user_id, + 'plan_id' => $license->plan_id, + 'quota' => $license->is_unlimited() ? 'Unlimited' : ( $license->is_single_site() ? 'Single Site' : $license->quota ), + 'activated' => $license->activated, + 'blocking' => $license->is_block_features ? 'Blocking' : 'Flexible', + 'type' => $license->is_whitelabeled ? 'Whitelabeled' : 'Normal', + 'license_key' => ( $license->is_whitelabeled || ! isset( $user_ids_map[ $license->user_id ] ) ) ? $license->get_html_escaped_masked_secret_key() : esc_html( $license->secret_key ), + 'expiration' => $license->expiration, + ]; + } + + 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' ) ) ) ) . '

'; + + fs_debug_generate_table( $data ); + } + } \ No newline at end of file diff --git a/templates/debug/debug-logger-switch.php b/templates/debug/debug-logger-switch.php new file mode 100644 index 000000000..26f725fd8 --- /dev/null +++ b/templates/debug/debug-logger-switch.php @@ -0,0 +1,31 @@ +
+ + +
+
+
+ +
\ No newline at end of file diff --git a/templates/debug/debug-logger.php b/templates/debug/debug-logger.php new file mode 100644 index 000000000..85bde5fd1 --- /dev/null +++ b/templates/debug/debug-logger.php @@ -0,0 +1,177 @@ + + +

+ +
+ + + + + + + +
+ + +
+ + +
+ +
+
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + +
#
{$log.log_order}.{$log.type}{$log.logger}{$log.function} + + {$log.message_short} + +
{$log.message}
+
{$log.file}:{$log.line}{$log.created}
+
+ + \ No newline at end of file diff --git a/templates/debug/debug-module-installs.php b/templates/debug/debug-module-installs.php new file mode 100644 index 000000000..f37a76c1b --- /dev/null +++ b/templates/debug/debug-module-installs.php @@ -0,0 +1,77 @@ + 0 ) { + $data['attributes'] = [ + 'id' => 'fs_' . $module_type . '_installs', + 'class' => 'widefat', + ]; + + $data['headers'] = [ + [ 'key' => 'id', 'val' => 'ID' ], + $VARS['is_multisite'] ? [ 'key' => 'blog_id', 'val' => 'Blog ID' ] : null, + $VARS['is_multisite'] ? [ 'key' => 'address', 'val' => 'Address' ] : null, + [ 'key' => 'slug', 'val' => 'Slug' ], + [ 'key' => 'user_id', 'val' => 'User ID' ], + [ 'key' => 'license_id', 'val' => 'License ID' ], + [ 'key' => 'plan', 'val' => 'Plan' ], + [ 'key' => 'public_key', 'val' => 'Public Key' ], + [ 'key' => 'secret_key', 'val' => 'Secret Key' ], + [ 'key' => 'actions', 'val' => 'Actions' ], + ]; + + $data['headers'] = array_filter( $data['headers'] ); + + $data['data'] = []; + + foreach ( $sites_map as $slug => $sites ) { + foreach ( $sites as $site ) { + $row = [ + 'id' => $site->id, + 'slug' => $slug, + 'user_id' => $site->user_id, + 'license_id' => ! empty( $site->license_id ) ? $site->license_id : '', + 'plan' => fs_debug_get_plan_name( $site, + $slug, + $module_type, + $all_plans, + $VARS['fs_options'] ), + 'public_key' => $site->public_key, + 'secret_key' => fs_debug_get_secret_key( $site, $module_type, $slug ), + 'actions' => array( + array( + 'fs_action' => 'delete_install', + 'module_id' => $site->plugin_id, + 'blog_id' => $VARS['is_multisite'] ? $site->blog_id : null, + 'module_type' => $module_type, + 'slug' => $slug, + 'label' => fs_esc_html_x_inline( 'Delete', 'verb', 'delete' ), + 'classes' => array( 'button' ), + ), + ), + ]; + + if ( $VARS['is_multisite'] ) { + $row['blog_id'] = $site->blog_id; + $row['address'] = $site->url; // Assumi che questo sia l'indirizzo. Potresti voler applicare fs_strip_url_protocol() + } + + $data['data'][] = $row; + } + } + + $title = esc_html( sprintf( fs_text_inline( '%s Installs', 'module-installs' ), + ( WP_FS__MODULE_TYPE_PLUGIN === $module_type ? fs_text_inline( 'Plugin', + 'plugin' ) : fs_text_inline( 'Theme', 'theme' ) ) ) ); + $title .= ' / '; + $title .= fs_esc_html_x_inline( 'Sites', 'like websites', 'sites' ); + + echo fs_debug_generate_table( $data, array( 'title' => $title ) ); + } + } \ No newline at end of file diff --git a/templates/debug/debug-modules.php b/templates/debug/debug-modules.php new file mode 100644 index 000000000..71de2d6e8 --- /dev/null +++ b/templates/debug/debug-modules.php @@ -0,0 +1,108 @@ +get_option( $module_type . 's' ), + FS_Plugin::get_class_name() ); + + $current_theme = wp_get_theme(); + + if ( is_array( $modules ) && count( $modules ) > 0 ) { + $data = [ + 'attributes' => [ + 'id' => 'fs_' . $module_type, + 'class' => 'widefat', + ], + 'headers' => [ + [ 'key' => 'id', 'val' => 'ID' ], + [ 'key' => 'slug', 'val' => 'Slug' ], + [ 'key' => 'version', 'val' => 'Version' ], + [ 'key' => 'title', 'val' => 'Title' ], + [ 'key' => 'api', 'val' => 'API' ], + [ 'key' => 'freemius_state', 'val' => 'Freemius State' ], + [ 'key' => 'module_path', 'val' => 'Module Path' ], + [ 'key' => 'public_key', 'val' => 'Public Key' ], + $VARS['is_multisite'] ? [ 'key' => 'network_blog', 'val' => 'Network Blog' ] : null, + $VARS['is_multisite'] ? [ 'key' => 'network_user', 'val' => 'Network User' ] : null, + [ 'key' => 'actions', 'val' => 'Actions' ], + ], + 'data' => [], + ]; + + $data['headers'] = array_filter( $data['headers'] ); + + foreach ( $modules as $slug => $module_data ) { + // Determine if the module is active based on the module type and current theme. + $is_active = ( WP_FS__MODULE_TYPE_THEME !== $module_type && is_plugin_active( $module_data->file ) ) || + ( $current_theme->stylesheet === $module_data->file ) || + ( is_child_theme() && ( $current_theme->parent() instanceof WP_Theme ) && $current_theme->parent()->stylesheet === $module_data->file ); + + $freemius_status = fs_text_x_inline( 'Off', 'as turned off' ); + + $row_style = 'default'; + $api_connectivity_status = fs_text_x_inline( 'Unknown', + 'API connectivity state is unknown' ); // Default status + + if ( $is_active ) { + $fs = freemius( $module_data->id ); + $active_modules_by_id[ $module_data->id ] = true; // Track active modules + + $has_api_connectivity = $fs->has_api_connectivity(); + + if ( $has_api_connectivity === true ) { + if ( $fs->is_on() ) { + $row_style = 'success'; + $api_connectivity_status = fs_text_x_inline( 'Connected', 'as connection was successful' ); + $freemius_status = fs_text_x_inline( 'On', 'as turned on' ); + } + } else if ( $has_api_connectivity === false ) { + $row_style = 'error'; + $api_connectivity_status = fs_text_x_inline( 'Blocked', 'as connection blocked' ); + } + } + + // Create the row data, setting 'row_style' based on 'is_active' status + $row = [ + 'id' => esc_html( $module_data->id ), + 'slug' => esc_html( $slug ), + 'version' => esc_html( $module_data->version ), + 'title' => esc_html( $module_data->title ), + 'api' => esc_html( $api_connectivity_status ), + 'freemius_state' => $freemius_status, + 'module_path' => $module_data->file, + 'public_key' => $module_data->public_key, + 'actions' => array(), + 'row_style' => $row_style, + ]; + + if ( $is_active ) { + if ( $fs->has_trial_plan() ) { + $row['actions'][] = array( + 'fs_action' => 'simulate_trial', + 'module_id' => $fs->get_id(), + 'label' => fs_esc_html_inline( 'Simulate Trial Promotion' ), + 'classes' => array( 'button', 'button-primary', 'simulate-trial' ), + ); + } + + if ( $fs->is_registered() ) { + $row['actions'][] = array( + 'href' => $fs->get_account_url(), + 'label' => fs_esc_html_inline( 'Account', 'account' ), + 'classes' => array( 'button' ), + ); + } + + if ( fs_is_network_admin() && ! $fs->is_network_upgrade_mode() ) { + $row['actions'][] = array( + 'fs_action' => 'simulate_network_upgrade', + 'module_id' => $fs->get_id(), + 'label' => fs_esc_html_inline( 'Simulate Network Upgrade' ), + ); + } + } + $data['data'][] = $row; + } + + $title = esc_html( ( WP_FS__MODULE_TYPE_PLUGIN == $module_type ) ? fs_text_inline( 'Plugins', 'plugins' ) : fs_text_inline( 'Themes', 'themes' ) ); + echo fs_debug_generate_table( $data, array( 'title' => $title ) ); + } + } diff --git a/templates/debug/debug-scheduled-crons.php b/templates/debug/debug-scheduled-crons.php new file mode 100644 index 000000000..aecbf7263 --- /dev/null +++ b/templates/debug/debug-scheduled-crons.php @@ -0,0 +1,89 @@ +get_option( $module_type . 's' ), FS_Plugin::get_class_name() ); + if ( is_array( $modules ) && count( $modules ) > 0 ) { + foreach ( $modules as $slug => $data ) { + if ( WP_FS__MODULE_TYPE_THEME === $module_type ) { + $current_theme = wp_get_theme(); + $is_active = ( $current_theme->stylesheet === $data->file ); + } else { + $is_active = is_plugin_active( $data->file ); + } + + /** + * @author Vova Feldman + * + * @since 1.2.1 Don't load data from inactive modules. + */ + if ( $is_active ) { + $fs = freemius( $data->id ); + + $next_execution = $fs->next_sync_cron(); + $last_execution = $fs->last_sync_cron(); + + if ( false !== $next_execution ) { + $scheduled_crons[ $slug ][] = array( + 'name' => $fs->get_plugin_name(), + 'slug' => $slug, + 'module_type' => $fs->get_module_type(), + 'type' => 'sync_cron', + 'last' => $last_execution, + 'next' => $next_execution, + ); + } + + $next_install_execution = $fs->next_install_sync(); + $last_install_execution = $fs->last_install_sync(); + + if ( false !== $next_install_execution || + false !== $last_install_execution + ) { + $scheduled_crons[ $slug ][] = array( + 'name' => $fs->get_plugin_name(), + 'slug' => $slug, + 'module_type' => $fs->get_module_type(), + 'type' => 'install_sync', + 'last' => $last_install_execution, + 'next' => $next_install_execution, + ); + } + } + } + } + } + + $data = [ + 'attributes' => [ + 'id' => 'active-modules-table', + 'class' => 'wp-list-table widefat fixed striped', + ], + 'headers' => [ + [ 'key' => 'name', 'val' => 'Name' ], + [ 'key' => 'slug', 'val' => 'Slug' ], + [ 'key' => 'module_type', 'val' => 'Module Type' ], + [ 'key' => 'type', 'val' => 'Sync Type' ], + [ 'key' => 'last', 'val' => 'Last Execution' ], + [ 'key' => 'next', 'val' => 'Next Execution' ], + ], + 'data' => [], + ]; + + foreach ( $scheduled_crons as $slug => $crons ) { + foreach ( $crons as $cron ) { + $data['data'][] = [ + 'name' => $cron['name'], + 'slug' => $cron['slug'], + 'module_type' => $cron['module_type'], + 'type' => $cron['type'], + 'last' => fs_debug_format_time( $cron['last'] ), + 'next' => fs_debug_format_time( $cron['next'] ), + ]; + } + } + + echo fs_debug_generate_table( $data, array( 'title' => $title, 'hidden' => true ) ); \ No newline at end of file diff --git a/templates/debug/debug-sdk-versions.php b/templates/debug/debug-sdk-versions.php new file mode 100644 index 000000000..2d21ff2c1 --- /dev/null +++ b/templates/debug/debug-sdk-versions.php @@ -0,0 +1,44 @@ + array( + 'id' => 'fs_sdk', + ), + 'headers' => array( + array( + 'key' => 'version', + 'val' => fs_esc_html_x_inline( 'Version', 'product version' ), + ), + array( + 'key' => 'sdk_path', + 'val' => fs_esc_html_inline( 'SDK Path' ), + ), + array( + 'key' => 'module_path', + 'val' => fs_esc_html_inline( 'Module Path' ), + ), + array( + 'key' => 'is_active', + 'val' => fs_esc_html_inline( 'Is Active' ), + ), + ), + 'data' => array_map( function ( $item ) { + $is_active = ( WP_FS__SDK_VERSION == $item->version ); + + return array( + 'version' => $item->version, + 'sdk_path' => '2', + 'module_path' => $item->plugin_path, + 'is_active' => ( $is_active ) ? 'Active' : 'Inactive', + 'row_style' => ( $is_active ) ? 'success' : 'default', + ); + }, $fs_active_plugins->plugins ), + + ), array( + 'title' => $title, + ) + ); diff --git a/templates/debug/debug-users.php b/templates/debug/debug-users.php new file mode 100644 index 000000000..55c351015 --- /dev/null +++ b/templates/debug/debug-users.php @@ -0,0 +1,47 @@ + 'id', 'val' => fs_esc_html_inline( 'ID', 'id' ) ], + [ 'key' => 'name', 'val' => fs_esc_html_inline( 'Name' ) ], + [ 'key' => 'email', 'val' => fs_esc_html_inline( 'Email' ) ], + [ 'key' => 'verified', 'val' => fs_esc_html_inline( 'Verified' ) ], + [ 'key' => 'public_key', 'val' => fs_esc_html_inline( 'Public Key' ) ], + [ 'key' => 'secret_key', 'val' => fs_esc_html_inline( 'Secret Key' ) ], + [ 'key' => 'actions', 'val' => fs_esc_html_inline( 'Actions' ) ], + ]; + + $data = []; + foreach ( $users as $user_id => $user ) { + $has_developer_license = isset( $users_with_developer_license_by_id[ $user->id ] ); + + $row = [ + 'id' => $user->id, + 'name' => $has_developer_license ? '' : esc_html( $user->get_name() ), + 'email' => $has_developer_license ? '' : '' . esc_html( $user->email ) . '', + 'verified' => $has_developer_license ? '' : esc_html( json_encode( $user->is_verified ) ), + 'public_key' => esc_html( $user->public_key ), + 'secret_key' => $has_developer_license ? FS_Plugin_License::mask_secret_key_for_html( $user->secret_key ) : esc_html( $user->secret_key ), + 'actions' => array(), + ]; + + if ( $has_developer_license ) { + $row['actions'][] = array( + 'fs_action' => 'delete_user', + 'user_id' => esc_attr( $user->id ), + 'classes' => array( 'button' ), + 'label' => fs_esc_html_x_inline( 'Delete', 'verb', 'delete' ), + ); + } + + $data[] = $row; + } + + $tableData = [ + 'attributes' => [ 'id' => 'fs_users', 'class' => 'widefat' ], + 'headers' => $headers, + 'data' => $data, + ]; + + echo fs_debug_generate_table( $tableData, array('title' => fs_esc_html_inline( 'Users' )) ); + diff --git a/templates/debug/logger.php b/templates/debug/logger.php deleted file mode 100644 index 852497a19..000000000 --- a/templates/debug/logger.php +++ /dev/null @@ -1,66 +0,0 @@ - -

- - - - - - - - - - - - - - - - - - > - - - - - - - - - - -
#
.get_id() ?> - %s', - esc_html( substr( $log['msg'], 0, 32 ) ) . ( 32 < strlen( $log['msg'] ) ? '...' : '' ) - ); - ?> -
- -
-
get_file() ) . ':' . $log['line']; - } - ?>
\ No newline at end of file diff --git a/templates/debug/scheduled-crons.php b/templates/debug/scheduled-crons.php deleted file mode 100644 index 47a715ea4..000000000 --- a/templates/debug/scheduled-crons.php +++ /dev/null @@ -1,136 +0,0 @@ -get_option( $module_type . 's' ), FS_Plugin::get_class_name() ); - if ( is_array( $modules ) && count( $modules ) > 0 ) { - foreach ( $modules as $slug => $data ) { - if ( WP_FS__MODULE_TYPE_THEME === $module_type ) { - $current_theme = wp_get_theme(); - $is_active = ( $current_theme->stylesheet === $data->file ); - } else { - $is_active = is_plugin_active( $data->file ); - } - - /** - * @author Vova Feldman - * - * @since 1.2.1 Don't load data from inactive modules. - */ - if ( $is_active ) { - $fs = freemius( $data->id ); - - $next_execution = $fs->next_sync_cron(); - $last_execution = $fs->last_sync_cron(); - - if ( false !== $next_execution ) { - $scheduled_crons[ $slug ][] = array( - 'name' => $fs->get_plugin_name(), - 'slug' => $slug, - 'module_type' => $fs->get_module_type(), - 'type' => 'sync_cron', - 'last' => $last_execution, - 'next' => $next_execution, - ); - } - - $next_install_execution = $fs->next_install_sync(); - $last_install_execution = $fs->last_install_sync(); - - if (false !== $next_install_execution || - false !== $last_install_execution - ) { - $scheduled_crons[ $slug ][] = array( - 'name' => $fs->get_plugin_name(), - 'slug' => $slug, - 'module_type' => $fs->get_module_type(), - 'type' => 'install_sync', - 'last' => $last_install_execution, - 'next' => $next_install_execution, - ); - } - } - } - } - } - - $sec_text = fs_text_x_inline( 'sec', 'seconds' ); -?> -

- - - - - - - - - - - - - - $crons ) : ?> - - - - - - - - - - - - -
diff --git a/templates/generic-table.php b/templates/generic-table.php new file mode 100644 index 000000000..b824b49db --- /dev/null +++ b/templates/generic-table.php @@ -0,0 +1,217 @@ + [ '#ffffff', '#f6f7f7' ], + 'success' => [ '#dfffdf', '#b3ffb3' ], + 'info' => [ '#d9d9ff', '#b3b3ff' ], + 'warning' => [ '#fff5cc', '#ffeb99' ], + 'error' => [ '#ffcccc', '#ff9999' ], + ]; + + $output = '
'; + + $output .= '

'; + + $output .= '' . ($options['hidden'] ? '▶' : '▼') . ' '; + + if (isset($options['title'])) { + $output .= esc_html($options['title']); + } + + $output .= '

'; + + $output .= ' $value ) { + $output .= ' ' . $key . '="' . $value . '"'; + } + } + + $output .= '>'; + + if ( isset( $data['headers'] ) ) { + $output .= ''; + + foreach ( $data['headers'] as $header ) { + $output .= ''; + } + $output .= ''; + } + + if ( isset( $data['data'] ) ) { + $output .= ''; + $row_count = 0; + + foreach ( $data['data'] as $row ) { + $style_name = isset( $row['row_style'] ) ? $row['row_style'] : 'default'; + $style = isset( $styles[ $style_name ] ) ? $styles[ $style_name ] : $styles['default']; + $row_bg_color = $style[ $row_count % 2 ]; + $output .= ''; + if ( isset( $data['headers'] ) ) { + foreach ( $data['headers'] as $header ) { + if ( isset( $row[ $header['key'] ] ) ) { + $output .= ''; + } + } + } else { + foreach ( $row as $value ) { + $output .= ''; + } + } + $output .= ''; + $row_count ++; + } + $output .= ''; + } + $output .= '
' . $header['val'] . '
' . fs_debug_render_cell( $row[ $header['key'] ] ) . '' . fs_debug_render_cell( $value ) . '
'; + + $output .= ""; + + + $output .= '
'; + + return $output; + } + } + + if ( ! function_exists( 'fs_debug_render_link' ) ) { + function fs_debug_render_link( $element ) { + $classes = ''; + if ( is_array( $element['classes'] ) ) { + $classes = join( ' ', $element['classes'] ); + } + $output = ''; + $output .= $element['label']; + $output .= ''; + + return $output; + } + } + + if ( ! function_exists( 'fs_debug_render_button' ) ) { + function fs_debug_render_button( $element ) { + $output = '
'; + + // Sanitize the 'fs_action' attribute before embedding it in HTML + $fs_action_sanitized = esc_attr( $element['fs_action'] ); + $output .= ''; + + // Generating the nonce, which is already sanitized by WordPress through wp_nonce_field + $output .= wp_nonce_field( $fs_action_sanitized, 'wpnonce', true, false ); + + foreach ( $element as $key => $value ) { + if ( in_array( $key, array( 'fs_action', 'label', 'classes' ) ) || is_null( $value ) ) { + continue; + } + // Sanitize attribute names and values before embedding them in HTML + $key_sanitized = esc_attr( $key ); + $value_sanitized = esc_attr( $value ); + $output .= ''; + } + + // Sanitize the button content (label) before embedding it in HTML + $label_sanitized = esc_html( $element['label'] ); + $classes = ''; + if ( is_array( $element['classes'] ) ) { + $classes = join( ' ', $element['classes'] ); + } + + $output .= ''; + $output .= '
'; + + return $output; + } + } + + if ( ! function_exists( 'fs_debug_render_cell' ) ) { + function fs_debug_render_cell( $raw_value ) { + $output = ''; + if ( is_array( $raw_value ) ) { + foreach ( $raw_value as $element ) { + if ( isset( $element['fs_action'] ) ) { + $output .= fs_debug_render_button( $element ); + } else if ( isset( $element['href'] ) ) { + $output .= fs_debug_render_link( $element ); + } + } + } else { + $output = $raw_value; + } + + return $output; + } + } + + if ( ! function_exists( 'fs_debug_format_time' ) ) { + /** + * @param $time + * + * @return string + */ + function fs_debug_format_time( $time ) { + if ( is_numeric( $time ) ) { + $sec_text = fs_text_x_inline( 'sec', 'seconds' ); + $in_x_text = fs_text_inline( 'In %s', 'in-x' ); + $x_ago_text = fs_text_inline( '%s ago', 'x-ago' ); + $diff = abs( WP_FS__SCRIPT_START_TIME - $time ); + $human_diff = ( $diff < MINUTE_IN_SECONDS ) ? + $diff . ' ' . $sec_text : + human_time_diff( WP_FS__SCRIPT_START_TIME, $time ); + + return esc_html( sprintf( + ( ( WP_FS__SCRIPT_START_TIME < $time ) ? + $in_x_text : + $x_ago_text ), + $human_diff + ) ); + } + + return ''; + } + } + + if ( ! function_exists( 'fs_debug_get_plan_name' ) ) { + function fs_debug_get_plan_name( $site, $slug, $module_type, $all_plans, $fs_options ) { + $plan_name = ''; + if ( FS_Plugin_Plan::is_valid_id( $site->plan_id ) ) { + if ( false === $all_plans ) { + $option_name = 'plans'; + if ( WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) { + $option_name = $module_type . '_' . $option_name; + } + + $all_plans = fs_get_entities( $fs_options->get_option( $option_name, array() ), + FS_Plugin_Plan::get_class_name() ); + } + + foreach ( $all_plans[ $slug ] as $plan ) { + $plan_id = Freemius::_decrypt( $plan->id ); + + if ( $site->plan_id == $plan_id ) { + $plan_name = Freemius::_decrypt( $plan->name ); + break; + } + } + } + + return $plan_name; + } + } + + if ( ! function_exists( 'fs_debug_get_secret_key' ) ) { + function fs_debug_get_secret_key( $site, $module_type, $slug ) { + $plugin_storage = FS_Storage::instance( $module_type, $slug ); + + return $plugin_storage->is_whitelabeled ? + FS_Plugin_License::mask_secret_key_for_html( $site->secret_key ) : + esc_html( $site->secret_key ); + } + } From 1d52af2564d5ed74bd57a4cbbc961378ecb03e8c Mon Sep 17 00:00:00 2001 From: Daniele Alessandra Date: Fri, 22 Mar 2024 19:02:34 +0100 Subject: [PATCH 2/2] [debug] Added Add-ons table --- templates/debug/debug-addons.php | 4 ++-- templates/debug/debug-licenses.php | 2 +- templates/generic-table.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/debug/debug-addons.php b/templates/debug/debug-addons.php index bd29c7efd..8fe2cdb68 100644 --- a/templates/debug/debug-addons.php +++ b/templates/debug/debug-addons.php @@ -29,7 +29,7 @@ 'data' => $data, ]; - $title = '

' . esc_html( sprintf( fs_text_inline( 'Add Ons of module %s', 'addons-of-x' ), $plugin_id ) ) . '

'; + $title = esc_html( sprintf( fs_text_inline( 'Add Ons of module %s', 'addons-of-x' ), $plugin_id ) ); - fs_debug_generate_table( $tableData, array('title' => $title) ); + echo fs_debug_generate_table( $tableData, array('title' => $title) ); } diff --git a/templates/debug/debug-licenses.php b/templates/debug/debug-licenses.php index f77668eca..094ef36de 100644 --- a/templates/debug/debug-licenses.php +++ b/templates/debug/debug-licenses.php @@ -42,6 +42,6 @@ ( WP_FS__MODULE_TYPE_PLUGIN === $module_type ? fs_text_inline( 'Plugin', 'plugin' ) : fs_text_inline( 'Theme', 'theme' ) ) ) ) . ''; - fs_debug_generate_table( $data ); + echo fs_debug_generate_table( $data ); } } \ No newline at end of file diff --git a/templates/generic-table.php b/templates/generic-table.php index b824b49db..db82e6720 100644 --- a/templates/generic-table.php +++ b/templates/generic-table.php @@ -105,7 +105,7 @@ function fs_debug_render_button( $element ) { $output .= ''; // Generating the nonce, which is already sanitized by WordPress through wp_nonce_field - $output .= wp_nonce_field( $fs_action_sanitized, 'wpnonce', true, false ); + $output .= wp_nonce_field( $fs_action_sanitized, '_wpnonce', true, false ); foreach ( $element as $key => $value ) { if ( in_array( $key, array( 'fs_action', 'label', 'classes' ) ) || is_null( $value ) ) {