PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
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
elementor / modules / global-classes / utils / template-library-global-classes-snapshot-builder.php

template-library-global-classes-snapshot-builder.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/global-classes/utils/template-library-global-classes-snapshot-builder.php

231 lines 5.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\GlobalClasses\Utils;
4
5 use Elementor\Core\Utils\Template_Library_Element_Iterator;
6 use Elementor\Core\Utils\Template_Library_Import_Export_Utils;
7 use Elementor\Core\Utils\Template_Library_Snapshot_Processor;
8 use Elementor\Modules\GlobalClasses\Global_Classes_Parser;
9 use Elementor\Modules\GlobalClasses\Global_Classes_Repository;
10 use Elementor\Modules\GlobalClasses\Global_Classes_Rest_Api;
11 use Elementor\Plugin;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 class Template_Library_Global_Classes_Snapshot_Builder extends Template_Library_Snapshot_Processor {
18
19 private static ?self $instance = null;
20
21 public static function make(): self {
22 if ( null === self::$instance ) {
23 self::$instance = new self();
24 }
25 return self::$instance;
26 }
27
28 public static function extract_used_class_ids_from_elements( array $elements ): array {
29 $ids = [];
30
31 if ( empty( $elements ) ) {
32 return [];
33 }
34
35 Template_Library_Element_Iterator::iterate(
36 $elements,
37 function ( $element_data ) use ( &$ids ) {
38 $class_values = $element_data['settings']['classes']['value'] ?? [];
39
40 if ( is_array( $class_values ) ) {
41 foreach ( $class_values as $class_id ) {
42 if ( is_string( $class_id ) && '' !== $class_id ) {
43 $ids[] = $class_id;
44 }
45 }
46 }
47
48 return $element_data;
49 }
50 );
51
52 return array_values( array_unique( $ids ) );
53 }
54
55 public static function build_snapshot_for_ids( array $ids ): ?array {
56 if ( empty( $ids ) || ! self::make()->can_access_repository() ) {
57 return null;
58 }
59
60 $ids = Template_Library_Import_Export_Utils::normalize_string_ids( $ids );
61
62 if ( empty( $ids ) ) {
63 return null;
64 }
65
66 $repository = Global_Classes_Repository::make()->set_preview( false );
67 $order = $repository->get_order();
68 $filtered_order = array_values( array_filter( $order, fn( $id ) => in_array( $id, $ids, true ) ) );
69 $filtered_items = $repository->get_by_ids( $ids );
70
71 if ( empty( $filtered_items ) ) {
72 return null;
73 }
74
75 return self::parse_snapshot_or_null( [
76 'items' => $filtered_items,
77 'order' => $filtered_order,
78 ] );
79 }
80
81 public static function build_snapshot_for_elements( array $elements ): ?array {
82 $ids = self::extract_used_class_ids_from_elements( $elements );
83
84 if ( empty( $ids ) ) {
85 return null;
86 }
87
88 return self::build_snapshot_for_ids( $ids );
89 }
90
91 public static function merge_snapshot_and_get_id_map( array $snapshot ): array {
92 return self::make()->merge_and_get_id_map( $snapshot );
93 }
94
95 public static function create_snapshot_as_new( array $snapshot ): array {
96 return self::make()->create_all_as_new( $snapshot );
97 }
98
99 protected function is_matching_item( array $existing_item, array $incoming_item ): bool {
100 // For global classes, if the labels match, we consider them the same item
101 // when merging, so we reuse the existing class and ignore incoming variants or extra fields.
102 return true;
103 }
104
105 protected function normalize_for_comparison( array $item ): array {
106 $id = $item['id'] ?? '';
107
108 if ( '' === $id ) {
109 return $item;
110 }
111
112 $parsed = self::parse_snapshot_or_null( [
113 'items' => [ $id => $item ],
114 'order' => [ $id ],
115 ] );
116
117 if ( null !== $parsed && isset( $parsed['items'][ $id ] ) ) {
118 return $parsed['items'][ $id ];
119 }
120
121 return $item;
122 }
123
124 protected function get_item_prefix(): string {
125 return Template_Library_Import_Export_Utils::GLOBAL_CLASS_ID_PREFIX;
126 }
127
128 protected function get_max_items(): int {
129 return Global_Classes_Rest_Api::MAX_ITEMS;
130 }
131
132 protected function can_access_repository(): bool {
133 return class_exists( Global_Classes_Repository::class ) && $this->has_active_kit();
134 }
135
136 protected function load_current_data(): array {
137 $repository = Global_Classes_Repository::make()->set_preview( true );
138 $labels = $repository->all_labels();
139
140 $items = [];
141 foreach ( $labels as $id => $label ) {
142 $items[ $id ] = [
143 'id' => $id,
144 'label' => $label,
145 ];
146 }
147
148 return [
149 'items' => $items,
150 'order' => $repository->get_order(),
151 ];
152 }
153
154 protected function parse_incoming_snapshot( array $snapshot ): ?array {
155 return self::parse_snapshot_or_null( $snapshot );
156 }
157
158 protected function get_incoming_items( array $parsed_snapshot ): array {
159 $items = [];
160 $order = $parsed_snapshot['order'] ?? array_keys( $parsed_snapshot['items'] ?? [] );
161
162 foreach ( $order as $id ) {
163 if ( isset( $parsed_snapshot['items'][ $id ] ) ) {
164 $items[ $id ] = $parsed_snapshot['items'][ $id ];
165 }
166 }
167
168 return $items;
169 }
170
171 protected function count_current_items( array $items ): int {
172 return count( $items );
173 }
174
175 protected function save_data( array $data, array $metadata ): array {
176 $new_items = $data['new_items'] ?? [];
177 $order = $data['order'] ?? [];
178
179 if ( empty( $new_items ) ) {
180 return [
181 'global_classes' => [
182 'added_items' => [],
183 'added_items_order' => [],
184 ],
185 ];
186 }
187
188 $repository = Global_Classes_Repository::make()->set_preview( false );
189 $added_ids = array_keys( $new_items );
190
191 $repository->apply_changes(
192 $new_items,
193 [
194 'added' => $added_ids,
195 'order' => true,
196 ],
197 $order
198 );
199
200 return [
201 'global_classes' => [
202 'added_items_order' => $added_ids,
203 'added_items' => $new_items,
204 ],
205 ];
206 }
207
208 protected function prepare_item_for_save( array $item, string $target_id ): array {
209 $item['id'] = $target_id;
210 return $item;
211 }
212
213 private function has_active_kit(): bool {
214 return (bool) Plugin::instance()->kits_manager->get_active_kit();
215 }
216
217 private static function parse_snapshot_or_null( array $snapshot ): ?array {
218 $snapshot['order'] = Global_Classes_Parser::sanitize_order(
219 $snapshot['items'] ?? [],
220 $snapshot['order'] ?? []
221 );
222
223 $parse_result = Global_Classes_Parser::make()->parse( $snapshot );
224 if ( ! $parse_result->is_valid() ) {
225 return null;
226 }
227
228 return $parse_result->unwrap();
229 }
230 }
231