PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
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 2 months ago DataHelper.php 2 months ago ExceptionalElements.php 1 month ago Preview.php 4 weeks ago Utils.php 1 month ago
DataHelper.php
206 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
115 $this->temp_data[ $element['id'] ] = $element;
116 }
117
118 /**
119 * Helper function to update interaction class names and element IDs.
120 *
121 * @param array &$interactions Interaction properties reference.
122 * @param string $prefix Optional prefix for class names.
123 */
124 private function update_interactions( &$interactions, $prefix, $styles ) {
125 // TODO: we need to set $this->styles if available in interaction panel.
126 foreach ( $interactions as $key => &$value ) {
127 if ( $key === 'deviceAndClassList' && isset( $value['classList'] ) && $prefix ) {
128 $value['classList'] = HelperFunctions::add_prefix_to_class_name( $prefix, $value['classList'] );
129 } elseif ( $key === 'elementAsTrigger' ) {
130 foreach ( $value as &$event ) {
131 foreach ( $event as &$preset_or_custom ) {
132 foreach ( $preset_or_custom as &$action ) {
133 $new_data = array();
134
135 foreach ( $action['data'] as $ele_id => $animation ) {
136 if ( strpos( $ele_id, '____info' ) !== false ) {
137 continue;
138 }
139
140 $new_id = $this->get_unique_new_id( $ele_id );
141 $new_data[ $new_id ] = $animation;
142
143 // Copy associated info and update classList if needed
144 $info_key = $ele_id . '____info';
145 if ( isset( $action['data'][ $info_key ] ) ) {
146 $info = $action['data'][ $info_key ];
147 if ( isset( $info['classList'] ) && $prefix ) {
148 $info['classList'] = HelperFunctions::add_prefix_to_class_name( $prefix, $info['classList'] );
149 }
150 $new_data[ $new_id . '____info' ] = $info;
151 }
152 }
153
154 $action['data'] = $new_data;
155 }
156 }
157 }
158 }
159 }
160 }
161
162 /**
163 * Recursively collect data and styles starting from a root block.
164 *
165 * @param string $root_id Root block ID.
166 * @param array &$data_n_styles Output container for blocks and styles.
167 * @param array $data Full block data set.
168 * @param array $styles Full styles data set.
169 */
170 public static function get_data_and_styles_from_root( $root_id, &$data_n_styles, $data = array(), $styles = array() ) {
171 if ( ! isset( $data[ $root_id ] ) ) {
172 return;
173 }
174
175 $block = $data[ $root_id ];
176 $data_n_styles['blocks'][ $root_id ] = $block;
177
178 // Add styles for this block
179 if ( ! empty( $block['styleIds'] ) ) {
180 foreach ( $block['styleIds'] as $style_id ) {
181 if ( isset( $styles[ $style_id ] ) ) {
182 $data_n_styles['styles'][ $style_id ] = $styles[ $style_id ];
183 }
184 }
185 }
186
187 // Traverse contents for text-based blocks
188 if ( isset( $block['name'] ) && in_array( $block['name'], array( 'paragraph', 'heading', 'text' ), true ) ) {
189 if ( ! empty( $block['properties']['contents'] ) && is_array( $block['properties']['contents'] ) ) {
190 foreach ( $block['properties']['contents'] as $content ) {
191 if ( is_array( $content ) && isset( $content['id'] ) ) {
192 self::get_data_and_styles_from_root( $content['id'], $data_n_styles, $data, $styles );
193 }
194 }
195 }
196 }
197
198 // Traverse children
199 if ( ! empty( $block['children'] ) ) {
200 foreach ( $block['children'] as $child_id ) {
201 self::get_data_and_styles_from_root( $child_id, $data_n_styles, $data, $styles );
202 }
203 }
204 }
205 }
206