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 / Utils.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
Utils.php
420 lines
1 <?php
2
3 /**
4 * Preivew scripts data and styles helper
5 *
6 * @package kirki
7 */
8
9 namespace Kirki\Frontend\Preview;
10
11 use Kirki\Ajax\Users;
12 use Kirki\API\ContentManager\ContentManagerHelper;
13 use Kirki\HelperFunctions;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * DataHelper Class
21 */
22 class Utils {
23
24 public static function get_items_data_from_dynamic_contents( $dynamic_content, $options ) {
25 $collection_type = $dynamic_content['collectionType'];
26 if ( $collection_type === 'users' ) {
27 return self::get_users_collection( $dynamic_content, $options );
28 } elseif ( $collection_type === 'posts' ) {
29 return self::get_posts_collection( $dynamic_content, $options );
30 } elseif ( $collection_type === 'terms' ) {
31 return self::get_terms_collection( $dynamic_content, $options );
32 } else {
33 $args = self::get_common_args( $dynamic_content, $options );
34 $collection = apply_filters( 'kirki_collection_' . $dynamic_content['collectionType'], false, $args );
35 $data = $collection ? $collection['data'] : array();
36 $pagination = $collection ? $collection['pagination'] : array();
37 $item_type = $collection && isset( $collection['itemType'] ) ? $collection['itemType'] : 'post';
38 return array(
39 'data' => $data,
40 'pagination' => $pagination,
41 'itemType' => $item_type,
42 );
43 }
44 }
45
46 private static function get_posts_collection( $dynamic_content, $options ) {
47 $args = self::get_common_args( $dynamic_content, $options );
48 $collection = HelperFunctions::get_posts( $args );
49 $data = $collection['data'];
50 $pagination = $collection['pagination'];
51 return array(
52 'data' => $data,
53 'pagination' => $pagination,
54 'itemType' => 'post',
55 );
56 }
57 private static function get_terms_collection( $dynamic_content, $options ) {
58 $args = array(
59 'taxonomy' => $dynamic_content['taxonomy'],
60 'hide_empty' => false,
61 'current_page' => isset( $options['page'] ) ? $options['page'] : 1,
62 'item_per_page' => isset( $dynamic_content['items'] ) ? $dynamic_content['items'] : 3,
63 'offset' => isset( $dynamic_content['offset'] ) ? $dynamic_content['offset'] : 0,
64 );
65
66 if ( isset( $dynamic_content['inherit'] ) && $dynamic_content['inherit'] ) {
67 if ( isset( $options['itemType'] ) && $options['itemType'] === 'post' ) {
68 $post_parent = $options['post']->ID;
69 } else {
70 $post_parent = HelperFunctions::get_post_id_if_possible_from_url();
71 }
72 $args['post_parent'] = $post_parent;
73 $args['inherit'] = true;
74 }
75
76 $terms = HelperFunctions::get_terms( $args );
77
78 $data = $terms['data'];
79 $pagination = $terms['pagination'];
80 return array(
81 'data' => $data,
82 'pagination' => $pagination,
83 'itemType' => 'term',
84 );
85 }
86
87 private static function get_users_collection( $dynamic_content, $options ) {
88 $args = array();
89
90 if ( isset( $dynamic_content['sorting'], $dynamic_content['sorting']['type'], $dynamic_content['sorting']['value'] ) ) {
91 $args['sorting']['order'] = $dynamic_content['sorting']['type'];
92 $args['sorting']['orderby'] = $dynamic_content['sorting']['value'];
93 }
94
95 if ( isset( $options['filters'] ) ) {
96 $args['filters'] = $options['filters'];
97 } elseif ( isset( $dynamic_content['filters'] ) ) {
98 $args['filters'] = $dynamic_content['filters'];
99 }
100
101 if ( isset( $dynamic_content['inherit'] ) && $dynamic_content['inherit'] === true ) {
102 $args['inherit'] = true;
103 $args['post_parent'] = $options['post']->ID ?? HelperFunctions::get_post_id_if_possible_from_url();
104 }
105
106 $item_per_page = isset( $dynamic_content['items'] ) ? $dynamic_content['items'] : 3;
107 $offset = isset( $dynamic_content['offset'] ) ? $dynamic_content['offset'] : 0;
108
109 $current_page = 1;
110 if ( isset( $options['page'] ) ) {
111 $current_page = $options['page'];
112 }
113
114 $args['current_page'] = $current_page;
115 $args['item_per_page'] = $item_per_page;
116 $args['offset'] = $offset;
117
118 if ( isset( $options['q'] ) ) {
119 $args['q'] = $options['q'];
120 }
121
122 $users = Users::get_users( $args );
123 $data = $users['data'];
124 $pagination = $users['pagination'];
125
126 return array(
127 'data' => $data,
128 'pagination' => $pagination,
129 'itemType' => 'user',
130 );
131 }
132
133
134 private static function get_common_args( $dynamic_content, $options ) {
135 $args = array();
136 if ( isset( $dynamic_content['sorting'], $dynamic_content['sorting']['type'], $dynamic_content['sorting']['value'] ) ) {
137 $args['sorting']['order'] = $dynamic_content['sorting']['type'];
138 $args['sorting']['orderby'] = $dynamic_content['sorting']['value'];
139 }
140
141 if ( isset( $options['filters'] ) ) {
142 $args['filters'] = $options['filters'];
143 } elseif ( isset( $dynamic_content['filters'] ) ) {
144 $args['filters'] = $dynamic_content['filters'];
145 }
146
147 $search_related_collection_ids = isset( $options['search_related_collection_ids'] ) ? $options['search_related_collection_ids'] : array();
148 $is_searchable_collection_element = isset($search_related_collection_ids[$options['element_id']]);
149
150 if ( ! isset( $dynamic_content['relatedCollection'] ) && $is_searchable_collection_element ) {
151
152 // This code is for taxonomy filter START
153 $filters = array();
154 $taxonomies = array();
155 if ( isset( $_GET['taxonomy'] ) && is_array( $_GET['taxonomy'] ) ) {
156 $taxonomies = $_GET['taxonomy'];
157 } elseif ( isset( $options['collection_param_filters'], $options['collection_param_filters']['taxonomy'] ) ) {
158 // This code came from pagination, filter, search api from frontend
159 $taxonomies = $options['collection_param_filters']['taxonomy'];
160 }
161 foreach ( $taxonomies as $name => $value ) {
162 if ( ! empty( $value ) ) {
163 $filters[] = array(
164 'id' => 'taxonomy',
165 'type' => 'taxonomy',
166 'taxonomy' => $name,
167 'terms' => $value,
168 );
169 }
170 }
171
172 $args['filters'] = array_merge( $args['filters'], $filters );
173 // This code is for taxonomy filter END
174
175 // This code is for post/cm filter START
176 $ref_post_ids = array();
177 if ( isset( $_GET['post'], $_GET['post']['cm_post'] ) && is_array( $_GET['post']['cm_post'] ) ) {
178 $ref_post_ids = array_map( 'intval', $_GET['post']['cm_post'] );
179 } elseif ( isset( $options['collection_param_filters'], $options['collection_param_filters']['post'], $options['collection_param_filters']['post']['cm_post'] ) ) {
180 // This code came from pagination, filter, search api from frontend
181 $ref_post_ids = $options['collection_param_filters']['post']['cm_post'];
182 }
183
184 if ( ! empty( $ref_post_ids ) ) {
185
186 $included_post_ids = ContentManagerHelper::get_post_ids_by_ref_post_ids( $ref_post_ids );
187
188 $args['IDs'] = $included_post_ids;
189 // Make sure all post ids are integers
190 }
191 // This code is for post/cm filter END
192 }
193
194 if ( isset( $dynamic_content['type'] ) ) {
195 $args['name'] = $dynamic_content['type'];
196 }
197
198 if ( isset( $dynamic_content['inherit'] ) && $dynamic_content['inherit'] === true ) {
199 $args['inherit'] = true;
200
201 if ( isset( $options['itemType'], $options[ $options['itemType'] ], $options[ $options['itemType'] ]->ID ) ) {
202 // if itemType is set and it has ID property then use it as post_parent
203 $args['post_parent'] = $options[ $options['itemType'] ]->ID;
204 } else {
205 $args['post_parent'] = $options['post']->ID ?? HelperFunctions::get_post_id_if_possible_from_url();
206 }
207
208 if ( isset( $options['user'], $options['user']['ID'] ) ) {
209 // update $filter data with this user author in property
210
211 $args['context'] = array(
212 'id' => $options['user']['ID'],
213 'collectionType' => 'user',
214 );
215 }
216 if ( isset( $options['term'] ) ) {
217 // update $filter data with this term in property
218 $args['context'] = array(
219 'id' => $options['term']['term_id'],
220 'slug' => $options['term']['slug'],
221 'taxonomy' => $options['term']['taxonomy'],
222 'collectionType' => 'term',
223 );
224 }
225
226 if ( isset( $options['comment'] ) ) {
227 $comment = $options['comment'];
228
229 if ( isset( $comment['id'] ) ) {
230
231 $args['context'] = array(
232 'comment_ID' => $comment['id'], // comment_ID
233 );
234 } elseif ( isset( $comment['comment_ID'] ) ) {
235 $args['context'] = array(
236 'comment_ID' => $comment['comment_ID'], // comment_ID
237 );
238 }
239 }
240
241 // for external plugin support
242 if ( isset( $options['itemType'], $options[ $options['itemType'] ] ) ) {
243 $args['parent_item_type'] = $options['itemType'];
244 $args['parent_item'] = $options[ $options['itemType'] ];
245 }
246 }
247 if ( isset( $dynamic_content['related'] ) && $dynamic_content['related'] === true ) {
248 $args['related'] = true;
249 $args['related_post_parent'] = $options['post']->ID ?? HelperFunctions::get_post_id_if_possible_from_url();
250 }
251
252 if ( ! empty( $dynamic_content['type'] ) && false !== strpos( $dynamic_content['type'], KIRKI_CONTENT_MANAGER_PREFIX ) ) {
253 $parent_id = str_replace( KIRKI_CONTENT_MANAGER_PREFIX . '_', '', $dynamic_content['type'] );
254 $args['post_parent'] = $parent_id;
255 }
256
257 $item_per_page = isset( $dynamic_content['items'] ) ? $dynamic_content['items'] : 3;
258 $offset = isset( $dynamic_content['offset'] ) ? $dynamic_content['offset'] : 0;
259
260 $current_page = 1;
261 if ( isset( $options['page'] ) ) {
262 $current_page = $options['page'];
263 }
264
265 $args['current_page'] = $current_page;
266 $args['item_per_page'] = $item_per_page;
267 $args['offset'] = $offset;
268
269 // if options has query value
270 if ( isset( $options['q'] ) ) {
271 $args['q'] = $options['q'];
272 }
273
274 if(empty($args['q'])){
275 $args['q'] = HelperFunctions::sanitize_text(isset($_REQUEST['q']) ?$_REQUEST['q']:'' );
276 }
277
278 if( (isset($options['inside_collection']) && $options['inside_collection'] === true) || !$is_searchable_collection_element ) {
279 // this is for nested collection like: terms, multiref etc
280 $args['current_page'] = 1;
281 $args['pagination'] = false;
282 $args['q'] = '';
283 }
284
285 return $args;
286 }
287
288 /**
289 * Dynamic content types whose values are plain text by nature and must never
290 * be rendered as live markup.
291 *
292 * Comment fields are the important case: their content is supplied by
293 * (potentially unauthenticated) visitors, so anything stored there is
294 * untrusted. Rich types — `post` (post_content), `reference`, `gallery`,
295 * `menu` — are deliberately excluded, because those are authored by users
296 * with editing capabilities and are expected to emit HTML.
297 *
298 * @return string[]
299 */
300 public static function get_plain_text_dynamic_content_types() {
301 return apply_filters( 'kirki_plain_text_dynamic_content_types', array( 'comment' ) );
302 }
303
304 /**
305 * Escape a resolved dynamic value when its type is plain text by nature.
306 *
307 * The value is decoded first and then escaped, so markup that was stored in
308 * entity-encoded form (`&lt;script&gt;`) is normalised to a single, escaped
309 * representation and displays as the literal text the visitor typed instead
310 * of turning back into a live tag further down the render pipeline.
311 *
312 * @param mixed $content The resolved dynamic value.
313 * @param array $dynamic_content The dynamic content definition.
314 * @return mixed The escaped value, or the value untouched when it is not plain text.
315 */
316 public static function esc_dynamic_text_value( $content, $dynamic_content ) {
317 if ( ! is_string( $content ) || '' === $content ) {
318 return $content;
319 }
320
321 $type = isset( $dynamic_content['type'] ) ? $dynamic_content['type'] : '';
322
323 if ( ! in_array( $type, self::get_plain_text_dynamic_content_types(), true ) ) {
324 return $content;
325 }
326
327 return htmlspecialchars(
328 html_entity_decode( $content, ENT_QUOTES | ENT_HTML5, 'UTF-8' ),
329 ENT_QUOTES,
330 'UTF-8'
331 );
332 }
333
334 public static function getDynamicRichTextValue( $dynamic_content, $options ) {
335 $html = '';
336 if ( ! $dynamic_content ) {
337 return $html;
338 }
339 // Post excerpt length for frontend. If post excerpt is used.
340 if (isset($dynamic_content['value']) && $dynamic_content['value'] === 'post_excerpt' && isset( $dynamic_content['postExcerptLength'] ) ) {
341 $post_excerpt_length = $dynamic_content['postExcerptLength'] ?? 55;
342 $GLOBALS['kirki_post_excerpt_length'] = $post_excerpt_length;
343 }
344
345 $contentInfo = array(
346 'dynamicContent' => $dynamic_content,
347 'options' => $options,
348 );
349
350 if ( isset( $options['itemType'], $options[ $options['itemType'] ], $options[ $options['itemType'] ]->ID ) ) {
351 $contentInfo['collectionItem'] = array(
352 'ID' => $options[ $options['itemType'] ]->ID,
353 );
354 }
355 $content = apply_filters( 'kirki_dynamic_content', false, $contentInfo );
356 if ( $content ) {
357 return self::esc_dynamic_text_value( $content, $dynamic_content );
358 }
359
360 // fix warning
361 if(!isset($dynamic_content['value'])){
362 $dynamic_content['value'] = false;
363 }
364
365 if ( $dynamic_content['type'] === 'post' ) {
366 $dynamic_options = array();
367 $cm_field_type = isset( $dynamic_content['cmFieldType'] ) ? $dynamic_content['cmFieldType'] : '';
368 if ( isset( $dynamic_content['format'] ) && 'post_date' === $dynamic_content['value'] ) {
369 $dynamic_options['format'] = $dynamic_content['format'];
370 } elseif ( ( 'post_time' === $dynamic_content['value'] || 'time' === $cm_field_type ) && isset( $dynamic_content['timeFormat'] ) ) {
371 $dynamic_options['timeFormat'] = $dynamic_content['timeFormat'];
372 }
373
374 $content = HelperFunctions::get_post_dynamic_content(
375 $dynamic_content['value'],
376 $options['post'] ?? null,
377 $dynamic_content['meta'] ?? '',
378 $dynamic_options
379 );
380
381 $html .= $content;
382 } elseif ( $dynamic_content['type'] === 'term' && isset( $options['term'] ) && isset( $options['term'][ $dynamic_content['value'] ] ) ) {
383 $html .= $options['term'][ $dynamic_content['value'] ];
384 } elseif ( $dynamic_content['type'] === 'user' ) {
385 // $html .= $options['user'][ $dynamic_content['value'] ];
386 $content = HelperFunctions::get_user_dynamic_content(
387 $dynamic_content['value'],
388 $options['user']['ID'] ?? null,
389 $dynamic_content['meta'] ?? ''
390 );
391
392 // Date may need to format
393 if ( 'registered_date' === $dynamic_content['value'] && isset( $dynamic_content['format'] ) ) {
394 $content = HelperFunctions::format_date( $content, $dynamic_content['format'] );
395 }
396
397 $html .= $content;
398
399 } elseif ( $dynamic_content['type'] === 'menu' && isset( $options['menu'] ) && isset( $options['menu']->{$dynamic_content['value']} ) ) {
400 $html .= $options['menu']->{$dynamic_content['value']};
401 } elseif ( $dynamic_content['type'] === 'others' && isset( $dynamic_content['value'] ) ) {
402 // Post count is used in collection item index.
403 if ( $dynamic_content['value'] === 'item_index' && isset( $options['item_index'] ) ) {
404 $content = $options['item_index'];
405
406 $html .= $content;
407 }
408 } else {
409 $item_type = $dynamic_content['type'];
410 if ( isset( $options[ $item_type ] ) ) {
411 $data = $options[ $item_type ] ?? array();
412 $value = isset( $data[ $dynamic_content['value'] ] ) ? $data[ $dynamic_content['value'] ] : '';
413 $html .= $value;
414 }
415 }
416
417 return $html;
418 }
419 }
420