elementor-safe-mode.php (3931B)
1 <?php 2 3 /** 4 * Plugin Name: Elementor Safe Mode 5 * Description: Safe Mode allows you to troubleshoot issues by only loading the editor, without loading the theme or any other plugin. 6 * Plugin URI: https://elementor.com/?utm_source=safe-mode&utm_campaign=plugin-uri&utm_medium=wp-dash 7 * Author: Elementor.com 8 * Version: 1.0.0 9 * Author URI: https://elementor.com/?utm_source=safe-mode&utm_campaign=author-uri&utm_medium=wp-dash 10 * 11 * Text Domain: elementor 12 * 13 * @package Elementor 14 * @category Safe Mode 15 * 16 * Elementor is free software: you can redistribute it and/or modify 17 * it under the terms of the GNU General Public License as published by 18 * the Free Software Foundation, either version 3 of the License, or 19 * any later version. 20 * 21 * Elementor is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 * GNU General Public License for more details. 25 */ 26 27 if ( ! defined( 'ABSPATH' ) ) { 28 exit; // Exit if accessed directly 29 } 30 31 class Safe_Mode { 32 33 const OPTION_ENABLED = 'elementor_safe_mode'; 34 const OPTION_TOKEN = self::OPTION_ENABLED . '_token'; 35 36 public function is_enabled() { 37 return get_option( self::OPTION_ENABLED ); 38 } 39 40 public function is_valid_token() { 41 $token = isset( $_COOKIE[ self::OPTION_TOKEN ] ) ? $_COOKIE[ self::OPTION_TOKEN ] : null; 42 43 if ( $token && get_option( self::OPTION_TOKEN ) === $token ) { 44 return true; 45 } 46 47 return false; 48 } 49 50 public function is_requested() { 51 return ! empty( $_REQUEST['elementor-mode'] ) && 'safe' === $_REQUEST['elementor-mode']; 52 } 53 54 public function is_editor() { 55 return is_admin() && isset( $_GET['action'] ) && 'elementor' === $_GET['action']; 56 } 57 58 public function is_editor_preview() { 59 return isset( $_GET['elementor-preview'] ); 60 } 61 62 public function is_editor_ajax() { 63 // PHPCS - There is already nonce verification in the Ajax Manager 64 return is_admin() && isset( $_POST['action'] ) && 'elementor_ajax' === $_POST['action']; // phpcs:ignore WordPress.Security.NonceVerification.Missing 65 } 66 67 public function add_hooks() { 68 add_filter( 'pre_option_active_plugins', function () { 69 return get_option( 'elementor_safe_mode_allowed_plugins' ); 70 } ); 71 72 add_filter( 'pre_option_stylesheet', function () { 73 return 'elementor-safe'; 74 } ); 75 76 add_filter( 'pre_option_template', function () { 77 return 'elementor-safe'; 78 } ); 79 80 add_action( 'elementor/init', function () { 81 do_action( 'elementor/safe_mode/init' ); 82 } ); 83 } 84 85 /** 86 * Plugin row meta. 87 * 88 * Adds row meta links to the plugin list table 89 * 90 * Fired by `plugin_row_meta` filter. 91 * 92 * @access public 93 * 94 * @param array $plugin_meta An array of the plugin's metadata, including 95 * the version, author, author URI, and plugin URI. 96 * @param string $plugin_file Path to the plugin file, relative to the plugins 97 * directory. 98 * 99 * @return array An array of plugin row meta links. 100 */ 101 public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) { 102 if ( basename( __FILE__ ) === $plugin_file ) { 103 $row_meta = [ 104 'docs' => '<a href="https://go.elementor.com/safe-mode/" aria-label="' . esc_attr( esc_html__( 'Learn More', 'elementor' ) ) . '" target="_blank">' . esc_html__( 'Learn More', 'elementor' ) . '</a>', 105 ]; 106 107 $plugin_meta = array_merge( $plugin_meta, $row_meta ); 108 } 109 110 return $plugin_meta; 111 } 112 113 public function __construct() { 114 add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 4 ); 115 116 $enabled_type = $this->is_enabled(); 117 118 if ( ! $enabled_type || ! $this->is_valid_token() ) { 119 return; 120 } 121 122 if ( ! $this->is_requested() && 'global' !== $enabled_type ) { 123 return; 124 } 125 126 if ( ! $this->is_editor() && ! $this->is_editor_preview() && ! $this->is_editor_ajax() ) { 127 return; 128 } 129 130 $this->add_hooks(); 131 } 132 } 133 134 new Safe_Mode();