| 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 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Module extends BaseModule { |
| 15 |
|
| 16 |
public function get_name() { |
| 17 |
return 'element-cache'; |
| 18 |
} |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
parent::__construct(); |
| 22 |
|
| 23 |
$this->register_experiments(); |
| 24 |
$this->add_advanced_tab_actions(); |
| 25 |
$this->register_shortcode(); |
| 26 |
} |
| 27 |
|
| 28 |
private function register_experiments() { |
| 29 |
Plugin::$instance->experiments->add_feature( [ |
| 30 |
'name' => 'e_element_cache', |
| 31 |
'title' => esc_html__( 'Element Caching', 'elementor' ), |
| 32 |
'tag' => esc_html__( 'Performance', 'elementor' ), |
| 33 |
'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' ), |
| 34 |
'release_status' => ExperimentsManager::RELEASE_STATUS_DEV, |
| 35 |
'default' => ExperimentsManager::STATE_INACTIVE, |
| 36 |
'generator_tag' => true, |
| 37 |
] ); |
| 38 |
} |
| 39 |
|
| 40 |
private function add_advanced_tab_actions() { |
| 41 |
if ( ! Plugin::$instance->experiments->is_feature_active( 'e_element_cache' ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$hooks = array( |
| 46 |
'elementor/element/common/_section_style/after_section_end' => '_css_classes', // Widgets |
| 47 |
); |
| 48 |
|
| 49 |
foreach ( $hooks as $hook => $injection_position ) { |
| 50 |
add_action( |
| 51 |
$hook, |
| 52 |
function( $element, $args ) use ( $injection_position ) { |
| 53 |
$this->add_control_to_advanced_tab( $element, $args, $injection_position ); |
| 54 |
}, |
| 55 |
10, |
| 56 |
2 |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
private function add_control_to_advanced_tab( Element_Base $element, $args, $injection_position ) { |
| 62 |
$element->start_injection( |
| 63 |
[ |
| 64 |
'of' => $injection_position, |
| 65 |
] |
| 66 |
); |
| 67 |
|
| 68 |
$control_data = [ |
| 69 |
'label' => esc_html__( 'Cache Settings', 'elementor' ), |
| 70 |
'type' => Controls_Manager::SELECT, |
| 71 |
'default' => '', |
| 72 |
'options' => [ |
| 73 |
'' => esc_html__( 'Default', 'elementor' ), |
| 74 |
'yes' => esc_html__( 'Inactive', 'elementor' ), |
| 75 |
'no' => esc_html__( 'Active', 'elementor' ), |
| 76 |
], |
| 77 |
]; |
| 78 |
|
| 79 |
$element->add_control( '_element_cache', $control_data ); |
| 80 |
|
| 81 |
$element->end_injection(); |
| 82 |
} |
| 83 |
|
| 84 |
private function register_shortcode() { |
| 85 |
add_shortcode( 'elementor-element', function ( $atts ) { |
| 86 |
if ( empty( $atts['data'] ) ) { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
|
| 90 |
$widget_data = json_decode( base64_decode( $atts['data'] ), true ); |
| 91 |
|
| 92 |
if ( empty( $widget_data ) || ! is_array( $widget_data ) ) { |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
ob_start(); |
| 97 |
|
| 98 |
$element = Plugin::$instance->elements_manager->create_element_instance( $widget_data ); |
| 99 |
|
| 100 |
if ( $element ) { |
| 101 |
$element->print_element(); |
| 102 |
} |
| 103 |
|
| 104 |
return ob_get_clean(); |
| 105 |
} ); |
| 106 |
} |
| 107 |
} |
| 108 |
|