PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev3
Elementor Website Builder – more than just a page builder v3.32.0-dev3
4.3.0-beta3 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 All 452 releases
← All changes | modules/global-classes/atomic-global-styles.php +34 -129 4.1.0-dev23.32.0-dev3 View file →
@@ -3,184 +3,89 @@
3 3 namespace Elementor\Modules\GlobalClasses;
4 4
5 5 use Elementor\Plugin;
6 6 use Elementor\Modules\AtomicWidgets\Styles\Atomic_Styles_Manager;
7 +use Elementor\Modules\AtomicWidgets\Cache_Validity;
7 8
8 9 class Atomic_Global_Styles {
9 10 const STYLES_KEY = 'global';
10 11
11 - private Global_Classes_Relations $relations;
12 -
13 - public function __construct( Global_Classes_Relations $relations ) {
14 - $this->relations = $relations;
15 - }
16 -
17 12 public function register_hooks() {
18 13 add_action(
19 14 'elementor/atomic-widgets/styles/register',
20 - fn( Atomic_Styles_Manager $styles_manager, array $post_ids ) => $this->register_styles( $styles_manager, $post_ids ),
15 + fn( Atomic_Styles_Manager $styles_manager, array $post_ids ) => $this->register_styles( $styles_manager ),
21 16 20,
22 17 2
23 18 );
24 19
25 - add_action(
26 - 'elementor/global_classes/update',
27 - fn( string $context, array $changes ) => $this->invalidate_cache_for_updated_classes( $context, $changes ),
28 - 10,
29 - 2
30 - );
20 + add_action( 'elementor/global_classes/update', fn( string $context ) => $this->invalidate_cache( $context ), 10, 1 );
31 21
32 22 add_action(
33 23 'deleted_post',
34 - fn( $post_id ) => $this->on_kit_delete( $post_id )
24 + fn( $post_id ) => $this->on_post_delete( $post_id )
35 25 );
36 26
37 27 add_action(
38 28 'elementor/core/files/clear_cache',
39 - fn() => $this->invalidate_all_cache(),
29 + fn() => $this->invalidate_cache(),
40 30 );
41 31
42 - add_filter(
43 - 'elementor/atomic-widgets/settings/transformers/classes',
32 + add_filter('elementor/atomic-widgets/settings/transformers/classes',
44 33 fn( $value ) => $this->transform_classes_names( $value )
45 34 );
46 35 }
47 36
48 - private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) {
49 - $context = $this->get_context();
37 + private function register_styles( Atomic_Styles_Manager $styles_manager ) {
38 + $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND;
50 39
51 - foreach ( $post_ids as $post_id ) {
52 - $get_styles = fn() => $this->get_document_global_styles( $post_id, $context );
40 + $get_styles = function () use ( $context ) {
41 + return Global_Classes_Repository::make()->context( $context )->all()->get_items()->map( function( $item ) {
42 + $item['id'] = $item['label'];
43 + return $item;
44 + })->all();
45 + };
53 46
54 - $styles_manager->register(
55 - [ self::STYLES_KEY, $post_id, $context ],
56 - $get_styles
57 - );
58 - }
47 + $styles_manager->register(
48 + self::STYLES_KEY . '-' . $context,
49 + $get_styles,
50 + [ self::STYLES_KEY, $context ]
51 + );
59 52 }
60 53
61 - private function get_document_global_styles( int $post_id, string $context ): array {
62 - $is_preview = Global_Classes_Repository::CONTEXT_PREVIEW === $context;
63 - $class_ids = $this->relations->set_preview( $is_preview )->get_styles_by_post( $post_id );
64 -
65 - if ( empty( $class_ids ) ) {
66 - return [];
67 - }
68 -
69 - $repository = Global_Classes_Repository::make();
70 -
71 - if ( $is_preview ) {
72 - $repository->set_preview( true );
73 - }
74 -
75 - $global_order = $repository->all_labels();
76 - $ordered_class_ids = array_values( array_intersect( array_keys( $global_order ), $class_ids ) );
77 -
78 - if ( empty( $ordered_class_ids ) ) {
79 - return [];
80 - }
81 -
82 - $items = $repository->get_by_ids( $ordered_class_ids );
83 - $reversed_order = array_reverse( $ordered_class_ids );
84 -
85 - $styles = [];
86 -
87 - foreach ( $reversed_order as $class_id ) {
88 - $item = $items[ $class_id ] ?? null;
89 -
90 - if ( ! $item ) {
91 - continue;
92 - }
93 -
94 - $item['id'] = $item['label'];
95 - $styles[] = $item;
96 - }
97 -
98 - return $styles;
99 - }
100 -
101 - private function on_kit_delete( $post_id ) {
54 + private function on_post_delete( $post_id ) {
102 55 if ( ! Plugin::$instance->kits_manager->is_kit( $post_id ) ) {
103 56 return;
104 57 }
105 58
106 - $this->invalidate_all_cache();
59 + $this->invalidate_cache();
107 60 }
108 61
109 - private function invalidate_cache_for_updated_classes( string $context, array $changes ) {
110 - if ( isset( $changes['order'] ) && $changes['order'] ) {
111 - $this->invalidate_all_cache( $context );
62 + private function invalidate_cache( ?string $context = null ) {
63 + $cache_validity = new Cache_Validity();
112 64
113 - return;
114 - }
115 -
116 - $affected = array_unique( array_merge(
117 - $changes['added'] ?? [],
118 - $changes['deleted'] ?? [],
119 - $changes['modified'] ?? []
120 - ) );
121 -
122 - if ( empty( $affected ) ) {
123 - return;
124 - }
125 -
126 - $document_ids = [];
127 - $is_preview = Global_Classes_Repository::CONTEXT_PREVIEW === $context;
128 -
129 - foreach ( $affected as $class_id ) {
130 - foreach ( $this->relations->set_preview( $is_preview )->get_posts_by_style( $class_id ) as $doc_id ) {
131 - $document_ids[ $doc_id ] = true;
132 - }
133 - }
134 -
135 - if ( empty( $document_ids ) ) {
136 - return;
137 - }
138 -
139 - foreach ( array_keys( $document_ids ) as $post_id ) {
140 - $this->invalidate_document_cache( $post_id, $context );
141 - }
142 - }
143 -
144 - private function invalidate_document_cache( int $post_id, ?string $context = null ) {
145 - if ( empty( $context ) ) {
146 - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $post_id ] );
147 - } else {
148 - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $post_id, $context ] );
149 - }
150 - }
151 -
152 - private function invalidate_all_cache( ?string $context = null ) {
153 65 if ( empty( $context ) || Global_Classes_Repository::CONTEXT_FRONTEND === $context ) {
154 - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] );
66 + $cache_validity->invalidate( [ self::STYLES_KEY ] );
155 67
156 68 return;
157 69 }
158 70
159 - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $context ] );
71 + $cache_validity->invalidate( [ self::STYLES_KEY, $context ] );
160 72 }
161 73
162 74 private function transform_classes_names( $ids ) {
163 - $context = $this->get_context();
75 + $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND;
164 76
165 - $repository = Global_Classes_Repository::make();
77 + $classes = Global_Classes_Repository::make()
78 + ->context( $context )
79 + ->all()
80 + ->get_items();
166 81
167 - if ( Global_Classes_Repository::CONTEXT_PREVIEW === $context ) {
168 - $repository->set_preview( true );
169 - }
82 + return array_map(
83 + function( $id ) use ( $classes ) {
84 + $class = $classes->get( $id );
170 85
171 - $labels = $repository->all_labels();
172 -
173 - return array_map(
174 - static function( $id ) use ( $labels ) {
175 - return $labels[ $id ] ?? $id;
86 + return $class ? $class['label'] : $id;
176 87 },
177 88 $ids
178 89 );
179 - }
180 -
181 - private function get_context(): string {
182 - return Plugin::$instance->preview->is_editor_or_preview()
183 - ? Global_Classes_Repository::CONTEXT_PREVIEW
184 - : Global_Classes_Repository::CONTEXT_FRONTEND;
185 90 }
186 91 }