PluginProbe
Smart Custom Fields / trunk
Smart Custom Fields vtrunk
1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.7.0 2.0.0 2.0.1 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 All 68 releases
smart-custom-fields / classes / class.scf.php

class.scf.php in Smart Custom Fields trunk, at classes/class.scf.php

810 lines 24.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package smart-custom-fields
4 * @author inc2734
5 * @license GPL-2.0+
6 */
7
8 /**
9 * SCF class.
10 */
11 class SCF {
12
13 /**
14 * Array of the registered fields ( Smart_Custom_Fields_Field_Base )
15 *
16 * @var array
17 */
18 protected static $fields = array();
19
20 /**
21 * Array of the custom options pages.
22 *
23 * @var array
24 */
25 protected static $options_pages = array();
26
27 /**
28 * Getting all of the post meta data to feel good.
29 *
30 * @param int $post_id Post id.
31 * @return array
32 */
33 public static function gets( $post_id = null ) {
34 if ( is_null( $post_id ) ) {
35 $post_id = get_the_ID();
36 }
37 $post_id = self::get_real_post_id( $post_id );
38
39 if ( empty( $post_id ) ) {
40 return null;
41 }
42
43 // Don't output meta data that not save in the SCF settings page
44 // Getting the settings data, judged to output meta data.
45 return self::get_all_meta( get_post( $post_id ) );
46 }
47
48 /**
49 * Getting the post meta data to feel good.
50 *
51 * @param string $name Group name or field name.
52 * @param int $post_id Post id.
53 * @return mixed
54 */
55 public static function get( $name, $post_id = null ) {
56 if ( is_null( $post_id ) ) {
57 $post_id = get_the_ID();
58 }
59 $post_id = self::get_real_post_id( $post_id );
60
61 if ( empty( $post_id ) ) {
62 return;
63 }
64
65 // Don't output meta data that not save in the SCF settings page.
66 // Getting the settings data, judged to output meta data.
67 return self::get_meta( get_post( $post_id ), $name );
68 }
69
70 /**
71 * Getting the user meta data to feel good.
72 *
73 * @param int $user_id User id.
74 * @param string $name Group name or field name.
75 * @return mixed
76 */
77 public static function get_user_meta( $user_id, $name = null ) {
78 if ( empty( $user_id ) ) {
79 return;
80 }
81
82 // If $name is null, return the all meta data.
83 if ( null === $name ) {
84 return self::get_all_meta( get_userdata( $user_id ) );
85 }
86
87 // Don't output meta data that not save in the SCF settings page.
88 // Getting the settings data, judged to output meta data.
89 return self::get_meta( get_userdata( $user_id ), $name );
90 }
91
92 /**
93 * Getting the term meta data to feel good.
94 *
95 * @param int $term_id Term id.
96 * @param string $taxonomy_name Taxonomy name.
97 * @param string $name Group name or field name.
98 * @return mixed
99 */
100 public static function get_term_meta( $term_id, $taxonomy_name, $name = null ) {
101 if ( empty( $term_id ) || empty( $taxonomy_name ) ) {
102 return;
103 }
104
105 // If $name is null, return the all meta data.
106 if ( null === $name ) {
107 return self::get_all_meta( get_term( $term_id, $taxonomy_name ) );
108 }
109
110 // Don't output meta data that not save in the SCF settings page
111 // Getting the settings data, judged to output meta data.
112 return self::get_meta( get_term( $term_id, $taxonomy_name ), $name );
113 }
114
115 /**
116 * Getting the custom options page meta data to feel good.
117 *
118 * @param string $menu_slug custom options page slug.
119 * @param string $name group name or field name.
120 * @return mixed
121 */
122 public static function get_option_meta( $menu_slug, $name = null ) {
123 if ( empty( $menu_slug ) ) {
124 return;
125 }
126
127 if ( ! isset( self::$options_pages[ $menu_slug ] ) ) {
128 return;
129 }
130
131 $option = self::generate_option_object( $menu_slug );
132
133 // If $name is null, return the all meta data.
134 if ( null === $name ) {
135 return self::get_all_meta( $option );
136 }
137
138 // Don't output meta data that not save in the SCF settings page
139 // Getting the settings data, judged to output meta data.
140 return self::get_meta( $option, $name );
141 }
142
143 /**
144 * Getting any meta data to feel good.
145 *
146 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
147 * @param string $name Group name or field name.
148 * @return mixed
149 */
150 protected static function get_meta( $wp_object, $name ) {
151 $cache = Smart_Custom_Fields_Cache::get_instance();
152 if ( $cache->get_meta( $wp_object, $name ) ) {
153 self::debug_cache_message( "use get cache. [name: {$name}]" );
154 return $cache->get_meta( $wp_object, $name );
155 } else {
156 self::debug_cache_message( "dont use get cache... [name: {$name}]" );
157 }
158
159 $settings = self::get_settings( $wp_object );
160 foreach ( $settings as $setting ) {
161 // If $name matches the group name, returns fields in the group as array.
162 $group = $setting->get_group( $name );
163 if ( $group ) {
164 $values_by_group = self::get_values_by_group( $wp_object, $group );
165 $cache->save_meta( $wp_object, $name, $values_by_group );
166 return $values_by_group;
167 }
168
169 // If $name doesn't matche the group name, returns the field that matches.
170 $groups = $setting->get_groups();
171 foreach ( $groups as $group ) {
172 $field = $group->get_field( $name );
173 if ( $field ) {
174 $is_repeatable = $group->is_repeatable();
175 $value_by_field = self::get_value_by_field( $wp_object, $field, $is_repeatable );
176 $cache->save_meta( $wp_object, $name, $value_by_field );
177 return $value_by_field;
178 }
179 }
180 }
181 }
182
183 /**
184 * Getting all of any meta data to feel good.
185 *
186 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
187 * @return mixed
188 */
189 protected static function get_all_meta( $wp_object ) {
190 $cache = Smart_Custom_Fields_Cache::get_instance();
191 $settings = self::get_settings( $wp_object );
192 $post_meta = array();
193 foreach ( $settings as $setting ) {
194 $groups = $setting->get_groups();
195 foreach ( $groups as $group ) {
196 $is_repeatable = $group->is_repeatable();
197 $group_name = $group->get_name();
198 if ( $is_repeatable && $group_name ) {
199 $values_by_group = self::get_values_by_group( $wp_object, $group );
200 $cache->save_meta( $wp_object, $group_name, $values_by_group );
201 $post_meta[ $group_name ] = $values_by_group;
202 } else {
203 $fields = $group->get_fields();
204 foreach ( $fields as $field ) {
205 $field_name = $field->get( 'name' );
206 $value_by_field = self::get_value_by_field( $wp_object, $field, $is_repeatable );
207 $cache->save_meta( $wp_object, $field_name, $value_by_field );
208 $post_meta[ $field_name ] = $value_by_field;
209 }
210 }
211 }
212 }
213 return $post_meta;
214 }
215
216 /**
217 * If in preview, return the preview post ID.
218 *
219 * @param int $post_id Post id.
220 * @return int
221 */
222 protected static function get_real_post_id( $post_id ) {
223 if ( is_preview() ) {
224 $preview_post = wp_get_post_autosave( $post_id );
225 if ( isset( $preview_post->ID ) ) {
226 $post_id = $preview_post->ID;
227 }
228 }
229 return $post_id;
230 }
231
232 /**
233 * Getting the meta data of the group.
234 * When group, Note the point that returned data are repetition.
235 *
236 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
237 * @param Smart_Custom_Fields_Group $group Group object.
238 * @return mixed
239 */
240 protected static function get_values_by_group( $wp_object, $group ) {
241 $is_repeatable = $group->is_repeatable();
242 $meta = array();
243 $fields = $group->get_fields();
244
245 foreach ( $fields as $field ) {
246 if ( $field->get_attribute( 'allow-multiple-data' ) ) {
247 $meta[0][ $field->get( 'name' ) ] = array();
248 } else {
249 $meta[0][ $field->get( 'name' ) ] = '';
250 }
251 }
252 $default_meta = $meta[0];
253 foreach ( $fields as $field ) {
254 $value_by_field = self::get_value_by_field( $wp_object, $field, $is_repeatable );
255 foreach ( $value_by_field as $i => $value ) {
256 $meta[ $i ][ $field->get( 'name' ) ] = $value;
257 }
258 }
259 foreach ( $meta as $i => $value ) {
260 $meta[ $i ] = array_merge( $default_meta, $value );
261 }
262 return $meta;
263 }
264
265 /**
266 * Getting the meta data of the field.
267 *
268 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
269 * @param Smart_Custom_Fields_Field_Base $field Field object.
270 * @param bool $is_repeatable Whether the group that this field belongs is repetition.
271 * @return mixed $post_meta
272 */
273 protected static function get_value_by_field( $wp_object, $field, $is_repeatable ) {
274 $field_name = $field->get( 'name' );
275 if ( ! $field_name ) {
276 return;
277 }
278
279 $meta = new Smart_Custom_Fields_Meta( $wp_object );
280
281 // In the case of multi-value items in the loop
282 $field_type = $field->get_attribute( 'type' );
283 $repeat_multiple_data = self::get_repeat_multiple_data( $wp_object );
284 if ( is_array( $repeat_multiple_data ) && isset( $repeat_multiple_data[ $field_name ] ) ) {
285 if ( $meta->is_saved_the_key( $field_name ) ) {
286 $_meta = $meta->get( $field_name );
287 } else {
288 $_meta = self::get_default_value( $field );
289 }
290 $start = 0;
291 $meta_value = array();
292 foreach ( $repeat_multiple_data[ $field_name ] as $repeat_multiple_key => $repeat_multiple_value ) {
293 if ( 0 === $repeat_multiple_value ) {
294 $value = array();
295 } else {
296 $value = array_slice( $_meta, $start, $repeat_multiple_value );
297 $start += $repeat_multiple_value;
298 }
299 $value = apply_filters( SCF_Config::PREFIX . 'validate-get-value', $value, $field_type );
300 $meta_value[ $repeat_multiple_key ] = $value;
301 }
302 } else {
303 // Other than that
304 $single = true;
305 if ( $field->get_attribute( 'allow-multiple-data' ) || $is_repeatable ) {
306 $single = false;
307 }
308 if ( $meta->is_saved_the_key( $field_name ) ) {
309 $meta_value = $meta->get( $field_name, $single );
310 } else {
311 $meta_value = self::get_default_value( $field, $single );
312 }
313 $meta_value = apply_filters( SCF_Config::PREFIX . 'validate-get-value', $meta_value, $field_type );
314 }
315 return $meta_value;
316 }
317
318 /**
319 * Return the default value.
320 *
321 * @param Smart_Custom_Fields_Field_Base $field Field object.
322 * @param bool $single Whether to return a single value. This parameter has no effect if $key is not specified.
323 * @return array|strings
324 */
325 public static function get_default_value( $field, $single = false ) {
326 if ( ! is_a( $field, 'Smart_Custom_Fields_Field_Base' ) ) {
327 if ( $single ) {
328 return '';
329 }
330 return array();
331 }
332
333 $choices = $field->get( 'choices' );
334 $default = $field->get( 'default' );
335
336 if ( $field->get_attribute( 'allow-multiple-data' ) ) {
337 $choices = self::choices_eol_to_array( $choices );
338 $default = self::choices_eol_to_array( $default );
339 $default_sanitized = array();
340
341 if ( self::is_assoc( $choices ) ) {
342 $_choices = array_flip( $choices );
343 } else {
344 $_choices = $choices;
345 }
346 foreach ( $default as $key => $value ) {
347 if ( in_array( $value, $_choices, true ) ) {
348 if ( preg_match( '/^\d+$/', $value ) ) {
349 $value = (int) $value;
350 }
351 $default_sanitized[ $key ] = $value;
352 }
353 }
354 return $default_sanitized;
355 }
356
357 if ( $single ) {
358 // Return string
359 return $default;
360 } else {
361 // Return array
362 if ( is_array( $default ) ) {
363 return $default;
364 }
365 if ( '' === $default || false === $default || null === $default ) {
366 return array();
367 }
368 return (array) $default;
369 }
370 }
371
372 /**
373 * Getting enabled custom field settings in the post type or the role or the term.
374 *
375 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
376 * @return array
377 */
378 public static function get_settings_posts( $wp_object ) {
379 $cache = Smart_Custom_Fields_Cache::get_instance();
380 $settings_posts = array();
381 if ( null !== $cache->get_settings_posts( $wp_object ) ) {
382 self::debug_cache_message( 'use settings posts cache.' );
383 return $cache->get_settings_posts( $wp_object );
384 } else {
385 self::debug_cache_message( 'dont use settings posts cache...' );
386 }
387
388 $meta = new Smart_Custom_Fields_Meta( $wp_object );
389 $types = $meta->get_types( false );
390
391 switch ( $meta->get_meta_type() ) {
392 case 'post':
393 $key = SCF_Config::PREFIX . 'condition';
394 break;
395 case 'user':
396 $key = SCF_Config::PREFIX . 'roles';
397 break;
398 case 'term':
399 $key = SCF_Config::PREFIX . 'taxonomies';
400 break;
401 case 'option':
402 $key = SCF_Config::PREFIX . 'options-pages';
403 break;
404 default:
405 $key = '';
406 }
407
408 if ( ! empty( $key ) && ( ! empty( $types ) ) ) {
409 $meta_query = array();
410 foreach ( $types as $type ) {
411 $meta_query[] = array(
412 'key' => $key,
413 'value' => sprintf( '"%s"', $type ),
414 'compare' => 'LIKE',
415 );
416 }
417 if ( $meta_query ) {
418 $meta_query['relation'] = 'OR';
419 }
420
421 $args = array(
422 'post_type' => SCF_Config::NAME,
423 'posts_per_page' => -1,
424 'order' => 'ASC',
425 'orderby' => 'menu_order',
426 'meta_query' => $meta_query, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
427 );
428
429 $settings_posts = get_posts( $args );
430 }
431
432 $cache = Smart_Custom_Fields_Cache::get_instance();
433 $cache->save_settings_posts( $wp_object, $settings_posts );
434 return $settings_posts;
435 }
436
437 /**
438 * Getting array of the Setting object.
439 *
440 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
441 * @return array
442 */
443 public static function get_settings( $wp_object ) {
444 $meta = new Smart_Custom_Fields_Meta( $wp_object );
445 $id = $meta->get_id();
446 $type = $meta->get_type( false );
447 $types = $meta->get_types( false );
448 $meta_type = $meta->get_meta_type();
449
450 // IF the post that has custom field settings according to post ID,
451 // don't display because the post ID would change in preview.
452 // So if in preview, re-getting post ID from original post (parent of the preview).
453 if ( 'post' === $meta_type && 'revision' === $wp_object->post_type ) {
454 $wp_object = get_post( $wp_object->post_parent );
455 }
456
457 $settings = array();
458
459 if ( ! empty( $types ) ) {
460 $settings_posts = self::get_settings_posts( $wp_object );
461 if ( 'post' === $meta_type ) {
462 $settings = self::get_settings_for_post( $wp_object, $settings_posts );
463 } elseif ( 'user' === $meta_type ) {
464 $settings = self::get_settings_for_profile( $wp_object, $settings_posts );
465 } elseif ( 'term' === $meta_type ) {
466 $settings = self::get_settings_for_term( $wp_object, $settings_posts );
467 } elseif ( 'option' === $meta_type ) {
468 $settings = self::get_settings_for_option( $wp_object, $settings_posts );
469 }
470 }
471 $settings = apply_filters(
472 SCF_Config::PREFIX . 'register-fields',
473 $settings,
474 $type,
475 $id,
476 $meta_type,
477 $types
478 );
479 if ( ! is_array( $settings ) ) {
480 $settings = array();
481 }
482 return $settings;
483 }
484
485 /**
486 * Getting the Setting object for post.
487 *
488 * @param WP_Post $wp_object WP_Post object.
489 * @param array $settings_posts Settings.
490 * @return array
491 */
492 protected static function get_settings_for_post( $wp_object, $settings_posts ) {
493 $cache = Smart_Custom_Fields_Cache::get_instance();
494 $settings = array();
495 foreach ( $settings_posts as $settings_post ) {
496 if ( $cache->get_settings( $settings_post->ID ) !== null ) {
497 self::debug_cache_message( "use settings cache. [id: {$settings_post->ID}]" );
498 $setting = $cache->get_settings( $settings_post->ID, $wp_object );
499 if ( $setting ) {
500 $settings[ $settings_post->ID ] = $setting;
501 }
502 continue;
503 }
504 self::debug_cache_message( "dont use settings cache... [SCF ID: {$settings_post->ID}] [post_type: {$wp_object->post_type}] [Post ID: {$wp_object->ID}]" );
505 $condition_post_ids_raw = get_post_meta(
506 $settings_post->ID,
507 SCF_Config::PREFIX . 'condition-post-ids',
508 true
509 );
510 if ( $condition_post_ids_raw ) {
511 $condition_post_ids_raw = explode( ',', $condition_post_ids_raw );
512 foreach ( $condition_post_ids_raw as $condition_post_id ) {
513 $condition_post_id = trim( $condition_post_id );
514 $setting = self::add_setting( $settings_post->ID, $settings_post->post_title );
515 if ( (int) $wp_object->ID === (int) $condition_post_id ) {
516 $settings[ $settings_post->ID ] = $setting;
517 }
518 $post = get_post( $condition_post_id );
519 if ( empty( $post ) ) {
520 $post = self::generate_post_object( $condition_post_id );
521 }
522 $cache->save_settings( $settings_post->ID, $setting, $post );
523 }
524 } else {
525 $setting = self::add_setting( $settings_post->ID, $settings_post->post_title );
526 $settings[ $settings_post->ID ] = $setting;
527 $cache->save_settings( $settings_post->ID, $setting );
528 }
529 }
530 return $settings;
531 }
532
533 /**
534 * Getting the Setting object for user.
535 *
536 * @param WP_User|WP_Term|stdClass $wp_object Object meta object.
537 * @param array $settings_posts Settings.
538 * @return array
539 */
540 protected static function get_settings_for_profile( $wp_object, $settings_posts ) {
541 $cache = Smart_Custom_Fields_Cache::get_instance();
542 $settings = array();
543 foreach ( $settings_posts as $settings_post ) {
544 if ( $cache->get_settings( $settings_post->ID ) !== null ) {
545 self::debug_cache_message( "use settings cache. [id: {$settings_post->ID}]" );
546 $settings[] = $cache->get_settings( $settings_post->ID );
547 continue;
548 }
549 self::debug_cache_message( "dont use settings cache... [id: {$settings_post->ID}]" );
550 $setting = self::add_setting( $settings_post->ID, $settings_post->post_title );
551 $settings[] = $setting;
552 $cache->save_settings( $settings_post->ID, $setting );
553 }
554 return $settings;
555 }
556
557 /**
558 * Getting the Setting object for term.
559 *
560 * @param WP_Term $wp_object WP_Term object.
561 * @param array $settings_posts Settings.
562 * @return array
563 */
564 protected static function get_settings_for_term( $wp_object, $settings_posts ) {
565 return self::get_settings_for_profile( $wp_object, $settings_posts );
566 }
567
568 /**
569 * Getting the Setting object for option.
570 *
571 * @param stdClass $wp_object stdClass object.
572 * @param array $settings_posts Settings.
573 * @return array
574 */
575 protected static function get_settings_for_option( $wp_object, $settings_posts ) {
576 return self::get_settings_for_profile( $wp_object, $settings_posts );
577 }
578
579 /**
580 * Getting delimited identification data of the repeated multi-value items.
581 *
582 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
583 * @return array
584 */
585 public static function get_repeat_multiple_data( $wp_object ) {
586 $cache = Smart_Custom_Fields_Cache::get_instance();
587 $repeat_multiple_data = array();
588 if ( $cache->get_repeat_multiple_data( $wp_object ) ) {
589 return $cache->get_repeat_multiple_data( $wp_object );
590 }
591
592 $meta = new Smart_Custom_Fields_Meta( $wp_object );
593 $_repeat_multiple_data = $meta->get( SCF_Config::PREFIX . 'repeat-multiple-data', true );
594 if ( ! empty( $_repeat_multiple_data ) ) {
595 $repeat_multiple_data = $_repeat_multiple_data;
596 }
597
598 $cache->save_repeat_multiple_data( $wp_object, $repeat_multiple_data );
599 return $repeat_multiple_data;
600 }
601
602 /**
603 * Return true if null or empty value.
604 *
605 * @param mixed $value Value.
606 * @return bool
607 */
608 public static function is_empty( &$value ) {
609 if ( isset( $value ) ) {
610 if ( is_null( $value ) || '' === $value ) {
611 return true;
612 }
613 return false;
614 }
615 return true;
616 }
617
618 /**
619 * Whether the associative array or not.
620 *
621 * @see http://qiita.com/ka215/items/a14e53547e717d2a564f
622 *
623 * @param array $data This argument should be expected an array.
624 * @param boolean $multidimensional True if a multidimensional array is inclusion into associative array, the default value is false.
625 * @return boolean
626 */
627 public static function is_assoc( $data, $multidimensional = false ) {
628 if ( ! is_array( $data ) || empty( $data ) ) {
629 return false;
630 }
631 $has_array = false;
632 foreach ( $data as $key => $value ) {
633 if ( is_array( $value ) ) {
634 $has_array = true;
635 }
636
637 if ( ! is_int( $key ) ) {
638 return true;
639 }
640 }
641 return $multidimensional && $has_array ? true : false;
642 }
643
644 /**
645 * Adding the available form field object.
646 *
647 * @param Smart_Custom_Fields_Field_Base $instance Field object.
648 */
649 public static function add_form_field_instance( Smart_Custom_Fields_Field_Base $instance ) {
650 $type = $instance->get_attribute( 'type' );
651 if ( ! empty( $type ) ) {
652 self::$fields[ $type ] = $instance;
653 }
654 }
655
656 /**
657 * Getting the available form field object.
658 *
659 * @param string $type Type of the form field.
660 * @return Smart_Custom_Fields_Field_Base
661 */
662 public static function get_form_field_instance( $type ) {
663 if ( ! empty( self::$fields[ $type ] ) ) {
664 return clone self::$fields[ $type ];
665 }
666 }
667
668 /**
669 * Getting all available form field object.
670 *
671 * @return array
672 */
673 public static function get_form_field_instances() {
674 $fields = array();
675 foreach ( self::$fields as $type => $instance ) {
676 $fields[ $type ] = self::get_form_field_instance( $type );
677 }
678 return $fields;
679 }
680
681 /**
682 * Getting custom fields that saved custo field settings page.
683 * Note that not return only one even define multiple fields with the same name of the field name.
684 *
685 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
686 * @param string $field_name Field name.
687 * @return Smart_Custom_Fields_Field_Base|null
688 */
689 public static function get_field( $wp_object, $field_name ) {
690 if ( '_locale' === $field_name || '_original_post' === $field_name ) {
691 return null;
692 }
693
694 $settings = self::get_settings( $wp_object );
695 foreach ( $settings as $setting ) {
696 $fields = $setting->get_fields();
697 if ( ! empty( $fields[ $field_name ] ) ) {
698 return $fields[ $field_name ];
699 }
700 }
701 }
702
703 /**
704 * Convert to array from newline delimiter $choices.
705 *
706 * @param string $choices Choices.
707 * @return array
708 */
709 public static function choices_eol_to_array( $choices ) {
710 if ( ! is_array( $choices ) ) {
711 if ( '' === $choices || false === $choices || null === $choices ) {
712 return array();
713 }
714 $_choices = str_replace( array( "\r\n", "\r", "\n" ), "\n", $choices );
715 $_choices = explode( "\n", $_choices );
716 $choices = array();
717 foreach ( $_choices as $_choice ) {
718 $_choice = array_map( 'trim', explode( '=>', $_choice ) );
719 if ( count( $_choice ) === 2 ) {
720 $choices[ $_choice[0] ] = $_choice[1];
721 } else {
722 $choices = array_merge( $choices, $_choice );
723 }
724 }
725 }
726 return $choices;
727 }
728
729 /**
730 * Return generated Setting object.
731 *
732 * @param string $id Post ID of custom field settings page.
733 * @param string $title Title of custom field settings page.
734 * @return Smart_Custom_Fields_Setting
735 */
736 public static function add_setting( $id, $title ) {
737 return new Smart_Custom_Fields_Setting( $id, $title );
738 }
739
740 /**
741 * Adding custom options page.
742 *
743 * @see https://developer.wordpress.org/reference/functions/add_menu_page/
744 *
745 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
746 * @param string $menu_title The text to be used for the menu.
747 * @param string $capability The capability required for this menu to be displayed to the user.
748 * @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu page and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().
749 * @param string $icon_url The URL to the icon to be used for this menu.
750 * @param int $position The position in the menu order this item should appear.
751 * @return string
752 */
753 public static function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $icon_url = '', $position = null ) {
754 self::$options_pages[ $menu_slug ] = $menu_title;
755 new Smart_Custom_Fields_Options_Page( $page_title, $menu_title, $capability, $menu_slug, $icon_url, $position );
756 return $menu_slug;
757 }
758
759 /**
760 * Return array of custom options pages
761 *
762 * @return array
763 */
764 public static function get_options_pages() {
765 return self::$options_pages;
766 }
767
768 /**
769 * Generate WP_Post object.
770 *
771 * @param int $post_id Post id.
772 * @param string $post_type Post type.
773 * @return WP_Post
774 */
775 public static function generate_post_object( $post_id, $post_type = null ) {
776 $post = new stdClass();
777 $post->ID = $post_id;
778 $post->post_type = $post_type;
779 return new WP_Post( $post );
780 }
781
782 /**
783 * Generate option object.
784 *
785 * @param string $menu_slug Menu slug.
786 * @return stdClass
787 */
788 public static function generate_option_object( $menu_slug ) {
789 $options_pages = self::get_options_pages();
790 if ( ! isset( $options_pages[ $menu_slug ] ) ) {
791 return;
792 }
793 $option = new stdClass();
794 $option->menu_slug = $menu_slug;
795 $option->menu_title = $options_pages[ $menu_slug ];
796 return $option;
797 }
798
799 /**
800 * Print cache usage.
801 *
802 * @param string $message Message.
803 */
804 protected static function debug_cache_message( $message ) {
805 if ( defined( 'SCF_DEBUG_CACHE' ) && SCF_DEBUG_CACHE === true ) {
806 echo wp_kses_post( $message ) . '<br />';
807 }
808 }
809 }
810