| 1 |
<?php |
| 2 |
namespace Elementor\Modules\ElementCache; |
| 3 |
|
| 4 |
use Elementor\Controls_Manager; |
| 5 |
use Elementor\Core\Base\Module as BaseModule; |
| 6 |
use Elementor\Core\Experiments\Manager as ExperimentsManager; |
| 7 |
use Elementor\Element_Base; |
| 8 |
use Elementor\Plugin; |
| 9 |
use Elementor\Settings; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
class Module extends BaseModule { |
| 16 |
|
| 17 |
public function get_name() { |
| 18 |
return 'element-cache'; |
| 19 |
} |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
parent::__construct(); |
| 23 |
|
| 24 |
$this->register_shortcode(); |
| 25 |
|
| 26 |
if ( ! Plugin::$instance->experiments->is_feature_active( 'e_element_cache' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$this->add_advanced_tab_actions(); |
| 31 |
|
| 32 |
if ( is_admin() ) { |
| 33 |
add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_fields' ], 100 ); |
| 34 |
} |
| 35 |
|
| 36 |
$this->clear_cache_on_site_changed(); |
| 37 |
} |
| 38 |
|
| 39 |
public static function get_experimental_data(): array { |
| 40 |
return [ |
| 41 |
'name' => 'e_element_cache', |
| 42 |
'title' => esc_html__( 'Element Caching', 'elementor' ), |
| 43 |
'tag' => esc_html__( 'Performance', 'elementor' ), |
| 44 |
'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' ), |
| 45 |
'release_status' => ExperimentsManager::RELEASE_STATUS_BETA, |
| 46 |
'default' => ExperimentsManager::STATE_INACTIVE, |
| 47 |
'new_site' => [ |
| 48 |
'default_active' => true, |
| 49 |
'minimum_installation_version' => '3.23.0', |
| 50 |
], |
| 51 |
'generator_tag' => true, |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
private function register_shortcode() { |
| 56 |
add_shortcode( 'elementor-element', function ( $atts ) { |
| 57 |
if ( empty( $atts['data'] ) ) { |
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
$widget_data = json_decode( base64_decode( $atts['data'] ), true ); |
| 62 |
|
| 63 |
if ( empty( $widget_data ) || ! is_array( $widget_data ) ) { |
| 64 |
return ''; |
| 65 |
} |
| 66 |
|
| 67 |
$widget_data['settings']['isShortcode'] = true; |
| 68 |
|
| 69 |
ob_start(); |
| 70 |
|
| 71 |
$element = Plugin::$instance->elements_manager->create_element_instance( $widget_data ); |
| 72 |
|
| 73 |
if ( $element ) { |
| 74 |
$element->print_element(); |
| 75 |
} |
| 76 |
|
| 77 |
return ob_get_clean(); |
| 78 |
} ); |
| 79 |
} |
| 80 |
|
| 81 |
private function add_advanced_tab_actions() { |
| 82 |
$hooks = [ |
| 83 |
'elementor/element/common/_section_style/after_section_end' => '_css_classes', // Widgets |
| 84 |
]; |
| 85 |
|
| 86 |
foreach ( $hooks as $hook => $injection_position ) { |
| 87 |
add_action( |
| 88 |
$hook, |
| 89 |
function( $element, $args ) use ( $injection_position ) { |
| 90 |
$this->add_control_to_advanced_tab( $element, $args, $injection_position ); |
| 91 |
}, |
| 92 |
10, |
| 93 |
2 |
| 94 |
); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private function add_control_to_advanced_tab( Element_Base $element, $args, $injection_position ) { |
| 99 |
$element->start_injection( |
| 100 |
[ |
| 101 |
'of' => $injection_position, |
| 102 |
] |
| 103 |
); |
| 104 |
|
| 105 |
$control_data = [ |
| 106 |
'label' => esc_html__( 'Cache Settings', 'elementor' ), |
| 107 |
'type' => Controls_Manager::SELECT, |
| 108 |
'default' => '', |
| 109 |
'options' => [ |
| 110 |
'' => esc_html__( 'Default', 'elementor' ), |
| 111 |
'yes' => esc_html__( 'Inactive', 'elementor' ), |
| 112 |
'no' => esc_html__( 'Active', 'elementor' ), |
| 113 |
], |
| 114 |
]; |
| 115 |
|
| 116 |
$element->add_control( '_element_cache', $control_data ); |
| 117 |
|
| 118 |
$element->end_injection(); |
| 119 |
} |
| 120 |
|
| 121 |
public function register_admin_fields( Settings $settings ) { |
| 122 |
$settings->add_field( |
| 123 |
Settings::TAB_PERFORMANCE, |
| 124 |
Settings::TAB_PERFORMANCE, |
| 125 |
'element_cache_ttl', |
| 126 |
[ |
| 127 |
'label' => esc_html__( 'Element Cache', 'elementor' ), |
| 128 |
'field_args' => [ |
| 129 |
'class' => 'elementor-element-cache-ttl', |
| 130 |
'type' => 'select', |
| 131 |
'std' => '24', |
| 132 |
'options' => [ |
| 133 |
'disable' => esc_html__( 'Disable', 'elementor' ), |
| 134 |
'1' => esc_html__( '1 Hour', 'elementor' ), |
| 135 |
'6' => esc_html__( '6 Hours', 'elementor' ), |
| 136 |
'12' => esc_html__( '12 Hours', 'elementor' ), |
| 137 |
'24' => esc_html__( '1 Day', 'elementor' ), |
| 138 |
'72' => esc_html__( '3 Days', 'elementor' ), |
| 139 |
'168' => esc_html__( '1 Week', 'elementor' ), |
| 140 |
'336' => esc_html__( '2 Weeks', 'elementor' ), |
| 141 |
'720' => esc_html__( '1 Month', 'elementor' ), |
| 142 |
'8760' => esc_html__( '1 Year', 'elementor' ), |
| 143 |
], |
| 144 |
'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' ), |
| 145 |
], |
| 146 |
] |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
private function clear_cache_on_site_changed() { |
| 151 |
add_action( 'activated_plugin', [ $this, 'clear_cache' ] ); |
| 152 |
add_action( 'deactivated_plugin', [ $this, 'clear_cache' ] ); |
| 153 |
add_action( 'switch_theme', [ $this, 'clear_cache' ] ); |
| 154 |
add_action( 'upgrader_process_complete', [ $this, 'clear_cache' ] ); |
| 155 |
|
| 156 |
add_action( 'update_option_elementor_element_cache_ttl', [ $this, 'clear_cache' ] ); |
| 157 |
} |
| 158 |
|
| 159 |
public function clear_cache() { |
| 160 |
Plugin::$instance->files_manager->clear_cache(); |
| 161 |
} |
| 162 |
} |
| 163 |
|