PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.1
6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / includes / Frontend / Preview / DataHelper.php
kirki / includes / Frontend / Preview Last commit date
templates 3 months ago DataHelper.php 3 days ago ExceptionalElements.php 3 days ago Preview.php 3 days ago Utils.php 3 days ago
DataHelper.php
240 lines
1 <?php
2 /**
3 * Preview scripts data and styles helper
4 *
5 * @package kirki
6 */
7
8 namespace Kirki\Frontend\Preview;
9
10 use Kirki\HelperFunctions;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 /**
17 * Class DataHelper
18 */
19 class DataHelper {
20
21 /**
22 * Temporary IDs store
23 *
24 * @var array
25 */
26 public $temp_ids = array();
27
28 /**
29 * Temporary Data store
30 *
31 * @var array
32 */
33 public $temp_data = array();
34 public $temp_styles = array();
35
36 /**
37 * Generate or retrieve a unique new ID for a given original ID.
38 *
39 * @param int $id Original ID.
40 * @param string $prefix ID prefix.
41 *
42 * @return string Unique new ID.
43 */
44 private function get_unique_new_id( $id, $prefix = 'kirki-s' ) {
45 if ( isset( $this->temp_ids[ $id ] ) ) {
46 return $this->temp_ids[ $id ];
47 }
48
49 $new_id = $prefix . '-' . uniqid();
50 $this->temp_ids[ $id ] = $new_id;
51
52 return $new_id;
53 }
54
55 /**
56 * Recursively update IDs and build a new data structure.
57 *
58 * @param array $data Source data array.
59 * @param string $root Root element ID.
60 * @param string $symbol_id Optional symbol ID for class prefixing.
61 */
62 public function rec_update_data_id_to_new_id( $data, $styles, $root, $symbol_id = null ) {
63 if ( ! isset( $data[ $root ] ) ) {
64 return;
65 }
66
67 $prefix = $symbol_id ? 'kirki-s' . $symbol_id : null;
68 $new_id = $this->get_unique_new_id( $root );
69 $element = $data[ $root ];
70
71 if($element['name'] === 'collection' || $element['name'] === 'slider'){
72 $element['original_id'] = $element['id'];
73 }
74 $element['id'] = $new_id;
75
76 if ( isset( $element['styleIds'] ) && ! empty( $element['styleIds'] ) ) {
77 foreach ( $element['styleIds'] as $style_id ) {
78 if ( isset( $styles[ $style_id ] ) ) {
79 $this->temp_styles[ $style_id ] = $styles[ $style_id ];
80 }
81 }
82 }
83 // Update parent ID
84 if ( $element['parentId'] !== null ) {
85 $element['parentId'] = $this->get_unique_new_id( $element['parentId'] );
86 }
87
88 // Update children
89 if ( ! empty( $element['children'] ) ) {
90 $children = array();
91 foreach ( $element['children'] as $child_id ) {
92 $child_new_id = $this->get_unique_new_id( $child_id );
93 $children[] = $child_new_id;
94 $this->rec_update_data_id_to_new_id( $data, $styles, $child_id, $symbol_id );
95 }
96 $element['children'] = $children;
97 }
98
99 // Update content elements
100 if ( isset( $element['properties']['contents'] ) && is_array( $element['properties']['contents'] ) ) {
101 $contents = array();
102 foreach ( $element['properties']['contents'] as $content ) {
103 if ( is_array( $content ) && isset( $content['id'] ) ) {
104 $content_id = $this->get_unique_new_id( $content['id'] );
105 $contents[] = array( 'id' => $content_id );
106 $this->rec_update_data_id_to_new_id( $data, $styles, $content['id'], $symbol_id );
107 } else {
108 $contents[] = $content;
109 }
110 }
111 $element['properties']['contents'] = $contents;
112 }
113
114 // Update interaction class names and element IDs
115 if ( isset( $element['properties']['interactions'] ) ) {
116 $this->update_interactions( $element['properties']['interactions'], $prefix, $styles );
117 }
118 if ( isset( $element['properties']['interactionLibrary'] ) ) {
119 $element['properties']['interactionLibrary'] = $this->update_interaction_library( $element['properties']['interactionLibrary'], $prefix, $data );
120 }
121
122 $this->temp_data[ $element['id'] ] = $element;
123 }
124
125 /**
126 * Helper function to update interaction class names and element IDs.
127 *
128 * @param array &$interactions Interaction properties reference.
129 * @param string $prefix Optional prefix for class names.
130 */
131 private function update_interactions( &$interactions, $prefix, $styles ) {
132 // TODO: we need to set $this->styles if available in interaction panel.
133 foreach ( $interactions as $key => &$value ) {
134 if ( $key === 'deviceAndClassList' && isset( $value['classList'] ) && $prefix ) {
135 $value['classList'] = HelperFunctions::add_prefix_to_class_name( $prefix, $value['classList'] );
136 } elseif ( $key === 'elementAsTrigger' ) {
137 foreach ( $value as &$event ) {
138 foreach ( $event as &$preset_or_custom ) {
139 foreach ( $preset_or_custom as &$action ) {
140 $new_data = array();
141
142 foreach ( $action['data'] as $ele_id => $animation ) {
143 if ( strpos( $ele_id, '____info' ) !== false ) {
144 continue;
145 }
146
147 $new_id = $this->get_unique_new_id( $ele_id );
148 $new_data[ $new_id ] = $animation;
149
150 // Copy associated info and update classList if needed
151 $info_key = $ele_id . '____info';
152 if ( isset( $action['data'][ $info_key ] ) ) {
153 $info = $action['data'][ $info_key ];
154 if ( isset( $info['classList'] ) && $prefix ) {
155 $info['classList'] = HelperFunctions::add_prefix_to_class_name( $prefix, $info['classList'] );
156 }
157 $new_data[ $new_id . '____info' ] = $info;
158 }
159 }
160
161 $action['data'] = $new_data;
162 }
163 }
164 }
165 }
166 }
167 }
168
169
170 private function update_interaction_library( $interactionLibrary, $prefix, $data ) {
171
172 foreach ( $interactionLibrary as $library_item_index => $library_item ) {
173
174 if ( ! isset( $library_item['data'] ) || ! is_array( $library_item['data'] ) ) {
175 continue;
176 }
177
178 foreach ( $library_item['data'] as $viewport => $viewport_data ) {
179 if ( ! isset( $viewport_data['animation-target']['targets'] ) || ! is_array( $viewport_data['animation-target']['targets'] ) ) {
180 continue;
181 }
182
183 foreach ( $viewport_data['animation-target']['targets'] as $target_index => $target ) {
184 if ( empty( $target['targetId'] ) || ! isset( $data[ $target['targetId'] ] ) ) {
185 continue;
186 }
187
188 $interactionLibrary[ $library_item_index ]['data'][ $viewport ]['animation-target']['targets'][ $target_index ]['targetId'] = $this->get_unique_new_id( $target['targetId'] );
189 }
190 }
191 }
192
193 return $interactionLibrary;
194 }
195
196 /**
197 * Recursively collect data and styles starting from a root block.
198 *
199 * @param string $root_id Root block ID.
200 * @param array &$data_n_styles Output container for blocks and styles.
201 * @param array $data Full block data set.
202 * @param array $styles Full styles data set.
203 */
204 public static function get_data_and_styles_from_root( $root_id, &$data_n_styles, $data = array(), $styles = array() ) {
205 if ( ! isset( $data[ $root_id ] ) ) {
206 return;
207 }
208
209 $block = $data[ $root_id ];
210 $data_n_styles['blocks'][ $root_id ] = $block;
211
212 // Add styles for this block
213 if ( ! empty( $block['styleIds'] ) ) {
214 foreach ( $block['styleIds'] as $style_id ) {
215 if ( isset( $styles[ $style_id ] ) ) {
216 $data_n_styles['styles'][ $style_id ] = $styles[ $style_id ];
217 }
218 }
219 }
220
221 // Traverse contents for text-based blocks
222 if ( isset( $block['name'] ) && in_array( $block['name'], array( 'paragraph', 'heading', 'text' ), true ) ) {
223 if ( ! empty( $block['properties']['contents'] ) && is_array( $block['properties']['contents'] ) ) {
224 foreach ( $block['properties']['contents'] as $content ) {
225 if ( is_array( $content ) && isset( $content['id'] ) ) {
226 self::get_data_and_styles_from_root( $content['id'], $data_n_styles, $data, $styles );
227 }
228 }
229 }
230 }
231
232 // Traverse children
233 if ( ! empty( $block['children'] ) ) {
234 foreach ( $block['children'] as $child_id ) {
235 self::get_data_and_styles_from_root( $child_id, $data_n_styles, $data, $styles );
236 }
237 }
238 }
239 }
240