|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CMB2 Meta Box |
|
4
|
|
|
* |
|
5
|
|
|
* @since 0.1.0 |
|
6
|
|
|
* |
|
7
|
|
|
* @category WordPress_Plugin |
|
8
|
|
|
* @package CMB2 Admin Extension |
|
9
|
|
|
* @author twoelevenjay |
|
10
|
|
|
* @license GPL-2.0+ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
if ( ! class_exists( 'CMB2_Meta_Box' ) ) { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CMB2_Meta_Box. |
|
17
|
|
|
*/ |
|
18
|
|
|
class CMB2_Meta_Box { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Field prefix. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $prefix = '_cmb2_'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Current field array. |
|
29
|
|
|
* Store the current field while adding user defined fields |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $field = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Current field arguments array. |
|
37
|
|
|
* Store the current field arguments while adding user defined fields |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $field_args = array(); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Instance of this class. |
|
45
|
|
|
* |
|
46
|
|
|
* @var object |
|
47
|
|
|
*/ |
|
48
|
|
|
protected static $instance; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Initiate CMB2 Admin Extension object. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 0.0.1 |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct() { |
|
56
|
|
|
|
|
57
|
|
|
add_action( 'pre_current_active_plugins', array( $this, 'hide_cmb2_plugins' ) ); |
|
58
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
59
|
|
|
add_action( 'cmb2_init', array( $this, 'init_user_defined_meta_boxes_and_fields' ) ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return an instance of this class. |
|
64
|
|
|
* |
|
65
|
|
|
* @return object A single instance of this class. |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function get_instance() { |
|
68
|
|
|
// If the single instance hasn't been set, set it now. |
|
69
|
|
|
if ( self::$instance === null ) { |
|
70
|
|
|
self::$instance = new self(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return self::$instance; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Determine if current user has permission to CMB2 view plugins. |
|
79
|
|
|
* |
|
80
|
|
|
* @since 0.0.1 |
|
81
|
|
|
*/ |
|
82
|
|
|
public function is_cmb2_allowed() { |
|
83
|
|
|
|
|
84
|
|
|
$cmb2_settings = get_option( '_cmb2_settings' ); |
|
85
|
|
|
|
|
86
|
|
|
if ( empty( $cmb2_settings ) || current_user_can( 'administrator' ) ) { |
|
87
|
|
|
// No settings saved. |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$current_user = wp_get_current_user(); |
|
92
|
|
|
$allowed_users = isset( $cmb2_settings['_cmb2_user_multicheckbox'] ) ? $cmb2_settings['_cmb2_user_multicheckbox'] : array(); |
|
93
|
|
|
|
|
94
|
|
|
return empty( $allowed_users ) || in_array( $current_user->ID, $allowed_users, true ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Only show CMB2 plugins to users defined in settings. |
|
99
|
|
|
* |
|
100
|
|
|
* @since 0.0.1 |
|
101
|
|
|
*/ |
|
102
|
|
|
public function hide_cmb2_plugins() { |
|
103
|
|
|
|
|
104
|
|
|
global $wp_list_table; |
|
|
|
|
|
|
105
|
|
|
if ( ! $this->is_cmb2_allowed() ) { |
|
106
|
|
|
$to_hide = array( CMB2AE_CMB2_PLUGIN_FILE, 'cmb2-admin-extension/cmb2-admin-extension.php' ); |
|
107
|
|
|
$plugins = $wp_list_table->items; |
|
108
|
|
|
foreach ( array_keys( $plugins ) as $key ) { |
|
109
|
|
|
if ( in_array( $key, $to_hide, true ) ) { |
|
110
|
|
|
unset( $wp_list_table->items[ $key ] ); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Enqueue CMB2 Admin Extension scripts and styles. |
|
118
|
|
|
* |
|
119
|
|
|
* @since 0.0.8 |
|
120
|
|
|
*/ |
|
121
|
|
|
public function enqueue_scripts() { |
|
122
|
|
|
|
|
123
|
|
|
$screen = get_current_screen(); |
|
124
|
|
|
if ( $screen->post_type === 'meta_box' ) { |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
wp_register_style( 'cmb2_admin_styles', CMB2AE_URI . '/css/meta-box-fields.css', array(), '0.0.8' ); |
|
127
|
|
|
wp_enqueue_style( 'cmb2_admin_styles' ); |
|
128
|
|
|
wp_enqueue_script( 'cmb2_admin_scripts', CMB2AE_URI . '/js/meta-box-fields.js', array( 'jquery' ), '0.0.8', true ); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Check if the field type is repeatable. |
|
134
|
|
|
* |
|
135
|
|
|
* @since 0.0.6 |
|
136
|
|
|
* @param string $field_type A CMB2 field type. |
|
137
|
|
|
* @return boolean. |
|
|
|
|
|
|
138
|
|
|
*/ |
|
139
|
|
|
public function is_repeatable( $field_type ) { |
|
140
|
|
|
|
|
141
|
|
|
$repeatable_fields = array( |
|
142
|
|
|
'text', |
|
143
|
|
|
'text_small', |
|
144
|
|
|
'text_medium', |
|
145
|
|
|
'text_email', |
|
146
|
|
|
'text_url', |
|
147
|
|
|
'text_money', |
|
148
|
|
|
'textarea', |
|
149
|
|
|
'textarea_small', |
|
150
|
|
|
'textarea_code', |
|
151
|
|
|
'text_date', |
|
152
|
|
|
'text_time', |
|
153
|
|
|
'select_timezone', |
|
154
|
|
|
'text_date_timestamp', |
|
155
|
|
|
'text_datetime_timestamp', |
|
156
|
|
|
'text_datetime_timestamp_timezone', |
|
157
|
|
|
'colorpicker', |
|
158
|
|
|
'select', |
|
159
|
|
|
'multicheck', |
|
160
|
|
|
'multicheck_inline', |
|
161
|
|
|
'file', |
|
162
|
|
|
'file_list', |
|
163
|
|
|
); |
|
164
|
|
|
return in_array( $field_type, $repeatable_fields, true ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Check if field types should have the options argument. |
|
169
|
|
|
* |
|
170
|
|
|
* @since 0.0.6 |
|
171
|
|
|
* @param string $field_type A CMB2 field type. |
|
172
|
|
|
* @return boolean. |
|
|
|
|
|
|
173
|
|
|
*/ |
|
174
|
|
|
private function has_options( $field_type ) { |
|
175
|
|
|
|
|
176
|
|
|
$options_fields = array( |
|
177
|
|
|
'radio', |
|
178
|
|
|
'radio_inline', |
|
179
|
|
|
'taxonomy_radio', |
|
180
|
|
|
'taxonomy_radio_inline', |
|
181
|
|
|
'select', |
|
182
|
|
|
'taxonomy_select', |
|
183
|
|
|
'multicheck', |
|
184
|
|
|
'multicheck_inline', |
|
185
|
|
|
'taxonomy_multicheck', |
|
186
|
|
|
'taxonomy_multicheck_inline', |
|
187
|
|
|
); |
|
188
|
|
|
return in_array( $field_type, $options_fields, true ); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Conditional to check if the field argument should be added.. |
|
193
|
|
|
* |
|
194
|
|
|
* @since 1.1.4 |
|
195
|
|
|
* @param string $field_options String of options for fields liek select. |
|
196
|
|
|
*/ |
|
197
|
|
|
private function add_option_arg( $field_options ) { |
|
198
|
|
|
$field_options = preg_split( "/\\r\\n|\\r|\\n/", $field_options ); |
|
199
|
|
|
$options = array(); |
|
200
|
|
|
foreach ( $field_options as $option ) { |
|
201
|
|
|
$opt_arr = explode( ',', $option ); |
|
202
|
|
|
if ( ! isset( $opt_arr[1] ) ) { |
|
203
|
|
|
$options[ $option ] = $option; |
|
204
|
|
|
continue; |
|
205
|
|
|
} |
|
206
|
|
|
$options[ $opt_arr[0] ] = $opt_arr[1]; |
|
207
|
|
|
} |
|
208
|
|
|
$this->field_args['options'] = $options; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Conditional to check if the field argument should be added.. |
|
213
|
|
|
* |
|
214
|
|
|
* @since 1.1.4 |
|
215
|
|
|
* @param array $arg_value A CMB2 field type. |
|
216
|
|
|
*/ |
|
217
|
|
|
public function add_strpos_arg( $arg_value ) { |
|
218
|
|
|
|
|
219
|
|
|
if ( strpos( $this->field['_cmb2_field_type_select'], $arg_value[0] ) !== false && isset( $this->field[ $arg_value[2] ] ) && $this->field[ $arg_value[2] ] !== '' ) { |
|
|
|
|
|
|
220
|
|
|
|
|
221
|
|
View Code Duplication |
if ( is_array( $arg_value[1] ) ) { |
|
|
|
|
|
|
222
|
|
|
$this->field_args[ $arg_value[1][0] ][ $arg_value[1][1] ] = $this->field[ $arg_value[2] ]; |
|
223
|
|
|
return; |
|
224
|
|
|
} |
|
225
|
|
|
$this->field_args[ $arg_value[1] ] = $this->field[ $arg_value[2] ]; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Add the field argument. |
|
231
|
|
|
* |
|
232
|
|
|
* @since 1.1.4 |
|
233
|
|
|
* @param string $arg Field definition. |
|
234
|
|
|
* @param string|array $value A CMB2 field type. |
|
235
|
|
|
*/ |
|
236
|
|
|
public function add_arg( $arg, $value ) { |
|
237
|
|
|
|
|
238
|
|
|
if ( $this->should_add_arg( $this->field, $arg, $value[1] ) ) { |
|
239
|
|
|
|
|
240
|
|
View Code Duplication |
if ( is_array( $value[1] ) ) { |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
$this->field_args[ $value[0] ][ $value[1][0] ] = $this->field[ $value[1][1] ]; |
|
243
|
|
|
return; |
|
244
|
|
|
} |
|
245
|
|
|
$this->field_args[ $value[0] ] = $this->field[ $value[1] ]; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Conditional to check if the field argument should be added. |
|
251
|
|
|
* |
|
252
|
|
|
* @since 0.1.4 |
|
253
|
|
|
* @param array $field Field definition. |
|
254
|
|
|
* @param string $field_type A CMB2 field type. |
|
255
|
|
|
* @param string $field_key Field key to check. |
|
256
|
|
|
* @return boolean. |
|
|
|
|
|
|
257
|
|
|
*/ |
|
258
|
|
|
public static function should_add_arg( $field, $field_type, $field_key ) { |
|
259
|
|
|
|
|
260
|
|
|
return ( $field['_cmb2_field_type_select'] === $field_type && ( ! empty( $field[ $field_key ] ) && $field[ $field_key ] !== '' ) ); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Add the current field to the metabox. |
|
265
|
|
|
* |
|
266
|
|
|
* @since 0.2.0 |
|
267
|
|
|
* @param array $field Field definition. |
|
268
|
|
|
* @param object $metabox Metabox object. |
|
269
|
|
|
*/ |
|
270
|
|
|
public function add_field( $field, $metabox ) { |
|
271
|
|
|
|
|
272
|
|
|
$field = wp_parse_args( $field, array( |
|
273
|
|
|
'_cmb2_name_text' => null, |
|
274
|
|
|
'_cmb2_decription_textarea' => null, |
|
275
|
|
|
'_cmb2_field_type_select' => null, |
|
276
|
|
|
'_cmb2_options_textarea' => false, |
|
277
|
|
|
'_cmb2_repeatable_checkbox' => null, |
|
278
|
|
|
'_cmb2_none_checkbox' => null, |
|
279
|
|
|
) ); |
|
280
|
|
|
|
|
281
|
|
|
$this->field = $field; |
|
282
|
|
|
$field_id = '_' . strtolower( str_replace( ' ', '_', $field['_cmb2_name_text'] ) ); |
|
283
|
|
|
|
|
284
|
|
|
$this->field_args = array( |
|
285
|
|
|
'name' => $field['_cmb2_name_text'], |
|
286
|
|
|
'desc' => $field['_cmb2_decription_textarea'], |
|
287
|
|
|
'id' => $field_id, |
|
288
|
|
|
'type' => $field['_cmb2_field_type_select'], |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
|
|
$field_options = $field['_cmb2_options_textarea']; |
|
292
|
|
|
if ( $field_options ) { |
|
293
|
|
|
$this->add_option_arg( $field_options ); |
|
294
|
|
|
} |
|
295
|
|
|
$should_add_strpos = array( |
|
296
|
|
|
array( 'tax', 'taxonomy', '_cmb2_tax_options_radio_inline' ), |
|
297
|
|
|
array( 'tax', array( 'options', 'no_terms_text' ), '_cmb2_no_terms_text' ), |
|
298
|
|
|
array( 'multicheck', 'select_all_button', '_cmb2_select_all_checkbox' ), |
|
299
|
|
|
); |
|
300
|
|
|
foreach ( $should_add_strpos as $arg_value ) { |
|
301
|
|
|
$this->add_strpos_arg( $arg_value ); |
|
302
|
|
|
} |
|
303
|
|
|
if ( $field['_cmb2_repeatable_checkbox'] === 'on' && $this->is_repeatable( $field['_cmb2_field_type_select'] ) ) { |
|
|
|
|
|
|
304
|
|
|
$this->field_args['repeatable'] = true; |
|
305
|
|
|
} |
|
306
|
|
|
if ( $field['_cmb2_none_checkbox'] === 'on' && $this->has_options( $field['_cmb2_field_type_select'] ) ) { |
|
|
|
|
|
|
307
|
|
|
$this->field_args['show_option_none'] = true; |
|
308
|
|
|
} |
|
309
|
|
|
$should_add = array( |
|
310
|
|
|
'text_url' => array( 'protocols', '_cmb2_protocols_checkbox' ), |
|
311
|
|
|
'text_money' => array( 'before_field', '_cmb2_currency_text' ), |
|
312
|
|
|
'text_time' => array( 'time_format', '_cmb2_time_format' ), |
|
313
|
|
|
'text_datetime_timestamp_timezone' => array( 'time_format', '_cmb2_time_format' ), |
|
314
|
|
|
'text_datetime_timestamp' => array( 'time_format', '_cmb2_time_format' ), |
|
315
|
|
|
'text_date' => array( 'date_format', '_cmb2_date_format' ), |
|
316
|
|
|
'text_date_timestamp' => array( 'date_format', '_cmb2_date_format' ), |
|
317
|
|
|
'select_timezone' => array( 'timezone_meta_key', '_cmb2_time_zone_key_select' ), |
|
318
|
|
|
'text_datetime_timestamp_timezone' => array( 'timezone_meta_key', '_cmb2_time_zone_key_select' ), |
|
319
|
|
|
); |
|
320
|
|
|
foreach ( $should_add as $arg => $value ) { |
|
321
|
|
|
$this->add_arg( $arg, $value ); |
|
322
|
|
|
} |
|
323
|
|
|
$metabox->add_field( $this->field_args ); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Get the metabox post meta. |
|
328
|
|
|
* |
|
329
|
|
|
* @since 0.2.0 |
|
330
|
|
|
* @param object $user_meta_box Post object. |
|
331
|
|
|
*/ |
|
332
|
|
|
public function get_meta_data( $user_meta_box ) { |
|
333
|
|
|
|
|
334
|
|
|
$prefix = $this->prefix; |
|
335
|
|
|
$metabox_id = $user_meta_box->ID; |
|
336
|
|
|
|
|
337
|
|
|
$meta_data['title'] = get_the_title( $metabox_id ); |
|
|
|
|
|
|
338
|
|
|
$meta_data['id'] = str_replace( '-', '_', $user_meta_box->post_name ); |
|
339
|
|
|
$meta_data['post_type'] = cmbf( $metabox_id, $prefix . 'post_type_multicheckbox' ); |
|
340
|
|
|
$post_id_text = cmbf( $metabox_id, $prefix . 'post_id_text' ); |
|
341
|
|
|
$meta_data['show_on'] = $post_id_text !== '' ? explode( ',', $post_id_text ) : array(); |
|
342
|
|
|
$meta_data['context'] = cmbf( $metabox_id, $prefix . 'context_radio' ); |
|
343
|
|
|
$meta_data['priority'] = cmbf( $metabox_id, $prefix . 'priority_radio' ); |
|
344
|
|
|
$meta_data['show_names'] = cmbf( $metabox_id, $prefix . 'show_names' ) === 'on' ? true : false; |
|
345
|
|
|
$meta_data['disable_styles'] = cmbf( $metabox_id, $prefix . 'disable_styles' ) === 'on' ? true : false; |
|
346
|
|
|
$meta_data['closed'] = cmbf( $metabox_id, $prefix . 'closed' ) === 'on' ? true : false; |
|
347
|
|
|
$meta_data['fields'] = cmbf( $metabox_id, $prefix . 'custom_field' ); |
|
348
|
|
|
|
|
349
|
|
|
return $meta_data; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Loop through user defined meta_box and creates the custom meta boxes and fields. |
|
354
|
|
|
* |
|
355
|
|
|
* @since 0.0.1 |
|
356
|
|
|
*/ |
|
357
|
|
|
public function init_user_defined_meta_boxes_and_fields() { |
|
358
|
|
|
|
|
359
|
|
|
$args = array( |
|
360
|
|
|
'post_type' => 'meta_box', |
|
361
|
|
|
'post_status' => 'publish', |
|
362
|
|
|
'posts_per_page' => -1, |
|
|
|
|
|
|
363
|
|
|
'suppress_filters' => false, |
|
364
|
|
|
); |
|
365
|
|
|
|
|
366
|
|
|
$user_meta_boxes = new WP_Query( $args ); |
|
367
|
|
|
|
|
368
|
|
|
foreach ( $user_meta_boxes->posts as $user_meta_box ) { |
|
369
|
|
|
|
|
370
|
|
|
$meta_data = $this->get_meta_data( $user_meta_box ); |
|
371
|
|
|
/** |
|
372
|
|
|
* Initiate the metabox. |
|
373
|
|
|
*/ |
|
374
|
|
|
$new_cmb2_args = array( |
|
375
|
|
|
'id' => $meta_data['id'], |
|
376
|
|
|
'title' => $meta_data['title'], |
|
377
|
|
|
'object_types' => $meta_data['post_type'], // Post type. |
|
378
|
|
|
'context' => $meta_data['context'], |
|
379
|
|
|
'priority' => $meta_data['priority'], |
|
380
|
|
|
'show_names' => $meta_data['show_names'], |
|
381
|
|
|
'cmb_styles' => $meta_data['disable_styles'], |
|
382
|
|
|
'closed' => $meta_data['closed'], |
|
383
|
|
|
); |
|
384
|
|
|
if ( ! empty( $meta_data['show_on'] ) ) { |
|
385
|
|
|
$new_cmb2_args['show_on'] = array( |
|
386
|
|
|
'key' => 'id', |
|
387
|
|
|
'value' => $meta_data['show_on'], |
|
388
|
|
|
); |
|
389
|
|
|
} |
|
390
|
|
|
${ 'cmb_' . $meta_data['id'] } = new_cmb2_box( $new_cmb2_args ); |
|
391
|
|
|
foreach ( $meta_data['fields'] as $field ) { |
|
392
|
|
|
|
|
393
|
|
|
$this->add_field( $field, ${ 'cmb_' . $meta_data['id'] } ); |
|
394
|
|
|
} |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
if ( ! function_exists( 'cmb2ae_metabox' ) ) { |
|
401
|
|
|
/** |
|
402
|
|
|
* Main instance of CMB2_Meta_Box. |
|
403
|
|
|
* |
|
404
|
|
|
* @since 0.1.0 |
|
405
|
|
|
* @return object Main instance of the CMB2_Meta_Box class. |
|
406
|
|
|
*/ |
|
407
|
|
|
function cmb2ae_metabox() { |
|
408
|
|
|
return CMB2_Meta_Box::get_instance(); |
|
409
|
|
|
} |
|
410
|
|
|
} |
|
411
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state