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