PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.2
Elementor Website Builder – more than just a page builder v3.31.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.31.2, at modules/element-cache/module.php

176 lines 5.1 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_STABLE,
50 'default' => ExperimentsManager::STATE_ACTIVE,
51 'generator_tag' => true,
52 ];
53 }
54
55 public function get_unique_id() {
56 $unique_id = get_option( static::OPTION_UNIQUE_ID );
57
58 if ( ! $unique_id ) {
59 $unique_id = md5( uniqid( wp_generate_password() ) );
60 update_option( static::OPTION_UNIQUE_ID, $unique_id );
61 }
62
63 return $unique_id;
64 }
65
66 private function register_shortcode() {
67 add_shortcode( 'elementor-element', function ( $atts ) {
68 if ( empty( $atts['data'] ) ) {
69 return '';
70 }
71
72 if ( empty( $atts['k'] ) || $atts['k'] !== $this->get_unique_id() ) {
73 return '';
74 }
75
76 $widget_data = json_decode( base64_decode( $atts['data'] ), true );
77
78 if ( empty( $widget_data ) || ! is_array( $widget_data ) ) {
79 return '';
80 }
81
82 ob_start();
83
84 $element = Plugin::$instance->elements_manager->create_element_instance( $widget_data );
85
86 if ( $element ) {
87 $element->print_element();
88 }
89
90 return ob_get_clean();
91 } );
92 }
93
94 private function add_advanced_tab_actions() {
95 $hooks = [
96 'elementor/element/common/_section_style/after_section_end' => '_css_classes', // Widgets
97 ];
98
99 foreach ( $hooks as $hook => $injection_position ) {
100 add_action(
101 $hook,
102 function( $element, $args ) use ( $injection_position ) {
103 $this->add_control_to_advanced_tab( $element, $args, $injection_position );
104 },
105 10,
106 2
107 );
108 }
109 }
110
111 private function add_control_to_advanced_tab( Element_Base $element, $args, $injection_position ) {
112 $element->start_injection(
113 [
114 'of' => $injection_position,
115 ]
116 );
117
118 $control_data = [
119 'label' => esc_html__( 'Cache Settings', 'elementor' ),
120 'type' => Controls_Manager::SELECT,
121 'default' => '',
122 'options' => [
123 '' => esc_html__( 'Default', 'elementor' ),
124 'yes' => esc_html__( 'Inactive', 'elementor' ),
125 'no' => esc_html__( 'Active', 'elementor' ),
126 ],
127 ];
128
129 $element->add_control( '_element_cache', $control_data );
130
131 $element->end_injection();
132 }
133
134 public function register_admin_fields( Settings $settings ) {
135 $settings->add_field(
136 Settings::TAB_PERFORMANCE,
137 Settings::TAB_PERFORMANCE,
138 'element_cache_ttl',
139 [
140 'label' => esc_html__( 'Element Cache', 'elementor' ),
141 'field_args' => [
142 'class' => 'elementor-element-cache-ttl',
143 'type' => 'select',
144 'std' => '24',
145 'options' => [
146 'disable' => esc_html__( 'Disable', 'elementor' ),
147 '1' => esc_html__( '1 Hour', 'elementor' ),
148 '6' => esc_html__( '6 Hours', 'elementor' ),
149 '12' => esc_html__( '12 Hours', 'elementor' ),
150 '24' => esc_html__( '1 Day', 'elementor' ),
151 '72' => esc_html__( '3 Days', 'elementor' ),
152 '168' => esc_html__( '1 Week', 'elementor' ),
153 '336' => esc_html__( '2 Weeks', 'elementor' ),
154 '720' => esc_html__( '1 Month', 'elementor' ),
155 '8760' => esc_html__( '1 Year', 'elementor' ),
156 ],
157 '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' ),
158 ],
159 ]
160 );
161 }
162
163 private function clear_cache_on_site_changed() {
164 add_action( 'activated_plugin', [ $this, 'clear_cache' ] );
165 add_action( 'deactivated_plugin', [ $this, 'clear_cache' ] );
166 add_action( 'switch_theme', [ $this, 'clear_cache' ] );
167 add_action( 'upgrader_process_complete', [ $this, 'clear_cache' ] );
168
169 add_action( 'update_option_elementor_element_cache_ttl', [ $this, 'clear_cache' ] );
170 }
171
172 public function clear_cache() {
173 Plugin::$instance->files_manager->clear_cache();
174 }
175 }
176