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 field's 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
|
42 |
|
public function __construct( $meta_box, $object_id = 0 ) { |
120
|
|
|
|
121
|
42 |
|
if ( empty( $meta_box['id'] ) ) { |
122
|
1 |
|
wp_die( __( 'Metabox configuration is required to have an ID parameter', 'cmb2' ) ); |
123
|
|
|
} |
124
|
|
|
|
125
|
42 |
|
$this->meta_box = wp_parse_args( $meta_box, $this->mb_defaults ); |
126
|
42 |
|
$this->meta_box['fields'] = array(); |
127
|
|
|
|
128
|
42 |
|
$this->object_id( $object_id ); |
129
|
42 |
|
$this->mb_object_type(); |
130
|
42 |
|
$this->cmb_id = $meta_box['id']; |
131
|
|
|
|
132
|
42 |
|
if ( ! empty( $meta_box['fields'] ) && is_array( $meta_box['fields'] ) ) { |
133
|
40 |
|
$this->add_fields( $meta_box['fields'] ); |
134
|
40 |
|
} |
135
|
|
|
|
136
|
42 |
|
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
|
42 |
|
do_action( "cmb2_init_{$this->cmb_id}", $this ); |
146
|
42 |
|
} |
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 |
View Code Duplication |
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="cmb2-wrap form-table"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">'; |
208
|
|
|
|
209
|
1 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Outputs the closing form markup and runs corresponding hooks: |
213
|
|
|
* 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
214
|
|
|
* @since 2.2.0 |
215
|
|
|
* @param integer $object_id Object ID |
216
|
|
|
* @param string $object_type Object type |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
1 |
View Code Duplication |
public function render_form_close( $object_id = 0, $object_type = '' ) { |
|
|
|
|
220
|
1 |
|
$object_type = $this->object_type( $object_type ); |
221
|
1 |
|
$object_id = $this->object_id( $object_id ); |
222
|
|
|
|
223
|
1 |
|
echo '</div></div>'; |
224
|
|
|
|
225
|
1 |
|
$this->render_hidden_fields(); |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Hook after form form has been rendered |
229
|
|
|
* |
230
|
|
|
* @param array $cmb_id The current box ID |
231
|
|
|
* @param int $object_id The ID of the current object |
232
|
|
|
* @param string $object_type The type of object you are working with. |
233
|
|
|
* Usually `post` (this applies to all post-types). |
234
|
|
|
* Could also be `comment`, `user` or `options-page`. |
235
|
|
|
* @param array $cmb This CMB2 object |
236
|
|
|
*/ |
237
|
1 |
|
do_action( 'cmb2_after_form', $this->cmb_id, $object_id, $object_type, $this ); |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Hook after form form has been rendered |
241
|
|
|
* |
242
|
|
|
* The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
243
|
|
|
* |
244
|
|
|
* The first dynamic portion of the hook name, $object_type, is the type of object |
245
|
|
|
* you are working with. Usually `post` (this applies to all post-types). |
246
|
|
|
* Could also be `comment`, `user` or `options-page`. |
247
|
|
|
* |
248
|
|
|
* @param int $object_id The ID of the current object |
249
|
|
|
* @param array $cmb This CMB2 object |
250
|
|
|
*/ |
251
|
1 |
|
do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
252
|
|
|
|
253
|
1 |
|
echo "\n<!-- End CMB2 Fields -->\n"; |
254
|
|
|
|
255
|
1 |
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Renders a field based on the field type |
259
|
|
|
* @since 2.2.0 |
260
|
|
|
* @param array $field_args A field configuration array. |
261
|
|
|
* @return mixed CMB2_Field object if successful. |
262
|
|
|
*/ |
263
|
1 |
|
public function render_field( $field_args ) { |
264
|
1 |
|
$field_args['context'] = $this->prop( 'context' ); |
265
|
|
|
|
266
|
1 |
|
if ( 'group' == $field_args['type'] ) { |
267
|
|
|
|
268
|
|
|
if ( ! isset( $field_args['show_names'] ) ) { |
269
|
|
|
$field_args['show_names'] = $this->prop( 'show_names' ); |
270
|
|
|
} |
271
|
|
|
$field = $this->render_group( $field_args ); |
272
|
|
|
|
273
|
1 |
|
} elseif ( 'hidden' == $field_args['type'] && $this->get_field( $field_args )->should_show() ) { |
274
|
|
|
// Save rendering for after the metabox |
275
|
|
|
$field = $this->add_hidden_field( array( |
276
|
|
|
'field_args' => $field_args, |
277
|
|
|
'object_type' => $this->object_type(), |
278
|
|
|
'object_id' => $this->object_id(), |
279
|
|
|
) ); |
280
|
|
|
|
281
|
|
|
} else { |
282
|
|
|
|
283
|
1 |
|
$field_args['show_names'] = $this->prop( 'show_names' ); |
284
|
|
|
|
285
|
|
|
// Render default fields |
286
|
1 |
|
$field = $this->get_field( $field_args )->render_field(); |
287
|
|
|
} |
288
|
|
|
|
289
|
1 |
|
return $field; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Render a repeatable group. |
294
|
|
|
* @param array $args Array of field arguments for a group field parent. |
295
|
|
|
* @return CMB2_Field|null Group field object. |
296
|
|
|
*/ |
297
|
2 |
|
public function render_group( $args ) { |
298
|
|
|
|
299
|
2 |
View Code Duplication |
if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
|
|
|
300
|
|
|
return; |
301
|
|
|
} |
302
|
|
|
|
303
|
2 |
|
$field_group = $this->get_field( $args ); |
304
|
|
|
|
305
|
|
|
// If field is requesting to be conditionally shown |
306
|
2 |
|
if ( ! $field_group || ! $field_group->should_show() ) { |
307
|
|
|
return; |
308
|
|
|
} |
309
|
|
|
|
310
|
2 |
|
$desc = $field_group->args( 'description' ); |
311
|
2 |
|
$label = $field_group->args( 'name' ); |
312
|
2 |
|
$sortable = $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
313
|
2 |
|
$repeat_class = $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
314
|
2 |
|
$group_val = (array) $field_group->value(); |
315
|
2 |
|
$nrows = count( $group_val ); |
316
|
2 |
|
$remove_disabled = $nrows <= 1 ? 'disabled="disabled" ' : ''; |
317
|
2 |
|
$field_group->index = 0; |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Allow for adding additional HTML attributes to a group wrapper. |
321
|
|
|
* |
322
|
|
|
* If you use this filter, make sure the resulting string always starts with a space! |
323
|
|
|
* |
324
|
|
|
* @since 2.1.3 |
325
|
|
|
* |
326
|
|
|
* @param string Current attributes string. |
327
|
|
|
* |
328
|
|
|
* @param array $group_args All group arguments. |
329
|
|
|
* |
330
|
|
|
* @param string $id The group_id of the current group. |
331
|
|
|
*/ |
332
|
2 |
|
$more_attributes = apply_filters( "cmb2_additional_group_attributes", '', $field_group->args, $args['id'] ); |
333
|
|
|
|
334
|
2 |
|
$field_group->peform_param_callback( 'before_group' ); |
335
|
|
|
|
336
|
2 |
|
echo '<div class="cmb-row cmb-repeat-group-wrap"><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, '>'; |
337
|
|
|
|
338
|
2 |
|
if ( $desc || $label ) { |
339
|
2 |
|
$class = $desc ? ' cmb-group-description' : ''; |
340
|
2 |
|
echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
341
|
2 |
|
if ( $label ) { |
342
|
2 |
|
echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
343
|
2 |
|
} |
344
|
2 |
|
if ( $desc ) { |
345
|
1 |
|
echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
346
|
1 |
|
} |
347
|
2 |
|
echo '</div></div>'; |
348
|
2 |
|
} |
349
|
|
|
|
350
|
2 |
|
if ( ! empty( $group_val ) ) { |
351
|
|
|
|
352
|
|
|
foreach ( $group_val as $group_key => $field_id ) { |
353
|
|
|
$this->render_group_row( $field_group, $remove_disabled ); |
354
|
|
|
$field_group->index++; |
355
|
|
|
} |
356
|
|
|
} else { |
357
|
2 |
|
$this->render_group_row( $field_group, $remove_disabled ); |
358
|
|
|
} |
359
|
|
|
|
360
|
2 |
|
if ( $field_group->args( 'repeatable' ) ) { |
361
|
1 |
|
echo '<div class="cmb-row"><div class="cmb-td"><p class="cmb-add-row"><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>'; |
362
|
1 |
|
} |
363
|
|
|
|
364
|
2 |
|
echo '</div></div></div>'; |
365
|
|
|
|
366
|
2 |
|
$field_group->peform_param_callback( 'after_group' ); |
367
|
|
|
|
368
|
2 |
|
return $field_group; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Render a repeatable group row |
373
|
|
|
* @since 1.0.2 |
374
|
|
|
* @param CMB2_Field $field_group CMB2_Field group field object |
375
|
|
|
* @param string $remove_disabled Attribute string to disable the remove button |
376
|
|
|
*/ |
377
|
2 |
|
public function render_group_row( $field_group, $remove_disabled ) { |
378
|
|
|
|
379
|
2 |
|
$field_group->peform_param_callback( 'before_group_row' ); |
380
|
2 |
|
$closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; |
381
|
|
|
|
382
|
|
|
echo ' |
383
|
2 |
|
<div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; |
384
|
|
|
|
385
|
2 |
|
if ( $field_group->args( 'repeatable' ) ) { |
386
|
1 |
|
echo '<button ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row"></button>'; |
387
|
1 |
|
} |
388
|
|
|
|
389
|
|
|
echo ' |
390
|
2 |
|
<div class="cmbhandle" title="' , __( 'Click to toggle', 'cmb2' ), '"><br></div> |
391
|
2 |
|
<h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> |
|
|
|
|
392
|
|
|
|
393
|
|
|
<div class="inside cmb-td cmb-nested cmb-field-list">'; |
394
|
|
|
// Loop and render repeatable group fields |
395
|
2 |
|
foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { |
396
|
2 |
|
if ( 'hidden' == $field_args['type'] ) { |
397
|
|
|
|
398
|
|
|
// Save rendering for after the metabox |
399
|
|
|
$this->add_hidden_field( array( |
400
|
|
|
'field_args' => $field_args, |
401
|
|
|
'group_field' => $field_group, |
402
|
|
|
) ); |
403
|
|
|
|
404
|
|
|
} else { |
405
|
|
|
|
406
|
2 |
|
$field_args['show_names'] = $field_group->args( 'show_names' ); |
407
|
2 |
|
$field_args['context'] = $field_group->args( 'context' ); |
408
|
|
|
|
409
|
2 |
|
$field = $this->get_field( $field_args, $field_group )->render_field(); |
|
|
|
|
410
|
|
|
} |
411
|
2 |
|
} |
412
|
2 |
|
if ( $field_group->args( 'repeatable' ) ) { |
413
|
|
|
echo ' |
414
|
|
|
<div class="cmb-row cmb-remove-field-row"> |
415
|
|
|
<div class="cmb-remove-row"> |
416
|
1 |
|
<button ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="button cmb-remove-group-row alignright">', $field_group->options( 'remove_button' ), '</button> |
417
|
|
|
</div> |
418
|
|
|
</div> |
419
|
|
|
'; |
420
|
1 |
|
} |
421
|
|
|
echo ' |
422
|
|
|
</div> |
423
|
|
|
</div> |
424
|
2 |
|
'; |
425
|
|
|
|
426
|
2 |
|
$field_group->peform_param_callback( 'after_group_row' ); |
427
|
2 |
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Add a hidden field to the list of hidden fields to be rendered later |
431
|
|
|
* @since 2.0.0 |
432
|
|
|
* @param array $args Array of arguments to be passed to CMB2_Field |
433
|
|
|
*/ |
434
|
|
|
public function add_hidden_field( $args ) { |
435
|
|
|
$field = new CMB2_Field( $args ); |
436
|
|
|
$this->hidden_fields[] = new CMB2_Types( $field ); |
437
|
|
|
|
438
|
|
|
return $field; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Loop through and output hidden fields |
443
|
|
|
* @since 2.0.0 |
444
|
|
|
*/ |
445
|
1 |
|
public function render_hidden_fields() { |
446
|
1 |
|
if ( ! empty( $this->hidden_fields ) ) { |
447
|
|
|
foreach ( $this->hidden_fields as $hidden ) { |
448
|
|
|
$hidden->render(); |
449
|
|
|
} |
450
|
|
|
} |
451
|
1 |
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Returns array of sanitized field values (without saving them) |
455
|
|
|
* @since 2.0.3 |
456
|
|
|
* @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
457
|
|
|
*/ |
458
|
2 |
|
public function get_sanitized_values( array $data_to_sanitize ) { |
459
|
2 |
|
$this->data_to_save = $data_to_sanitize; |
460
|
2 |
|
$stored_id = $this->object_id(); |
461
|
|
|
|
462
|
|
|
// We do this So CMB will sanitize our data for us, but not save it |
463
|
2 |
|
$this->object_id( '_' ); |
464
|
|
|
|
465
|
|
|
// Ensure temp. data store is empty |
466
|
2 |
|
cmb2_options( 0 )->set(); |
467
|
|
|
|
468
|
|
|
// Process/save fields |
469
|
2 |
|
$this->process_fields(); |
470
|
|
|
|
471
|
|
|
// Get data from temp. data store |
472
|
2 |
|
$sanitized_values = cmb2_options( 0 )->get_options(); |
473
|
|
|
|
474
|
|
|
// Empty out temp. data store again |
475
|
2 |
|
cmb2_options( 0 )->set(); |
476
|
|
|
|
477
|
|
|
// Reset the object id |
478
|
2 |
|
$this->object_id( $stored_id ); |
479
|
|
|
|
480
|
2 |
|
return $sanitized_values; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Loops through and saves field data |
485
|
|
|
* @since 1.0.0 |
486
|
|
|
* @param int $object_id Object ID |
487
|
|
|
* @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
488
|
|
|
* @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
489
|
|
|
*/ |
490
|
|
|
public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
|
|
|
491
|
|
|
|
492
|
|
|
// Fall-back to $_POST data |
493
|
|
|
$this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST; |
494
|
|
|
$object_id = $this->object_id( $object_id ); |
495
|
|
|
$object_type = $this->object_type( $object_type ); |
496
|
|
|
|
497
|
|
|
$this->process_fields(); |
498
|
|
|
|
499
|
|
|
// If options page, save the updated options |
500
|
|
|
if ( 'options-page' == $object_type ) { |
501
|
|
|
cmb2_options( $object_id )->set(); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Fires after all fields have been saved. |
506
|
|
|
* |
507
|
|
|
* The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
508
|
|
|
* Usually `post` (this applies to all post-types). |
509
|
|
|
* Could also be `comment`, `user` or `options-page`. |
510
|
|
|
* |
511
|
|
|
* @param int $object_id The ID of the current object |
512
|
|
|
* @param array $cmb_id The current box ID |
513
|
|
|
* @param string $updated Array of field ids that were updated. |
514
|
|
|
* Will only include field ids that had values change. |
515
|
|
|
* @param array $cmb This CMB2 object |
516
|
|
|
*/ |
517
|
|
|
do_action( "cmb2_save_{$object_type}_fields", $object_id, $this->cmb_id, $this->updated, $this ); |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Fires after all fields have been saved. |
521
|
|
|
* |
522
|
|
|
* The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
523
|
|
|
* |
524
|
|
|
* The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
525
|
|
|
* Usually `post` (this applies to all post-types). |
526
|
|
|
* Could also be `comment`, `user` or `options-page`. |
527
|
|
|
* |
528
|
|
|
* @param int $object_id The ID of the current object |
529
|
|
|
* @param string $updated Array of field ids that were updated. |
530
|
|
|
* Will only include field ids that had values change. |
531
|
|
|
* @param array $cmb This CMB2 object |
532
|
|
|
*/ |
533
|
|
|
do_action( "cmb2_save_{$object_type}_fields_{$this->cmb_id}", $object_id, $this->updated, $this ); |
534
|
|
|
|
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Process and save form fields |
539
|
|
|
* @since 2.0.0 |
540
|
|
|
*/ |
541
|
2 |
|
public function process_fields() { |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Fires before fields have been processed/saved. |
545
|
|
|
* |
546
|
|
|
* The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
547
|
|
|
* |
548
|
|
|
* The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
549
|
|
|
* Usually `post` (this applies to all post-types). |
550
|
|
|
* Could also be `comment`, `user` or `options-page`. |
551
|
|
|
* |
552
|
|
|
* @param array $cmb This CMB2 object |
553
|
|
|
* @param int $object_id The ID of the current object |
554
|
|
|
*/ |
555
|
2 |
|
do_action( "cmb2_{$this->object_type()}_process_fields_{$this->cmb_id}", $this, $this->object_id() ); |
556
|
|
|
|
557
|
|
|
// Remove the show_on properties so saving works |
558
|
2 |
|
$this->prop( 'show_on', array() ); |
559
|
|
|
|
560
|
|
|
// save field ids of those that are updated |
561
|
2 |
|
$this->updated = array(); |
562
|
|
|
|
563
|
2 |
|
foreach ( $this->prop( 'fields' ) as $field_args ) { |
564
|
2 |
|
$this->process_field( $field_args ); |
565
|
2 |
|
} |
566
|
2 |
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Process and save a field |
570
|
|
|
* @since 2.0.0 |
571
|
|
|
* @param array $field_args Array of field arguments |
572
|
|
|
*/ |
573
|
2 |
|
public function process_field( $field_args ) { |
574
|
|
|
|
575
|
2 |
|
switch ( $field_args['type'] ) { |
576
|
|
|
|
577
|
2 |
|
case 'group': |
578
|
1 |
|
$this->save_group( $field_args ); |
579
|
1 |
|
break; |
580
|
|
|
|
581
|
1 |
|
case 'title': |
582
|
|
|
// Don't process title fields |
583
|
|
|
break; |
584
|
|
|
|
585
|
1 |
|
default: |
586
|
|
|
|
587
|
|
|
// Save default fields |
588
|
1 |
|
$field = new CMB2_Field( array( |
589
|
1 |
|
'field_args' => $field_args, |
590
|
1 |
|
'object_type' => $this->object_type(), |
591
|
1 |
|
'object_id' => $this->object_id(), |
592
|
1 |
|
) ); |
593
|
|
|
|
594
|
1 |
|
if ( $field->save_field_from_data( $this->data_to_save ) ) { |
595
|
1 |
|
$this->updated[] = $field->id(); |
596
|
1 |
|
} |
597
|
|
|
|
598
|
1 |
|
break; |
599
|
2 |
|
} |
600
|
|
|
|
601
|
2 |
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Save a repeatable group |
605
|
|
|
*/ |
606
|
1 |
|
public function save_group( $args ) { |
607
|
|
|
|
608
|
1 |
View Code Duplication |
if ( ! isset( $args['id'], $args['fields'], $this->data_to_save[ $args['id'] ] ) || ! is_array( $args['fields'] ) ) { |
|
|
|
|
609
|
|
|
return; |
610
|
|
|
} |
611
|
|
|
|
612
|
1 |
|
$field_group = new CMB2_Field( array( |
613
|
1 |
|
'field_args' => $args, |
614
|
1 |
|
'object_type' => $this->object_type(), |
615
|
1 |
|
'object_id' => $this->object_id(), |
616
|
1 |
|
) ); |
617
|
1 |
|
$base_id = $field_group->id(); |
618
|
1 |
|
$old = $field_group->get_data(); |
619
|
|
|
// Check if group field has sanitization_cb |
620
|
1 |
|
$group_vals = $field_group->sanitization_cb( $this->data_to_save[ $base_id ] ); |
621
|
1 |
|
$saved = array(); |
622
|
1 |
|
$field_group->index = 0; |
623
|
1 |
|
$field_group->data_to_save = $this->data_to_save; |
624
|
|
|
|
625
|
1 |
|
foreach ( array_values( $field_group->fields() ) as $field_args ) { |
626
|
1 |
|
$field = new CMB2_Field( array( |
627
|
1 |
|
'field_args' => $field_args, |
628
|
1 |
|
'group_field' => $field_group, |
629
|
1 |
|
) ); |
630
|
1 |
|
$sub_id = $field->id( true ); |
631
|
|
|
|
632
|
1 |
|
foreach ( (array) $group_vals as $field_group->index => $post_vals ) { |
633
|
|
|
|
634
|
|
|
// Get value |
635
|
1 |
|
$new_val = isset( $group_vals[ $field_group->index ][ $sub_id ] ) |
636
|
1 |
|
? $group_vals[ $field_group->index ][ $sub_id ] |
637
|
1 |
|
: false; |
638
|
|
|
|
639
|
|
|
// Sanitize |
640
|
1 |
|
$new_val = $field->sanitization_cb( $new_val ); |
641
|
|
|
|
642
|
1 |
|
if ( is_array( $new_val ) && $field->args( 'has_supporting_data' ) ) { |
643
|
1 |
|
if ( $field->args( 'repeatable' ) ) { |
644
|
1 |
|
$_new_val = array(); |
645
|
1 |
|
foreach ( $new_val as $group_index => $grouped_data ) { |
646
|
|
|
// Add the supporting data to the $saved array stack |
647
|
1 |
|
$saved[ $field_group->index ][ $grouped_data['supporting_field_id'] ][] = $grouped_data['supporting_field_value']; |
648
|
|
|
// Reset var to the actual value |
649
|
1 |
|
$_new_val[ $group_index ] = $grouped_data['value']; |
650
|
1 |
|
} |
651
|
1 |
|
$new_val = $_new_val; |
652
|
1 |
|
} else { |
653
|
|
|
// Add the supporting data to the $saved array stack |
654
|
1 |
|
$saved[ $field_group->index ][ $new_val['supporting_field_id'] ] = $new_val['supporting_field_value']; |
655
|
|
|
// Reset var to the actual value |
656
|
1 |
|
$new_val = $new_val['value']; |
657
|
|
|
} |
658
|
1 |
|
} |
659
|
|
|
|
660
|
|
|
// Get old value |
661
|
1 |
|
$old_val = is_array( $old ) && isset( $old[ $field_group->index ][ $sub_id ] ) |
662
|
1 |
|
? $old[ $field_group->index ][ $sub_id ] |
663
|
1 |
|
: false; |
664
|
|
|
|
665
|
1 |
|
$is_updated = ( ! empty( $new_val ) && $new_val != $old_val ); |
666
|
1 |
|
$is_removed = ( empty( $new_val ) && ! empty( $old_val ) ); |
667
|
|
|
// Compare values and add to `$updated` array |
668
|
1 |
|
if ( $is_updated || $is_removed ) { |
669
|
1 |
|
$this->updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id; |
670
|
1 |
|
} |
671
|
|
|
|
672
|
|
|
// Add to `$saved` array |
|
|
|
|
673
|
1 |
|
$saved[ $field_group->index ][ $sub_id ] = $new_val; |
674
|
|
|
|
675
|
1 |
|
} |
676
|
1 |
|
$saved[ $field_group->index ] = array_filter( $saved[ $field_group->index ] ); |
677
|
1 |
|
} |
678
|
1 |
|
$saved = array_filter( $saved ); |
679
|
|
|
|
680
|
1 |
|
$field_group->update_data( $saved, true ); |
681
|
1 |
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* Get object id from global space if no id is provided |
685
|
|
|
* @since 1.0.0 |
686
|
|
|
* @param integer $object_id Object ID |
687
|
|
|
* @return integer $object_id Object ID |
688
|
|
|
*/ |
689
|
42 |
|
public function object_id( $object_id = 0 ) { |
|
|
|
|
690
|
42 |
|
global $pagenow; |
|
|
|
|
691
|
|
|
|
692
|
42 |
|
if ( $object_id ) { |
693
|
13 |
|
$this->object_id = $object_id; |
694
|
13 |
|
return $this->object_id; |
695
|
|
|
} |
696
|
|
|
|
697
|
42 |
|
if ( $this->object_id ) { |
698
|
11 |
|
return $this->object_id; |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
// Try to get our object ID from the global space |
702
|
41 |
|
switch ( $this->object_type() ) { |
703
|
41 |
|
case 'user': |
704
|
|
|
$object_id = isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id; |
705
|
|
|
$object_id = ! $object_id && 'user-new.php' != $pagenow && isset( $GLOBALS['user_ID'] ) ? $GLOBALS['user_ID'] : $object_id; |
706
|
|
|
break; |
707
|
|
|
|
708
|
41 |
|
case 'comment': |
709
|
|
|
$object_id = isset( $_REQUEST['c'] ) ? $_REQUEST['c'] : $object_id; |
710
|
|
|
$object_id = ! $object_id && isset( $GLOBALS['comments']->comment_ID ) ? $GLOBALS['comments']->comment_ID : $object_id; |
711
|
|
|
break; |
712
|
|
|
|
713
|
41 |
|
case 'term': |
714
|
|
|
$object_id = isset( $_REQUEST['tag_ID'] ) ? $_REQUEST['tag_ID'] : $object_id; |
715
|
|
|
break; |
716
|
|
|
|
717
|
41 |
|
default: |
718
|
41 |
|
$object_id = isset( $GLOBALS['post']->ID ) ? $GLOBALS['post']->ID : $object_id; |
719
|
41 |
|
$object_id = isset( $_REQUEST['post'] ) ? $_REQUEST['post'] : $object_id; |
720
|
41 |
|
break; |
721
|
41 |
|
} |
722
|
|
|
|
723
|
|
|
// reset to id or 0 |
724
|
41 |
|
$this->object_id = $object_id ? $object_id : 0; |
725
|
|
|
|
726
|
41 |
|
return $this->object_id; |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Sets the $object_type based on metabox settings |
731
|
|
|
* @since 1.0.0 |
732
|
|
|
* @return string Object type |
733
|
|
|
*/ |
734
|
42 |
|
public function mb_object_type() { |
735
|
|
|
|
736
|
42 |
|
if ( null !== $this->mb_object_type ) { |
737
|
12 |
|
return $this->mb_object_type; |
738
|
|
|
} |
739
|
|
|
|
740
|
42 |
|
if ( $this->is_options_page_mb() ) { |
741
|
35 |
|
$this->mb_object_type = 'options-page'; |
742
|
35 |
|
return $this->mb_object_type; |
743
|
|
|
} |
744
|
|
|
|
745
|
42 |
|
if ( ! $this->prop( 'object_types' ) ) { |
746
|
40 |
|
$this->mb_object_type = 'post'; |
747
|
40 |
|
return $this->mb_object_type; |
748
|
|
|
} |
749
|
|
|
|
750
|
3 |
|
$type = false; |
751
|
|
|
// check if 'object_types' is a string |
752
|
3 |
|
if ( is_string( $this->prop( 'object_types' ) ) ) { |
753
|
|
|
$type = $this->prop( 'object_types' ); |
754
|
|
|
} |
755
|
|
|
// if it's an array of one, extract it |
756
|
3 |
|
elseif ( is_array( $this->prop( 'object_types' ) ) && 1 === count( $this->prop( 'object_types' ) ) ) { |
757
|
3 |
|
$cpts = $this->prop( 'object_types' ); |
758
|
3 |
|
$type = is_string( end( $cpts ) ) |
759
|
3 |
|
? end( $cpts ) |
760
|
3 |
|
: false; |
761
|
3 |
|
} |
762
|
|
|
|
763
|
3 |
|
if ( ! $type ) { |
764
|
|
|
$this->mb_object_type = 'post'; |
765
|
|
|
return $this->mb_object_type; |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
// Get our object type |
769
|
|
|
switch ( $type ) { |
770
|
|
|
|
771
|
3 |
|
case 'user': |
772
|
3 |
|
case 'comment': |
773
|
3 |
|
case 'term': |
774
|
1 |
|
$this->mb_object_type = $type; |
775
|
1 |
|
break; |
776
|
|
|
|
777
|
2 |
|
default: |
778
|
2 |
|
$this->mb_object_type = 'post'; |
779
|
2 |
|
break; |
780
|
2 |
|
} |
781
|
|
|
|
782
|
3 |
|
return $this->mb_object_type; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Determines if metabox is for an options page |
787
|
|
|
* @since 1.0.1 |
788
|
|
|
* @return boolean True/False |
789
|
|
|
*/ |
790
|
42 |
|
public function is_options_page_mb() { |
791
|
42 |
|
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'] ) ); |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* Returns the object type |
796
|
|
|
* @since 1.0.0 |
797
|
|
|
* @return string Object type |
798
|
|
|
*/ |
799
|
42 |
|
public function object_type( $object_type = '' ) { |
800
|
42 |
|
if ( $object_type ) { |
801
|
12 |
|
$this->object_type = $object_type; |
802
|
12 |
|
return $this->object_type; |
803
|
|
|
} |
804
|
|
|
|
805
|
42 |
|
if ( $this->object_type ) { |
806
|
42 |
|
return $this->object_type; |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
global $pagenow; |
|
|
|
|
810
|
|
|
|
811
|
|
|
if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) { |
812
|
|
|
$this->object_type = 'user'; |
813
|
|
|
|
814
|
|
|
} elseif ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) { |
815
|
|
|
$this->object_type = 'comment'; |
816
|
|
|
|
817
|
|
|
} elseif ( 'edit-tags.php' == $pagenow ) { |
818
|
|
|
$this->object_type = 'term'; |
819
|
|
|
|
820
|
|
|
} else { |
821
|
|
|
$this->object_type = 'post'; |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
return $this->object_type; |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
/** |
828
|
|
|
* Get metabox property and optionally set a fallback |
829
|
|
|
* @since 2.0.0 |
830
|
|
|
* @param string $property Metabox config property to retrieve |
831
|
|
|
* @param mixed $fallback Fallback value to set if no value found |
832
|
|
|
* @return mixed Metabox config property value or false |
833
|
|
|
*/ |
834
|
42 |
|
public function prop( $property, $fallback = null ) { |
835
|
42 |
|
if ( array_key_exists( $property, $this->meta_box ) ) { |
836
|
42 |
|
return $this->meta_box[ $property ]; |
837
|
1 |
|
} elseif ( $fallback ) { |
838
|
1 |
|
return $this->meta_box[ $property ] = $fallback; |
839
|
|
|
} |
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
/** |
843
|
|
|
* Get a field object |
844
|
|
|
* |
845
|
|
|
* @since 2.0.3 |
846
|
|
|
* |
847
|
|
|
* @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
848
|
|
|
* @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
849
|
|
|
* |
850
|
|
|
* @return CMB2_Field|false CMB2_Field object (or false) |
851
|
|
|
*/ |
852
|
15 |
|
public function get_field( $field, $field_group = null ) { |
853
|
15 |
|
if ( is_a( $field, 'CMB2_Field' ) ) { |
854
|
|
|
return $field; |
|
|
|
|
855
|
|
|
} |
856
|
|
|
|
857
|
15 |
|
$field_id = is_string( $field ) ? $field : $field['id']; |
858
|
|
|
|
859
|
15 |
|
$parent_field_id = ! empty( $field_group ) ? $field_group->id() : ''; |
860
|
15 |
|
$ids = $this->get_field_ids( $field_id, $parent_field_id, true ); |
|
|
|
|
861
|
|
|
|
862
|
15 |
|
if ( ! $ids ) { |
863
|
|
|
return false; |
864
|
|
|
} |
865
|
|
|
|
866
|
15 |
|
list( $field_id, $sub_field_id ) = $ids; |
867
|
|
|
|
868
|
15 |
|
$index = implode( '', $ids ) . ( $field_group ? $field_group->index : '' ); |
869
|
15 |
|
if ( array_key_exists( $index, $this->fields ) ) { |
870
|
3 |
|
return $this->fields[ $index ]; |
871
|
|
|
} |
872
|
|
|
|
873
|
13 |
|
$this->fields[ $index ] = new CMB2_Field( $this->get_field_args( $field_id, $field, $sub_field_id, $field_group ) ); |
874
|
|
|
|
875
|
13 |
|
return $this->fields[ $index ]; |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
/** |
879
|
|
|
* Handles determining which type of arguments to pass to CMB2_Field |
880
|
|
|
* @since 2.0.7 |
881
|
|
|
* @param mixed $field_id Field (or group field) ID |
882
|
|
|
* @param mixed $field_args Array of field arguments |
883
|
|
|
* @param mixed $sub_field_id Sub field ID (if field_group exists) |
884
|
|
|
* @param mixed $field_group If a sub-field, will be the parent group CMB2_Field object |
885
|
|
|
* @return array Array of CMB2_Field arguments |
886
|
|
|
*/ |
887
|
13 |
|
public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
888
|
|
|
|
889
|
|
|
// Check if group is passed and if fields were added in the old-school fields array |
890
|
13 |
|
if ( $field_group && ( $sub_field_id || 0 === $sub_field_id ) ) { |
891
|
|
|
|
892
|
|
|
// Update the fields array w/ any modified properties inherited from the group field |
893
|
2 |
|
$this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] = $field_args; |
894
|
|
|
|
895
|
|
|
return array( |
896
|
2 |
|
'field_args' => $field_args, |
897
|
2 |
|
'group_field' => $field_group, |
898
|
2 |
|
); |
899
|
|
|
|
900
|
|
|
} |
901
|
|
|
|
902
|
13 |
|
if ( is_array( $field_args ) ) { |
903
|
2 |
|
$this->meta_box['fields'][ $field_id ] = array_merge( $field_args, $this->meta_box['fields'][ $field_id ] ); |
904
|
2 |
|
} |
905
|
|
|
|
906
|
|
|
return array( |
907
|
13 |
|
'field_args' => $this->meta_box['fields'][ $field_id ], |
908
|
13 |
|
'object_type' => $this->object_type(), |
909
|
13 |
|
'object_id' => $this->object_id(), |
910
|
13 |
|
); |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
/** |
914
|
|
|
* When fields are added in the old-school way, intitate them as they should be |
915
|
|
|
* @since 2.1.0 |
916
|
|
|
* @param array $fields Array of fields to add |
917
|
|
|
* @param mixed $parent_field_id Parent field id or null |
918
|
|
|
*/ |
919
|
40 |
|
protected function add_fields( $fields, $parent_field_id = null ) { |
920
|
40 |
|
foreach ( $fields as $field ) { |
921
|
|
|
|
922
|
40 |
|
$sub_fields = false; |
923
|
40 |
|
if ( array_key_exists( 'fields', $field ) ) { |
924
|
|
|
$sub_fields = $field['fields']; |
925
|
|
|
unset( $field['fields'] ); |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
$field_id = $parent_field_id |
929
|
40 |
|
? $this->add_group_field( $parent_field_id, $field ) |
930
|
40 |
|
: $this->add_field( $field ); |
931
|
|
|
|
932
|
40 |
|
if ( $sub_fields ) { |
933
|
|
|
$this->add_fields( $sub_fields, $field_id ); |
934
|
|
|
} |
935
|
40 |
|
} |
936
|
40 |
|
} |
937
|
|
|
|
938
|
|
|
/** |
939
|
|
|
* Add a field to the metabox |
940
|
|
|
* @since 2.0.0 |
941
|
|
|
* @param array $field Metabox field config array |
942
|
|
|
* @param int $position (optional) Position of metabox. 1 for first, etc |
943
|
|
|
* @return mixed Field id or false |
944
|
|
|
*/ |
945
|
42 |
|
public function add_field( array $field, $position = 0 ) { |
946
|
42 |
|
if ( ! is_array( $field ) || ! array_key_exists( 'id', $field ) ) { |
947
|
|
|
return false; |
948
|
|
|
} |
949
|
|
|
|
950
|
42 |
|
$this->_add_field_to_array( |
951
|
42 |
|
$field, |
952
|
42 |
|
$this->meta_box['fields'], |
953
|
|
|
$position |
954
|
42 |
|
); |
955
|
|
|
|
956
|
42 |
|
return $field['id']; |
957
|
|
|
} |
958
|
|
|
|
959
|
|
|
/** |
960
|
|
|
* Add a field to a group |
961
|
|
|
* @since 2.0.0 |
962
|
|
|
* @param string $parent_field_id The field id of the group field to add the field |
963
|
|
|
* @param array $field Metabox field config array |
964
|
|
|
* @param int $position (optional) Position of metabox. 1 for first, etc |
965
|
|
|
* @return mixed Array of parent/field ids or false |
966
|
|
|
*/ |
967
|
3 |
|
public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
968
|
3 |
|
if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) { |
969
|
|
|
return false; |
970
|
|
|
} |
971
|
|
|
|
972
|
3 |
|
$parent_field = $this->meta_box['fields'][ $parent_field_id ]; |
973
|
|
|
|
974
|
3 |
|
if ( 'group' !== $parent_field['type'] ) { |
975
|
|
|
return false; |
976
|
|
|
} |
977
|
|
|
|
978
|
3 |
|
if ( ! isset( $parent_field['fields'] ) ) { |
979
|
3 |
|
$this->meta_box['fields'][ $parent_field_id ]['fields'] = array(); |
980
|
3 |
|
} |
981
|
|
|
|
982
|
3 |
|
$this->_add_field_to_array( |
983
|
3 |
|
$field, |
984
|
3 |
|
$this->meta_box['fields'][ $parent_field_id ]['fields'], |
985
|
|
|
$position |
986
|
3 |
|
); |
987
|
|
|
|
988
|
3 |
|
return array( $parent_field_id, $field['id'] ); |
989
|
|
|
} |
990
|
|
|
|
991
|
|
|
/** |
992
|
|
|
* Add a field array to a fields array in desired position |
993
|
|
|
* @since 2.0.2 |
994
|
|
|
* @param array $field Metabox field config array |
995
|
|
|
* @param array &$fields Array (passed by reference) to append the field (array) to |
996
|
|
|
* @param integer $position Optionally specify a position in the array to be inserted |
997
|
|
|
*/ |
998
|
42 |
|
protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
999
|
42 |
|
if ( $position ) { |
1000
|
1 |
|
cmb2_utils()->array_insert( $fields, array( $field['id'] => $field ), $position ); |
1001
|
1 |
|
} else { |
1002
|
42 |
|
$fields[ $field['id'] ] = $field; |
1003
|
|
|
} |
1004
|
42 |
|
} |
1005
|
|
|
|
1006
|
|
|
/** |
1007
|
|
|
* Remove a field from the metabox |
1008
|
|
|
* @since 2.0.0 |
1009
|
|
|
* @param string $field_id The field id of the field to remove |
1010
|
|
|
* @param string $parent_field_id (optional) The field id of the group field to remove field from |
1011
|
|
|
* @return bool True if field was removed |
1012
|
|
|
*/ |
1013
|
2 |
|
public function remove_field( $field_id, $parent_field_id = '' ) { |
1014
|
2 |
|
$ids = $this->get_field_ids( $field_id, $parent_field_id ); |
1015
|
|
|
|
1016
|
2 |
|
if ( ! $ids ) { |
1017
|
|
|
return false; |
1018
|
|
|
} |
1019
|
|
|
|
1020
|
2 |
|
list( $field_id, $sub_field_id ) = $ids; |
1021
|
|
|
|
1022
|
2 |
|
unset( $this->fields[ implode( '', $ids ) ] ); |
1023
|
|
|
|
1024
|
2 |
|
if ( ! $sub_field_id ) { |
1025
|
1 |
|
unset( $this->meta_box['fields'][ $field_id ] ); |
1026
|
1 |
|
return true; |
1027
|
|
|
} |
1028
|
|
|
|
1029
|
1 |
|
unset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] ); |
1030
|
1 |
|
unset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] ); |
1031
|
1 |
|
return true; |
1032
|
|
|
} |
1033
|
|
|
|
1034
|
|
|
/** |
1035
|
|
|
* Update or add a property to a field |
1036
|
|
|
* @since 2.0.0 |
1037
|
|
|
* @param string $field_id Field id |
1038
|
|
|
* @param string $property Field property to set/update |
1039
|
|
|
* @param mixed $value Value to set the field property |
1040
|
|
|
* @param string $parent_field_id (optional) The field id of the group field to remove field from |
1041
|
|
|
* @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
1042
|
|
|
*/ |
1043
|
4 |
|
public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
1044
|
4 |
|
$ids = $this->get_field_ids( $field_id, $parent_field_id ); |
1045
|
|
|
|
1046
|
4 |
|
if ( ! $ids ) { |
1047
|
2 |
|
return false; |
1048
|
|
|
} |
1049
|
|
|
|
1050
|
2 |
|
list( $field_id, $sub_field_id ) = $ids; |
1051
|
|
|
|
1052
|
2 |
|
if ( ! $sub_field_id ) { |
1053
|
2 |
|
$this->meta_box['fields'][ $field_id ][ $property ] = $value; |
1054
|
2 |
|
return $field_id; |
1055
|
|
|
} |
1056
|
|
|
|
1057
|
|
|
$this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ][ $property ] = $value; |
1058
|
|
|
return $field_id; |
1059
|
|
|
} |
1060
|
|
|
|
1061
|
|
|
/** |
1062
|
|
|
* Check if field ids match a field and return the index/field id |
1063
|
|
|
* @since 2.0.2 |
1064
|
|
|
* @param string $field_id Field id |
1065
|
|
|
* @param string $parent_field_id (optional) Parent field id |
1066
|
|
|
* @return mixed Array of field/parent ids, or false |
1067
|
|
|
*/ |
1068
|
19 |
|
public function get_field_ids( $field_id, $parent_field_id = '' ) { |
1069
|
19 |
|
$sub_field_id = $parent_field_id ? $field_id : ''; |
1070
|
19 |
|
$field_id = $parent_field_id ? $parent_field_id : $field_id; |
1071
|
19 |
|
$fields =& $this->meta_box['fields']; |
1072
|
|
|
|
1073
|
19 |
|
if ( ! array_key_exists( $field_id, $fields ) ) { |
1074
|
2 |
|
$field_id = $this->search_old_school_array( $field_id, $fields ); |
1075
|
2 |
|
} |
1076
|
|
|
|
1077
|
19 |
|
if ( false === $field_id ) { |
1078
|
2 |
|
return false; |
1079
|
|
|
} |
1080
|
|
|
|
1081
|
17 |
|
if ( ! $sub_field_id ) { |
1082
|
17 |
|
return array( $field_id, $sub_field_id ); |
1083
|
|
|
} |
1084
|
|
|
|
1085
|
3 |
|
if ( 'group' !== $fields[ $field_id ]['type'] ) { |
1086
|
|
|
return false; |
1087
|
|
|
} |
1088
|
|
|
|
1089
|
3 |
|
if ( ! array_key_exists( $sub_field_id, $fields[ $field_id ]['fields'] ) ) { |
1090
|
|
|
$sub_field_id = $this->search_old_school_array( $sub_field_id, $fields[ $field_id ]['fields'] ); |
1091
|
|
|
} |
1092
|
|
|
|
1093
|
3 |
|
return false === $sub_field_id ? false : array( $field_id, $sub_field_id ); |
1094
|
|
|
} |
1095
|
|
|
|
1096
|
|
|
/** |
1097
|
|
|
* When using the old array filter, it is unlikely field array indexes will be the field id |
1098
|
|
|
* @since 2.0.2 |
1099
|
|
|
* @param string $field_id The field id |
1100
|
|
|
* @param array $fields Array of fields to search |
1101
|
|
|
* @return mixed Field index or false |
1102
|
|
|
*/ |
1103
|
2 |
|
public function search_old_school_array( $field_id, $fields ) { |
1104
|
2 |
|
$ids = wp_list_pluck( $fields, 'id' ); |
1105
|
2 |
|
$index = array_search( $field_id, $ids ); |
1106
|
2 |
|
return false !== $index ? $index : false; |
1107
|
|
|
} |
1108
|
|
|
|
1109
|
|
|
/** |
1110
|
|
|
* Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
1111
|
|
|
* |
1112
|
|
|
* @since 2.0.9 |
1113
|
|
|
* |
1114
|
|
|
* @return bool Whether this cmb should be shown. |
1115
|
|
|
*/ |
1116
|
|
View Code Duplication |
public function should_show() { |
|
|
|
|
1117
|
|
|
// Default to showing this cmb |
1118
|
|
|
$show = true; |
1119
|
|
|
|
1120
|
|
|
// Use the callback to determine showing the cmb, if it exists |
1121
|
|
|
if ( is_callable( $this->prop( 'show_on_cb' ) ) ) { |
1122
|
|
|
$show = (bool) call_user_func( $this->prop( 'show_on_cb' ), $this ); |
1123
|
|
|
} |
1124
|
|
|
|
1125
|
|
|
return $show; |
1126
|
|
|
} |
1127
|
|
|
|
1128
|
|
|
/** |
1129
|
|
|
* Generate a unique nonce field for each registered meta_box |
1130
|
|
|
* @since 2.0.0 |
1131
|
|
|
* @return string unique nonce hidden input |
1132
|
|
|
*/ |
1133
|
1 |
|
public function nonce_field() { |
1134
|
1 |
|
wp_nonce_field( $this->nonce(), $this->nonce(), false, true ); |
1135
|
1 |
|
} |
1136
|
|
|
|
1137
|
|
|
/** |
1138
|
|
|
* Generate a unique nonce for each registered meta_box |
1139
|
|
|
* @since 2.0.0 |
1140
|
|
|
* @return string unique nonce string |
1141
|
|
|
*/ |
1142
|
1 |
|
public function nonce() { |
1143
|
1 |
|
if ( $this->generated_nonce ) { |
1144
|
1 |
|
return $this->generated_nonce; |
1145
|
|
|
} |
1146
|
1 |
|
$this->generated_nonce = sanitize_html_class( 'nonce_' . basename( __FILE__ ) . $this->cmb_id ); |
1147
|
1 |
|
return $this->generated_nonce; |
1148
|
|
|
} |
1149
|
|
|
|
1150
|
|
|
/** |
1151
|
|
|
* Magic getter for our object. |
1152
|
|
|
* @param string $field |
1153
|
|
|
* @throws Exception Throws an exception if the field is invalid. |
1154
|
|
|
* @return mixed |
1155
|
|
|
*/ |
1156
|
42 |
|
public function __get( $field ) { |
1157
|
|
|
switch ( $field ) { |
1158
|
42 |
|
case 'cmb_id': |
1159
|
42 |
|
case 'meta_box': |
1160
|
42 |
|
case 'updated': |
1161
|
42 |
|
return $this->{$field}; |
1162
|
3 |
|
case 'object_id': |
1163
|
1 |
|
return $this->object_id(); |
1164
|
2 |
|
default: |
1165
|
2 |
|
throw new Exception( 'Invalid ' . __CLASS__ . ' property: ' . $field ); |
1166
|
2 |
|
} |
1167
|
|
|
} |
1168
|
|
|
|
1169
|
|
|
} |
1170
|
|
|
|
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.