| 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 support@freemius.com.' ); |
| 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 |
} |
| 512 |
|