-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
1234 lines (1010 loc) · 36.4 KB
/
init.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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Script Name: Custom Metaboxes and Fields
Contributors: Andrew Norcross (@norcross / andrewnorcross.com)
Jared Atchison (@jaredatch / jaredatchison.com)
Bill Erickson (@billerickson / billerickson.net)
Justin Sternberg (@jtsternberg / dsgnwrks.pro)
Description: This will create metaboxes with custom fields that will blow your mind.
Version: 1.0.1
*/
/**
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
/************************************************************************
You should not edit the code below or things might explode!
*************************************************************************/
// Autoload helper classes
spl_autoload_register('cmb_Meta_Box::autoload_helpers');
$meta_boxes = array();
$meta_boxes = apply_filters( 'cmb_meta_boxes', $meta_boxes );
foreach ( $meta_boxes as $meta_box ) {
$my_box = new cmb_Meta_Box( $meta_box );
}
define( 'CMB_META_BOX_URL', cmb_Meta_Box::get_meta_box_url() );
/**
* Create meta boxes
*/
class cmb_Meta_Box {
/**
* Current version number
* @var string
* @since 1.0.0
*/
const CMB_VERSION = '1.0.1';
/**
* Metabox Config array
* @var array
* @since 0.9.0
*/
protected $_meta_box;
/**
* Metabox Defaults
* @var array
* @since 1.0.1
*/
protected static $mb_defaults = array(
'id' => '',
'title' => '',
'pages' => array(), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'show_on' => array( 'key' => false, 'value' => false ), // Specific post IDs or page templates to display this metabox
'cmb_styles' => true, // Include cmb bundled stylesheet
'fields' => array(),
);
/**
* Metabox Form ID
* @var string
* @since 0.9.4
*/
protected $form_id = 'post';
/**
* Current field config array
* @var array
* @since 1.0.0
*/
public static $field = array();
/**
* Object ID for metabox meta retrieving/saving
* @var int
* @since 1.0.0
*/
protected static $object_id = 0;
/**
* Type of object being saved. (e.g., post, user, or comment)
* @var string
* @since 1.0.0
*/
protected static $object_type = '';
/**
* Whether scripts/styles have been enqueued yet
* @var bool
* @since 1.0.0
*/
protected static $is_enqueued = false;
/**
* Type of object specified by the metabox Config
* @var string
* @since 1.0.0
*/
protected static $mb_object_type = 'post';
/**
* Array of all options from manage-options metaboxes
* @var array
* @since 1.0.0
*/
protected static $options = array();
/**
* Get started
*/
function __construct( $meta_box ) {
$meta_box = self::set_mb_defaults( $meta_box );
$allow_frontend = apply_filters( 'cmb_allow_frontend', true, $meta_box );
if ( ! is_admin() && ! $allow_frontend )
return;
$this->_meta_box = $meta_box;
self::set_mb_type( $meta_box );
$types = wp_list_pluck( $meta_box['fields'], 'type' );
$upload = in_array( 'file', $types ) || in_array( 'file_list', $types );
global $pagenow;
$show_filters = 'cmb_Meta_Box_Show_Filters';
foreach ( get_class_methods( $show_filters ) as $filter ) {
add_filter( 'cmb_show_on', array( $show_filters, $filter ), 10, 2 );
}
// register our scripts and styles for cmb
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ), 8 );
if ( self::get_object_type() == 'post' ) {
add_action( 'admin_menu', array( $this, 'add_metaboxes' ) );
add_action( 'add_attachment', array( $this, 'save_post' ) );
add_action( 'edit_attachment', array( $this, 'save_post' ) );
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
add_action( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );
if ( $upload && in_array( $pagenow, array( 'page.php', 'page-new.php', 'post.php', 'post-new.php' ) ) ) {
add_action( 'admin_head', array( $this, 'add_post_enctype' ) );
}
}
if ( self::get_object_type() == 'user' ) {
$priority = 10;
if ( isset( $meta_box['priority'] ) ) {
if ( is_numeric( $meta_box['priority'] ) )
$priority = $meta_box['priority'];
elseif ( $meta_box['priority'] == 'high' )
$priority = 5;
elseif ( $meta_box['priority'] == 'low' )
$priority = 20;
}
add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority );
add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority );
add_action( 'personal_options_update', array( $this, 'save_user' ) );
add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
if ( $upload && in_array( $pagenow, array( 'profile.php', 'user-edit.php' ) ) ) {
$this->form_id = 'your-profile';
add_action( 'admin_head', array( $this, 'add_post_enctype' ) );
}
}
}
/**
* Autoloads files with classes when needed
* @since 1.0.0
* @param string $class_name Name of the class being requested
*/
public static function autoload_helpers( $class_name ) {
if ( class_exists( $class_name, false ) )
return;
// for PHP versions < 5.3
$dir = defined( '__DIR__' ) ? __DIR__ : dirname( __FILE__ );
$file = "$dir/helpers/$class_name.php";
if ( file_exists( $file ) )
@include( $file );
}
/**
* Registers scripts and styles for CMB
* @since 1.0.0
*/
public function register_scripts() {
// Should only be run once
if ( self::$is_enqueued )
return;
global $wp_version;
// Only use minified files if SCRIPT_DEBUG is off
$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
// scripts required for cmb
$scripts = array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', /*'media-upload', */'cmb-timepicker' );
// styles required for cmb
$styles = array();
// if we're 3.5 or later, user wp-color-picker
if ( 3.5 <= $wp_version ) {
$scripts[] = 'wp-color-picker';
$styles[] = 'wp-color-picker';
if ( ! is_admin() ) {
// we need to register colorpicker on the front-end
wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), self::CMB_VERSION );
wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), self::CMB_VERSION );
wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array(
'clear' => __( 'Clear' ),
'defaultString' => __( 'Default' ),
'pick' => __( 'Select Color' ),
'current' => __( 'Current Color' ),
) );
}
} else {
// otherwise use the older 'farbtastic'
$scripts[] = 'farbtastic';
$styles[] = 'farbtastic';
}
wp_register_script( 'cmb-timepicker', CMB_META_BOX_URL . 'js/jquery.timePicker.min.js' );
wp_register_script( 'cmb-scripts', CMB_META_BOX_URL .'js/cmb'. $min .'.js', $scripts, self::CMB_VERSION );
wp_enqueue_media();
wp_localize_script( 'cmb-scripts', 'cmb_l10', array(
'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ),
'script_debug' => defined('SCRIPT_DEBUG') && SCRIPT_DEBUG,
'new_admin_style' => version_compare( $wp_version, '3.7', '>' ),
'object_type' => self::get_object_type(),
'upload_file' => 'Use this file',
'remove_image' => 'Remove Image',
'remove_file' => 'Remove',
'file' => 'File:',
'download' => 'Download',
'ajaxurl' => admin_url( '/admin-ajax.php' ),
) );
wp_register_style( 'cmb-styles', CMB_META_BOX_URL . 'style'. $min .'.css', $styles );
// Ok, we've enqueued our scripts/styles
self::$is_enqueued = true;
}
/**
* Enqueues scripts and styles for CMB
* @since 1.0.0
*/
public function do_scripts( $hook ) {
// only enqueue our scripts/styles on the proper pages
if ( $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'page-new.php' || $hook == 'page.php' ) {
wp_enqueue_script( 'cmb-scripts' );
// default is to show cmb styles on post pages
if ( $this->_meta_box['cmb_styles'] != false )
wp_enqueue_style( 'cmb-styles' );
}
}
/**
* Add encoding attribute
*/
public function add_post_enctype() {
echo '
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#'. $this->form_id .'").attr("enctype", "multipart/form-data");
jQuery("#'. $this->form_id .'").attr("encoding", "multipart/form-data");
});
</script>';
}
/**
* Add metaboxes (to 'post' object type)
*/
public function add_metaboxes() {
foreach ( $this->_meta_box['pages'] as $page ) {
if ( apply_filters( 'cmb_show_on', true, $this->_meta_box ) )
add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array( $this, 'post_metabox' ), $page, $this->_meta_box['context'], $this->_meta_box['priority']) ;
}
}
/**
* Display metaboxes for a post object
* @since 1.0.0
*/
public function post_metabox() {
if ( ! $this->_meta_box )
return;
self::show_form( $this->_meta_box, get_the_ID(), 'post' );
}
/**
* Display metaboxes for a user object
* @since 1.0.0
*/
public function user_metabox() {
if ( ! $this->_meta_box )
return;
if ( 'user' != self::set_mb_type( $this->_meta_box ) )
return;
if ( ! apply_filters( 'cmb_show_on', true, $this->_meta_box ) )
return;
wp_enqueue_script( 'cmb-scripts' );
// default is to NOT show cmb styles on user profile page
if ( $this->_meta_box['cmb_styles'] != false )
wp_enqueue_style( 'cmb-styles' );
self::show_form( $this->_meta_box );
}
/**
* Loops through and displays fields
* @since 1.0.0
* @param array $meta_box Metabox config array
* @param int $object_id Object ID
* @param string $object_type Type of object being saved. (e.g., post, user, or comment)
*/
public static function show_form( $meta_box, $object_id = 0, $object_type = '' ) {
$meta_box = self::set_mb_defaults( $meta_box );
// Set/get type
$object_type = self::set_object_type( $object_type ? $object_type : self::set_mb_type( $meta_box ) );
// Set/get ID
$object_id = self::set_object_id( $object_id ? $object_id : self::get_object_id() );
// get box types
$types = cmb_Meta_Box_types::get();
// Use nonce for verification
echo "\n<!-- Begin CMB Fields -->\n";
wp_nonce_field( self::nonce(), 'wp_meta_box_nonce', false, true );
do_action( 'cmb_before_table', $meta_box, $object_id, $object_type );
echo '<table class="form-table cmb_metabox">';
foreach ( $meta_box['fields'] as $field ) {
if ( isset( $field['on_front'] ) && $field['on_front'] == false )
continue;
self::$field =& $field;
// Set up blank or default values for empty ones
if ( ! isset( $field['name'] ) ) $field['name'] = '';
if ( ! isset( $field['desc'] ) ) $field['desc'] = '';
if ( ! isset( $field['default'] ) ) {
// Phase out 'std', and use 'default' instead
$field['default'] = isset( $field['std'] ) ? $field['std'] : '';
}
// Allow a filter override of the default value
$field['default'] = apply_filters( 'cmb_default_filter', $field['default'], $field, $object_id, $object_type );
// 'cmb_std_filter' deprectated, use 'cmb_default_filter' instead
$field['default'] = apply_filters( 'cmb_std_filter', $field['default'], $field, $object_id, $object_type );
$field['allow'] = 'file' == $field['type'] && ! isset( $field['allow'] ) ? array( 'url', 'attachment' ) : array();
$field['save_id'] = 'file' == $field['type'] && ! isset( $field['save_id'] );
$field['multiple'] = 'multicheck' == $field['type'];
// Allow an override for the field's value
// (assuming no one would want to save 'cmb_no_override_val' as a value)
$meta = apply_filters( 'cmb_override_meta_value', 'cmb_no_override_val', $object_id, $field, $object_type );
// If no override, get our meta
if ( $meta === 'cmb_no_override_val' )
$meta = self::get_data();
$classes = '';
$field['repeatable'] = isset( $field['repeatable'] ) && $field['repeatable'];
$classes .= $field['repeatable'] ? ' cmb-repeat' : '';
// 'inline' flag, or _inline in the field type, set to true
$inline = ( isset( $field['inline'] ) && $field['inline'] || false !== stripos( $field['type'], '_inline' ) );
$classes .= $inline ? ' cmb-inline' : '';
echo '<tr class="cmb-type-'. sanitize_html_class( $field['type'] ) .' cmb_id_'. sanitize_html_class( $field['id'] ) . $classes .'">';
if ( $field['type'] == "title" ) {
echo '<td colspan="2">';
} else {
if ( isset( $meta_box['show_names'] ) && $meta_box['show_names'] == true ) {
$style = $object_type == 'post' ? ' style="width:18%"' : '';
echo '<th'. $style .'><label for="', $field['id'], '">', $field['name'], '</label></th>';
} else {
echo '<label style="display:none;" for="', $field['id'], '">', $field['name'], '</label></th>';
}
echo '<td>';
}
echo empty( $field['before'] ) ? '' : $field['before'];
call_user_func( array( $types, $field['type'] ), $field, $meta, $object_id, $object_type );
echo empty( $field['after'] ) ? '' : $field['after'];
echo '</td>','</tr>';
}
echo '</table>';
do_action( 'cmb_after_table', $meta_box, $object_id, $object_type );
echo "\n<!-- End CMB Fields -->\n";
}
/**
* Save data from metabox
*/
public function save_post( $post_id, $post = false ) {
$post_type = $post ? $post->post_type : get_post_type( $post_id );
// check permissions
if (
// check nonce
! isset( $_POST['wp_meta_box_nonce'] )
|| ! wp_verify_nonce( $_POST['wp_meta_box_nonce'], self::nonce() )
// check if autosave
|| defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE
// check user editing permissions
|| ( 'page' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) )
|| ! current_user_can( 'edit_post', $post_id )
// get the metabox post_types & compare it to this post_type
|| ! in_array( $post_type, $this->_meta_box['pages'] )
)
return $post_id;
self::save_fields( $this->_meta_box, $post_id, 'post' );
}
/**
* Save data from metabox
*/
public function save_user( $user_id ) {
// check permissions
// @todo more hardening?
if (
// check nonce
! isset( $_POST['wp_meta_box_nonce'] )
|| ! wp_verify_nonce( $_POST['wp_meta_box_nonce'], self::nonce() )
)
return $user_id;
self::save_fields( $this->_meta_box, $user_id, 'user' );
}
/**
* Loops through and saves field data
* @since 1.0.0
* @param array $meta_box Metabox config array
* @param int $object_id Object ID
* @param string $object_type Type of object being saved. (e.g., post, user, or comment)
*/
public static function save_fields( $meta_box, $object_id, $object_type = '' ) {
$meta_box = self::set_mb_defaults( $meta_box );
$meta_box['show_on'] = empty( $meta_box['show_on'] ) ? array( 'key' => false, 'value' => false ) : $meta_box['show_on'];
if ( ! apply_filters( 'cmb_show_on', true, $meta_box ) )
return;
self::set_object_id( $object_id );
// Set/get type
$object_type = self::set_object_type( $object_type ? $object_type : self::set_mb_type( $meta_box ) );
// save field ids of those that are updated
$updated = array();
foreach ( $meta_box['fields'] as $field ) {
self::$field =& $field;
$name = $field['id'];
if ( ! isset( $field['multiple'] ) )
$field['multiple'] = ( 'multicheck' == $field['type'] ) ? true : false;
$old = self::get_data();
$new = isset( $_POST[ $field['id'] ] ) ? $_POST[ $field['id'] ] : null;
if ( $object_type == 'post' ) {
if (
isset( $field['taxonomy'] )
&& in_array( $field['type'], array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_multicheck' ) )
)
$new = wp_set_object_terms( $object_id, $new, $field['taxonomy'] );
}
if ( isset( $field['repeatable'] ) && $field['repeatable'] && is_array( $new ) ) {
$new = array_filter( $new );
}
switch ( $field['type'] ) {
case 'textarea':
case 'textarea_small':
$new = esc_textarea( $new );
break;
case 'textarea_code':
$new = htmlspecialchars_decode( stripslashes( $new ) );
break;
case 'text_date_timestamp':
$new = strtotime( $new );
break;
case 'file':
$_id_name = $field['id'] .'_id';
// get _id old value
$_id_old = self::get_data( $_id_name );
// If specified NOT to save the file ID
if ( isset( $field['save_id'] ) && ! $field['save_id'] ) {
$_new_id = '';
} else {
// otherwise get the file ID
$_new_id = isset( $_POST[ $_id_name ] ) ? $_POST[ $_id_name ] : null;
// If there is no ID saved yet, try to get it from the url
if ( isset( $_POST[ $field['id'] ] ) && $_POST[ $field['id'] ] && ! $_new_id ) {
$_new_id = self::image_id_from_url( esc_url_raw( $_POST[ $field['id'] ] ) );
}
}
if ( $_new_id && $_new_id != $_id_old ) {
$updated[] = $_id_name;
self::update_data( $_new_id, $_id_name );
} elseif ( '' == $_new_id && $_id_old ) {
$updated[] = $_id_name;
self::remove_data( $_id_name, $old );
}
break;
default:
// Check if this metabox field has a registered validation callback
$new = self::sanitization_cb( $new );
break;
}
if ( $field['multiple'] ) {
self::remove_data( $name );
if ( ! empty( $new ) ) {
foreach ( $new as $add_new ) {
$updated[] = $name;
self::update_data( $add_new, $name, true );
}
}
} elseif ( ! empty( $new ) && $new != $old ) {
$updated[] = $name;
self::update_data( $new );
} elseif ( empty( $new ) ) {
if ( ! empty( $old ) )
$updated[] = $name;
self::remove_data( $name );
}
}
// If options page, save the updated options
if ( $object_type == 'options-page' )
self::save_option( $object_id );
do_action( "cmb_save_{$object_type}_fields", $object_id, $meta_box['id'], $updated, $meta_box );
}
/**
* Returns a timezone string representing the default timezone for the site.
*
* Roughly copied from WordPress, as get_option('timezone_string') will return
* and empty string if no value has beens set on the options page.
* A timezone string is required by the wp_timezone_choice() used by the
* select_timezone field.
*
* @since 1.0.0
* @return string Timezone string
*/
public static function timezone_string() {
$current_offset = get_option( 'gmt_offset' );
$tzstring = get_option( 'timezone_string' );
if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
if ( 0 == $current_offset )
$tzstring = 'UTC+0';
elseif ( $current_offset < 0 )
$tzstring = 'UTC' . $current_offset;
else
$tzstring = 'UTC+' . $current_offset;
}
return $tzstring;
}
/**
* Returns time string offset by timezone
* @since 1.0.0
* @param string $tzstring Time string
* @return string Offset time string
*/
public static function timezone_offset( $tzstring ) {
if ( !empty( $tzstring ) ) {
if ( substr( $tzstring, 0, 3 ) === 'UTC' ) {
$tzstring = str_replace( array( ':15',':30',':45' ), array( '.25','.5','.75' ), $tzstring );
return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS );
}
$date_time_zone_selected = new DateTimeZone( $tzstring );
$tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
return $tz_offset;
}
return 0;
}
/**
* Offset a time value based on timezone
* @since 1.0.0
* @param integer $object_id Object ID
* @return string Offset time string
*/
public static function field_timezone_offset( $object_id = 0 ) {
$tzstring = self::field_timezone( $object_id );
return self::timezone_offset( $tzstring );
}
/**
* Return timezone string
* @since 1.0.0
* @param integer $object_id Object ID
* @return string Timezone string
*/
public static function field_timezone( $object_id = 0 ) {
$tzstring = null;
if ( ! ( $object_id = self::get_object_id( $object_id ) ) )
return $tzstring;
if ( array_key_exists( 'timezone', self::$field ) && self::$field['timezone'] ) {
$tzstring = self::$field['timezone'];
} else if ( array_key_exists( 'timezone_meta_key', self::$field ) && self::$field['timezone_meta_key'] ) {
$tzstring = self::get_data( self::$field['timezone_meta_key'] );
return $tzstring;
}
return false;
}
/**
* Get object id from global space if no id is provided
* @since 1.0.0
* @param integer $object_id Object ID
* @return integer $object_id Object ID
*/
public static function get_object_id( $object_id = 0 ) {
if ( $object_id )
return $object_id;
if ( self::$object_id )
return self::$object_id;
// Try to get our object ID from the global space
switch ( self::get_object_type() ) {
case 'user':
$object_id = isset( $GLOBALS['user_ID'] ) ? $GLOBALS['user_ID'] : $object_id;
$object_id = isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id;
break;
default:
$object_id = isset( $GLOBALS['post']->ID ) ? $GLOBALS['post']->ID : $object_id;
$object_id = isset( $_REQUEST['post'] ) ? $_REQUEST['post'] : $object_id;
break;
}
// reset to id or 0
self::set_object_id( $object_id ? $object_id : 0 );
return self::$object_id;
}
/**
* Explicitly Set object id
* @since 1.0.0
* @param integer $object_id Object ID
* @return integer $object_id Object ID
*/
public static function set_object_id( $object_id ) {
return self::$object_id = $object_id;
}
/**
* Sets the $object_type based on metabox settings
* @since 1.0.0
* @param array|string $meta_box Metabox config array or explicit setting
* @return string Object type
*/
public static function set_mb_type( $meta_box ) {
if ( is_string( $meta_box ) ) {
self::$mb_object_type = $meta_box;
return self::get_mb_type();
}
if ( ! isset( $meta_box['pages'] ) )
return self::get_mb_type();
$type = false;
// check if 'pages' is a string
if ( self::is_options_page_mb( $meta_box ) )
$type = 'options-page';
// check if 'pages' is a string
elseif ( is_string( $meta_box['pages'] ) )
$type = $meta_box['pages'];
// if it's an array of one, extract it
elseif ( is_array( $meta_box['pages'] ) && count( $meta_box['pages'] === 1 ) )
$type = is_string( end( $meta_box['pages'] ) ) ? end( $meta_box['pages'] ) : false;
if ( !$type )
return self::get_mb_type();
// Get our object type
if ( 'user' == $type )
self::$mb_object_type = 'user';
elseif ( 'comment' == $type )
self::$mb_object_type = 'comment';
elseif ( 'options-page' == $type )
self::$mb_object_type = 'options-page';
else
self::$mb_object_type = 'post';
return self::get_mb_type();
}
/**
* Determines if metabox is for an options page
* @since 1.0.1
* @param array $meta_box Metabox config array
* @return boolean True/False
*/
public static function is_options_page_mb( $meta_box ) {
return ( isset( $meta_box['show_on']['key'] ) && 'options-page' === $meta_box['show_on']['key'] );
}
/**
* Returns the object type
* @since 1.0.0
* @return string Object type
*/
public static function get_object_type() {
if ( self::$object_type )
return self::$object_type;
global $pagenow;
if (
$pagenow == 'user-edit.php'
|| $pagenow == 'profile.php'
)
self::set_object_type( 'user' );
elseif (
$pagenow == 'edit-comments.php'
|| $pagenow == 'comment.php'
)
self::set_object_type( 'comment' );
else
self::set_object_type( 'post' );
return self::$object_type;
}
/**
* Sets the object type
* @since 1.0.0
* @return string Object type
*/
public static function set_object_type( $object_type ) {
return self::$object_type = $object_type;
}
/**
* Returns the object type
* @since 1.0.0
* @return string Object type
*/
public static function get_mb_type() {
return self::$mb_object_type;
}
/**
* Returns the nonce value for wp_meta_box_nonce
* @since 1.0.0
* @return string Nonce value
*/
public static function nonce() {
return basename( __FILE__ );
}
/**
* Utility method that attempts to get an attachment's ID by it's url
* @since 1.0.0
* @param string $img_url Attachment url
* @return mixed Attachment ID or false
*/
public static function image_id_from_url( $img_url ) {
global $wpdb;
// Get just the file name
if ( false !== strpos( $img_url, '/' ) ) {
$explode = explode( '/', $img_url );
$img_url = end( $explode );
}
// And search for a fuzzy match of the file name
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid LIKE '%%%s%%' LIMIT 1;", $img_url ) );
// If we found an attachement ID, return it
if ( !empty( $attachment ) && is_array( $attachment ) )
return $attachment[0];
// No luck
return false;
}
/**
* Checks if field has a registered validation callback
* @since 1.0.1
* @param mixed $meta_value Meta value
* @param array $field Field config array
* @return mixed Possibly validated meta value
*/
public static function sanitization_cb( $meta_value, $field = array() ) {
if ( empty( $meta_value ) )
return $meta_value;
$field = $field !== array() ? $field : self::$field;
// Check if the field has a registered validation callback
$cb = self::maybe_callback( $field, 'sanitization_cb' );
if ( false === $cb ) {
// If requestion NO validation, return meta value
return $meta_value;
} elseif ( $cb ) {
// Ok, callback is good, let's run it.
return call_user_func( $cb, $meta_value, $field );
}
// Validation via 'cmb_Meta_Box_Sanitize' (with fallback filter)
return call_user_func( array( cmb_Meta_Box_Sanitize::get(), $field['type'] ), $meta_value, $field );
}
/**
* Checks if field has a callback value
* @since 1.0.1
* @param array $field Field config array
* @param string $cb Callback string
* @return mixed NULL, false for NO validation, or $cb string if it exists.
*/
public static function maybe_callback( $field, $cb ) {
if ( ! isset( $field[ $cb ] ) )
return;
// Check if metabox is requesting NO validation
$cb = false !== $field[ $cb ] && 'false' !== $field[ $cb ] ? $field[ $cb ] : false;
// If requestion NO validation, return false
if ( ! $cb )
return false;
if (
// Standard function
( is_string( $cb ) && function_exists( $cb ) )
// Or Class method
|| ( is_array( $cb ) && is_callable( $cb ) )
) {
return $cb;
}
}
/**
* Defines the url which is used to load local resources.
* This may need to be filtered for local Window installations.
* If resources do not load, please check the wiki for details.
* @since 1.0.1
* @return string URL to CMB resources
*/
public static function get_meta_box_url() {
if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) {
// Windows
$content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR );
$content_url = str_replace( $content_dir, WP_CONTENT_URL, dirname(__FILE__) );
$cmb_url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url );
} else {
$cmb_url = str_replace(
array(WP_CONTENT_DIR, WP_PLUGIN_DIR),
array(WP_CONTENT_URL, WP_PLUGIN_URL),
dirname( __FILE__ )
);
}
return trailingslashit( apply_filters('cmb_meta_box_url', $cmb_url ) );
}
/**
* Fills in empty metabox parameters with defaults
* @since 1.0.1
* @param array $meta_box Metabox config array
* @return array Modified Metabox config array
*/
public static function set_mb_defaults( $meta_box ) {
return wp_parse_args( $meta_box, self::$mb_defaults );
}
/**
* Retrieves metadata/option data
* @since 1.0.1
* @param string $field_id Meta key/Option array key
* @return mixed Meta/Option value
*/
public static function get_data( $field_id = '' ) {
$type = self::get_object_type();
$id = self::get_object_id();
$field_id = $field_id ? $field_id : self::$field['id'];
$data = 'options-page' === $type
? self::get_option( $id, $field_id )
: get_metadata( $type, $id, $field_id, !self::$field['multiple'] /* If multicheck this can be multiple values */ );
return $data;
}
/**
* Updates metadata/option data
* @since 1.0.1
* @param mixed $value Value to update data with
* @param string $field_id Meta key/Option array key
* @param bool $multiple Whether data is an array (add_metadata)
*/
public static function update_data( $value, $field_id = '', $multiple = false ) {
$type = self::get_object_type();
$id = self::get_object_id();
$field_id = $field_id ? $field_id : self::$field['id'];
if ( 'options-page' === $type ) {
self::update_option( $id, $field_id, $value );
} else {
if ( $multiple ) {
add_metadata( $type, $id, $field_id, $value, false );
} else {
update_metadata( $type, $id, $field_id, $value );
}
}
}