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 / Ajax / DynamicContent.php
kirki / includes / Ajax Last commit date
Collaboration 1 week ago Apps.php 1 month ago Collection.php 1 month ago Comments.php 3 months ago DynamicContent.php 2 days ago ExportImport.php 2 days ago Form.php 1 month ago Media.php 2 weeks ago Page.php 1 week ago PageSettings.php 1 month ago RBAC.php 1 month ago Symbol.php 1 month ago Taxonomy.php 3 months ago TemplateExportImport.php 3 months ago UserData.php 1 month ago Users.php 3 months ago Walkthrough.php 1 week ago WordpressData.php 3 months ago WpAdmin.php 2 weeks ago
DynamicContent.php
988 lines
1 <?php
2
3 /**
4 * DynamicContent manager API
5 *
6 * @package kirki
7 */
8
9 namespace Kirki\Ajax;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 use Kirki\API\ContentManager\ContentManagerHelper;
16 use Kirki\Frontend\Preview\Utils;
17 use Kirki\HelperFunctions;
18
19 use function PHPSTORM_META\map;
20
21 /**
22 * DynamicContent API Class
23 */
24 class DynamicContent {
25
26
27 /**
28 * Get Dynamic element data
29 *
30 * @return void wpjson response
31 */
32 /**
33 * Get Dynamic element data
34 *
35 * @return void wpjson response
36 */
37 public static function get_dynamic_element_data() { //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
38 $content_info = HelperFunctions::sanitize_text( isset( $_GET['contentInfo'] ) ? $_GET['contentInfo'] : null );
39 $content_info = json_decode( stripslashes( $content_info ), true );
40 $content = self::resolve_dynamic_element_data( $content_info );
41 wp_send_json( $content );
42 }
43
44 /**
45 * Resolve dynamic element data for a single content info.
46 *
47 * @param array $content_info The content info array.
48 * @return mixed The resolved content.
49 */
50 private static function resolve_dynamic_element_data( $content_info ) {
51 $content = apply_filters( 'kirki_dynamic_content', false, $content_info );
52 if ( $content !== false ) {
53 // Keep the editor preview in step with the frontend: plain-text
54 // dynamic values (comment fields) are escaped, never live markup.
55 return Utils::esc_dynamic_text_value(
56 $content,
57 isset( $content_info['dynamicContent'] ) ? $content_info['dynamicContent'] : array()
58 );
59 }
60
61 $dynamic_content = isset( $content_info['dynamicContent'] ) ? $content_info['dynamicContent'] : array();
62 $content_type = isset( $dynamic_content['type'] ) ? $dynamic_content['type'] : 'post';
63 $cm_field_type = isset( $dynamic_content['cmFieldType'] ) ? $dynamic_content['cmFieldType'] : '';
64 $time_format = isset( $dynamic_content['timeFormat'] ) ? $dynamic_content['timeFormat'] : 'h:i a';
65 $date_format = isset( $dynamic_content['format'] ) ? $dynamic_content['format'] : 'MM-DD-YYYY';
66 $content_value = isset( $dynamic_content['value'] ) ? $dynamic_content['value'] : 'post_title';
67 $meta_name = isset( $dynamic_content['meta'] ) ? $dynamic_content['meta'] : '';
68 $post_id = (int) HelperFunctions::sanitize_text( isset( $content_info['post_id'] ) ? $content_info['post_id'] : null );
69
70 // Post excerpt length for kirki editor
71 if ( ! empty( $content_value ) && $content_value === 'post_excerpt' && isset( $dynamic_content['postExcerptLength'] ) ) {
72 $GLOBALS['kirki_post_excerpt_length'] = (int) $dynamic_content['postExcerptLength'];
73 }
74
75 switch ( $content_type ) {
76 case 'post': {
77 $post = null;
78 if ( ! empty( $post_id ) ) {
79 $post = get_post( $post_id );
80 } elseif ( isset( $content_info['collectionItem'], $content_info['collectionItem']['ID'] ) ) {
81 $post = get_post( $content_info['collectionItem']['ID'] );
82 }
83 $dynamic_options = array();
84 if ( $cm_field_type === 'time' || $content_value === 'post_time' ) {
85 $dynamic_options['timeFormat'] = $time_format;
86 }
87 if ( $cm_field_type === 'date' || $content_value === 'post_date' ) {
88 $dynamic_options['format'] = $date_format;
89 }
90 return HelperFunctions::get_post_dynamic_content( $content_value, $post, $meta_name, $dynamic_options );
91 }
92
93 case 'author': {
94 $post = null;
95 if ( ! empty( $post_id ) ) {
96 $post = get_post( $post_id );
97 } elseif ( isset( $content_info['collectionItem'], $content_info['collectionItem']['ID'] ) ) {
98 $post = get_post( $content_info['collectionItem']['ID'] );
99 }
100 return HelperFunctions::get_post_dynamic_content( $content_value, $post );
101 }
102
103 case 'user': {
104 $user_id = ! empty( $post_id ) ? $post_id : get_current_user_id();
105 if ( isset( $content_info['collectionItem'], $content_info['collectionItem']['ID'] ) ) {
106 $user_id = $content_info['collectionItem']['ID'];
107 }
108 $dynamic_options = array();
109 if ( $content_value === 'registered_date' ) {
110 $dynamic_options['format'] = $date_format;
111 }
112 return HelperFunctions::get_user_dynamic_content( $content_value, $user_id, $meta_name, $dynamic_options );
113 }
114
115 case 'site': {
116 return HelperFunctions::get_post_dynamic_content( $content_value );
117 }
118
119 case 'term': {
120 $term_id = $post_id;
121 return HelperFunctions::get_term_dynamic_content( $content_value, $term_id, $meta_name );
122 }
123
124 default: {
125 return false;
126 }
127 }
128 }
129
130 /**
131 * Get batched dynamic element data
132 *
133 * @return void wpjson response
134 */
135 public static function get_dynamic_element_data_batch() { //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
136 $items = HelperFunctions::sanitize_text( isset( $_POST['items'] ) ? $_POST['items'] : null );
137 $items = json_decode( stripslashes( $items ), true );
138
139 if ( ! is_array( $items ) ) {
140 wp_send_json( array() );
141 }
142
143 $results = array();
144
145 foreach ( $items as $item ) {
146 $id = isset( $item['id'] ) ? $item['id'] : '';
147 $content_info = isset( $item['payload'] ) ? $item['payload'] : array();
148
149 // Isolate excerpt-length global so it does not leak across batch items
150 $old_excerpt_length = isset( $GLOBALS['kirki_post_excerpt_length'] ) ? $GLOBALS['kirki_post_excerpt_length'] : null;
151
152 $data = self::resolve_dynamic_element_data( $content_info );
153
154 if ( null !== $old_excerpt_length ) {
155 $GLOBALS['kirki_post_excerpt_length'] = $old_excerpt_length;
156 } else {
157 unset( $GLOBALS['kirki_post_excerpt_length'] );
158 }
159
160 $results[] = array(
161 'id' => $id,
162 'data' => $data,
163 );
164 }
165
166 wp_send_json( $results );
167 }
168
169
170 public static function get_default_condition_data( $post ) {
171 $post_fields = array(
172 array(
173 'value' => 'post_id',
174 'title' => 'Post ID',
175 'operand' => array(
176 'type' => 'search',
177 'item' => 'post',
178 ),
179 'operator_type' => 'dropdown_operators',
180 ),
181 array(
182 'value' => 'post_title',
183 'title' => 'Post Title',
184 'operand' => null,
185 'operator_type' => 'text_operators',
186 ),
187 array(
188 'value' => 'post_author',
189 'title' => 'Post Author',
190 'operand' => array(
191 'type' => 'search',
192 'item' => 'user',
193 ),
194 'operator_type' => 'dropdown_operators',
195 ),
196 array(
197 'value' => 'post_date',
198 'title' => 'Post Date',
199 'operand' => array( 'type' => 'date' ),
200 'operator_type' => 'date_operators',
201 ),
202 array(
203 'value' => 'post_time',
204 'title' => 'Post Time',
205 'operand' => array( 'type' => 'time' ),
206 'operator_type' => 'text_operators',
207 ),
208 );
209
210 $user_fields = array(
211 array(
212 'value' => 'display_name',
213 'title' => 'Display Name',
214 'operand' => null,
215 'operator_type' => 'text_operators',
216 ),
217 array(
218 'value' => 'user_email',
219 'title' => 'User Email',
220 'operand' => null,
221 'operator_type' => 'text_operators',
222 ),
223 array(
224 'value' => 'user_nicename',
225 'title' => 'User Nice Name',
226 'operand' => null,
227 'operator_type' => 'text_operators',
228 ),
229 array(
230 'value' => 'registered_date',
231 'title' => 'Registered Date',
232 'operand' => array( 'type' => 'date' ),
233 'operator_type' => 'date_operators',
234 ),
235 array(
236 'value' => 'registered_time',
237 'title' => 'Registered Time',
238 'operand' => array( 'type' => 'time' ),
239 'operator_type' => 'text_operators',
240 ),
241 );
242
243 // Add post fields
244 foreach ( $post_fields as $field ) {
245 $post['post'][ $field['value'] ] = array(
246 'title' => $field['title'],
247 'operator_type' => $field['operator_type'],
248 'operand' => $field['operand'],
249 );
250 }
251
252 // Add user fields
253 foreach ( $user_fields as $field ) {
254 $post['user'][ $field['value'] ] = array(
255 'title' => $field['title'],
256 'operator_type' => $field['operator_type'],
257 'operand' => $field['operand'],
258 );
259 }
260
261 return $post;
262 }
263
264
265 public static function get_visibility_condition_fields() {
266 $conditions = array();
267 $collection_data = HelperFunctions::sanitize_text( isset( $_GET['collectionData'] ) ? $_GET['collectionData'] : false );
268 if ( $collection_data ) {
269 $collection_data = json_decode( $collection_data, true );
270 }
271
272 $type = $collection_data['type'];
273 $collection_type = $collection_data['collectionType'];
274
275 $roles = array_map(
276 fn( $key, $value) => array(
277 'value' => $key,
278 'title' => $value,
279 ),
280 array_keys( wp_roles()->role_names ),
281 wp_roles()->role_names
282 );
283 $roles[] = array(
284 'value' => 'logged_in',
285 'title' => 'Logged In',
286 );
287 $roles[] = array(
288 'value' => 'guest',
289 'title' => 'Guest',
290 );
291
292 $conditions['user'] = array(
293 'title' => 'User',
294 'fields' => array(
295 array(
296 'value' => 'display_name',
297 'title' => 'Display Name',
298 'operator_type' => 'text_operators',
299 ),
300 array(
301 'value' => 'user_login',
302 'title' => 'Username',
303 'operator_type' => 'text_operators',
304 ),
305 array(
306 'value' => 'user_nicename',
307 'title' => 'Nice Name',
308 'operator_type' => 'text_operators',
309 ),
310 array(
311 'value' => 'user_email',
312 'title' => 'Email',
313 'operator_type' => 'text_operators',
314 ),
315 array(
316 'value' => 'user_registered',
317 'title' => 'Registered Date',
318 'operator_type' => 'date_operators',
319 'operand_type' => KIRKI_PLUGIN_SETTINGS['DATEPICKER'],
320 ),
321 array(
322 'value' => 'role',
323 'title' => 'Role',
324 'operator_type' => 'list_operators',
325 'operand_type' => array_merge(
326 KIRKI_PLUGIN_SETTINGS['SELECT'],
327 array(
328 'options' => $roles,
329 )
330 ),
331 ),
332 ),
333 );
334
335 if ( $collection_type === 'posts' ) {
336 $conditions['post'] = array(
337 'title' => 'Post',
338 'fields' => array(),
339 );
340 $conditions['post']['fields'] = array(
341 array(
342 'value' => 'post_id',
343 'title' => 'ID',
344 'operator_type' => 'dropdown_operators',
345 ),
346 array(
347 'value' => 'post_title',
348 'title' => 'Title',
349 'operator_type' => 'text_operators',
350 ),
351 array(
352 'value' => 'post_author',
353 'title' => 'Author',
354 'operator_type' => 'dropdown_operators',
355 'operand_type' => array_merge(
356 KIRKI_PLUGIN_SETTINGS['SELECT'],
357 array(
358 'options' => array_map(
359 fn( $a) => array(
360 'value' => $a->data->ID,
361 'title' => $a->data->display_name,
362 ),
363 get_users()
364 ),
365 )
366 ),
367 ),
368 array(
369 'value' => 'post_date',
370 'title' => 'Date',
371 'operator_type' => 'date_operators',
372 'operand_type' => KIRKI_PLUGIN_SETTINGS['DATEPICKER'],
373 ),
374 );
375 }
376 $conditions = apply_filters( 'kirki_visibility_condition_fields', $conditions, $collection_data );
377
378 wp_send_json( $conditions );
379 }
380
381
382
383 public static function getCustomFields() {
384 return array(
385 array(
386 'type' => 'text',
387 'dynamicContentType' => 'content',
388 ),
389 array(
390 'type' => 'rich-text',
391 'dynamicContentType' => 'content',
392 ),
393 array(
394 'type' => 'image',
395 'dynamicContentType' => 'image',
396 ),
397 array(
398 'type' => 'video',
399 'dynamicContentType' => 'video',
400 ),
401 array(
402 'type' => 'email',
403 'dynamicContentType' => array( 'content', 'anchor' ),
404 ),
405 array(
406 'type' => 'phone',
407 'dynamicContentType' => array( 'content', 'anchor' ),
408 ),
409 array(
410 'type' => 'number',
411 'dynamicContentType' => 'content',
412 ),
413 array(
414 'type' => 'date',
415 'dynamicContentType' => 'content',
416 ),
417 array(
418 'type' => 'time',
419 'dynamicContentType' => 'content',
420 ),
421 array( 'type' => 'switch' ), // no dynamicContentType
422 array(
423 'type' => 'option',
424 'dynamicContentType' => array( 'content', 'anchor' ),
425 ),
426 array(
427 'type' => 'url',
428 'dynamicContentType' => array( 'content', 'anchor' ),
429 ),
430 array(
431 'type' => 'file',
432 'dynamicContentType' => array( 'anchor' ),
433 ),
434 array( 'type' => 'reference' ), // no dynamicContentType
435 array( 'type' => 'multi-reference' ), // no dynamicContentType
436 array(
437 'type' => 'gallery',
438 'dynamicContentType' => 'gallery',
439 ),
440 );
441 }
442
443 public static function sortContentManagerCustomFieldsByDynamicContentType() {
444 $custom_fields = self::getCustomFields();
445 $sorted_data = array(
446 'content' => array(),
447 'image' => array(),
448 'video' => array(),
449 'anchor' => array(),
450 'gallery' => array(),
451 );
452
453 foreach ( $custom_fields as $field ) {
454 if ( ! isset( $field['dynamicContentType'] ) ) {
455 continue;
456 }
457
458 if ( is_array( $field['dynamicContentType'] ) ) {
459 foreach ( $field['dynamicContentType'] as $type ) {
460 if ( array_key_exists( $type, $sorted_data ) ) {
461 $sorted_data[ $type ][] = $field['type'];
462 }
463 }
464 } elseif ( is_string( $field['dynamicContentType'] ) ) {
465 $type = $field['dynamicContentType'];
466 if ( array_key_exists( $type, $sorted_data ) ) {
467 $sorted_data[ $type ][] = $field['type'];
468 }
469 }
470 }
471
472 return $sorted_data;
473 }
474
475
476
477 public static function convertKirkiCMSFieldsToSelectOptions( $type, $fields ) {
478 if ( ! is_array( $fields ) ) {
479 return array();
480 }
481
482 $sorted_cms_fields = self::sortContentManagerCustomFieldsByDynamicContentType();
483
484 if ( ! isset( $sorted_cms_fields[ $type ] ) ) {
485 return array();
486 }
487
488 $current_type_cms_fields = $sorted_cms_fields[ $type ];
489
490 $options = array();
491
492 foreach ( $fields as $field ) {
493 if ( in_array( $field['type'], $current_type_cms_fields ) ) {
494 $options[] = array(
495 'title' => isset( $field['title'] ) ? $field['title'] : '',
496 'value' => isset( $field['id'] ) ? $field['id'] : '',
497 'type' => isset( $field['type'] ) ? $field['type'] : '',
498 );
499 }
500 }
501
502 return $options;
503 }
504
505
506 public static function get_default_dynamic_content_fields() {
507 return array(
508 'type' => array(
509 'content' => array(
510 'id' => 'content',
511 ),
512 'image' => array(
513 'id' => 'image',
514 ),
515 'video' => array(
516 'id' => 'video',
517 ),
518 'anchor' => array(
519 'id' => 'anchor',
520 ),
521 ),
522 'typeValues' => array(
523 'content' => array(
524 array(
525 'value' => 'manual',
526 'title' => 'Manual',
527 ),
528 array(
529 'value' => 'post',
530 'title' => 'Post',
531 ),
532 array(
533 'value' => 'term',
534 'title' => 'Term',
535 ),
536 array(
537 'value' => 'comment',
538 'title' => 'Comment',
539 ),
540 array(
541 'value' => 'user',
542 'title' => 'User',
543 ),
544 array(
545 'value' => 'site',
546 'title' => 'Site',
547 ),
548 array(
549 'value' => 'others',
550 'title' => 'Others',
551 ),
552 ),
553 'image' => array(
554 array(
555 'value' => 'manual',
556 'title' => 'Manual',
557 ),
558 array(
559 'value' => 'post',
560 'title' => 'Post',
561 ),
562 array(
563 'value' => 'site',
564 'title' => 'Site',
565 ),
566 array(
567 'value' => 'author',
568 'title' => 'Author',
569 ),
570 array(
571 'value' => 'user',
572 'title' => 'User',
573 ),
574 ),
575 'video' => array(
576 array(
577 'value' => 'manual',
578 'title' => 'Manual',
579 ),
580 array(
581 'value' => 'post',
582 'title' => 'Post',
583 ),
584 ),
585 'anchor' => array(
586 array(
587 'value' => 'manual',
588 'title' => 'Manual',
589 ),
590 array(
591 'value' => 'post',
592 'title' => 'Post',
593 ),
594 array(
595 'value' => 'term',
596 'title' => 'Term',
597 ),
598 array(
599 'value' => 'author',
600 'title' => 'Author',
601 ),
602 array(
603 'value' => 'user',
604 'title' => 'User',
605 ),
606 ),
607 ),
608 'typeValuesAttr' => array(
609 'content' => array(
610 'manual' => array(
611 array(
612 'value' => 'none',
613 'title' => 'None',
614 ),
615 ),
616 'post' => array(
617 array(
618 'value' => 'post_id',
619 'title' => 'ID',
620 ),
621 array(
622 'value' => 'post_title',
623 'title' => 'Title',
624 ),
625 array(
626 'value' => 'post_author',
627 'title' => 'Author',
628 ),
629 array(
630 'value' => 'post_date',
631 'title' => 'Date',
632 ),
633 array(
634 'value' => 'post_time',
635 'title' => 'Time',
636 ),
637 array(
638 'value' => 'post_content',
639 'title' => 'Content',
640 ),
641 array(
642 'value' => 'post_excerpt',
643 'title' => 'Excerpt',
644 ),
645 array(
646 'value' => 'post_meta',
647 'title' => 'Meta',
648 ),
649 ),
650 'comment' => array(
651 array(
652 'value' => 'comment_id',
653 'title' => 'ID',
654 ),
655 array(
656 'value' => 'parent_id',
657 'title' => 'Parent ID',
658 ),
659 array(
660 'value' => 'post_id',
661 'title' => 'Post ID',
662 ),
663 array(
664 'value' => 'comment_author',
665 'title' => 'Author',
666 ),
667 array(
668 'value' => 'comment_author_email',
669 'title' => 'Author email',
670 ),
671 array(
672 'value' => 'comment_date',
673 'title' => 'Date',
674 ),
675 array(
676 'value' => 'comment_time',
677 'title' => 'Time',
678 ),
679 array(
680 'value' => 'comment_content',
681 'title' => 'Content',
682 ),
683 array(
684 'value' => 'comment_karma',
685 'title' => 'Karma',
686 ),
687 ),
688 'term' => array(
689 array(
690 'value' => 'name',
691 'title' => 'Name',
692 ),
693 array(
694 'value' => 'slug',
695 'title' => 'Slug',
696 ),
697 array(
698 'value' => 'description',
699 'title' => 'Description',
700 ),
701 array(
702 'value' => 'count',
703 'title' => 'Count',
704 ),
705 ),
706 'page' => array(
707 array(
708 'value' => 'none',
709 'title' => 'None',
710 ),
711 ),
712 'user' => array(
713 array(
714 'value' => 'none',
715 'title' => 'None',
716 ),
717 array(
718 'value' => 'display_name',
719 'title' => 'Display Name',
720 ),
721 array(
722 'value' => 'user_email',
723 'title' => 'Email',
724 ),
725 array(
726 'value' => 'user_nicename',
727 'title' => 'Nice Name',
728 ),
729 array(
730 'value' => 'registered_date',
731 'title' => 'Registered Date',
732 ),
733 array(
734 'value' => 'registered_time',
735 'title' => 'Registered Time',
736 ),
737 array(
738 'value' => 'initials',
739 'title' => 'Initials',
740 ),
741 array(
742 'value' => 'user_meta',
743 'title' => 'Meta',
744 ),
745 ),
746 'site' => array(
747 array(
748 'value' => 'site_name',
749 'title' => 'Site Name',
750 ),
751 array(
752 'value' => 'site_description',
753 'title' => 'Site Description',
754 ),
755 array(
756 'value' => 'site_url',
757 'title' => 'Site URL',
758 ),
759 ),
760 'others' => array(
761 array(
762 'value' => 'none',
763 'title' => 'None',
764 ),
765 array(
766 'value' => 'item_index',
767 'title' => 'Item Index',
768 ),
769 ),
770 ),
771 'image' => array(
772 'manual' => array(
773 array(
774 'value' => 'none',
775 'title' => 'None',
776 ),
777 ),
778 'post' => array(
779 array(
780 'value' => 'none',
781 'title' => 'None',
782 ),
783 array(
784 'value' => 'featured_image',
785 'title' => 'Featured Image',
786 ),
787 array(
788 'value' => 'post_meta',
789 'title' => 'Post Meta',
790 ),
791 ),
792 'site' => array(
793 array(
794 'value' => 'none',
795 'title' => 'None',
796 ),
797 array(
798 'value' => 'site_logo',
799 'title' => 'Site Logo',
800 ),
801 ),
802 'author' => array(
803 array(
804 'value' => 'none',
805 'title' => 'None',
806 ),
807 array(
808 'value' => 'author_profile_picture',
809 'title' => 'Author Profile Picture',
810 ),
811 ),
812 'user' => array(
813 array(
814 'value' => 'none',
815 'title' => 'None',
816 ),
817 array(
818 'value' => 'profile_image',
819 'title' => 'Profile Image',
820 ),
821 ),
822 ),
823 'video' => array(
824 'manual' => array(
825 array(
826 'value' => 'none',
827 'title' => 'None',
828 ),
829 ),
830 'post' => array(
831 array(
832 'value' => 'none',
833 'title' => 'None',
834 ),
835 ),
836 ),
837 'anchor' => array(
838 'manual' => array(
839 array(
840 'value' => 'none',
841 'title' => 'None',
842 ),
843 ),
844 'post' => array(
845 array(
846 'value' => 'none',
847 'title' => 'None',
848 ),
849 array(
850 'value' => 'post_page_link',
851 'title' => 'Post link',
852 ),
853 ),
854 'term' => array(
855 array(
856 'value' => 'link',
857 'title' => 'Link',
858 ),
859 ),
860 'author' => array(
861 array(
862 'value' => 'none',
863 'title' => 'None',
864 ),
865 array(
866 'value' => 'author_posts_page_link',
867 'title' => 'Author Posts',
868 ),
869 ),
870 'user' => array(
871 array(
872 'value' => 'none',
873 'title' => 'None',
874 ),
875 array(
876 'value' => 'user_url',
877 'title' => 'User URL',
878 ),
879 ),
880 ),
881 ),
882 );
883 }
884
885 public static function get_dynamic_content_fields() {
886 $data = self::get_default_dynamic_content_fields();
887
888 $collection_data = HelperFunctions::sanitize_text( isset( $_GET['collectionData'] ) ? $_GET['collectionData'] : false );
889 if ( $collection_data ) {
890 $collection_data = json_decode( $collection_data, true );
891 }
892 $type = $collection_data['type'] ?? '';
893 $collection_type = $collection_data['collectionType'] ?? '';
894 $element_content_type = $collection_data['elementContentType'] ?? '';
895
896 if ( ! empty( $element_content_type ) && isset( $data['type'][ $element_content_type ] ) ) {
897 $clean_data = array();
898 $clean_data['type'][ $element_content_type ] = $data['type'][ $element_content_type ];
899 $clean_data['typeValues'][ $element_content_type ] = $data['typeValues'][ $element_content_type ] ?? array();
900 $clean_data['typeValuesAttr'][ $element_content_type ] = $data['typeValuesAttr'][ $element_content_type ] ?? array();
901 $data = $clean_data;
902 }
903
904 if ( $collection_type === 'posts' && ! empty( $type ) && str_contains( $type, KIRKI_CONTENT_MANAGER_PREFIX ) ) {
905 $post_parent = str_replace( KIRKI_CONTENT_MANAGER_PREFIX . '_', '', $type );
906 $post = ContentManagerHelper::get_post_type( $post_parent, true );
907
908 if ( $post ) {
909 $default_post_fields = $data['typeValuesAttr'][ $element_content_type ]['post'] ?? array();
910 $post_fields = $post['fields'] ?? array();
911
912 if ( ! empty( $post_fields ) && is_array( $post_fields ) && ! empty( array_filter( $post_fields, fn( $obj ) => isset( $obj['type'] ) && $obj['type'] === 'reference' ) ) ) {
913 $current_type_values = $data['typeValues'][ $element_content_type ] ?? array();
914 $data['typeValues'][ $element_content_type ] = array_merge(
915 is_array( $current_type_values ) ? $current_type_values : array(),
916 array(
917 array(
918 'value' => 'reference',
919 'title' => 'Reference',
920 ),
921 )
922 );
923
924 $data['availableRefNames'] = array_reduce(
925 array_filter( $post_fields, fn( $obj ) => isset( $obj['type'] ) && $obj['type'] === 'reference' ),
926 function ( $carry, $obj ) {
927 $carry[] = array(
928 'value' => $obj['id'] ?? '',
929 'title' => $obj['title'] ?? '',
930 'post_id' => $obj['ref_collection'] ?? '',
931 );
932 return $carry;
933 },
934 array()
935 );
936
937 $data['typeValuesAttr'][ $element_content_type ]['reference'] = array_reduce(
938 array_filter( $post_fields, fn( $obj ) => isset( $obj['type'] ) && $obj['type'] === 'reference' ),
939 function ( $carry, $obj ) use ( $default_post_fields, $element_content_type ) {
940 $ref_fields = $obj['fields'][0]['fields'] ?? array();
941 $carry[ $obj['id'] ] = array_merge(
942 $default_post_fields,
943 self::convertKirkiCMSFieldsToSelectOptions( $element_content_type, $ref_fields )
944 );
945 return $carry;
946 },
947 array()
948 );
949 }
950
951 if ( ! empty( $data['typeValues'][ $element_content_type ] ) && is_array( $data['typeValues'][ $element_content_type ] ) ) {
952 $data['typeValues'][ $element_content_type ] = array_map(
953 fn( $obj ) => ( isset( $obj['value'] ) && $obj['value'] === 'post' ) ? array(
954 'value' => $obj['value'],
955 'title' => $post['post_title'] ?? 'Post',
956 ) : array(
957 'value' => $obj['value'] ?? '',
958 'title' => $obj['title'] ?? '',
959 ),
960 $data['typeValues'][ $element_content_type ]
961 );
962 }
963
964 $data['cmPostTypeTitle'] = $post['post_title'] ?? '';
965
966 $cm_fields = self::convertKirkiCMSFieldsToSelectOptions( $element_content_type, $post_fields );
967
968 if ( isset( $post['basic_fields']['post_title']['title'] ) ) {
969 array_unshift(
970 $cm_fields,
971 array(
972 'title' => $post['basic_fields']['post_title']['title'],
973 'value' => 'post_title',
974 'type' => 'text',
975 )
976 );
977 }
978
979 $data['typeValuesAttr'][ $element_content_type ]['post'] = $cm_fields;
980 $data['typeValuesAttr'][ $element_content_type ]['default_post'] = $default_post_fields;
981 }
982 }
983
984 $data = apply_filters( 'kirki_dynamic_content_fields', $data, $collection_data );
985 wp_send_json( $data );
986 }
987 }
988