1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMB2 - The core metabox object |
4
|
|
|
* |
5
|
|
|
* @category WordPress_Plugin |
6
|
|
|
* @package CMB2 |
7
|
|
|
* @author WebDevStudios |
8
|
|
|
* @license GPL-2.0+ |
9
|
|
|
* @link http://webdevstudios.com |
10
|
|
|
* |
11
|
|
|
* @property-read string $cmb_id |
12
|
|
|
* @property-read array $meta_box |
13
|
|
|
* @property-read array $updated |
14
|
|
|
* @property-read bool $has_columns |
15
|
|
|
* @property-read array $tax_metaboxes_to_remove |
16
|
|
|
*/ |
17
|
|
|
class CMB2 extends CMB2_Base { |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The object properties name. |
21
|
|
|
* @var string |
22
|
|
|
* @since 2.2.3 |
23
|
|
|
*/ |
24
|
|
|
protected $properties_name = 'meta_box'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Metabox Config array |
28
|
|
|
* @var array |
29
|
|
|
* @since 0.9.0 |
30
|
|
|
*/ |
31
|
|
|
protected $meta_box = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Type of object registered for metabox. (e.g., post, user, or comment) |
35
|
|
|
* @var string |
36
|
|
|
* @since 1.0.0 |
37
|
|
|
*/ |
38
|
|
|
protected $mb_object_type = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* List of fields that are changed/updated on save |
42
|
|
|
* @var array |
43
|
|
|
* @since 1.1.0 |
44
|
|
|
*/ |
45
|
|
|
protected $updated = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Metabox Defaults |
49
|
|
|
* @var array |
50
|
|
|
* @since 1.0.1 |
51
|
|
|
*/ |
52
|
|
|
protected $mb_defaults = array( |
53
|
|
|
'id' => '', |
54
|
|
|
'title' => '', |
55
|
|
|
// Post type slug, or 'user', 'term', 'comment', or 'options-page' |
|
|
|
|
56
|
|
|
'object_types' => array(), |
57
|
|
|
/* |
58
|
|
|
* The context within the screen where the boxes should display. Available contexts vary |
59
|
|
|
* from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. |
60
|
|
|
* |
61
|
|
|
* For placement in locations outside of a metabox, other options include: |
62
|
|
|
* 'form_top', 'before_permalink', 'after_title', 'after_editor' |
63
|
|
|
* |
64
|
|
|
* Comments screen contexts include 'normal' and 'side'. Default is 'normal'. |
65
|
|
|
*/ |
66
|
|
|
'context' => 'normal', |
67
|
|
|
'priority' => 'high', |
68
|
|
|
'show_names' => true, // Show field names on the left |
69
|
|
|
'show_on_cb' => null, // Callback to determine if metabox should display. |
70
|
|
|
'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb' |
71
|
|
|
'cmb_styles' => true, // Include CMB2 stylesheet |
72
|
|
|
'enqueue_js' => true, // Include CMB2 JS |
73
|
|
|
'fields' => array(), |
74
|
|
|
'hookup' => true, |
75
|
|
|
'save_fields' => true, // Will not save during hookup if false |
76
|
|
|
'closed' => false, // Default to metabox being closed? |
77
|
|
|
'taxonomies' => array(), |
78
|
|
|
'new_user_section' => 'add-new-user', // or 'add-existing-user' |
79
|
|
|
'new_term_section' => true, |
80
|
|
|
'show_in_rest' => false, |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Metabox field objects |
85
|
|
|
* @var array |
86
|
|
|
* @since 2.0.3 |
87
|
|
|
*/ |
88
|
|
|
protected $fields = array(); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* An array of hidden fields to output at the end of the form |
92
|
|
|
* @var array |
93
|
|
|
* @since 2.0.0 |
94
|
|
|
*/ |
95
|
|
|
protected $hidden_fields = array(); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Array of key => value data for saving. Likely $_POST data. |
99
|
|
|
* @var string |
100
|
|
|
* @since 2.0.0 |
101
|
|
|
*/ |
102
|
|
|
protected $generated_nonce = ''; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Whether there are fields to be shown in columns. Set in CMB2::add_field(). |
106
|
|
|
* @var bool |
107
|
|
|
* @since 2.2.2 |
108
|
|
|
*/ |
109
|
|
|
protected $has_columns = false; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* If taxonomy field is requesting to remove_default, we store the taxonomy here. |
113
|
|
|
* @var array |
114
|
|
|
* @since 2.2.3 |
115
|
|
|
*/ |
116
|
|
|
protected $tax_metaboxes_to_remove = array(); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get started |
120
|
|
|
* @since 0.4.0 |
121
|
|
|
* @param array $config Metabox config array |
122
|
|
|
* @param integer $object_id Optional object id |
123
|
|
|
*/ |
124
|
|
|
public function __construct( $config, $object_id = 0 ) { |
125
|
|
|
|
126
|
|
|
if ( empty( $config['id'] ) ) { |
127
|
|
|
wp_die( esc_html__( 'Metabox configuration is required to have an ID parameter.', 'cmb2' ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->meta_box = wp_parse_args( $config, $this->mb_defaults ); |
131
|
|
|
$this->meta_box['fields'] = array(); |
132
|
|
|
|
133
|
|
|
$this->object_id( $object_id ); |
134
|
|
|
$this->mb_object_type(); |
135
|
|
|
$this->cmb_id = $config['id']; |
136
|
|
|
|
137
|
|
|
if ( ! empty( $config['fields'] ) && is_array( $config['fields'] ) ) { |
138
|
|
|
$this->add_fields( $config['fields'] ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
CMB2_Boxes::add( $this ); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Hook during initiation of CMB2 object |
145
|
|
|
* |
146
|
|
|
* The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. |
147
|
|
|
* |
148
|
|
|
* @param array $cmb This CMB2 object |
149
|
|
|
*/ |
150
|
|
|
do_action( "cmb2_init_{$this->cmb_id}", $this ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Loops through and displays fields |
155
|
|
|
* @since 1.0.0 |
156
|
|
|
* @param int $object_id Object ID |
157
|
|
|
* @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
158
|
|
|
*/ |
159
|
|
|
public function show_form( $object_id = 0, $object_type = '' ) { |
160
|
|
|
$this->render_form_open( $object_id, $object_type ); |
161
|
|
|
|
162
|
|
|
foreach ( $this->prop( 'fields' ) as $field_args ) { |
163
|
|
|
$this->render_field( $field_args ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->render_form_close( $object_id, $object_type ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Outputs the opening form markup and runs corresponding hooks: |
171
|
|
|
* 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
172
|
|
|
* @since 2.2.0 |
173
|
|
|
* @param integer $object_id Object ID |
174
|
|
|
* @param string $object_type Object type |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function render_form_open( $object_id = 0, $object_type = '' ) { |
178
|
|
|
$object_type = $this->object_type( $object_type ); |
179
|
|
|
$object_id = $this->object_id( $object_id ); |
180
|
|
|
|
181
|
|
|
echo "\n<!-- Begin CMB2 Fields -->\n"; |
182
|
|
|
|
183
|
|
|
$this->nonce_field(); |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Hook before form table begins |
187
|
|
|
* |
188
|
|
|
* @param array $cmb_id The current box ID |
189
|
|
|
* @param int $object_id The ID of the current object |
190
|
|
|
* @param string $object_type The type of object you are working with. |
191
|
|
|
* Usually `post` (this applies to all post-types). |
192
|
|
|
* Could also be `comment`, `user` or `options-page`. |
193
|
|
|
* @param array $cmb This CMB2 object |
194
|
|
|
*/ |
195
|
|
|
do_action( 'cmb2_before_form', $this->cmb_id, $object_id, $object_type, $this ); |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Hook before form table begins |
199
|
|
|
* |
200
|
|
|
* The first dynamic portion of the hook name, $object_type, is the type of object |
201
|
|
|
* you are working with. Usually `post` (this applies to all post-types). |
202
|
|
|
* Could also be `comment`, `user` or `options-page`. |
203
|
|
|
* |
204
|
|
|
* The second dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
205
|
|
|
* |
206
|
|
|
* @param array $cmb_id The current box ID |
207
|
|
|
* @param int $object_id The ID of the current object |
208
|
|
|
* @param array $cmb This CMB2 object |
209
|
|
|
*/ |
210
|
|
|
do_action( "cmb2_before_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
211
|
|
|
|
212
|
|
|
echo '<div class="', $this->box_classes(), '"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">'; |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Defines the classes for the CMB2 form/wrap. |
218
|
|
|
* |
219
|
|
|
* @since 2.0.0 |
220
|
|
|
* @return string Space concatenated list of classes |
221
|
|
|
*/ |
222
|
|
|
public function box_classes() { |
223
|
|
|
|
224
|
|
|
$classes = array( 'cmb2-wrap', 'form-table' ); |
225
|
|
|
|
226
|
|
|
// Use the callback to fetch classes. |
227
|
|
View Code Duplication |
if ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
|
|
|
|
228
|
|
|
$added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
229
|
|
|
$classes = array_merge( $classes, $added_classes ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
View Code Duplication |
if ( $added_classes = $this->prop( 'classes' ) ) { |
|
|
|
|
233
|
|
|
$added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
234
|
|
|
$classes = array_merge( $classes, $added_classes ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Add our context classes for non-standard metaboxes. |
239
|
|
|
* |
240
|
|
|
* @since 2.2.4 |
241
|
|
|
*/ |
242
|
|
|
if ( ! empty( $this->prop( 'context' ) ) && in_array( $this->prop( 'context' ), array( 'form_top', 'before_permalink', 'after_title', 'after_editor' ) ) ) { |
243
|
|
|
|
244
|
|
|
// Include the postbox wrapper if we have no title, unless that property is set to false. |
245
|
|
|
if ( empty( $this->prop( 'title' ) ) && empty( $this->prop( 'remove_box_wrap' ) ) ) { |
246
|
|
|
$context[] = 'postbox cmb2-context-wrap-no-title'; |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// Include a generic context wrapper. |
250
|
|
|
$context[] = 'cmb2-context-wrap'; |
|
|
|
|
251
|
|
|
|
252
|
|
|
// Include a context-type based context wrapper. |
253
|
|
|
$context[] = 'cmb2-context-wrap-' . $this->prop( 'context' ); |
254
|
|
|
|
255
|
|
|
// Include an ID based context wrapper as well. |
256
|
|
|
$context[] = 'cmb2-context-wrap-' . $this->prop( 'id' ); |
257
|
|
|
|
258
|
|
|
// And merge all the classes back into the array. |
259
|
|
|
$classes = array_merge( $classes, $context ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Globally filter box wrap classes |
264
|
|
|
* |
265
|
|
|
* @since 2.2.2 |
266
|
|
|
* |
267
|
|
|
* @param string $classes Array of classes for the cmb2-wrap. |
268
|
|
|
* @param CMB2 $cmb This CMB2 object. |
269
|
|
|
*/ |
270
|
|
|
$classes = apply_filters( 'cmb2_wrap_classes', $classes, $this ); |
271
|
|
|
|
272
|
|
|
// Clean up. |
273
|
|
|
$classes = array_map( 'strip_tags', array_filter( $classes ) ); |
274
|
|
|
|
275
|
|
|
// Remove any duplicates. |
276
|
|
|
$classes = array_unique( $classes ); |
277
|
|
|
|
278
|
|
|
// Make a string. |
279
|
|
|
return implode( ' ', $classes ); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Outputs the closing form markup and runs corresponding hooks: |
284
|
|
|
* 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
285
|
|
|
* @since 2.2.0 |
286
|
|
|
* @param integer $object_id Object ID |
287
|
|
|
* @param string $object_type Object type |
288
|
|
|
* @return void |
289
|
|
|
*/ |
290
|
|
|
public function render_form_close( $object_id = 0, $object_type = '' ) { |
291
|
|
|
$object_type = $this->object_type( $object_type ); |
292
|
|
|
$object_id = $this->object_id( $object_id ); |
293
|
|
|
|
294
|
|
|
echo '</div></div>'; |
295
|
|
|
|
296
|
|
|
$this->render_hidden_fields(); |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Hook after form form has been rendered |
300
|
|
|
* |
301
|
|
|
* @param array $cmb_id The current box ID |
302
|
|
|
* @param int $object_id The ID of the current object |
303
|
|
|
* @param string $object_type The type of object you are working with. |
304
|
|
|
* Usually `post` (this applies to all post-types). |
305
|
|
|
* Could also be `comment`, `user` or `options-page`. |
306
|
|
|
* @param array $cmb This CMB2 object |
307
|
|
|
*/ |
308
|
|
|
do_action( 'cmb2_after_form', $this->cmb_id, $object_id, $object_type, $this ); |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Hook after form form has been rendered |
312
|
|
|
* |
313
|
|
|
* The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
314
|
|
|
* |
315
|
|
|
* The first dynamic portion of the hook name, $object_type, is the type of object |
316
|
|
|
* you are working with. Usually `post` (this applies to all post-types). |
317
|
|
|
* Could also be `comment`, `user` or `options-page`. |
318
|
|
|
* |
319
|
|
|
* @param int $object_id The ID of the current object |
320
|
|
|
* @param array $cmb This CMB2 object |
321
|
|
|
*/ |
322
|
|
|
do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
323
|
|
|
|
324
|
|
|
echo "\n<!-- End CMB2 Fields -->\n"; |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Renders a field based on the field type |
330
|
|
|
* @since 2.2.0 |
331
|
|
|
* @param array $field_args A field configuration array. |
332
|
|
|
* @return mixed CMB2_Field object if successful. |
333
|
|
|
*/ |
334
|
|
|
public function render_field( $field_args ) { |
335
|
|
|
$field_args['context'] = $this->prop( 'context' ); |
336
|
|
|
|
337
|
|
|
if ( 'group' == $field_args['type'] ) { |
338
|
|
|
|
339
|
|
|
if ( ! isset( $field_args['show_names'] ) ) { |
340
|
|
|
$field_args['show_names'] = $this->prop( 'show_names' ); |
341
|
|
|
} |
342
|
|
|
$field = $this->render_group( $field_args ); |
343
|
|
|
|
344
|
|
|
} elseif ( 'hidden' == $field_args['type'] && $this->get_field( $field_args )->should_show() ) { |
345
|
|
|
// Save rendering for after the metabox |
346
|
|
|
$field = $this->add_hidden_field( $field_args ); |
347
|
|
|
|
348
|
|
|
} else { |
349
|
|
|
|
350
|
|
|
$field_args['show_names'] = $this->prop( 'show_names' ); |
351
|
|
|
|
352
|
|
|
// Render default fields |
353
|
|
|
$field = $this->get_field( $field_args )->render_field(); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $field; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Render a repeatable group. |
361
|
|
|
* @param array $args Array of field arguments for a group field parent. |
362
|
|
|
* @return CMB2_Field|null Group field object. |
363
|
|
|
*/ |
364
|
|
|
public function render_group( $args ) { |
365
|
|
|
|
366
|
|
|
if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
367
|
|
|
return; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$field_group = $this->get_field( $args ); |
371
|
|
|
|
372
|
|
|
// If field is requesting to be conditionally shown |
373
|
|
|
if ( ! $field_group || ! $field_group->should_show() ) { |
374
|
|
|
return; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
$desc = $field_group->args( 'description' ); |
378
|
|
|
$label = $field_group->args( 'name' ); |
379
|
|
|
$group_val = (array) $field_group->value(); |
380
|
|
|
$remove_disabled = count( $group_val ) <= 1 ? 'disabled="disabled" ' : ''; |
381
|
|
|
$field_group->index = 0; |
382
|
|
|
|
383
|
|
|
$field_group->peform_param_callback( 'before_group' ); |
384
|
|
|
|
385
|
|
|
echo '<div class="cmb-row cmb-repeat-group-wrap ', $field_group->row_classes(), '" data-fieldtype="group"><div class="cmb-td"><div data-groupid="', $field_group->id(), '" id="', $field_group->id(), '_repeat" ', $this->group_wrap_attributes( $field_group ), '>'; |
386
|
|
|
|
387
|
|
|
if ( $desc || $label ) { |
388
|
|
|
$class = $desc ? ' cmb-group-description' : ''; |
389
|
|
|
echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
390
|
|
|
if ( $label ) { |
391
|
|
|
echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
392
|
|
|
} |
393
|
|
|
if ( $desc ) { |
394
|
|
|
echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
395
|
|
|
} |
396
|
|
|
echo '</div></div>'; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
if ( ! empty( $group_val ) ) { |
400
|
|
|
foreach ( $group_val as $group_key => $field_id ) { |
401
|
|
|
$this->render_group_row( $field_group, $remove_disabled ); |
402
|
|
|
$field_group->index++; |
403
|
|
|
} |
404
|
|
|
} else { |
405
|
|
|
$this->render_group_row( $field_group, $remove_disabled ); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
if ( $field_group->args( 'repeatable' ) ) { |
409
|
|
|
echo '<div class="cmb-row"><div class="cmb-td"><p class="cmb-add-row"><button type="button" data-selector="', $field_group->id(), '_repeat" data-grouptitle="', $field_group->options( 'group_title' ), '" class="cmb-add-group-row button">', $field_group->options( 'add_button' ), '</button></p></div></div>'; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
echo '</div></div></div>'; |
413
|
|
|
|
414
|
|
|
$field_group->peform_param_callback( 'after_group' ); |
415
|
|
|
|
416
|
|
|
return $field_group; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Get the group wrap attributes, which are passed through a filter. |
421
|
|
|
* @since 2.2.3 |
422
|
|
|
* @param CMB2_Field $field_group The group CMB2_Field object. |
423
|
|
|
* @return string The attributes string. |
424
|
|
|
*/ |
425
|
|
|
public function group_wrap_attributes( $field_group ) { |
426
|
|
|
$classes = 'cmb-nested cmb-field-list cmb-repeatable-group'; |
427
|
|
|
$classes .= $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
428
|
|
|
$classes .= $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
429
|
|
|
|
430
|
|
|
$group_wrap_attributes = array( |
431
|
|
|
'class' => $classes, |
432
|
|
|
'style' => 'width:100%;', |
433
|
|
|
); |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Allow for adding additional HTML attributes to a group wrapper. |
437
|
|
|
* |
438
|
|
|
* The attributes will be an array of key => value pairs for each attribute. |
439
|
|
|
* |
440
|
|
|
* @since 2.2.2 |
441
|
|
|
* |
442
|
|
|
* @param string $group_wrap_attributes Current attributes array. |
443
|
|
|
* |
444
|
|
|
* @param CMB2_Field $field_group The group CMB2_Field object. |
445
|
|
|
*/ |
446
|
|
|
$group_wrap_attributes = apply_filters( 'cmb2_group_wrap_attributes', $group_wrap_attributes, $field_group ); |
447
|
|
|
|
448
|
|
|
return CMB2_Utils::concat_attrs( $group_wrap_attributes ); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Render a repeatable group row |
453
|
|
|
* @since 1.0.2 |
454
|
|
|
* @param CMB2_Field $field_group CMB2_Field group field object |
455
|
|
|
* @param string $remove_disabled Attribute string to disable the remove button |
456
|
|
|
*/ |
457
|
|
|
public function render_group_row( $field_group, $remove_disabled ) { |
458
|
|
|
|
459
|
|
|
$field_group->peform_param_callback( 'before_group_row' ); |
460
|
|
|
$closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; |
461
|
|
|
|
462
|
|
|
echo ' |
463
|
|
|
<div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; |
464
|
|
|
|
465
|
|
|
if ( $field_group->args( 'repeatable' ) ) { |
466
|
|
|
echo '<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row" title="', esc_attr( $field_group->options( 'remove_button' ) ), '"></button>'; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
echo ' |
470
|
|
|
<div class="cmbhandle" title="' , esc_attr__( 'Click to toggle', 'cmb2' ), '"><br></div> |
471
|
|
|
<h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> |
|
|
|
|
472
|
|
|
|
473
|
|
|
<div class="inside cmb-td cmb-nested cmb-field-list">'; |
474
|
|
|
// Loop and render repeatable group fields |
475
|
|
|
foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { |
476
|
|
|
if ( 'hidden' == $field_args['type'] ) { |
477
|
|
|
|
478
|
|
|
// Save rendering for after the metabox |
479
|
|
|
$this->add_hidden_field( $field_args, $field_group ); |
480
|
|
|
|
481
|
|
|
} else { |
482
|
|
|
|
483
|
|
|
$field_args['show_names'] = $field_group->args( 'show_names' ); |
484
|
|
|
$field_args['context'] = $field_group->args( 'context' ); |
485
|
|
|
|
486
|
|
|
$field = $this->get_field( $field_args, $field_group )->render_field(); |
|
|
|
|
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
if ( $field_group->args( 'repeatable' ) ) { |
490
|
|
|
echo ' |
491
|
|
|
<div class="cmb-row cmb-remove-field-row"> |
492
|
|
|
<div class="cmb-remove-row"> |
493
|
|
|
<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="button cmb-remove-group-row alignright">', $field_group->options( 'remove_button' ), '</button> |
494
|
|
|
</div> |
495
|
|
|
</div> |
496
|
|
|
'; |
497
|
|
|
} |
498
|
|
|
echo ' |
499
|
|
|
</div> |
500
|
|
|
</div> |
501
|
|
|
'; |
502
|
|
|
|
503
|
|
|
$field_group->peform_param_callback( 'after_group_row' ); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Add a hidden field to the list of hidden fields to be rendered later |
508
|
|
|
* @since 2.0.0 |
509
|
|
|
* @param array $field_args Array of field arguments to be passed to CMB2_Field |
510
|
|
|
* @param CMB2_Field|null $field_group CMB2_Field group field object |
511
|
|
|
*/ |
512
|
|
|
public function add_hidden_field( $field_args, $field_group = null ) { |
513
|
|
|
if ( isset( $field_args['field_args'] ) ) { |
514
|
|
|
// For back-compatibility. |
515
|
|
|
$field = new CMB2_Field( $field_args ); |
516
|
|
|
} else { |
517
|
|
|
$field = $this->get_new_field( $field_args, $field_group ); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
$type = new CMB2_Types( $field ); |
521
|
|
|
|
522
|
|
|
if ( $field_group ) { |
523
|
|
|
$type->iterator = $field_group->index; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
$this->hidden_fields[] = $type; |
527
|
|
|
|
528
|
|
|
return $field; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Loop through and output hidden fields |
533
|
|
|
* @since 2.0.0 |
534
|
|
|
*/ |
535
|
|
|
public function render_hidden_fields() { |
536
|
|
|
if ( ! empty( $this->hidden_fields ) ) { |
537
|
|
|
foreach ( $this->hidden_fields as $hidden ) { |
538
|
|
|
$hidden->render(); |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Returns array of sanitized field values (without saving them) |
545
|
|
|
* @since 2.0.3 |
546
|
|
|
* @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
547
|
|
|
*/ |
548
|
|
|
public function get_sanitized_values( array $data_to_sanitize ) { |
549
|
|
|
$this->data_to_save = $data_to_sanitize; |
550
|
|
|
$stored_id = $this->object_id(); |
551
|
|
|
|
552
|
|
|
// We do this So CMB will sanitize our data for us, but not save it |
553
|
|
|
$this->object_id( '_' ); |
554
|
|
|
|
555
|
|
|
// Ensure temp. data store is empty |
556
|
|
|
cmb2_options( 0 )->set(); |
557
|
|
|
|
558
|
|
|
// We want to get any taxonomy values back. |
559
|
|
|
add_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' ); |
560
|
|
|
|
561
|
|
|
// Process/save fields |
562
|
|
|
$this->process_fields(); |
563
|
|
|
|
564
|
|
|
// Put things back the way they were. |
565
|
|
|
remove_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' ); |
566
|
|
|
|
567
|
|
|
// Get data from temp. data store |
568
|
|
|
$sanitized_values = cmb2_options( 0 )->get_options(); |
569
|
|
|
|
570
|
|
|
// Empty out temp. data store again |
571
|
|
|
cmb2_options( 0 )->set(); |
572
|
|
|
|
573
|
|
|
// Reset the object id |
574
|
|
|
$this->object_id( $stored_id ); |
575
|
|
|
|
576
|
|
|
return $sanitized_values; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Loops through and saves field data |
581
|
|
|
* @since 1.0.0 |
582
|
|
|
* @param int $object_id Object ID |
583
|
|
|
* @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
584
|
|
|
* @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
585
|
|
|
*/ |
586
|
|
|
public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
|
|
|
587
|
|
|
|
588
|
|
|
// Fall-back to $_POST data |
589
|
|
|
$this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST; |
590
|
|
|
$object_id = $this->object_id( $object_id ); |
591
|
|
|
$object_type = $this->object_type( $object_type ); |
592
|
|
|
|
593
|
|
|
$this->process_fields(); |
594
|
|
|
|
595
|
|
|
// If options page, save the updated options |
596
|
|
|
if ( 'options-page' == $object_type ) { |
597
|
|
|
cmb2_options( $object_id )->set(); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
$this->after_save(); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Process and save form fields |
605
|
|
|
* @since 2.0.0 |
606
|
|
|
*/ |
607
|
|
|
public function process_fields() { |
608
|
|
|
|
609
|
|
|
$this->pre_process(); |
610
|
|
|
|
611
|
|
|
// Remove the show_on properties so saving works |
612
|
|
|
$this->prop( 'show_on', array() ); |
613
|
|
|
|
614
|
|
|
// save field ids of those that are updated |
615
|
|
|
$this->updated = array(); |
616
|
|
|
|
617
|
|
|
foreach ( $this->prop( 'fields' ) as $field_args ) { |
618
|
|
|
$this->process_field( $field_args ); |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Process and save a field |
624
|
|
|
* @since 2.0.0 |
625
|
|
|
* @param array $field_args Array of field arguments |
626
|
|
|
*/ |
627
|
|
|
public function process_field( $field_args ) { |
628
|
|
|
|
629
|
|
|
switch ( $field_args['type'] ) { |
630
|
|
|
|
631
|
|
|
case 'group': |
632
|
|
|
if ( $this->save_group( $field_args ) ) { |
633
|
|
|
$this->updated[] = $field_args['id']; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
break; |
637
|
|
|
|
638
|
|
|
case 'title': |
639
|
|
|
// Don't process title fields |
640
|
|
|
break; |
641
|
|
|
|
642
|
|
|
default: |
|
|
|
|
643
|
|
|
|
644
|
|
|
$field = $this->get_new_field( $field_args ); |
645
|
|
|
|
646
|
|
|
if ( $field->save_field_from_data( $this->data_to_save ) ) { |
647
|
|
|
$this->updated[] = $field->id(); |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
break; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
public function pre_process() { |
656
|
|
|
/** |
657
|
|
|
* Fires before fields have been processed/saved. |
658
|
|
|
* |
659
|
|
|
* The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
660
|
|
|
* |
661
|
|
|
* The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
662
|
|
|
* Usually `post` (this applies to all post-types). |
663
|
|
|
* Could also be `comment`, `user` or `options-page`. |
664
|
|
|
* |
665
|
|
|
* @param array $cmb This CMB2 object |
666
|
|
|
* @param int $object_id The ID of the current object |
667
|
|
|
*/ |
668
|
|
|
do_action( "cmb2_{$this->object_type()}_process_fields_{$this->cmb_id}", $this, $this->object_id() ); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
public function after_save() { |
672
|
|
|
$object_type = $this->object_type(); |
673
|
|
|
$object_id = $this->object_id(); |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Fires after all fields have been saved. |
677
|
|
|
* |
678
|
|
|
* The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
679
|
|
|
* Usually `post` (this applies to all post-types). |
680
|
|
|
* Could also be `comment`, `user` or `options-page`. |
681
|
|
|
* |
682
|
|
|
* @param int $object_id The ID of the current object |
683
|
|
|
* @param array $cmb_id The current box ID |
684
|
|
|
* @param string $updated Array of field ids that were updated. |
685
|
|
|
* Will only include field ids that had values change. |
686
|
|
|
* @param array $cmb This CMB2 object |
687
|
|
|
*/ |
688
|
|
|
do_action( "cmb2_save_{$object_type}_fields", $object_id, $this->cmb_id, $this->updated, $this ); |
689
|
|
|
|
690
|
|
|
/** |
691
|
|
|
* Fires after all fields have been saved. |
692
|
|
|
* |
693
|
|
|
* The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
694
|
|
|
* |
695
|
|
|
* The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
696
|
|
|
* Usually `post` (this applies to all post-types). |
697
|
|
|
* Could also be `comment`, `user` or `options-page`. |
698
|
|
|
* |
699
|
|
|
* @param int $object_id The ID of the current object |
700
|
|
|
* @param string $updated Array of field ids that were updated. |
701
|
|
|
* Will only include field ids that had values change. |
702
|
|
|
* @param array $cmb This CMB2 object |
703
|
|
|
*/ |
704
|
|
|
do_action( "cmb2_save_{$object_type}_fields_{$this->cmb_id}", $object_id, $this->updated, $this ); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Save a repeatable group |
709
|
|
|
* @since 1.x.x |
710
|
|
|
* @param array $args Field arguments array |
711
|
|
|
* @return mixed Return of CMB2_Field::update_data() |
712
|
|
|
*/ |
713
|
|
|
public function save_group( $args ) { |
714
|
|
|
if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
715
|
|
|
return; |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
return $this->save_group_field( $this->get_new_field( $args ) ); |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* Save a repeatable group |
723
|
|
|
* @since 1.x.x |
724
|
|
|
* @param CMB2_Field $field_group CMB2_Field group field object |
725
|
|
|
* @return mixed Return of CMB2_Field::update_data() |
726
|
|
|
*/ |
727
|
|
|
public function save_group_field( $field_group ) { |
728
|
|
|
$base_id = $field_group->id(); |
729
|
|
|
|
730
|
|
|
if ( ! isset( $this->data_to_save[ $base_id ] ) ) { |
731
|
|
|
return; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
$old = $field_group->get_data(); |
735
|
|
|
// Check if group field has sanitization_cb |
736
|
|
|
$group_vals = $field_group->sanitization_cb( $this->data_to_save[ $base_id ] ); |
737
|
|
|
$saved = array(); |
738
|
|
|
|
739
|
|
|
$field_group->index = 0; |
740
|
|
|
$field_group->data_to_save = $this->data_to_save; |
741
|
|
|
|
742
|
|
|
foreach ( array_values( $field_group->fields() ) as $field_args ) { |
743
|
|
|
if ( 'title' === $field_args['type'] ) { |
744
|
|
|
// Don't process title fields |
745
|
|
|
continue; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
$field = $this->get_new_field( $field_args, $field_group ); |
749
|
|
|
$sub_id = $field->id( true ); |
750
|
|
|
|
751
|
|
|
foreach ( (array) $group_vals as $field_group->index => $post_vals ) { |
752
|
|
|
|
753
|
|
|
// Get value |
754
|
|
|
$new_val = isset( $group_vals[ $field_group->index ][ $sub_id ] ) |
755
|
|
|
? $group_vals[ $field_group->index ][ $sub_id ] |
756
|
|
|
: false; |
757
|
|
|
|
758
|
|
|
// Sanitize |
759
|
|
|
$new_val = $field->sanitization_cb( $new_val ); |
760
|
|
|
|
761
|
|
|
if ( is_array( $new_val ) && $field->args( 'has_supporting_data' ) ) { |
762
|
|
|
if ( $field->args( 'repeatable' ) ) { |
763
|
|
|
$_new_val = array(); |
764
|
|
|
foreach ( $new_val as $group_index => $grouped_data ) { |
765
|
|
|
// Add the supporting data to the $saved array stack |
766
|
|
|
$saved[ $field_group->index ][ $grouped_data['supporting_field_id'] ][] = $grouped_data['supporting_field_value']; |
767
|
|
|
// Reset var to the actual value |
768
|
|
|
$_new_val[ $group_index ] = $grouped_data['value']; |
769
|
|
|
} |
770
|
|
|
$new_val = $_new_val; |
771
|
|
|
} else { |
772
|
|
|
// Add the supporting data to the $saved array stack |
773
|
|
|
$saved[ $field_group->index ][ $new_val['supporting_field_id'] ] = $new_val['supporting_field_value']; |
774
|
|
|
// Reset var to the actual value |
775
|
|
|
$new_val = $new_val['value']; |
776
|
|
|
} |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
// Get old value |
780
|
|
|
$old_val = is_array( $old ) && isset( $old[ $field_group->index ][ $sub_id ] ) |
781
|
|
|
? $old[ $field_group->index ][ $sub_id ] |
782
|
|
|
: false; |
783
|
|
|
|
784
|
|
|
$is_updated = ( ! CMB2_Utils::isempty( $new_val ) && $new_val !== $old_val ); |
785
|
|
|
$is_removed = ( CMB2_Utils::isempty( $new_val ) && ! CMB2_Utils::isempty( $old_val ) ); |
786
|
|
|
|
787
|
|
|
// Compare values and add to `$updated` array |
788
|
|
|
if ( $is_updated || $is_removed ) { |
789
|
|
|
$this->updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id; |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
// Add to `$saved` array |
|
|
|
|
793
|
|
|
$saved[ $field_group->index ][ $sub_id ] = $new_val; |
794
|
|
|
|
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
$saved[ $field_group->index ] = CMB2_Utils::filter_empty( $saved[ $field_group->index ] ); |
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
$saved = CMB2_Utils::filter_empty( $saved ); |
801
|
|
|
|
802
|
|
|
return $field_group->update_data( $saved, true ); |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* Get object id from global space if no id is provided |
807
|
|
|
* @since 1.0.0 |
808
|
|
|
* @param integer $object_id Object ID |
809
|
|
|
* @return integer $object_id Object ID |
810
|
|
|
*/ |
811
|
|
|
public function object_id( $object_id = 0 ) { |
|
|
|
|
812
|
|
|
global $pagenow; |
|
|
|
|
813
|
|
|
|
814
|
|
|
if ( $object_id ) { |
815
|
|
|
$this->object_id = $object_id; |
816
|
|
|
return $this->object_id; |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
if ( $this->object_id ) { |
820
|
|
|
return $this->object_id; |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
// Try to get our object ID from the global space |
824
|
|
|
switch ( $this->object_type() ) { |
825
|
|
|
case 'user': |
826
|
|
|
$object_id = isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id; |
827
|
|
|
$object_id = ! $object_id && 'user-new.php' != $pagenow && isset( $GLOBALS['user_ID'] ) ? $GLOBALS['user_ID'] : $object_id; |
828
|
|
|
break; |
829
|
|
|
|
830
|
|
|
case 'comment': |
831
|
|
|
$object_id = isset( $_REQUEST['c'] ) ? $_REQUEST['c'] : $object_id; |
832
|
|
|
$object_id = ! $object_id && isset( $GLOBALS['comments']->comment_ID ) ? $GLOBALS['comments']->comment_ID : $object_id; |
833
|
|
|
break; |
834
|
|
|
|
835
|
|
|
case 'term': |
836
|
|
|
$object_id = isset( $_REQUEST['tag_ID'] ) ? $_REQUEST['tag_ID'] : $object_id; |
837
|
|
|
break; |
838
|
|
|
|
839
|
|
|
default: |
840
|
|
|
$object_id = isset( $GLOBALS['post']->ID ) ? $GLOBALS['post']->ID : $object_id; |
841
|
|
|
$object_id = isset( $_REQUEST['post'] ) ? $_REQUEST['post'] : $object_id; |
842
|
|
|
break; |
843
|
|
|
} |
844
|
|
|
|
845
|
|
|
// reset to id or 0 |
846
|
|
|
$this->object_id = $object_id ? $object_id : 0; |
847
|
|
|
|
848
|
|
|
return $this->object_id; |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* Sets the $object_type based on metabox settings |
853
|
|
|
* @since 1.0.0 |
854
|
|
|
* @return string Object type |
855
|
|
|
*/ |
856
|
|
|
public function mb_object_type() { |
857
|
|
|
if ( null !== $this->mb_object_type ) { |
858
|
|
|
return $this->mb_object_type; |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
if ( $this->is_options_page_mb() ) { |
862
|
|
|
$this->mb_object_type = 'options-page'; |
863
|
|
|
return $this->mb_object_type; |
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
$registered_types = $this->box_types(); |
867
|
|
|
|
868
|
|
|
$type = ''; |
869
|
|
|
|
870
|
|
|
// if it's an array of one, extract it |
871
|
|
|
if ( 1 === count( $registered_types ) ) { |
872
|
|
|
$last = end( $registered_types ); |
873
|
|
|
if ( is_string( $last ) ) { |
874
|
|
|
$type = $last; |
875
|
|
|
} |
876
|
|
|
} elseif ( ( $curr_type = $this->current_object_type() ) && in_array( $curr_type, $registered_types, true ) ) { |
877
|
|
|
$type = $curr_type; |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
// Get our object type |
881
|
|
|
switch ( $type ) { |
882
|
|
|
|
883
|
|
|
case 'user': |
884
|
|
|
case 'comment': |
885
|
|
|
case 'term': |
886
|
|
|
$this->mb_object_type = $type; |
887
|
|
|
break; |
888
|
|
|
|
889
|
|
|
default: |
890
|
|
|
$this->mb_object_type = 'post'; |
891
|
|
|
break; |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
return $this->mb_object_type; |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
/** |
898
|
|
|
* Gets the box 'object_types' array based on box settings. |
899
|
|
|
* @since 2.2.3 |
900
|
|
|
* @return array Object types |
901
|
|
|
*/ |
902
|
|
|
public function box_types() { |
903
|
|
|
return CMB2_Utils::ensure_array( $this->prop( 'object_types' ), array( 'post' ) ); |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
/** |
907
|
|
|
* Determines if metabox is for an options page |
908
|
|
|
* @since 1.0.1 |
909
|
|
|
* @return boolean True/False |
910
|
|
|
*/ |
911
|
|
|
public function is_options_page_mb() { |
912
|
|
|
return ( isset( $this->meta_box['show_on']['key'] ) && 'options-page' === $this->meta_box['show_on']['key'] || array_key_exists( 'options-page', $this->meta_box['show_on'] ) ); |
913
|
|
|
} |
914
|
|
|
|
915
|
|
|
/** |
916
|
|
|
* Returns the object type |
917
|
|
|
* @since 1.0.0 |
918
|
|
|
* @return string Object type |
919
|
|
|
*/ |
920
|
|
|
public function object_type( $object_type = '' ) { |
921
|
|
|
if ( $object_type ) { |
922
|
|
|
$this->object_type = $object_type; |
923
|
|
|
return $this->object_type; |
924
|
|
|
} |
925
|
|
|
|
926
|
|
|
if ( $this->object_type ) { |
927
|
|
|
return $this->object_type; |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
$this->object_type = $this->current_object_type(); |
931
|
|
|
|
932
|
|
|
return $this->object_type; |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
/** |
936
|
|
|
* Get the object type for the current page, based on the $pagenow global. |
937
|
|
|
* @since 2.2.2 |
938
|
|
|
* @return string Page object type name. |
939
|
|
|
*/ |
940
|
|
View Code Duplication |
public function current_object_type() { |
|
|
|
|
941
|
|
|
global $pagenow; |
|
|
|
|
942
|
|
|
$type = 'post'; |
943
|
|
|
|
944
|
|
|
if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) { |
945
|
|
|
$type = 'user'; |
946
|
|
|
} |
947
|
|
|
|
948
|
|
|
if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) { |
949
|
|
|
$type = 'comment'; |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) { |
953
|
|
|
$type = 'term'; |
954
|
|
|
} |
955
|
|
|
|
956
|
|
|
return $type; |
957
|
|
|
} |
958
|
|
|
|
959
|
|
|
/** |
960
|
|
|
* Set metabox property. |
961
|
|
|
* @since 2.2.2 |
962
|
|
|
* @param string $property Metabox config property to retrieve |
963
|
|
|
* @param mixed $value Value to set if no value found |
964
|
|
|
* @return mixed Metabox config property value or false |
965
|
|
|
*/ |
966
|
|
|
public function set_prop( $property, $value ) { |
967
|
|
|
$this->meta_box[ $property ] = $value; |
968
|
|
|
|
969
|
|
|
return $this->prop( $property ); |
970
|
|
|
} |
971
|
|
|
|
972
|
|
|
/** |
973
|
|
|
* Get metabox property and optionally set a fallback |
974
|
|
|
* @since 2.0.0 |
975
|
|
|
* @param string $property Metabox config property to retrieve |
976
|
|
|
* @param mixed $fallback Fallback value to set if no value found |
977
|
|
|
* @return mixed Metabox config property value or false |
978
|
|
|
*/ |
979
|
|
|
public function prop( $property, $fallback = null ) { |
980
|
|
|
if ( array_key_exists( $property, $this->meta_box ) ) { |
981
|
|
|
return $this->meta_box[ $property ]; |
982
|
|
|
} elseif ( $fallback ) { |
983
|
|
|
return $this->meta_box[ $property ] = $fallback; |
984
|
|
|
} |
985
|
|
|
} |
986
|
|
|
|
987
|
|
|
/** |
988
|
|
|
* Get a field object |
989
|
|
|
* @since 2.0.3 |
990
|
|
|
* @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
991
|
|
|
* @param CMB2_Field|null $field_group (optional) CMB2_Field object (group parent) |
992
|
|
|
* @return CMB2_Field|false CMB2_Field object (or false) |
993
|
|
|
*/ |
994
|
|
|
public function get_field( $field, $field_group = null ) { |
995
|
|
|
if ( $field instanceof CMB2_Field ) { |
996
|
|
|
return $field; |
997
|
|
|
} |
998
|
|
|
|
999
|
|
|
$field_id = is_string( $field ) ? $field : $field['id']; |
1000
|
|
|
|
1001
|
|
|
$parent_field_id = ! empty( $field_group ) ? $field_group->id() : ''; |
1002
|
|
|
$ids = $this->get_field_ids( $field_id, $parent_field_id ); |
1003
|
|
|
|
1004
|
|
|
if ( ! $ids ) { |
1005
|
|
|
return false; |
1006
|
|
|
} |
1007
|
|
|
|
1008
|
|
|
list( $field_id, $sub_field_id ) = $ids; |
1009
|
|
|
|
1010
|
|
|
$index = implode( '', $ids ) . ( $field_group ? $field_group->index : '' ); |
1011
|
|
|
if ( array_key_exists( $index, $this->fields ) ) { |
1012
|
|
|
return $this->fields[ $index ]; |
1013
|
|
|
} |
1014
|
|
|
|
1015
|
|
|
$this->fields[ $index ] = new CMB2_Field( $this->get_field_args( $field_id, $field, $sub_field_id, $field_group ) ); |
1016
|
|
|
|
1017
|
|
|
return $this->fields[ $index ]; |
1018
|
|
|
} |
1019
|
|
|
|
1020
|
|
|
/** |
1021
|
|
|
* Handles determining which type of arguments to pass to CMB2_Field |
1022
|
|
|
* @since 2.0.7 |
1023
|
|
|
* @param mixed $field_id Field (or group field) ID |
1024
|
|
|
* @param mixed $field_args Array of field arguments |
1025
|
|
|
* @param mixed $sub_field_id Sub field ID (if field_group exists) |
1026
|
|
|
* @param CMB2_Field|null $field_group If a sub-field, will be the parent group CMB2_Field object |
1027
|
|
|
* @return array Array of CMB2_Field arguments |
1028
|
|
|
*/ |
1029
|
|
|
public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
1030
|
|
|
|
1031
|
|
|
// Check if group is passed and if fields were added in the old-school fields array |
1032
|
|
|
if ( $field_group && ( $sub_field_id || 0 === $sub_field_id ) ) { |
1033
|
|
|
|
1034
|
|
|
// Update the fields array w/ any modified properties inherited from the group field |
1035
|
|
|
$this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] = $field_args; |
1036
|
|
|
|
1037
|
|
|
return $this->get_default_args( $field_args, $field_group ); |
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
if ( is_array( $field_args ) ) { |
1041
|
|
|
$this->meta_box['fields'][ $field_id ] = array_merge( $field_args, $this->meta_box['fields'][ $field_id ] ); |
1042
|
|
|
} |
1043
|
|
|
|
1044
|
|
|
return $this->get_default_args( $this->meta_box['fields'][ $field_id ] ); |
1045
|
|
|
} |
1046
|
|
|
|
1047
|
|
|
/** |
1048
|
|
|
* Get default field arguments specific to this CMB2 object. |
1049
|
|
|
* @since 2.2.0 |
1050
|
|
|
* @param array $field_args Metabox field config array. |
1051
|
|
|
* @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
1052
|
|
|
* @return array Array of field arguments. |
1053
|
|
|
*/ |
1054
|
|
View Code Duplication |
protected function get_default_args( $field_args, $field_group = null ) { |
|
|
|
|
1055
|
|
|
if ( $field_group ) { |
1056
|
|
|
$args = array( |
1057
|
|
|
'field_args' => $field_args, |
1058
|
|
|
'group_field' => $field_group, |
1059
|
|
|
); |
1060
|
|
|
} else { |
1061
|
|
|
$args = array( |
1062
|
|
|
'field_args' => $field_args, |
1063
|
|
|
'object_type' => $this->object_type(), |
1064
|
|
|
'object_id' => $this->object_id(), |
1065
|
|
|
'cmb_id' => $this->cmb_id, |
1066
|
|
|
); |
1067
|
|
|
} |
1068
|
|
|
|
1069
|
|
|
return $args; |
1070
|
|
|
} |
1071
|
|
|
|
1072
|
|
|
/** |
1073
|
|
|
* Get a new field object specific to this CMB2 object. |
1074
|
|
|
* @since 2.2.0 |
1075
|
|
|
* @param array $field_args Metabox field config array. |
1076
|
|
|
* @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
1077
|
|
|
* @return CMB2_Field CMB2_Field object |
1078
|
|
|
*/ |
1079
|
|
|
protected function get_new_field( $field_args, $field_group = null ) { |
1080
|
|
|
return new CMB2_Field( $this->get_default_args( $field_args, $field_group ) ); |
1081
|
|
|
} |
1082
|
|
|
|
1083
|
|
|
/** |
1084
|
|
|
* When fields are added in the old-school way, intitate them as they should be |
1085
|
|
|
* @since 2.1.0 |
1086
|
|
|
* @param array $fields Array of fields to add |
1087
|
|
|
* @param mixed $parent_field_id Parent field id or null |
1088
|
|
|
*/ |
1089
|
|
|
protected function add_fields( $fields, $parent_field_id = null ) { |
1090
|
|
|
foreach ( $fields as $field ) { |
1091
|
|
|
|
1092
|
|
|
$sub_fields = false; |
1093
|
|
|
if ( array_key_exists( 'fields', $field ) ) { |
1094
|
|
|
$sub_fields = $field['fields']; |
1095
|
|
|
unset( $field['fields'] ); |
1096
|
|
|
} |
1097
|
|
|
|
1098
|
|
|
$field_id = $parent_field_id |
1099
|
|
|
? $this->add_group_field( $parent_field_id, $field ) |
1100
|
|
|
: $this->add_field( $field ); |
1101
|
|
|
|
1102
|
|
|
if ( $sub_fields ) { |
1103
|
|
|
$this->add_fields( $sub_fields, $field_id ); |
1104
|
|
|
} |
1105
|
|
|
} |
1106
|
|
|
} |
1107
|
|
|
|
1108
|
|
|
/** |
1109
|
|
|
* Add a field to the metabox |
1110
|
|
|
* @since 2.0.0 |
1111
|
|
|
* @param array $field Metabox field config array |
1112
|
|
|
* @param int $position (optional) Position of metabox. 1 for first, etc |
1113
|
|
|
* @return string|false Field id or false |
1114
|
|
|
*/ |
1115
|
|
|
public function add_field( array $field, $position = 0 ) { |
1116
|
|
|
if ( ! is_array( $field ) || ! array_key_exists( 'id', $field ) ) { |
1117
|
|
|
return false; |
1118
|
|
|
} |
1119
|
|
|
|
1120
|
|
|
if ( 'oembed' === $field['type'] ) { |
1121
|
|
|
// Initiate oembed Ajax hooks |
1122
|
|
|
cmb2_ajax(); |
1123
|
|
|
} |
1124
|
|
|
|
1125
|
|
|
if ( isset( $field['column'] ) && false !== $field['column'] ) { |
1126
|
|
|
$field = $this->define_field_column( $field ); |
1127
|
|
|
} |
1128
|
|
|
|
1129
|
|
|
if ( isset( $field['taxonomy'] ) && ! empty( $field['remove_default'] ) ) { |
1130
|
|
|
$this->tax_metaboxes_to_remove[ $field['taxonomy'] ] = $field['taxonomy']; |
1131
|
|
|
} |
1132
|
|
|
|
1133
|
|
|
$this->_add_field_to_array( |
1134
|
|
|
$field, |
1135
|
|
|
$this->meta_box['fields'], |
1136
|
|
|
$position |
1137
|
|
|
); |
1138
|
|
|
|
1139
|
|
|
return $field['id']; |
1140
|
|
|
} |
1141
|
|
|
|
1142
|
|
|
/** |
1143
|
|
|
* Defines a field's column if requesting to be show in admin columns. |
1144
|
|
|
* @since 2.2.3 |
1145
|
|
|
* @param array $field Metabox field config array. |
1146
|
|
|
* @return array Modified metabox field config array. |
1147
|
|
|
*/ |
1148
|
|
|
protected function define_field_column( array $field ) { |
1149
|
|
|
$this->has_columns = true; |
1150
|
|
|
|
1151
|
|
|
$column = is_array( $field['column'] ) ? $field['column'] : array(); |
1152
|
|
|
|
1153
|
|
|
$field['column'] = wp_parse_args( $column, array( |
1154
|
|
|
'name' => isset( $field['name'] ) ? $field['name'] : '', |
1155
|
|
|
'position' => false, |
1156
|
|
|
) ); |
1157
|
|
|
|
1158
|
|
|
return $field; |
1159
|
|
|
} |
1160
|
|
|
|
1161
|
|
|
/** |
1162
|
|
|
* Add a field to a group |
1163
|
|
|
* @since 2.0.0 |
1164
|
|
|
* @param string $parent_field_id The field id of the group field to add the field |
1165
|
|
|
* @param array $field Metabox field config array |
1166
|
|
|
* @param int $position (optional) Position of metabox. 1 for first, etc |
1167
|
|
|
* @return mixed Array of parent/field ids or false |
1168
|
|
|
*/ |
1169
|
|
|
public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
1170
|
|
|
if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) { |
1171
|
|
|
return false; |
1172
|
|
|
} |
1173
|
|
|
|
1174
|
|
|
$parent_field = $this->meta_box['fields'][ $parent_field_id ]; |
1175
|
|
|
|
1176
|
|
|
if ( 'group' !== $parent_field['type'] ) { |
1177
|
|
|
return false; |
1178
|
|
|
} |
1179
|
|
|
|
1180
|
|
|
if ( ! isset( $parent_field['fields'] ) ) { |
1181
|
|
|
$this->meta_box['fields'][ $parent_field_id ]['fields'] = array(); |
1182
|
|
|
} |
1183
|
|
|
|
1184
|
|
|
$this->_add_field_to_array( |
1185
|
|
|
$field, |
1186
|
|
|
$this->meta_box['fields'][ $parent_field_id ]['fields'], |
1187
|
|
|
$position |
1188
|
|
|
); |
1189
|
|
|
|
1190
|
|
|
return array( $parent_field_id, $field['id'] ); |
1191
|
|
|
} |
1192
|
|
|
|
1193
|
|
|
/** |
1194
|
|
|
* Add a field array to a fields array in desired position |
1195
|
|
|
* @since 2.0.2 |
1196
|
|
|
* @param array $field Metabox field config array |
1197
|
|
|
* @param array &$fields Array (passed by reference) to append the field (array) to |
1198
|
|
|
* @param integer $position Optionally specify a position in the array to be inserted |
1199
|
|
|
*/ |
1200
|
|
|
protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
1201
|
|
|
if ( $position ) { |
1202
|
|
|
CMB2_Utils::array_insert( $fields, array( $field['id'] => $field ), $position ); |
1203
|
|
|
} else { |
1204
|
|
|
$fields[ $field['id'] ] = $field; |
1205
|
|
|
} |
1206
|
|
|
} |
1207
|
|
|
|
1208
|
|
|
/** |
1209
|
|
|
* Remove a field from the metabox |
1210
|
|
|
* @since 2.0.0 |
1211
|
|
|
* @param string $field_id The field id of the field to remove |
1212
|
|
|
* @param string $parent_field_id (optional) The field id of the group field to remove field from |
1213
|
|
|
* @return bool True if field was removed |
1214
|
|
|
*/ |
1215
|
|
|
public function remove_field( $field_id, $parent_field_id = '' ) { |
1216
|
|
|
$ids = $this->get_field_ids( $field_id, $parent_field_id ); |
1217
|
|
|
|
1218
|
|
|
if ( ! $ids ) { |
1219
|
|
|
return false; |
1220
|
|
|
} |
1221
|
|
|
|
1222
|
|
|
list( $field_id, $sub_field_id ) = $ids; |
1223
|
|
|
|
1224
|
|
|
unset( $this->fields[ implode( '', $ids ) ] ); |
1225
|
|
|
|
1226
|
|
|
if ( ! $sub_field_id ) { |
1227
|
|
|
unset( $this->meta_box['fields'][ $field_id ] ); |
1228
|
|
|
return true; |
1229
|
|
|
} |
1230
|
|
|
|
1231
|
|
|
if ( isset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] ) ) { |
1232
|
|
|
unset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] ); |
1233
|
|
|
} |
1234
|
|
|
if ( isset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] ) ) { |
1235
|
|
|
unset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] ); |
1236
|
|
|
} |
1237
|
|
|
return true; |
1238
|
|
|
} |
1239
|
|
|
|
1240
|
|
|
/** |
1241
|
|
|
* Update or add a property to a field |
1242
|
|
|
* @since 2.0.0 |
1243
|
|
|
* @param string $field_id Field id |
1244
|
|
|
* @param string $property Field property to set/update |
1245
|
|
|
* @param mixed $value Value to set the field property |
1246
|
|
|
* @param string $parent_field_id (optional) The field id of the group field to remove field from |
1247
|
|
|
* @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
1248
|
|
|
*/ |
1249
|
|
|
public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
1250
|
|
|
$ids = $this->get_field_ids( $field_id, $parent_field_id ); |
1251
|
|
|
|
1252
|
|
|
if ( ! $ids ) { |
1253
|
|
|
return false; |
1254
|
|
|
} |
1255
|
|
|
|
1256
|
|
|
list( $field_id, $sub_field_id ) = $ids; |
1257
|
|
|
|
1258
|
|
|
if ( ! $sub_field_id ) { |
1259
|
|
|
$this->meta_box['fields'][ $field_id ][ $property ] = $value; |
1260
|
|
|
return $field_id; |
1261
|
|
|
} |
1262
|
|
|
|
1263
|
|
|
$this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ][ $property ] = $value; |
1264
|
|
|
return $field_id; |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
/** |
1268
|
|
|
* Check if field ids match a field and return the index/field id |
1269
|
|
|
* @since 2.0.2 |
1270
|
|
|
* @param string $field_id Field id |
1271
|
|
|
* @param string $parent_field_id (optional) Parent field id |
1272
|
|
|
* @return mixed Array of field/parent ids, or false |
1273
|
|
|
*/ |
1274
|
|
|
public function get_field_ids( $field_id, $parent_field_id = '' ) { |
1275
|
|
|
$sub_field_id = $parent_field_id ? $field_id : ''; |
1276
|
|
|
$field_id = $parent_field_id ? $parent_field_id : $field_id; |
1277
|
|
|
$fields =& $this->meta_box['fields']; |
1278
|
|
|
|
1279
|
|
|
if ( ! array_key_exists( $field_id, $fields ) ) { |
1280
|
|
|
$field_id = $this->search_old_school_array( $field_id, $fields ); |
1281
|
|
|
} |
1282
|
|
|
|
1283
|
|
|
if ( false === $field_id ) { |
1284
|
|
|
return false; |
1285
|
|
|
} |
1286
|
|
|
|
1287
|
|
|
if ( ! $sub_field_id ) { |
1288
|
|
|
return array( $field_id, $sub_field_id ); |
1289
|
|
|
} |
1290
|
|
|
|
1291
|
|
|
if ( 'group' !== $fields[ $field_id ]['type'] ) { |
1292
|
|
|
return false; |
1293
|
|
|
} |
1294
|
|
|
|
1295
|
|
|
if ( ! array_key_exists( $sub_field_id, $fields[ $field_id ]['fields'] ) ) { |
1296
|
|
|
$sub_field_id = $this->search_old_school_array( $sub_field_id, $fields[ $field_id ]['fields'] ); |
1297
|
|
|
} |
1298
|
|
|
|
1299
|
|
|
return false === $sub_field_id ? false : array( $field_id, $sub_field_id ); |
1300
|
|
|
} |
1301
|
|
|
|
1302
|
|
|
/** |
1303
|
|
|
* When using the old array filter, it is unlikely field array indexes will be the field id |
1304
|
|
|
* @since 2.0.2 |
1305
|
|
|
* @param string $field_id The field id |
1306
|
|
|
* @param array $fields Array of fields to search |
1307
|
|
|
* @return mixed Field index or false |
1308
|
|
|
*/ |
1309
|
|
|
public function search_old_school_array( $field_id, $fields ) { |
1310
|
|
|
$ids = wp_list_pluck( $fields, 'id' ); |
1311
|
|
|
$index = array_search( $field_id, $ids ); |
1312
|
|
|
return false !== $index ? $index : false; |
1313
|
|
|
} |
1314
|
|
|
|
1315
|
|
|
/** |
1316
|
|
|
* Handles metabox property callbacks, and passes this $cmb object as property. |
1317
|
|
|
* @since 2.2.3 |
1318
|
|
|
* @param callable $cb The callback method/function/closure |
1319
|
|
|
* @return mixed Return of the callback function. |
1320
|
|
|
*/ |
1321
|
|
|
protected function do_callback( $cb ) { |
1322
|
|
|
return call_user_func( $cb, $this ); |
1323
|
|
|
} |
1324
|
|
|
|
1325
|
|
|
/** |
1326
|
|
|
* Generate a unique nonce field for each registered meta_box |
1327
|
|
|
* @since 2.0.0 |
1328
|
|
|
* @return string unique nonce hidden input |
1329
|
|
|
*/ |
1330
|
|
|
public function nonce_field() { |
1331
|
|
|
wp_nonce_field( $this->nonce(), $this->nonce(), false, true ); |
1332
|
|
|
} |
1333
|
|
|
|
1334
|
|
|
/** |
1335
|
|
|
* Generate a unique nonce for each registered meta_box |
1336
|
|
|
* @since 2.0.0 |
1337
|
|
|
* @return string unique nonce string |
1338
|
|
|
*/ |
1339
|
|
|
public function nonce() { |
1340
|
|
|
if ( $this->generated_nonce ) { |
1341
|
|
|
return $this->generated_nonce; |
1342
|
|
|
} |
1343
|
|
|
$this->generated_nonce = sanitize_html_class( 'nonce_' . basename( __FILE__ ) . $this->cmb_id ); |
1344
|
|
|
return $this->generated_nonce; |
1345
|
|
|
} |
1346
|
|
|
|
1347
|
|
|
/** |
1348
|
|
|
* Magic getter for our object. |
1349
|
|
|
* @param string $field |
1350
|
|
|
* @throws Exception Throws an exception if the field is invalid. |
1351
|
|
|
* @return mixed |
1352
|
|
|
*/ |
1353
|
|
|
public function __get( $field ) { |
1354
|
|
|
switch ( $field ) { |
1355
|
|
|
case 'updated': |
1356
|
|
|
case 'has_columns': |
1357
|
|
|
case 'tax_metaboxes_to_remove': |
1358
|
|
|
return $this->{$field}; |
1359
|
|
|
default: |
1360
|
|
|
return parent::__get( $field ); |
1361
|
|
|
} |
1362
|
|
|
} |
1363
|
|
|
|
1364
|
|
|
} |
1365
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.