register_ajax_action( 'enable_safe_mode', [ $this, 'ajax_enable_safe_mode' ] ); $ajax->register_ajax_action( 'disable_safe_mode', [ $this, 'disable_safe_mode' ] ); } /** * @param Tools $tools_page */ public function add_admin_button( $tools_page ) { $tools_page->add_fields( Settings::TAB_GENERAL, 'tools', [ 'safe_mode' => [ 'label' => esc_html__( 'Safe Mode', 'elementor' ), 'field_args' => [ 'type' => 'select', 'std' => $this->is_enabled(), 'options' => [ '' => esc_html__( 'Disable', 'elementor' ), 'global' => esc_html__( 'Enable', 'elementor' ), ], 'desc' => esc_html__( 'Safe Mode allows you to troubleshoot issues by only loading the editor, without loading the theme or any other plugin.', 'elementor' ), ], ], ] ); } public function on_update_safe_mode( $value ) { if ( 'yes' === $value || 'global' === $value ) { $this->enable_safe_mode(); } else { $this->disable_safe_mode(); } return $value; } public function ajax_enable_safe_mode( $data ) { // It will run `$this->>update_safe_mode`. update_option( 'elementor_safe_mode', 'yes' ); $document = Plugin::$instance->documents->get( $data['editor_post_id'] ); if ( $document ) { return add_query_arg( 'elementor-mode', 'safe', $document->get_edit_url() ); } return false; } public function enable_safe_mode() { if ( ! current_user_can( 'install_plugins' ) ) { return; } WP_Filesystem(); $this->update_allowed_plugins(); if ( ! is_dir( WPMU_PLUGIN_DIR ) ) { wp_mkdir_p( WPMU_PLUGIN_DIR ); add_option( 'elementor_safe_mode_created_mu_dir', true ); } if ( ! is_dir( WPMU_PLUGIN_DIR ) ) { wp_die( esc_html__( 'Cannot enable Safe Mode', 'elementor' ) ); } $results = copy_dir( __DIR__ . '/mu-plugin/', WPMU_PLUGIN_DIR ); if ( is_wp_error( $results ) ) { return; } $token = md5( wp_rand() ); // Only who own this key can use 'elementor-safe-mode'. update_option( self::OPTION_TOKEN, $token ); // Save for later use. setcookie( self::OPTION_TOKEN, $token, time() + HOUR_IN_SECONDS, COOKIEPATH ); } public function disable_safe_mode() { if ( ! current_user_can( 'install_plugins' ) ) { return; } $file_path = WP_CONTENT_DIR . '/mu-plugins/elementor-safe-mode.php'; if ( file_exists( $file_path ) ) { unlink( $file_path ); } if ( get_option( 'elementor_safe_mode_created_mu_dir' ) ) { // It will be removed only if it's empty and don't have other mu-plugins. @rmdir( WPMU_PLUGIN_DIR ); } delete_option( 'elementor_safe_mode' ); delete_option( 'elementor_safe_mode_allowed_plugins' ); delete_option( 'theme_mods_elementor-safe' ); delete_option( 'elementor_safe_mode_created_mu_dir' ); delete_option( self::OPTION_TOKEN ); setcookie( self::OPTION_TOKEN, '', 1 ); } public function filter_preview_url( $url ) { return add_query_arg( 'elementor-mode', 'safe', $url ); } public function filter_template() { return ELEMENTOR_PATH . 'modules/page-templates/templates/canvas.php'; } public function print_safe_mode_css() { ?> print_safe_mode_css() ); ?>