PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.4
Elementor Website Builder – more than just a page builder v4.1.4
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 / variables / utils / template-library-variables.php

template-library-variables.php in Elementor Website Builder – more than just a page builder 4.1.4, at modules/variables/utils/template-library-variables.php

212 lines 7.2 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\Variables\Utils;
4
5 use Elementor\Core\Utils\Template_Library_Import_Export_Utils;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 class Template_Library_Variables {
12
13 public static function add_variables_snapshot( array $snapshots, $content, $template_id, array $export_data ): array {
14 if ( ! is_array( $content ) ) {
15 return $snapshots;
16 }
17
18 if ( ! empty( $snapshots['global_variables'] ) ) {
19 return $snapshots;
20 }
21
22 $global_classes_snapshot = $snapshots['global_classes'] ?? null;
23 $snapshot = self::build_snapshot_for_elements( $content, $global_classes_snapshot );
24
25 if ( ! empty( $snapshot ) ) {
26 $snapshots['global_variables'] = $snapshot;
27 }
28
29 return $snapshots;
30 }
31
32 public static function extract_variables_from_data( array $snapshots, array $decoded_data, array $data ): array {
33 $snapshot = $decoded_data['global_variables'] ?? null;
34
35 if ( ! empty( $snapshot ) && is_array( $snapshot ) ) {
36 $snapshots['global_variables'] = $snapshot;
37 }
38
39 return $snapshots;
40 }
41
42 public static function process_variables_import( array $result, string $import_mode, array $data ): array {
43 $snapshot = $data['global_variables'] ?? null;
44
45 if ( empty( $snapshot ) || ! is_array( $snapshot ) ) {
46 return $result;
47 }
48
49 $processed = Template_Library_Import_Export_Utils::process_import_by_mode(
50 $import_mode,
51 $result['content'],
52 $snapshot,
53 [ self::class, 'merge_snapshot_and_get_id_map' ],
54 [ self::class, 'create_all_as_new' ],
55 [ self::class, 'rewrite_elements_variable_ids' ],
56 [ self::class, 'flatten_elements_variables' ]
57 );
58
59 $result['content'] = $processed['content'];
60 $result['updated_global_variables'] = $processed['operation_result']['variables'] ?? null;
61 $result['variables_id_map'] = $processed['id_map'];
62 $result['variables_to_flatten'] = $processed['ids_to_flatten'];
63 $result['variables_snapshot'] = $snapshot;
64
65 return $result;
66 }
67
68 private static function build_snapshot_for_elements( array $elements, ?array $global_classes_snapshot = null ): ?array {
69 return Template_Library_Variables_Snapshot_Builder::build_snapshot_for_elements( $elements, $global_classes_snapshot );
70 }
71
72 public static function merge_snapshot_and_get_id_map( array $snapshot ): array {
73 return Template_Library_Variables_Snapshot_Builder::merge_snapshot_and_get_id_map( $snapshot );
74 }
75
76 public static function rewrite_elements_variable_ids( array $elements, array $id_map ): array {
77 return Template_Library_Variables_Element_Transformer::rewrite_elements_variable_ids( $elements, $id_map );
78 }
79
80 public static function flatten_elements_variables( array $elements, array $global_variables, ?array $only_ids = null ): array {
81 return Template_Library_Variables_Element_Transformer::flatten_elements_variables( $elements, $global_variables, $only_ids );
82 }
83
84 public static function create_all_as_new( array $snapshot ): array {
85 return Template_Library_Variables_Snapshot_Builder::create_snapshot_as_new( $snapshot );
86 }
87
88 public static function transform_variables_in_classes_snapshot( array $classes_snapshot, string $import_mode, array $result, array $data ): array {
89 $variables_id_map = $result['variables_id_map'] ?? [];
90 $variables_to_flatten = $result['variables_to_flatten'] ?? [];
91 $variables_snapshot = $result['variables_snapshot'] ?? ( $data['global_variables'] ?? null );
92
93 if ( Template_Library_Import_Export_Utils::IMPORT_MODE_KEEP_FLATTEN === $import_mode && ! empty( $variables_snapshot ) ) {
94 $classes_snapshot = self::flatten_variables_in_classes_snapshot( $classes_snapshot, $variables_snapshot );
95 } else {
96 if ( ! empty( $variables_id_map ) ) {
97 $classes_snapshot = self::rewrite_variable_ids_in_classes_snapshot( $classes_snapshot, $variables_id_map );
98 }
99
100 if ( ! empty( $variables_to_flatten ) && ! empty( $variables_snapshot ) ) {
101 $classes_snapshot = self::flatten_variables_in_classes_snapshot( $classes_snapshot, $variables_snapshot, $variables_to_flatten );
102 }
103 }
104
105 return $classes_snapshot;
106 }
107
108 public static function rewrite_variable_ids_in_classes_snapshot( array $snapshot, array $id_map ): array {
109 if ( empty( $snapshot['items'] ) || empty( $id_map ) ) {
110 return $snapshot;
111 }
112
113 $variable_types = Variable_Type_Keys::get_all();
114
115 foreach ( $snapshot['items'] as $class_id => &$class_item ) {
116 if ( empty( $class_item['variants'] ) || ! is_array( $class_item['variants'] ) ) {
117 continue;
118 }
119
120 foreach ( $class_item['variants'] as &$variant ) {
121 if ( empty( $variant['props'] ) || ! is_array( $variant['props'] ) ) {
122 continue;
123 }
124
125 $variant['props'] = self::rewrite_variable_refs_recursive( $variant['props'], $id_map, $variable_types );
126 }
127 }
128
129 return $snapshot;
130 }
131
132 private static function rewrite_variable_refs_recursive( array $data, array $id_map, array $variable_types ): array {
133 foreach ( $data as $key => $value ) {
134 if ( ! is_array( $value ) ) {
135 continue;
136 }
137
138 $type = $value['$$type'] ?? null;
139 $val = $value['value'] ?? null;
140
141 if ( $type && in_array( $type, $variable_types, true ) && is_string( $val ) && isset( $id_map[ $val ] ) ) {
142 $data[ $key ]['value'] = $id_map[ $val ];
143 continue;
144 }
145
146 $data[ $key ] = self::rewrite_variable_refs_recursive( $value, $id_map, $variable_types );
147 }
148
149 return $data;
150 }
151
152 public static function flatten_variables_in_classes_snapshot( array $classes_snapshot, array $variables_snapshot, ?array $only_ids = null ): array {
153 if ( empty( $classes_snapshot['items'] ) ) {
154 return $classes_snapshot;
155 }
156
157 $variable_data = $variables_snapshot['data'] ?? [];
158 $variable_types = Variable_Type_Keys::get_all();
159 $type_map = Variable_Type_Keys::get_type_mappings();
160
161 $ids_to_flatten = null !== $only_ids ? array_fill_keys( $only_ids, true ) : null;
162
163 foreach ( $classes_snapshot['items'] as $class_id => &$class_item ) {
164 if ( empty( $class_item['variants'] ) || ! is_array( $class_item['variants'] ) ) {
165 continue;
166 }
167
168 foreach ( $class_item['variants'] as &$variant ) {
169 if ( empty( $variant['props'] ) || ! is_array( $variant['props'] ) ) {
170 continue;
171 }
172
173 $variant['props'] = self::flatten_variable_refs_in_props( $variant['props'], $variable_data, $variable_types, $type_map, $ids_to_flatten );
174 }
175 }
176
177 return $classes_snapshot;
178 }
179
180 private static function flatten_variable_refs_in_props( array $data, array $variable_data, array $variable_types, array $type_map, ?array $ids_to_flatten = null ): array {
181 foreach ( $data as $key => $value ) {
182 if ( ! is_array( $value ) ) {
183 continue;
184 }
185
186 $type = $value['$$type'] ?? null;
187 $var_id = $value['value'] ?? null;
188
189 if ( $type && in_array( $type, $variable_types, true ) && is_string( $var_id ) && isset( $variable_data[ $var_id ] ) ) {
190 if ( null !== $ids_to_flatten && ! isset( $ids_to_flatten[ $var_id ] ) ) {
191 continue;
192 }
193
194 $resolved_type = $type_map[ $type ] ?? null;
195 $resolved_value = $variable_data[ $var_id ]['value'] ?? null;
196
197 if ( $resolved_type && null !== $resolved_value ) {
198 $data[ $key ] = [
199 '$$type' => $resolved_type,
200 'value' => Variable_Type_Keys::convert_value_for_resolved_type( $resolved_type, $resolved_value ),
201 ];
202 }
203 continue;
204 }
205
206 $data[ $key ] = self::flatten_variable_refs_in_props( $value, $variable_data, $variable_types, $type_map, $ids_to_flatten );
207 }
208
209 return $data;
210 }
211 }
212