PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0
Elementor Website Builder – more than just a page builder v4.2.0
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 / components / styles / component-styles.php

component-styles.php in Elementor Website Builder – more than just a page builder 4.2.0, at modules/components/styles/component-styles.php

113 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Components\Styles;
4
5 use Elementor\Core\Base\Document;
6 use Elementor\Core\Utils\Collection;
7 use Elementor\Modules\AtomicWidgets\Styles\CacheValidity\Cache_Validity;
8 use Elementor\Modules\AtomicWidgets\Utils\Utils;
9
10 /**
11 * Component styles fetching for render
12 */
13 class Component_Styles {
14 const CACHE_ROOT_KEY = 'component-styles-related-posts';
15
16 public function register_hooks() {
17 add_action( 'elementor/post/render', fn( $post_id ) => $this->render_post( $post_id ) );
18
19 add_action( 'elementor/document/after_save', fn( Document $document ) => $this->invalidate_cache(
20 [ $document->get_main_post()->ID ]
21 ), 20, 2 );
22
23 add_action(
24 'elementor/core/files/clear_cache',
25 fn() => $this->invalidate_cache(),
26 );
27
28 add_filter(
29 'elementor/document/related_posts',
30 fn( array $related, $post_id ) => $this->get_related_posts( $related, $post_id ),
31 10,
32 2
33 );
34 }
35
36 private function render_post( string $post_id ) {
37 $component_ids = $this->get_component_ids_from_post_cached( $post_id );
38
39 $this->declare_components_rendered( $component_ids );
40 }
41
42 /**
43 * Handler for the `elementor/document/related_posts` filter.
44 *
45 * @param int[] $related Accumulated ids from previous filter handlers.
46 * @param string|int $post_id Parent post id being inspected.
47 * @return int[] Merged list of related post ids.
48 */
49 private function get_related_posts( array $related, $post_id ): array {
50 $component_ids = $this->get_component_ids_from_post_cached( (string) $post_id );
51
52 return array_values( array_unique( array_merge( $related, array_map( 'intval', $component_ids ) ) ) );
53 }
54
55 /**
56 * Returns the component ids embedded in $post_id, reading from the
57 * traversal cache when available and writing to it otherwise.
58 *
59 * Both the render-action listener and the `elementor/document/related_posts`
60 * filter handler delegate here so the traversal logic lives in one place.
61 */
62 private function get_component_ids_from_post_cached( string $post_id ): array {
63 $cache_validity = new Cache_Validity();
64
65 if ( $cache_validity->is_valid( [ self::CACHE_ROOT_KEY, $post_id ] ) ) {
66 return (array) $cache_validity->get_meta( [ self::CACHE_ROOT_KEY, $post_id ] );
67 }
68
69 $components = $this->get_components_from_post( $post_id );
70 $component_ids = Collection::make( $components )
71 ->filter( fn( $component ) => isset( $component['settings']['component_instance']['value']['component_id']['value'] ) )
72 ->map( fn( $component ) => $component['settings']['component_instance']['value']['component_id']['value'] )
73 ->unique()
74 ->all();
75
76 $cache_validity->validate( [ self::CACHE_ROOT_KEY, $post_id ], $component_ids );
77
78 return $component_ids;
79 }
80
81 private function declare_components_rendered( array $post_ids ) {
82 foreach ( $post_ids as $post_id ) {
83 do_action( 'elementor/post/render', $post_id );
84 }
85 }
86
87 private function get_components_from_post( string $post_id ): array {
88 $components = [];
89
90 Utils::traverse_post_elements( $post_id, function( $element_data ) use ( &$components ) {
91 if ( isset( $element_data['widgetType'] ) && 'e-component' === $element_data['widgetType'] ) {
92 $components[] = $element_data;
93 }
94 } );
95
96 return $components;
97 }
98
99 private function invalidate_cache( ?array $post_ids = null ) {
100 $cache_validity = new Cache_Validity();
101
102 if ( empty( $post_ids ) ) {
103 $cache_validity->invalidate( [ self::CACHE_ROOT_KEY ] );
104
105 return;
106 }
107
108 foreach ( $post_ids as $post_id ) {
109 $cache_validity->invalidate( [ self::CACHE_ROOT_KEY, $post_id ] );
110 }
111 }
112 }
113