PluginProbe
Elementor Website Builder – more than just a page builder / 3.29.2
Elementor Website Builder – more than just a page builder v3.29.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / element-cache / module.php

module.php in Elementor Website Builder – more than just a page builder 3.29.2, at modules/element-cache/module.php

180 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 const OPTION_UNIQUE_ID = '_elementor_element_cache_unique_id';
18
19 public function get_name() {
20 return 'element-cache';
21 }
22
23 public function __construct() {
24 parent::__construct();
25
26 $this->register_shortcode();
27
28 if ( ! Plugin::$instance->experiments->is_feature_active( 'e_element_cache' ) ) {
29 return;
30 }
31
32 add_filter( 'elementor/element_cache/unique_id', [ $this, 'get_unique_id' ] );
33
34 $this->add_advanced_tab_actions();
35
36 if ( is_admin() ) {
37 add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_fields' ], 100 );
38 }
39
40 $this->clear_cache_on_site_changed();
41 }
42
43 public static function get_experimental_data(): array {
44 return [
45 'name' => 'e_element_cache',
46 'title' => esc_html__( 'Element Caching', 'elementor' ),
47 'tag' => esc_html__( 'Performance', 'elementor' ),
48 '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' ),
49 'release_status' => ExperimentsManager::RELEASE_STATUS_BETA,
50 'default' => ExperimentsManager::STATE_INACTIVE,
51 'new_site' => [
52 'default_active' => true,
53 'minimum_installation_version' => '3.23.0',
54 ],
55 'generator_tag' => true,
56 ];
57 }
58
59 public function get_unique_id() {
60 $unique_id = get_option( static::OPTION_UNIQUE_ID );
61
62 if ( ! $unique_id ) {
63 $unique_id = md5( uniqid( wp_generate_password() ) );
64 update_option( static::OPTION_UNIQUE_ID, $unique_id );
65 }
66
67 return $unique_id;
68 }
69
70 private function register_shortcode() {
71 add_shortcode( 'elementor-element', function ( $atts ) {
72 if ( empty( $atts['data'] ) ) {
73 return '';
74 }
75
76 if ( empty( $atts['k'] ) || $atts['k'] !== $this->get_unique_id() ) {
77 return '';
78 }
79
80 $widget_data = json_decode( base64_decode( $atts['data'] ), true );
81
82 if ( empty( $widget_data ) || ! is_array( $widget_data ) ) {
83 return '';
84 }
85
86 ob_start();
87
88 $element = Plugin::$instance->elements_manager->create_element_instance( $widget_data );
89
90 if ( $element ) {
91 $element->print_element();
92 }
93
94 return ob_get_clean();
95 } );
96 }
97
98 private function add_advanced_tab_actions() {
99 $hooks = [
100 'elementor/element/common/_section_style/after_section_end' => '_css_classes', // Widgets
101 ];
102
103 foreach ( $hooks as $hook => $injection_position ) {
104 add_action(
105 $hook,
106 function( $element, $args ) use ( $injection_position ) {
107 $this->add_control_to_advanced_tab( $element, $args, $injection_position );
108 },
109 10,
110 2
111 );
112 }
113 }
114
115 private function add_control_to_advanced_tab( Element_Base $element, $args, $injection_position ) {
116 $element->start_injection(
117 [
118 'of' => $injection_position,
119 ]
120 );
121
122 $control_data = [
123 'label' => esc_html__( 'Cache Settings', 'elementor' ),
124 'type' => Controls_Manager::SELECT,
125 'default' => '',
126 'options' => [
127 '' => esc_html__( 'Default', 'elementor' ),
128 'yes' => esc_html__( 'Inactive', 'elementor' ),
129 'no' => esc_html__( 'Active', 'elementor' ),
130 ],
131 ];
132
133 $element->add_control( '_element_cache', $control_data );
134
135 $element->end_injection();
136 }
137
138 public function register_admin_fields( Settings $settings ) {
139 $settings->add_field(
140 Settings::TAB_PERFORMANCE,
141 Settings::TAB_PERFORMANCE,
142 'element_cache_ttl',
143 [
144 'label' => esc_html__( 'Element Cache', 'elementor' ),
145 'field_args' => [
146 'class' => 'elementor-element-cache-ttl',
147 'type' => 'select',
148 'std' => '24',
149 'options' => [
150 'disable' => esc_html__( 'Disable', 'elementor' ),
151 '1' => esc_html__( '1 Hour', 'elementor' ),
152 '6' => esc_html__( '6 Hours', 'elementor' ),
153 '12' => esc_html__( '12 Hours', 'elementor' ),
154 '24' => esc_html__( '1 Day', 'elementor' ),
155 '72' => esc_html__( '3 Days', 'elementor' ),
156 '168' => esc_html__( '1 Week', 'elementor' ),
157 '336' => esc_html__( '2 Weeks', 'elementor' ),
158 '720' => esc_html__( '1 Month', 'elementor' ),
159 '8760' => esc_html__( '1 Year', 'elementor' ),
160 ],
161 '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' ),
162 ],
163 ]
164 );
165 }
166
167 private function clear_cache_on_site_changed() {
168 add_action( 'activated_plugin', [ $this, 'clear_cache' ] );
169 add_action( 'deactivated_plugin', [ $this, 'clear_cache' ] );
170 add_action( 'switch_theme', [ $this, 'clear_cache' ] );
171 add_action( 'upgrader_process_complete', [ $this, 'clear_cache' ] );
172
173 add_action( 'update_option_elementor_element_cache_ttl', [ $this, 'clear_cache' ] );
174 }
175
176 public function clear_cache() {
177 Plugin::$instance->files_manager->clear_cache();
178 }
179 }
180