← All changes
|
modules/components/styles/component-styles.php
+38
-8
4.0.9
→
4.3.0-beta3
View file →
| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | use Elementor\Core\Base\Document; |
| 6 | 6 | use Elementor\Core\Utils\Collection; |
| 7 | 7 | use Elementor\Modules\AtomicWidgets\Styles\CacheValidity\Cache_Validity; |
| 8 | 8 | use Elementor\Modules\AtomicWidgets\Utils\Utils; |
| 9 | +use Elementor\Modules\Components\PropTypes\Component_Instance_Prop_Type; | |
| 9 | 10 | |
| 10 | 11 | /** |
| 11 | 12 | * Component styles fetching for render |
| 12 | 13 | */ |
| @@ -23,31 +24,60 @@ | ||
| 23 | 24 | add_action( |
| 24 | 25 | 'elementor/core/files/clear_cache', |
| 25 | 26 | fn() => $this->invalidate_cache(), |
| 26 | 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 | + ); | |
| 27 | 35 | } |
| 28 | 36 | |
| 29 | 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 { | |
| 30 | 64 | $cache_validity = new Cache_Validity(); |
| 31 | 65 | |
| 32 | 66 | if ( $cache_validity->is_valid( [ self::CACHE_ROOT_KEY, $post_id ] ) ) { |
| 33 | - $component_ids = $cache_validity->get_meta( [ self::CACHE_ROOT_KEY, $post_id ] ); | |
| 34 | - | |
| 35 | - $this->declare_components_rendered( $component_ids ); | |
| 36 | - | |
| 37 | - return; | |
| 67 | + return (array) $cache_validity->get_meta( [ self::CACHE_ROOT_KEY, $post_id ] ); | |
| 38 | 68 | } |
| 39 | 69 | |
| 40 | 70 | $components = $this->get_components_from_post( $post_id ); |
| 41 | 71 | $component_ids = Collection::make( $components ) |
| 42 | - ->filter( fn( $component ) => isset( $component['settings']['component_instance']['value']['component_id']['value'] ) ) | |
| 43 | - ->map( fn( $component ) => $component['settings']['component_instance']['value']['component_id']['value'] ) | |
| 72 | + ->map( fn( $component ) => Component_Instance_Prop_Type::extract_component_id( $component['settings'] ?? [] ) ) | |
| 73 | + ->filter( fn( $component_id ) => null !== $component_id ) | |
| 44 | 74 | ->unique() |
| 45 | 75 | ->all(); |
| 46 | 76 | |
| 47 | 77 | $cache_validity->validate( [ self::CACHE_ROOT_KEY, $post_id ], $component_ids ); |
| 48 | 78 | |
| 49 | - $this->declare_components_rendered( $component_ids ); | |
| 79 | + return $component_ids; | |
| 50 | 80 | } |
| 51 | 81 | |
| 52 | 82 | private function declare_components_rendered( array $post_ids ) { |
| 53 | 83 | foreach ( $post_ids as $post_id ) { |