PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.1.0
Post Grid Gutenberg Blocks – PostX v5.1.0
5.1.1 5.1.0 5.0.41 5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 All 240 releases
ultimate-post / addons / dynamic_content / includes / DCLegacyFields.php

DCLegacyFields.php in Post Grid Gutenberg Blocks – PostX 5.1.0, at addons/dynamic_content/includes/DCLegacyFields.php

563 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ULTP;
3
4 defined( 'ABSPATH' ) || exit;
5
6 /**
7 * Dynamic Content: ACF, Meta Box and Pods for PostX Pro before 1.9.0
8 *
9 * PostX Pro 1.9.0 and later answer `ultp_dc_custom_field_sources`, `ultp_dc_field_value` and
10 * `ultp_dc_is_field_key` itself. Older Pro does not, so DCController loads this class only when
11 * Xpo::is_pro_before_1_9_0(), to keep those sites as they were. Delete it when 1.9.0 becomes the
12 * supported floor.
13 */
14 class DCLegacyFields {
15
16 const PREFIXES = array( 'acf_', 'mb_', 'pods_' );
17
18 public function __construct() {
19 add_filter( 'ultp_dc_custom_field_sources', array( $this, 'add_sources' ), 10, 2 );
20 add_filter( 'ultp_dc_field_value', array( $this, 'get_field_value' ), 10, 3 );
21 add_filter( 'ultp_dc_is_field_key', array( $this, 'is_field_key' ), 10, 2 );
22 }
23
24 /**
25 * Add the ACF, Meta Box and Pods field lists.
26 *
27 * @param array $sources Sources added so far.
28 * @param array $args `post_id`, `post_type`, `field_type`.
29 * @return array
30 */
31 public function add_sources( $sources, $args = array() ) {
32 if ( ! is_array( $sources ) ) {
33 $sources = array();
34 }
35 $post_id = isset( $args['post_id'] ) ? intval( $args['post_id'] ) : 0;
36 $post_type = isset( $args['post_type'] ) ? $args['post_type'] : '';
37 $field_type = isset( $args['field_type'] ) ? $args['field_type'] : '';
38
39 $sources['acf'] = $this->get_acf_fields( $post_id, $field_type, $post_type );
40 $sources['mb'] = $this->get_mb_fields( $post_id, $field_type, $post_type );
41 $sources['pods'] = $this->get_pods_fields( $post_id, $field_type, $post_type );
42
43 return $sources;
44 }
45
46 /**
47 * Whether a dynamic content key is an ACF, Meta Box or Pods field.
48 *
49 * @param bool $is_field Answer so far.
50 * @param string $key Key with its prefix.
51 * @return bool
52 */
53 public function is_field_key( $is_field, $key = '' ) {
54 if ( $is_field ) {
55 return true;
56 }
57 foreach ( self::PREFIXES as $prefix ) {
58 if ( self::check_prefix( $key, $prefix ) ) {
59 return true;
60 }
61 }
62 return false;
63 }
64
65 /**
66 * Value of an ACF, Meta Box or Pods field.
67 *
68 * @param mixed $value Value so far; `null` when nothing has answered.
69 * @param string $meta_key Key with its prefix.
70 * @param mixed $post_id Post ID, already resolved by PostX (may be empty).
71 * @return mixed `null` when the key is not one of these.
72 */
73 public function get_field_value( $value, $meta_key = '', $post_id = '' ) {
74 if ( null !== $value || ! is_string( $meta_key ) || '' === $meta_key ) {
75 return $value;
76 }
77
78 if ( self::check_prefix_and_remove( $meta_key, 'acf_' ) ) {
79 if ( class_exists( 'ACF' ) ) {
80 if ( ! empty( $post_id ) ) {
81 return self::extract_acf_value( get_field( $meta_key, $post_id ) );
82 } else {
83 return get_field( $meta_key, false );
84 }
85 }
86 return '';
87 }
88
89 if ( self::check_prefix_and_remove( $meta_key, 'mb_' ) ) {
90 if ( function_exists( 'rwmb_get_value' ) ) {
91 $field_value = rwmb_get_value( $meta_key, array(), $post_id );
92 return self::extract_mb_value( $field_value, $meta_key, $post_id );
93 }
94 return '';
95 }
96
97 if ( self::check_prefix_and_remove( $meta_key, 'pods_' ) ) {
98 if ( function_exists( 'pods' ) ) {
99 $pods = pods( get_post_type( $post_id ), $post_id );
100 if ( is_object( $pods ) && method_exists( $pods, 'exists' ) &&
101 $pods->exists() && method_exists( $pods, 'field' )
102 ) {
103 return self::extract_pods_value( $pods->field( $meta_key ) );
104 }
105 }
106 return '';
107 }
108
109 return $value;
110 }
111
112 /**
113 * Get ACF fields
114 *
115 * @param int $post_id
116 * @param string $field_type
117 * @param string $post_type
118 *
119 * @return array
120 */
121 public function get_acf_fields( $post_id, $field_type, $post_type ) {
122 $res = array(
123 'fields' => array(),
124 '_fields' => array(),
125 'prefixes' => array(),
126 );
127
128 if ( ! class_exists( 'ACF' ) ) {
129 return $res;
130 }
131
132 if ( empty( $post_id ) && empty( $post_type ) ) {
133 global $post;
134 $post_id = isset( $post->ID ) ? $post->ID : '';
135 }
136
137 $allowed_types = self::get_allowed_acf_field_types( $field_type );
138
139 // If post_id set set, return ACF fields for that post
140 if ( ! empty( $post_id ) ) {
141 $fields = get_field_objects( $post_id );
142 if ( is_array( $fields ) ) {
143 foreach ( $fields as $field ) {
144 if ( in_array( $field['type'], $allowed_types, true ) ) {
145 $res['fields'][] = array(
146 'value' => $field['name'],
147 'label' => $field['label'],
148 );
149 }
150
151 $res['_fields'][] = $field['name'];
152
153 // For filtering out pesky repeater field's sub fields
154 if ( $field['type'] === 'repeater' ) {
155 $res['prefixes'][] = $field['name'];
156 }
157 }
158 }
159 } elseif ( ! empty( $post_type ) ) {
160 $groups = acf_get_field_groups( array( 'post_type' => $post_type ) );
161 foreach ( $groups as $group ) {
162 $fields = acf_get_fields( $group['ID'] );
163 if ( is_array( $fields ) ) {
164 foreach ( $fields as $field ) {
165 if ( in_array( $field['type'], $allowed_types, true ) ) {
166 $res['fields'][] = array(
167 'value' => $field['name'],
168 'label' => $field['label'] . ' [' . $group['title'] . ']',
169 );
170 }
171 $res['_fields'][] = $field['name'];
172
173 if ( $field['type'] === 'repeater' ) {
174 $res['prefixes'][] = $field['name'];
175 }
176 }
177 }
178 }
179 }
180
181 return $res;
182 }
183
184 /**
185 * Get Meta Box fields
186 *
187 * @param int $post_id
188 * @param string $field_type
189 * @param string $post_type
190 *
191 * @return array
192 */
193 public function get_mb_fields( $post_id, $field_type, $post_type ) {
194 $res = array(
195 'fields' => array(),
196 '_fields' => array(),
197 'prefixes' => array(),
198 );
199
200 if ( ! function_exists( 'rwmb_get_field_settings' ) ) {
201 return $res;
202 }
203
204 $value = null;
205
206 if ( empty( $post_id ) && empty( $post_type ) ) {
207 global $post;
208 $value = isset( $post->ID ) ? $post->ID : '';
209 } elseif ( ! empty( $post_id ) ) {
210 $value = $post_id;
211 } elseif ( ! empty( $post_type ) ) {
212 $value = $post_type;
213 }
214
215 if ( empty( $value ) ) {
216 return $res;
217 }
218
219 $allowed_types = self::get_allowed_mb_field_type( $field_type );
220
221 $fields = rwmb_get_object_fields( $value );
222
223 if ( is_array( $fields ) ) {
224 foreach ( $fields as $field ) {
225 if ( in_array( $field['type'], $allowed_types, true ) ) {
226 $res['fields'][] = array(
227 'value' => $field['id'],
228 'label' => $field['name'],
229 );
230 }
231
232 $res['_fields'][] = $field['id'];
233 }
234 }
235
236 return $res;
237 }
238
239 /**
240 * Get Pods fields
241 *
242 * @param int $post_id
243 * @param string $field_type
244 * @param string $post_type
245 *
246 * @return array
247 */
248 public function get_pods_fields( $post_id, $field_type, $post_type ) {
249 $res = array(
250 'fields' => array(),
251 '_fields' => array(),
252 'prefixes' => array(),
253 );
254
255 if ( ! function_exists( 'pods' ) ) {
256 return $res;
257 }
258
259 if ( empty( $post_id ) ) {
260 global $post;
261 $post_id = isset( $post->ID ) ? $post->ID : '';
262 }
263
264 if ( empty( $post_type ) ) {
265 $post_type = get_post_type( $post_id );
266 }
267
268 if ( empty( $post_type ) ) {
269 return $res;
270 }
271
272 $allowed_types = self::get_allowed_pods_field_types( $field_type );
273
274 $pods = pods( $post_type, $post_id );
275 if ( is_object( $pods ) && method_exists( $pods, 'exists' ) && $pods->exists() && method_exists( $pods, 'fields' ) ) {
276 $fields = $pods->fields();
277 if ( is_array( $fields ) ) {
278 foreach ( $fields as $field ) {
279 if ( in_array( $field['type'], $allowed_types, true ) &&
280 'field' === $field['object_type'] &&
281 'post_type' === $field['object_storage_type']
282 ) {
283 $res['fields'][] = array(
284 'value' => $field['name'],
285 'label' => $field['label'],
286 );
287 }
288
289 $res['_fields'][] = $field['name'];
290 }
291 }
292 }
293
294 return $res;
295 }
296
297 /**
298 * Get ACF allowed field types
299 *
300 * @param string $type optional, possible values 'text'|'image'|'url'.
301 *
302 * @return array
303 */
304 public static function get_allowed_acf_field_types( $type = 'text' ) {
305
306 if ( 'image' === $type ) {
307 return array(
308 'image',
309 );
310 } elseif ( 'url' === $type ) {
311 return array(
312 'text',
313 'email',
314 'image',
315 'file',
316 'page_link',
317 'url',
318 'link',
319 );
320 }
321
322 return array(
323 'text',
324 'textarea',
325 'number',
326 'range',
327 'email',
328 'url',
329 'password',
330 'wysiwyg',
331 'select',
332 'checkbox',
333 'radio',
334 'true_false',
335 'date_picker',
336 'time_picker',
337 'date_time_picker',
338 'color_picker',
339 );
340 }
341
342 /**
343 * Get Meta Box allowed field types
344 *
345 * @param string $type optional, possible values 'text'|'image'|'url'.
346 *
347 * @return array
348 */
349 public static function get_allowed_mb_field_type( $type ) {
350
351 if ( $type === 'image' ) {
352 return array(
353 'image',
354 'image_advanced',
355 'image_upload',
356 'single_image',
357 'url',
358 'file',
359 'file_advanced',
360 'file_input',
361 'file_upload',
362 );
363 } elseif ( $type === 'url' ) {
364 return array(
365 'url',
366 'file',
367 'file_advanced',
368 'file_input',
369 'file_upload',
370 );
371 }
372
373 return array(
374 'text',
375 'email',
376 'number',
377 'textaraa',
378 'select',
379 'radio',
380 'checkbox',
381 'checkbox_list',
382 );
383 }
384
385 /**
386 * Get Pods allowed field types
387 *
388 * @param string $type optional, possible values 'text'|'image'|'url'.
389 *
390 * @return array
391 */
392 public static function get_allowed_pods_field_types( $type = 'text' ) {
393
394 if ( 'image' === $type ) {
395 return array(
396 'images',
397 'file',
398 );
399 } elseif ( 'url' === $type ) {
400 return array(
401 'text',
402 'email',
403 'images',
404 'file',
405 'link',
406 'website',
407 );
408 }
409
410 return array(
411 'text',
412 'paragraph',
413 'password',
414 'phone',
415 'time',
416 'website',
417 'number',
418 'wysiwyg',
419 'email',
420 'link',
421 'boolean',
422 'code',
423 'number',
424 'currency',
425 'date',
426 'datetime',
427 );
428 }
429
430 /**
431 * Formats ACF value based upon data type
432 *
433 * @param array|string $value ACF Meta Value.
434 *
435 * @return string
436 */
437 private static function extract_acf_value( $value ) {
438 if ( is_array( $value ) ) {
439 $image_id = absint( $value['id'] );
440 $value = wp_get_attachment_image_src( $image_id );
441 } elseif ( is_numeric( $value ) ) {
442 return $value;
443 }
444
445 return $value;
446 }
447
448 /**
449 * Formats Pods value based upon data type
450 *
451 * @param array|string $value Pods Meta Value.
452 *
453 * @return string
454 */
455 private static function extract_pods_value( $value ) {
456 if ( is_array( $value ) ) {
457
458 // Single image
459 if ( isset( $value['guid'] ) ) {
460 return $value['guid'];
461 }
462
463 // Multi image
464 if ( isset( $value[0]['guid'] ) ) {
465 return $value[0]['guid'];
466 }
467
468 // Repeater
469 if ( isset( $value[0] ) &&
470 ( is_string( $value[0] ) || is_numeric( $value[0] ) || is_bool( $value[0] ) )
471 ) {
472 return $value[0];
473 }
474
475 return '';
476 }
477
478 return $value;
479 }
480
481 /**
482 * Formats Meta Box value based upon data type
483 *
484 * @param array|string $custom_fields Meta Box value.
485 * @param string $meta_key Field ID.
486 * @param mixed $post_id Post ID.
487 *
488 * @return string
489 */
490 private static function extract_mb_value( $custom_fields, $meta_key, $post_id ) {
491
492 if ( ! is_array( $custom_fields ) ) {
493 return strval( $custom_fields );
494 }
495
496 $field_data = rwmb_get_field_settings( $meta_key, array(), $post_id );
497
498 // Process the first element if its an array
499 foreach ( $custom_fields as $field ) {
500 $field_type = $field_data['type'];
501 $field_value = '';
502
503 switch ( $field_type ) {
504 case 'checkbox':
505 case 'radio':
506 $field_value = isset( $field['checked'] ) ? 'checked' : '';
507 break;
508 case 'select':
509 case 'radio_list':
510 case 'checkbox_list':
511 case 'taxonomy':
512 case 'taxonomy_advanced':
513 case 'post':
514 case 'post_advanced':
515 case 'post_checkbox_list':
516 case 'post_select':
517 $field_value = isset( $field['selected'] ) ? $field['selected'] : '';
518 break;
519 case 'file':
520 case 'file_input':
521 case 'file_advanced':
522 case 'file_upload':
523 $field_value = isset( $field['url'] ) ? $field['url'] : $field['id'];
524 break;
525 case 'single_image':
526 $field_value = wp_get_attachment_image_url( $custom_fields['ID'], 'full' );
527 break;
528 case 'image':
529 case 'image_advanced':
530 case 'image_upload':
531 case 'plupload_image':
532 case 'thickbox_image':
533 $field_value = wp_get_attachment_image_url( $field['ID'], 'full' );
534 break;
535 case 'date':
536 case 'datetime':
537 case 'time':
538 $field_value = $field['date'];
539 break;
540 default:
541 $field_value = $field['value'];
542 break;
543 }
544
545 return $field_value;
546 }
547
548 return '';
549 }
550
551 private static function check_prefix( $key, $prefix ) {
552 return is_string( $key ) && substr( $key, 0, strlen( $prefix ) ) === $prefix;
553 }
554
555 private static function check_prefix_and_remove( &$key, $prefix ) {
556 if ( substr( $key, 0, strlen( $prefix ) ) === $prefix ) {
557 $key = substr( $key, strlen( $prefix ) );
558 return true;
559 }
560 return false;
561 }
562 }
563