| 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 |
|
| 29 |
private function render_post( string $post_id ) { |
| 30 |
$cache_validity = new Cache_Validity(); |
| 31 |
|
| 32 |
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; |
| 38 |
} |
| 39 |
|
| 40 |
$components = $this->get_components_from_post( $post_id ); |
| 41 |
$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'] ) |
| 44 |
->unique() |
| 45 |
->all(); |
| 46 |
|
| 47 |
$cache_validity->validate( [ self::CACHE_ROOT_KEY, $post_id ], $component_ids ); |
| 48 |
|
| 49 |
$this->declare_components_rendered( $component_ids ); |
| 50 |
} |
| 51 |
|
| 52 |
private function declare_components_rendered( array $post_ids ) { |
| 53 |
foreach ( $post_ids as $post_id ) { |
| 54 |
do_action( 'elementor/post/render', $post_id ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
private function get_components_from_post( string $post_id ): array { |
| 59 |
$components = []; |
| 60 |
|
| 61 |
Utils::traverse_post_elements( $post_id, function( $element_data ) use ( &$components ) { |
| 62 |
if ( isset( $element_data['widgetType'] ) && 'e-component' === $element_data['widgetType'] ) { |
| 63 |
$components[] = $element_data; |
| 64 |
} |
| 65 |
} ); |
| 66 |
|
| 67 |
return $components; |
| 68 |
} |
| 69 |
|
| 70 |
private function invalidate_cache( ?array $post_ids = null ) { |
| 71 |
$cache_validity = new Cache_Validity(); |
| 72 |
|
| 73 |
if ( empty( $post_ids ) ) { |
| 74 |
$cache_validity->invalidate( [ self::CACHE_ROOT_KEY ] ); |
| 75 |
|
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
foreach ( $post_ids as $post_id ) { |
| 80 |
$cache_validity->invalidate( [ self::CACHE_ROOT_KEY, $post_id ] ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|