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