PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev3
Elementor Website Builder – more than just a page builder v4.0.0-dev3
4.3.1 4.3.0 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 All 454 releases
← All changes | modules/global-classes/atomic-global-styles.php +29 -128 4.1.0-beta2 → 4.0.0-dev3 View file →
@@ -7,150 +7,58 @@
7 7
8 8 class Atomic_Global_Styles {
9 9 const STYLES_KEY = 'global';
10 10
11 - private Global_Classes_Relations $relations;
12 -
13 - public function __construct( Global_Classes_Relations $relations ) {
14 - $this->relations = $relations;
15 - }
16 -
17 11 public function register_hooks() {
18 12 add_action(
19 13 'elementor/atomic-widgets/styles/register',
20 - fn( Atomic_Styles_Manager $styles_manager, array $post_ids ) => $this->register_styles( $styles_manager, $post_ids ),
14 + fn( Atomic_Styles_Manager $styles_manager ) => $this->register_styles( $styles_manager ),
21 15 20,
22 16 2
23 17 );
24 18
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 - );
19 + add_action( 'elementor/global_classes/update', fn( string $context ) => $this->invalidate_cache( $context ), 10, 1 );
31 20
32 21 add_action(
33 22 'deleted_post',
34 - fn( $post_id ) => $this->on_kit_delete( $post_id )
23 + fn( $post_id ) => $this->on_post_delete( $post_id )
35 24 );
36 25
37 26 add_action(
38 27 'elementor/core/files/clear_cache',
39 - fn() => $this->invalidate_all_cache(),
28 + fn() => $this->invalidate_cache(),
40 29 );
41 30
42 - add_filter(
43 - 'elementor/atomic-widgets/settings/transformers/classes',
31 + add_filter('elementor/atomic-widgets/settings/transformers/classes',
44 32 fn( $value ) => $this->transform_classes_names( $value )
45 33 );
46 34 }
47 35
48 - private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) {
49 - $context = $this->get_context();
36 + private function register_styles( Atomic_Styles_Manager $styles_manager ) {
37 + $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND;
50 38
51 - foreach ( $post_ids as $post_id ) {
52 - $get_styles = fn() => $this->get_document_global_styles( $post_id, $context );
39 + $get_styles = function () use ( $context ) {
40 + return Global_Classes_Repository::make()->context( $context )->all()->get_ordered_items()->map( function( $item ) {
41 + $item['id'] = $item['label'];
42 + return $item;
43 + })->reverse()->all(); // we should reverse the order of the items so that the last in the original array should be rendered first (to be overridden by the previous ones)
44 + };
53 45
54 - $styles_manager->register(
55 - [ self::STYLES_KEY, $post_id, $context ],
56 - $get_styles
57 - );
58 - }
46 + $styles_manager->register(
47 + [ self::STYLES_KEY, $context ],
48 + $get_styles,
49 + );
59 50 }
60 51
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 ) {
52 + private function on_post_delete( $post_id ) {
102 53 if ( ! Plugin::$instance->kits_manager->is_kit( $post_id ) ) {
103 54 return;
104 55 }
105 56
106 - $this->invalidate_all_cache();
57 + $this->invalidate_cache();
107 58 }
108 59
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 );
112 -
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 ) {
60 + private function invalidate_cache( ?string $context = null ) {
153 61 if ( empty( $context ) || Global_Classes_Repository::CONTEXT_FRONTEND === $context ) {
154 62 do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] );
155 63
156 64 return;
@@ -159,28 +67,21 @@
159 67 do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $context ] );
160 68 }
161 69
162 70 private function transform_classes_names( $ids ) {
163 - $context = $this->get_context();
71 + $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND;
164 72
165 - $repository = Global_Classes_Repository::make();
73 + $classes = Global_Classes_Repository::make()
74 + ->context( $context )
75 + ->all()
76 + ->get_items();
166 77
167 - if ( Global_Classes_Repository::CONTEXT_PREVIEW === $context ) {
168 - $repository->set_preview( true );
169 - }
78 + return array_map(
79 + function( $id ) use ( $classes ) {
80 + $class = $classes->get( $id );
170 81
171 - $labels = $repository->all_labels();
172 -
173 - return array_map(
174 - static function( $id ) use ( $labels ) {
175 - return $labels[ $id ] ?? $id;
82 + return $class ? $class['label'] : $id;
176 83 },
177 84 $ids
178 85 );
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 86 }
186 87 }