← All changes
|
freemius/includes/managers/class-fs-debug-manager.php
+508
-0
51.1.2
→
51.1.86
View file →
| @@ -1,0 +1,508 @@ | ||
| 1 | +<?php | |
| 2 | + /** | |
| 3 | + * @author Daniele Alessandra (@danielealessandra) | |
| 4 | + * @copyright Copyright (c) 2024, Freemius, Inc. | |
| 5 | + * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 | |
| 6 | + * @package Freemius | |
| 7 | + * @since 2.6.2 | |
| 8 | + */ | |
| 9 | + | |
| 10 | + class FS_DebugManager { | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * @author Vova Feldman (@svovaf) | |
| 14 | + * Moved from Freemius | |
| 15 | + * | |
| 16 | + * @since 1.0.8 | |
| 17 | + */ | |
| 18 | + static function _add_debug_section() { | |
| 19 | + if ( ! is_super_admin() ) { | |
| 20 | + // Add debug page only for super-admins. | |
| 21 | + return; | |
| 22 | + } | |
| 23 | + | |
| 24 | + Freemius::get_static_logger()->entrance(); | |
| 25 | + | |
| 26 | + $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION ); | |
| 27 | + | |
| 28 | + if ( WP_FS__DEV_MODE ) { | |
| 29 | + // Add top-level debug menu item. | |
| 30 | + $hook = FS_Admin_Menu_Manager::add_page( | |
| 31 | + $title, | |
| 32 | + $title, | |
| 33 | + 'manage_options', | |
| 34 | + 'freemius', | |
| 35 | + array( self::class, '_debug_page_render' ) | |
| 36 | + ); | |
| 37 | + } else { | |
| 38 | + // Add hidden debug page. | |
| 39 | + $hook = FS_Admin_Menu_Manager::add_subpage( | |
| 40 | + '', | |
| 41 | + $title, | |
| 42 | + $title, | |
| 43 | + 'manage_options', | |
| 44 | + 'freemius', | |
| 45 | + array( self::class, '_debug_page_render' ) | |
| 46 | + ); | |
| 47 | + } | |
| 48 | + | |
| 49 | + if ( ! empty( $hook ) ) { | |
| 50 | + add_action( "load-$hook", array( self::class, '_debug_page_actions' ) ); | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * @author Vova Feldman (@svovaf) | |
| 56 | + * Moved from Freemius | |
| 57 | + * | |
| 58 | + * @since 1.0.8 | |
| 59 | + */ | |
| 60 | + static function _debug_page_actions() { | |
| 61 | + Freemius::_clean_admin_content_section(); | |
| 62 | + | |
| 63 | + if ( fs_request_is_action( 'restart_freemius' ) ) { | |
| 64 | + check_admin_referer( 'restart_freemius' ); | |
| 65 | + | |
| 66 | + if ( ! is_multisite() ) { | |
| 67 | + // Clear accounts data. | |
| 68 | + Freemius::get_accounts()->clear( null, true ); | |
| 69 | + } else { | |
| 70 | + $sites = Freemius::get_sites(); | |
| 71 | + foreach ( $sites as $site ) { | |
| 72 | + $blog_id = Freemius::get_site_blog_id( $site ); | |
| 73 | + Freemius::get_accounts()->clear( $blog_id, true ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + // Clear network level storage. | |
| 77 | + Freemius::get_accounts()->clear( true, true ); | |
| 78 | + } | |
| 79 | + | |
| 80 | + // Clear SDK reference cache. | |
| 81 | + delete_option( 'fs_active_plugins' ); | |
| 82 | + } else if ( fs_request_is_action( 'clear_updates_data' ) ) { | |
| 83 | + check_admin_referer( 'clear_updates_data' ); | |
| 84 | + | |
| 85 | + if ( ! is_multisite() ) { | |
| 86 | + set_site_transient( 'update_plugins', null ); | |
| 87 | + set_site_transient( 'update_themes', null ); | |
| 88 | + } else { | |
| 89 | + $current_blog_id = get_current_blog_id(); | |
| 90 | + | |
| 91 | + $sites = Freemius::get_sites(); | |
| 92 | + foreach ( $sites as $site ) { | |
| 93 | + switch_to_blog( Freemius::get_site_blog_id( $site ) ); | |
| 94 | + | |
| 95 | + set_site_transient( 'update_plugins', null ); | |
| 96 | + set_site_transient( 'update_themes', null ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + switch_to_blog( $current_blog_id ); | |
| 100 | + } | |
| 101 | + } else if ( fs_request_is_action( 'reset_deactivation_snoozing' ) ) { | |
| 102 | + check_admin_referer( 'reset_deactivation_snoozing' ); | |
| 103 | + | |
| 104 | + Freemius::reset_deactivation_snoozing(); | |
| 105 | + } else if ( fs_request_is_action( 'simulate_trial' ) ) { | |
| 106 | + check_admin_referer( 'simulate_trial' ); | |
| 107 | + | |
| 108 | + $fs = freemius( fs_request_get( 'module_id' ) ); | |
| 109 | + | |
| 110 | + // Update SDK install to at least 24 hours before. | |
| 111 | + $fs->get_storage()->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC ); | |
| 112 | + // Unset the trial shown timestamp. | |
| 113 | + unset( $fs->get_storage()->trial_promotion_shown ); | |
| 114 | + } else if ( fs_request_is_action( 'simulate_network_upgrade' ) ) { | |
| 115 | + check_admin_referer( 'simulate_network_upgrade' ); | |
| 116 | + | |
| 117 | + $fs = freemius( fs_request_get( 'module_id' ) ); | |
| 118 | + | |
| 119 | + Freemius::set_network_upgrade_mode( $fs->get_storage() ); | |
| 120 | + } else if ( fs_request_is_action( 'delete_install' ) ) { | |
| 121 | + check_admin_referer( 'delete_install' ); | |
| 122 | + | |
| 123 | + Freemius::_delete_site_by_slug( | |
| 124 | + fs_request_get( 'slug' ), | |
| 125 | + fs_request_get( 'module_type' ), | |
| 126 | + true, | |
| 127 | + fs_request_get( 'blog_id', null ) | |
| 128 | + ); | |
| 129 | + } else if ( fs_request_is_action( 'delete_user' ) ) { | |
| 130 | + check_admin_referer( 'delete_user' ); | |
| 131 | + | |
| 132 | + self::delete_user( fs_request_get( 'user_id' ) ); | |
| 133 | + } else if ( fs_request_is_action( 'download_logs' ) ) { | |
| 134 | + check_admin_referer( 'download_logs' ); | |
| 135 | + | |
| 136 | + $download_url = FS_Logger::download_db_logs( | |
| 137 | + fs_request_get( 'filters', false, 'post' ) | |
| 138 | + ); | |
| 139 | + | |
| 140 | + if ( false === $download_url ) { | |
| 141 | + wp_die( 'Oops... there was an error while generating the logs download file. Please try again and if it doesn\'t work contact [email protected].' ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + fs_redirect( $download_url ); | |
| 145 | + } else if ( fs_request_is_action( 'migrate_options_to_network' ) ) { | |
| 146 | + check_admin_referer( 'migrate_options_to_network' ); | |
| 147 | + | |
| 148 | + Freemius::migrate_options_to_network(); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * @author Vova Feldman (@svovaf) | |
| 154 | + * Moved from Freemius | |
| 155 | + * | |
| 156 | + * @since 1.0.8 | |
| 157 | + */ | |
| 158 | + static function _debug_page_render() { | |
| 159 | + Freemius::get_static_logger()->entrance(); | |
| 160 | + | |
| 161 | + $all_modules_sites = self::get_all_modules_sites(); | |
| 162 | + | |
| 163 | + $licenses_by_module_type = self::get_all_licenses_by_module_type(); | |
| 164 | + | |
| 165 | + $vars = array( | |
| 166 | + 'plugin_sites' => $all_modules_sites[ WP_FS__MODULE_TYPE_PLUGIN ], | |
| 167 | + 'theme_sites' => $all_modules_sites[ WP_FS__MODULE_TYPE_THEME ], | |
| 168 | + 'users' => Freemius::get_all_users(), | |
| 169 | + 'addons' => Freemius::get_all_addons(), | |
| 170 | + 'account_addons' => Freemius::get_all_account_addons(), | |
| 171 | + 'plugin_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_PLUGIN ], | |
| 172 | + 'theme_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_THEME ], | |
| 173 | + ); | |
| 174 | + | |
| 175 | + fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' ); | |
| 176 | + fs_require_once_template( 'debug.php', $vars ); | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * @author Vova Feldman (@svovaf) | |
| 181 | + * Moved from Freemius | |
| 182 | + * | |
| 183 | + * @since 1.2.1.6 | |
| 184 | + */ | |
| 185 | + static function _get_debug_log() { | |
| 186 | + check_admin_referer( 'fs_get_debug_log' ); | |
| 187 | + | |
| 188 | + if ( ! is_super_admin() ) { | |
| 189 | + return; | |
| 190 | + } | |
| 191 | + | |
| 192 | + if (!FS_Logger::is_storage_logging_on()) { | |
| 193 | + return; | |
| 194 | + } | |
| 195 | + | |
| 196 | + $limit = min( ! empty( $_POST['limit'] ) ? absint( $_POST['limit'] ) : 200, 200 ); | |
| 197 | + $offset = min( ! empty( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 200, 200 ); | |
| 198 | + | |
| 199 | + $logs = FS_Logger::load_db_logs( | |
| 200 | + fs_request_get( 'filters', false, 'post' ), | |
| 201 | + $limit, | |
| 202 | + $offset | |
| 203 | + ); | |
| 204 | + | |
| 205 | + Freemius::shoot_ajax_success( $logs ); | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * @author Vova Feldman (@svovaf) | |
| 210 | + * Moved from Freemius | |
| 211 | + * | |
| 212 | + * @since 1.2.1.7 | |
| 213 | + */ | |
| 214 | + static function _get_db_option() { | |
| 215 | + check_admin_referer( 'fs_get_db_option' ); | |
| 216 | + | |
| 217 | + $option_name = fs_request_get( 'option_name' ); | |
| 218 | + | |
| 219 | + if ( ! is_super_admin() || | |
| 220 | + ! fs_starts_with( $option_name, 'fs_' ) | |
| 221 | + ) { | |
| 222 | + Freemius::shoot_ajax_failure(); | |
| 223 | + } | |
| 224 | + | |
| 225 | + $value = get_option( $option_name ); | |
| 226 | + | |
| 227 | + $result = array( | |
| 228 | + 'name' => $option_name, | |
| 229 | + ); | |
| 230 | + | |
| 231 | + if ( false !== $value ) { | |
| 232 | + if ( ! is_string( $value ) ) { | |
| 233 | + $value = json_encode( $value ); | |
| 234 | + } | |
| 235 | + | |
| 236 | + $result['value'] = $value; | |
| 237 | + } | |
| 238 | + | |
| 239 | + Freemius::shoot_ajax_success( $result ); | |
| 240 | + } | |
| 241 | + | |
| 242 | + /** | |
| 243 | + * @author Vova Feldman (@svovaf) | |
| 244 | + * Moved from Freemius | |
| 245 | + * | |
| 246 | + * @since 1.2.1.7 | |
| 247 | + */ | |
| 248 | + static function _set_db_option() { | |
| 249 | + check_admin_referer( 'fs_set_db_option' ); | |
| 250 | + | |
| 251 | + $option_name = fs_request_get( 'option_name' ); | |
| 252 | + | |
| 253 | + if ( ! is_super_admin() || | |
| 254 | + ! fs_starts_with( $option_name, 'fs_' ) | |
| 255 | + ) { | |
| 256 | + Freemius::shoot_ajax_failure(); | |
| 257 | + } | |
| 258 | + | |
| 259 | + $option_value = fs_request_get_raw( 'option_value' ); | |
| 260 | + | |
| 261 | + if ( ! empty( $option_value ) ) { | |
| 262 | + update_option( $option_name, $option_value ); | |
| 263 | + } | |
| 264 | + | |
| 265 | + Freemius::shoot_ajax_success(); | |
| 266 | + } | |
| 267 | + | |
| 268 | + /** | |
| 269 | + * @author Vova Feldman (@svovaf) | |
| 270 | + * Moved from Freemius | |
| 271 | + * | |
| 272 | + * @since 1.1.7.3 | |
| 273 | + */ | |
| 274 | + static function _toggle_debug_mode() { | |
| 275 | + check_admin_referer( 'fs_toggle_debug_mode' ); | |
| 276 | + | |
| 277 | + if ( ! is_super_admin() ) { | |
| 278 | + return; | |
| 279 | + } | |
| 280 | + | |
| 281 | + $is_on = fs_request_get( 'is_on', false, 'post' ); | |
| 282 | + | |
| 283 | + if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) { | |
| 284 | + if ( $is_on ) { | |
| 285 | + self::_turn_on_debug_mode(); | |
| 286 | + } else { | |
| 287 | + self::_turn_off_debug_mode(); | |
| 288 | + } | |
| 289 | + | |
| 290 | + // Logic to turn debugging off automatically | |
| 291 | + if ( 1 == $is_on ) { | |
| 292 | + // Plan a single event triggering after 24 hours to turn debugging off. | |
| 293 | + wp_schedule_single_event( time() + 24 * HOUR_IN_SECONDS, 'fs_debug_turn_off_logging_hook' ); | |
| 294 | + } else { | |
| 295 | + // Cancels any planned event when debugging is turned off manually. | |
| 296 | + $timestamp = wp_next_scheduled( 'fs_debug_turn_off_logging_hook' ); | |
| 297 | + if ( $timestamp ) { | |
| 298 | + wp_unschedule_event( $timestamp, 'fs_debug_turn_off_logging_hook' ); | |
| 299 | + } | |
| 300 | + } | |
| 301 | + } | |
| 302 | + | |
| 303 | + exit; | |
| 304 | + } | |
| 305 | + | |
| 306 | + /** | |
| 307 | + * @author Daniele Alessandra (@danielealessandra) | |
| 308 | + * @since 2.6.2 | |
| 309 | + * | |
| 310 | + */ | |
| 311 | + static function _turn_off_debug_mode() { | |
| 312 | + self::update_debug_mode_option( 0 ); | |
| 313 | + FS_Logger::_set_storage_logging( false ); | |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 317 | + * @author Daniele Alessandra (@danielealessandra) | |
| 318 | + * @since 2.6.2 | |
| 319 | + * | |
| 320 | + */ | |
| 321 | + static function _turn_on_debug_mode() { | |
| 322 | + self::update_debug_mode_option( 1 ); | |
| 323 | + FS_Logger::_set_storage_logging(); | |
| 324 | + } | |
| 325 | + | |
| 326 | + /** | |
| 327 | + * @author Leo Fajardo (@leorw) | |
| 328 | + * Moved from Freemius | |
| 329 | + * | |
| 330 | + * @param string $url | |
| 331 | + * @param array $request | |
| 332 | + * | |
| 333 | + * @since 2.1.0 | |
| 334 | + * | |
| 335 | + */ | |
| 336 | + public static function enrich_request_for_debug( &$url, &$request ) { | |
| 337 | + if ( WP_FS__DEBUG_SDK || isset( $_COOKIE['XDEBUG_SESSION'] ) ) { | |
| 338 | + $url = add_query_arg( 'XDEBUG_SESSION_START', rand( 0, 9999999 ), $url ); | |
| 339 | + $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url ); | |
| 340 | + | |
| 341 | + $request['cookies'] = array( | |
| 342 | + new WP_Http_Cookie( array( | |
| 343 | + 'name' => 'XDEBUG_SESSION', | |
| 344 | + 'value' => 'PHPSTORM', | |
| 345 | + ) ), | |
| 346 | + ); | |
| 347 | + } | |
| 348 | + } | |
| 349 | + | |
| 350 | + /** | |
| 351 | + * @author Leo Fajardo (@leorw) | |
| 352 | + * Moved from Freemius | |
| 353 | + * | |
| 354 | + * @return array | |
| 355 | + * | |
| 356 | + * @since 2.0.0 | |
| 357 | + * | |
| 358 | + */ | |
| 359 | + private static function get_all_licenses_by_module_type() { | |
| 360 | + $licenses = Freemius::get_account_option( 'all_licenses' ); | |
| 361 | + | |
| 362 | + $licenses_by_module_type = array( | |
| 363 | + WP_FS__MODULE_TYPE_PLUGIN => array(), | |
| 364 | + WP_FS__MODULE_TYPE_THEME => array(), | |
| 365 | + ); | |
| 366 | + | |
| 367 | + if ( ! is_array( $licenses ) ) { | |
| 368 | + return $licenses_by_module_type; | |
| 369 | + } | |
| 370 | + | |
| 371 | + foreach ( $licenses as $module_id => $module_licenses ) { | |
| 372 | + $fs = Freemius::get_instance_by_id( $module_id ); | |
| 373 | + if ( false === $fs ) { | |
| 374 | + continue; | |
| 375 | + } | |
| 376 | + | |
| 377 | + $licenses_by_module_type[ $fs->get_module_type() ] = array_merge( $licenses_by_module_type[ $fs->get_module_type() ], | |
| 378 | + $module_licenses ); | |
| 379 | + } | |
| 380 | + | |
| 381 | + return $licenses_by_module_type; | |
| 382 | + } | |
| 383 | + | |
| 384 | + /** | |
| 385 | + * Moved from the Freemius class. | |
| 386 | + * | |
| 387 | + * @author Leo Fajardo (@leorw) | |
| 388 | + * | |
| 389 | + * @return array | |
| 390 | + * | |
| 391 | + * @since 2.5.0 | |
| 392 | + */ | |
| 393 | + static function get_all_modules_sites() { | |
| 394 | + Freemius::get_static_logger()->entrance(); | |
| 395 | + | |
| 396 | + $sites_by_type = array( | |
| 397 | + WP_FS__MODULE_TYPE_PLUGIN => array(), | |
| 398 | + WP_FS__MODULE_TYPE_THEME => array(), | |
| 399 | + ); | |
| 400 | + | |
| 401 | + $module_types = array_keys( $sites_by_type ); | |
| 402 | + | |
| 403 | + if ( ! is_multisite() ) { | |
| 404 | + foreach ( $module_types as $type ) { | |
| 405 | + $sites_by_type[ $type ] = Freemius::get_all_sites( $type ); | |
| 406 | + | |
| 407 | + foreach ( $sites_by_type[ $type ] as $slug => $install ) { | |
| 408 | + $sites_by_type[ $type ][ $slug ] = array( $install ); | |
| 409 | + } | |
| 410 | + } | |
| 411 | + } else { | |
| 412 | + $sites = Freemius::get_sites(); | |
| 413 | + | |
| 414 | + foreach ( $sites as $site ) { | |
| 415 | + $blog_id = Freemius::get_site_blog_id( $site ); | |
| 416 | + | |
| 417 | + foreach ( $module_types as $type ) { | |
| 418 | + $installs = Freemius::get_all_sites( $type, $blog_id ); | |
| 419 | + | |
| 420 | + foreach ( $installs as $slug => $install ) { | |
| 421 | + if ( ! isset( $sites_by_type[ $type ][ $slug ] ) ) { | |
| 422 | + $sites_by_type[ $type ][ $slug ] = array(); | |
| 423 | + } | |
| 424 | + | |
| 425 | + $install->blog_id = $blog_id; | |
| 426 | + | |
| 427 | + $sites_by_type[ $type ][ $slug ][] = $install; | |
| 428 | + } | |
| 429 | + } | |
| 430 | + } | |
| 431 | + } | |
| 432 | + | |
| 433 | + return $sites_by_type; | |
| 434 | + } | |
| 435 | + | |
| 436 | + /** | |
| 437 | + * Delete user. | |
| 438 | + * | |
| 439 | + * @author Vova Feldman (@svovaf) | |
| 440 | + * | |
| 441 | + * @param number $user_id | |
| 442 | + * @param bool $store | |
| 443 | + * | |
| 444 | + * @return false|int The user ID if deleted. Otherwise, FALSE (when install not exist). | |
| 445 | + * @since 2.0.0 | |
| 446 | + * | |
| 447 | + */ | |
| 448 | + public static function delete_user( $user_id, $store = true ) { | |
| 449 | + $users = Freemius::get_all_users(); | |
| 450 | + | |
| 451 | + if ( ! is_array( $users ) || ! isset( $users[ $user_id ] ) ) { | |
| 452 | + return false; | |
| 453 | + } | |
| 454 | + | |
| 455 | + unset( $users[ $user_id ] ); | |
| 456 | + | |
| 457 | + self::$_accounts->set_option( 'users', $users, $store ); | |
| 458 | + | |
| 459 | + return $user_id; | |
| 460 | + } | |
| 461 | + | |
| 462 | + /** | |
| 463 | + * @author Daniele Alessandra (@danielealessandra) | |
| 464 | + * | |
| 465 | + * @return void | |
| 466 | + * @since 2.6.2 | |
| 467 | + * | |
| 468 | + */ | |
| 469 | + public static function load_required_static() { | |
| 470 | + if ( ! WP_FS__DEMO_MODE ) { | |
| 471 | + add_action( ( fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu', array( | |
| 472 | + self::class, | |
| 473 | + '_add_debug_section', | |
| 474 | + ) ); | |
| 475 | + } | |
| 476 | + | |
| 477 | + add_action( "wp_ajax_fs_toggle_debug_mode", array( self::class, '_toggle_debug_mode' ) ); | |
| 478 | + | |
| 479 | + Freemius::add_ajax_action_static( 'get_debug_log', array( self::class, '_get_debug_log' ) ); | |
| 480 | + Freemius::add_ajax_action_static( 'get_db_option', array( self::class, '_get_db_option' ) ); | |
| 481 | + Freemius::add_ajax_action_static( 'set_db_option', array( self::class, '_set_db_option' ) ); | |
| 482 | + } | |
| 483 | + | |
| 484 | + /** | |
| 485 | + * @author Daniele Alessandra (@danielealessandra) | |
| 486 | + * | |
| 487 | + * @return void | |
| 488 | + * | |
| 489 | + * @since 2.6.2 | |
| 490 | + */ | |
| 491 | + public static function register_hooks() { | |
| 492 | + add_action( 'fs_debug_turn_off_logging_hook', array( self::class, '_turn_off_debug_mode' ) ); | |
| 493 | + } | |
| 494 | + | |
| 495 | + /** | |
| 496 | + * @author Daniele Alessandra (@danielealessandra) | |
| 497 | + * | |
| 498 | + * @param int $is_on | |
| 499 | + * | |
| 500 | + * @return void | |
| 501 | + * | |
| 502 | + * @since 2.6.2 | |
| 503 | + */ | |
| 504 | + private static function update_debug_mode_option( $is_on ) { | |
| 505 | + update_option( 'fs_debug_mode', $is_on ); | |
| 506 | + } | |
| 507 | + | |
| 508 | + } | |