-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New: Implement caching mechanism on render elements [ED-13883] #25730
Changes from all commits
b350809
b865189
30b7660
5e8b1c8
d0fe91b
bb03fd3
d120722
71a8285
9986587
4722e38
8a3763e
ac5dcb3
b96747c
ece2f12
8c71865
345a5d6
9020f22
d207a4f
d24b30d
a902c4e
af16d38
4f1f492
a8c3acb
fcdf1d1
4bffa48
c53758d
bb1c404
0ec9c7c
af1ee13
61ce410
6cf45c3
d793912
c958aa8
b6bee26
184923d
39dad02
825e4f5
824a7c2
e8a3e08
0dd1002
07e7116
1a52e37
d7b1bfe
484f80c
3f5dc5c
7e74fbb
6e5794e
da14e22
641f609
1c80936
a8d99e0
1303569
d460607
8f25772
aa9dd54
2fd4573
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,8 @@ abstract class Document extends Controls_Stack { | |
|
||
const BUILT_WITH_ELEMENTOR_META_KEY = '_elementor_edit_mode'; | ||
|
||
const CACHE_META_KEY = '_elementor_element_cache'; | ||
|
||
/** | ||
* Document publish status. | ||
*/ | ||
|
@@ -857,6 +859,9 @@ public function save( $data ) { | |
|
||
$post_css->delete(); | ||
|
||
// Remove Document Cache | ||
$this->delete_cache(); | ||
|
||
/** | ||
* After document save. | ||
* | ||
|
@@ -1447,7 +1452,7 @@ public function get_main_meta( $key ) { | |
* @access public | ||
* | ||
* @param string $key Meta data key. | ||
* @param string $value Meta data value. | ||
* @param mixed $value Meta data value. | ||
* | ||
* @return bool|int | ||
*/ | ||
|
@@ -1785,6 +1790,68 @@ protected function save_settings( $settings ) { | |
* @access protected | ||
*/ | ||
protected function print_elements( $elements_data ) { | ||
if ( ! Plugin::$instance->experiments->is_feature_active( 'e_element_cache' ) ) { | ||
$this->do_print_elements( $elements_data ); | ||
|
||
return; | ||
} | ||
|
||
$cached_data = $this->get_document_cache(); | ||
|
||
if ( false === $cached_data ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KingYes We might be able to extract this a bit?
|
||
add_filter( 'elementor/element/should_render_shortcode', '__return_true' ); | ||
|
||
$scripts_to_queue = []; | ||
$styles_to_queue = []; | ||
|
||
global $wp_scripts, $wp_styles; | ||
|
||
$should_store_scripts = $wp_scripts instanceof \WP_Scripts && $wp_styles instanceof \WP_Styles; | ||
if ( $should_store_scripts ) { | ||
$scripts_ignored = $wp_scripts->queue; | ||
$styles_ignored = $wp_styles->queue; | ||
} | ||
|
||
ob_start(); | ||
|
||
$this->do_print_elements( $elements_data ); | ||
|
||
if ( $should_store_scripts ) { | ||
$scripts_to_queue = array_values( array_diff( $wp_scripts->queue, $scripts_ignored ) ); | ||
$styles_to_queue = array_values( array_diff( $wp_styles->queue, $styles_ignored ) ); | ||
} | ||
|
||
$cached_data = [ | ||
'content' => ob_get_clean(), | ||
louiswol94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'scripts' => $scripts_to_queue, | ||
'styles' => $styles_to_queue, | ||
]; | ||
|
||
if ( $this->should_store_cache_elements() ) { | ||
$this->set_document_cache( $cached_data ); | ||
} | ||
|
||
remove_filter( 'elementor/element/should_render_shortcode', '__return_true' ); | ||
} else { | ||
if ( ! empty( $cached_data['scripts'] ) ) { | ||
foreach ( $cached_data['scripts'] as $script_handle ) { | ||
wp_enqueue_script( $script_handle ); | ||
} | ||
} | ||
|
||
if ( ! empty( $cached_data['styles'] ) ) { | ||
foreach ( $cached_data['styles'] as $style_handle ) { | ||
wp_enqueue_style( $style_handle ); | ||
} | ||
} | ||
} | ||
|
||
if ( ! empty( $cached_data['content'] ) ) { | ||
echo $cached_data['content']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | ||
louiswol94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
protected function do_print_elements( $elements_data ) { | ||
// Collect all data updaters that should be updated on runtime. | ||
$runtime_elements_iteration_actions = $this->get_runtime_elements_iteration_actions(); | ||
|
||
|
@@ -1803,6 +1870,52 @@ protected function print_elements( $elements_data ) { | |
} | ||
} | ||
|
||
public function set_document_cache( $value ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is too big. Can we extract cache manipulation to a separate class or trait? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it should be within a cache module, I'm not sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about extracting it to the caching module. But we have a lots of codes connected to this class, so I just put it here for now. |
||
$expiration = '+12 hours'; | ||
KingYes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$data = [ | ||
'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ), | ||
'value' => $value, | ||
]; | ||
|
||
$this->update_json_meta( static::CACHE_META_KEY, $data ); | ||
} | ||
|
||
private function get_document_cache() { | ||
$cache = $this->get_json_meta( static::CACHE_META_KEY ); | ||
|
||
if ( empty( $cache['timeout'] ) ) { | ||
return false; | ||
} | ||
|
||
if ( current_time( 'timestamp' ) > $cache['timeout'] ) { | ||
return false; | ||
} | ||
|
||
if ( ! is_array( $cache['value'] ) ) { | ||
return false; | ||
} | ||
|
||
return $cache['value']; | ||
} | ||
|
||
protected function delete_cache() { | ||
$this->delete_meta( static::CACHE_META_KEY ); | ||
} | ||
|
||
private function should_store_cache_elements() { | ||
static $should_store_cache_elements = null; | ||
|
||
if ( null === $should_store_cache_elements ) { | ||
$should_store_cache_elements = ( | ||
! is_admin() | ||
&& ! Plugin::$instance->preview->is_preview_mode() | ||
); | ||
} | ||
|
||
return $should_store_cache_elements; | ||
} | ||
|
||
protected function register_document_controls() { | ||
$this->start_controls_section( | ||
'document_settings', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,12 @@ public function enable_assets( array $assets_data ) { | |
foreach ( $assets_data as $assets_type => $assets_list ) { | ||
foreach ( $assets_list as $asset_name ) { | ||
$this->assets[ $assets_type ][ $asset_name ]['enabled'] = true; | ||
|
||
if ( 'scripts' === $assets_type ) { | ||
wp_enqueue_script( $asset_name ); | ||
Comment on lines
+61
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KingYes - Currently pro assets will not be loaded (if core will be upgraded but not the pro), see the usage of |
||
} else { | ||
wp_enqueue_style( $asset_name ); | ||
} | ||
louiswol94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
@@ -75,6 +81,9 @@ public function add_assets( array $assets ) { | |
$this->assets = array_replace_recursive( $this->assets, $assets ); | ||
} | ||
|
||
/** | ||
* @deprecated 3.22.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the method that we want to keep no? because you now enqueue instead of enable, so why not move the code you have in |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KingYes - Not sure I understand the latest changes related to the loader, I see that you've restored the call to enqueue_assets(), but why not go with the direction that you need?:
|
||
public function enqueue_assets() { | ||
$assets = $this->get_assets(); | ||
$is_preview_mode = Plugin::$instance->preview->is_preview_mode(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you implement this change now already?
We often forget to update such things at a later stage.