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