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