PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta2
Elementor Website Builder – more than just a page builder v4.3.0-beta2
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.3.0-beta2, at modules/components/styles/component-styles.php

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