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