namespace Elementor\Modules\ElementCache;
use Elementor\Controls_Manager;
use Elementor\Core\Base\Module as BaseModule;
use Elementor\Core\Experiments\Manager as ExperimentsManager;
use Elementor\Element_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
class Module extends BaseModule {
const OPTION_UNIQUE_ID = '_elementor_element_cache_unique_id';
public function get_name() {
public function __construct() {
$this->register_shortcode();
if ( ! Plugin::$instance->experiments->is_feature_active( 'e_element_cache' ) ) {
add_filter( 'elementor/element_cache/unique_id', [ $this, 'get_unique_id' ] );
$this->add_advanced_tab_actions();
add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_fields' ], 100 );
$this->clear_cache_on_site_changed();
public static function get_experimental_data(): array {
'name' => 'e_element_cache',
'title' => esc_html__( 'Element Caching', 'elementor' ),
'tag' => esc_html__( 'Performance', 'elementor' ),
'description' => esc_html__( 'Elements caching reduces loading times by serving up a copy of an element instead of rendering it fresh every time the page is loaded. When active, Elementor will determine which elements can benefit from static loading - but you can override this.', 'elementor' ),
'release_status' => ExperimentsManager::RELEASE_STATUS_STABLE,
'default' => ExperimentsManager::STATE_ACTIVE,
public function get_unique_id() {
$unique_id = get_option( static::OPTION_UNIQUE_ID );
$unique_id = md5( uniqid( wp_generate_password() ) );
update_option( static::OPTION_UNIQUE_ID, $unique_id );
private function register_shortcode() {
add_shortcode( 'elementor-element', function ( $atts ) {
if ( empty( $atts['data'] ) ) {
if ( empty( $atts['k'] ) || $atts['k'] !== $this->get_unique_id() ) {
$widget_data = json_decode( base64_decode( $atts['data'] ), true );
if ( empty( $widget_data ) || ! is_array( $widget_data ) ) {
$element = Plugin::$instance->elements_manager->create_element_instance( $widget_data );
$element->print_element();
private function add_advanced_tab_actions() {
'elementor/element/common/_section_style/after_section_end' => '_css_classes', // Widgets
foreach ( $hooks as $hook => $injection_position ) {
function( $element, $args ) use ( $injection_position ) {
$this->add_control_to_advanced_tab( $element, $args, $injection_position );
private function add_control_to_advanced_tab( Element_Base $element, $args, $injection_position ) {
$element->start_injection(
'of' => $injection_position,
'label' => esc_html__( 'Cache Settings', 'elementor' ),
'type' => Controls_Manager::SELECT,
'' => esc_html__( 'Default', 'elementor' ),
'yes' => esc_html__( 'Inactive', 'elementor' ),
'no' => esc_html__( 'Active', 'elementor' ),
$element->add_control( '_element_cache', $control_data );
$element->end_injection();
public function register_admin_fields( Settings $settings ) {
Settings::TAB_PERFORMANCE,
Settings::TAB_PERFORMANCE,
'label' => esc_html__( 'Element Cache', 'elementor' ),
'class' => 'elementor-element-cache-ttl',
'disable' => esc_html__( 'Disable', 'elementor' ),
'1' => esc_html__( '1 Hour', 'elementor' ),
'6' => esc_html__( '6 Hours', 'elementor' ),
'12' => esc_html__( '12 Hours', 'elementor' ),
'24' => esc_html__( '1 Day', 'elementor' ),
'72' => esc_html__( '3 Days', 'elementor' ),
'168' => esc_html__( '1 Week', 'elementor' ),
'336' => esc_html__( '2 Weeks', 'elementor' ),
'720' => esc_html__( '1 Month', 'elementor' ),
'8760' => esc_html__( '1 Year', 'elementor' ),
'desc' => esc_html__( 'Specify the duration for which data is stored in the cache. Elements caching speeds up loading by serving pre-rendered copies of elements, rather than rendering them fresh each time. This control ensures efficient performance and up-to-date content.', 'elementor' ),
private function clear_cache_on_site_changed() {
add_action( 'activated_plugin', [ $this, 'clear_cache' ] );
add_action( 'deactivated_plugin', [ $this, 'clear_cache' ] );
add_action( 'switch_theme', [ $this, 'clear_cache' ] );
add_action( 'upgrader_process_complete', [ $this, 'clear_cache' ] );
add_action( 'update_option_elementor_element_cache_ttl', [ $this, 'clear_cache' ] );
public function clear_cache() {
Plugin::$instance->files_manager->clear_cache();