PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
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 4 days ago ExceptionalElements.php 4 days ago Preview.php 4 days ago Utils.php 1 month ago
DataHelper.php
236 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 $element['id'] = $new_id;
71
72 if ( isset( $element['styleIds'] ) && ! empty( $element['styleIds'] ) ) {
73 foreach ( $element['styleIds'] as $style_id ) {
74 if ( isset( $styles[ $style_id ] ) ) {
75 $this->temp_styles[ $style_id ] = $styles[ $style_id ];
76 }
77 }
78 }
79 // Update parent ID
80 if ( $element['parentId'] !== null ) {
81 $element['parentId'] = $this->get_unique_new_id( $element['parentId'] );
82 }
83
84 // Update children
85 if ( ! empty( $element['children'] ) ) {
86 $children = array();
87 foreach ( $element['children'] as $child_id ) {
88 $child_new_id = $this->get_unique_new_id( $child_id );
89 $children[] = $child_new_id;
90 $this->rec_update_data_id_to_new_id( $data, $styles, $child_id, $symbol_id );
91 }
92 $element['children'] = $children;
93 }
94
95 // Update content elements
96 if ( isset( $element['properties']['contents'] ) && is_array( $element['properties']['contents'] ) ) {
97 $contents = array();
98 foreach ( $element['properties']['contents'] as $content ) {
99 if ( is_array( $content ) && isset( $content['id'] ) ) {
100 $content_id = $this->get_unique_new_id( $content['id'] );
101 $contents[] = array( 'id' => $content_id );
102 $this->rec_update_data_id_to_new_id( $data, $styles, $content['id'], $symbol_id );
103 } else {
104 $contents[] = $content;
105 }
106 }
107 $element['properties']['contents'] = $contents;
108 }
109
110 // Update interaction class names and element IDs
111 if ( isset( $element['properties']['interactions'] ) ) {
112 $this->update_interactions( $element['properties']['interactions'], $prefix, $styles );
113 }
114 if ( isset( $element['properties']['interactionLibrary'] ) ) {
115 $element['properties']['interactionLibrary'] = $this->update_interaction_library( $element['properties']['interactionLibrary'], $prefix, $data );
116 }
117
118 $this->temp_data[ $element['id'] ] = $element;
119 }
120
121 /**
122 * Helper function to update interaction class names and element IDs.
123 *
124 * @param array &$interactions Interaction properties reference.
125 * @param string $prefix Optional prefix for class names.
126 */
127 private function update_interactions( &$interactions, $prefix, $styles ) {
128 // TODO: we need to set $this->styles if available in interaction panel.
129 foreach ( $interactions as $key => &$value ) {
130 if ( $key === 'deviceAndClassList' && isset( $value['classList'] ) && $prefix ) {
131 $value['classList'] = HelperFunctions::add_prefix_to_class_name( $prefix, $value['classList'] );
132 } elseif ( $key === 'elementAsTrigger' ) {
133 foreach ( $value as &$event ) {
134 foreach ( $event as &$preset_or_custom ) {
135 foreach ( $preset_or_custom as &$action ) {
136 $new_data = array();
137
138 foreach ( $action['data'] as $ele_id => $animation ) {
139 if ( strpos( $ele_id, '____info' ) !== false ) {
140 continue;
141 }
142
143 $new_id = $this->get_unique_new_id( $ele_id );
144 $new_data[ $new_id ] = $animation;
145
146 // Copy associated info and update classList if needed
147 $info_key = $ele_id . '____info';
148 if ( isset( $action['data'][ $info_key ] ) ) {
149 $info = $action['data'][ $info_key ];
150 if ( isset( $info['classList'] ) && $prefix ) {
151 $info['classList'] = HelperFunctions::add_prefix_to_class_name( $prefix, $info['classList'] );
152 }
153 $new_data[ $new_id . '____info' ] = $info;
154 }
155 }
156
157 $action['data'] = $new_data;
158 }
159 }
160 }
161 }
162 }
163 }
164
165
166 private function update_interaction_library( $interactionLibrary, $prefix, $data ) {
167
168 foreach ( $interactionLibrary as $library_item_index => $library_item ) {
169
170 if ( ! isset( $library_item['data'] ) || ! is_array( $library_item['data'] ) ) {
171 continue;
172 }
173
174 foreach ( $library_item['data'] as $viewport => $viewport_data ) {
175 if ( ! isset( $viewport_data['animation-target']['targets'] ) || ! is_array( $viewport_data['animation-target']['targets'] ) ) {
176 continue;
177 }
178
179 foreach ( $viewport_data['animation-target']['targets'] as $target_index => $target ) {
180 if ( empty( $target['targetId'] ) || ! isset( $data[ $target['targetId'] ] ) ) {
181 continue;
182 }
183
184 $interactionLibrary[ $library_item_index ]['data'][ $viewport ]['animation-target']['targets'][ $target_index ]['targetId'] = $this->get_unique_new_id( $target['targetId'] );
185 }
186 }
187 }
188
189 return $interactionLibrary;
190 }
191
192 /**
193 * Recursively collect data and styles starting from a root block.
194 *
195 * @param string $root_id Root block ID.
196 * @param array &$data_n_styles Output container for blocks and styles.
197 * @param array $data Full block data set.
198 * @param array $styles Full styles data set.
199 */
200 public static function get_data_and_styles_from_root( $root_id, &$data_n_styles, $data = array(), $styles = array() ) {
201 if ( ! isset( $data[ $root_id ] ) ) {
202 return;
203 }
204
205 $block = $data[ $root_id ];
206 $data_n_styles['blocks'][ $root_id ] = $block;
207
208 // Add styles for this block
209 if ( ! empty( $block['styleIds'] ) ) {
210 foreach ( $block['styleIds'] as $style_id ) {
211 if ( isset( $styles[ $style_id ] ) ) {
212 $data_n_styles['styles'][ $style_id ] = $styles[ $style_id ];
213 }
214 }
215 }
216
217 // Traverse contents for text-based blocks
218 if ( isset( $block['name'] ) && in_array( $block['name'], array( 'paragraph', 'heading', 'text' ), true ) ) {
219 if ( ! empty( $block['properties']['contents'] ) && is_array( $block['properties']['contents'] ) ) {
220 foreach ( $block['properties']['contents'] as $content ) {
221 if ( is_array( $content ) && isset( $content['id'] ) ) {
222 self::get_data_and_styles_from_root( $content['id'], $data_n_styles, $data, $styles );
223 }
224 }
225 }
226 }
227
228 // Traverse children
229 if ( ! empty( $block['children'] ) ) {
230 foreach ( $block['children'] as $child_id ) {
231 self::get_data_and_styles_from_root( $child_id, $data_n_styles, $data, $styles );
232 }
233 }
234 }
235 }
236