PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0
Elementor Website Builder – more than just a page builder v3.35.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 / atomic-widgets / styles / atomic-widget-styles.php

atomic-widget-styles.php in Elementor Website Builder – more than just a page builder 3.35.0, at modules/atomic-widgets/styles/atomic-widget-styles.php

90 lines 2.7 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\AtomicWidgets\Styles;
4
5 use Elementor\Core\Base\Document;
6 use Elementor\Modules\AtomicWidgets\Utils\Utils;
7 use Elementor\Modules\GlobalClasses\Utils\Atomic_Elements_Utils;
8 use Elementor\Plugin;
9
10 class Atomic_Widget_Styles {
11 const STYLES_KEY = 'local';
12 const CONTEXT_FRONTEND = 'frontend';
13 const CONTEXT_PREVIEW = 'preview';
14
15 public function register_hooks() {
16 add_action( 'elementor/atomic-widgets/styles/register', function( Atomic_Styles_Manager $styles_manager, array $post_ids ) {
17 $this->register_styles( $styles_manager, $post_ids );
18 }, 30, 2 );
19
20 add_action( 'elementor/document/after_save', fn( Document $document ) => $this->invalidate_cache(
21 [ $document->get_main_post()->ID ],
22 $this->get_context( ! Utils::is_post_published( $document ) )
23 ), 20, 2 );
24
25 add_action(
26 'elementor/core/files/clear_cache',
27 fn() => $this->invalidate_cache(),
28 );
29
30 add_action(
31 'deleted_post',
32 fn( $post_id ) => $this->invalidate_cache( [ $post_id ] )
33 );
34 }
35
36 private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) {
37 $context = $this->get_context( is_preview() );
38
39 foreach ( $post_ids as $post_id ) {
40 $get_styles = fn() => $this->parse_post_styles( $post_id );
41
42 $styles_manager->register( [ self::STYLES_KEY, $post_id, $context ], $get_styles );
43 }
44 }
45
46 private function parse_post_styles( $post_id ) {
47 $post_styles = [];
48
49 Utils::traverse_post_elements( $post_id, function( $element_data ) use ( &$post_styles ) {
50 $post_styles = array_merge( $post_styles, $this->parse_element_style( $element_data ) );
51 } );
52
53 return $post_styles;
54 }
55
56 private function parse_element_style( array $element_data ) {
57 $element_type = Atomic_Elements_Utils::get_element_type( $element_data );
58 $element_instance = Atomic_Elements_Utils::get_element_instance( $element_type );
59
60 if ( ! Utils::is_atomic( $element_instance ) ) {
61 return [];
62 }
63
64 return $element_data['styles'] ?? [];
65 }
66
67 private function invalidate_cache( ?array $post_ids = null, ?string $context = null ) {
68 if ( empty( $post_ids ) ) {
69 do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] );
70
71 return;
72 }
73
74 $is_post_status_publish = self::CONTEXT_FRONTEND === $context;
75
76 // When a user publishes a post, we should invalidate the styles of the draft too
77 foreach ( $post_ids as $post_id ) {
78 do_action( 'elementor/atomic-widgets/styles/clear',
79 empty( $context ) || $is_post_status_publish
80 ? [ self::STYLES_KEY, $post_id ]
81 : [ self::STYLES_KEY, $post_id, $context ]
82 );
83 }
84 }
85
86 private function get_context( bool $is_preview ) {
87 return $is_preview ? self::CONTEXT_PREVIEW : self::CONTEXT_FRONTEND;
88 }
89 }
90