PluginProbe
WPGet API – Connect to any external REST API / 2.1.3
WPGet API – Connect to any external REST API v2.1.3
1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.10 2.2.2 2.2.3 All 97 releases
wpgetapi / lib / cmb2 / includes / CMB2.php

CMB2.php in WPGet API – Connect to any external REST API 2.1.3, at lib/cmb2/includes/CMB2.php

1,820 lines 52.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CMB2 - The core metabox object
4 *
5 * @category WordPress_Plugin
6 * @package CMB2
7 * @author CMB2 team
8 * @license GPL-2.0+
9 * @link https://cmb2.io
10 *
11 * @property-read string $cmb_id
12 * @property-read array $meta_box
13 * @property-read array $updated
14 * @property-read bool $has_columns
15 * @property-read array $tax_metaboxes_to_remove
16 */
17
18 /**
19 * The main CMB2 object for storing box data/properties.
20 */
21 class CMB2 extends CMB2_Base {
22
23 /**
24 * The object properties name.
25 *
26 * @var string
27 * @since 2.2.3
28 */
29 protected $properties_name = 'meta_box';
30
31 /**
32 * Metabox Config array
33 *
34 * @var array
35 * @since 0.9.0
36 */
37 protected $meta_box = array();
38
39 /**
40 * Type of object registered for metabox. (e.g., post, user, or comment)
41 *
42 * @var string
43 * @since 1.0.0
44 */
45 protected $mb_object_type = null;
46
47 /**
48 * List of fields that are changed/updated on save
49 *
50 * @var array
51 * @since 1.1.0
52 */
53 protected $updated = array();
54
55 /**
56 * Metabox Defaults
57 *
58 * @var array
59 * @since 1.0.1
60 */
61 protected $mb_defaults = array(
62 'id' => '',
63 'title' => '',
64 // Post type slug, or 'user', 'term', 'comment', or 'options-page'.
65 'object_types' => array(),
66
67 /**
68 * The context within the screen where the boxes should display. Available contexts vary
69 * from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'.
70 *
71 * For placement in locations outside of a metabox, other options include:
72 * 'form_top', 'before_permalink', 'after_title', 'after_editor'
73 *
74 * Comments screen contexts include 'normal' and 'side'. Default is 'normal'.
75 */
76 'context' => 'normal',
77 'priority' => 'high', // Or 10 for options pages.
78 'show_names' => true, // Show field names on the left.
79 'show_on_cb' => null, // Callback to determine if metabox should display.
80 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb'.
81 'cmb_styles' => true, // Include CMB2 stylesheet.
82 'enqueue_js' => true, // Include CMB2 JS.
83 'fields' => array(),
84
85 /**
86 * Handles hooking CMB2 forms/metaboxes into the post/attachement/user/options-page screens
87 * and handles hooking in and saving those fields.
88 */
89 'hookup' => true,
90 'save_fields' => true, // Will not save during hookup if false.
91 'closed' => false, // Default metabox to being closed.
92 'taxonomies' => array(),
93 'new_user_section' => 'add-new-user', // or 'add-existing-user'.
94 'new_term_section' => true,
95 'show_in_rest' => false,
96 'classes' => null, // Optionally add classes to the CMB2 wrapper.
97 'classes_cb' => '', // Optionally add classes to the CMB2 wrapper (via a callback).
98
99 /*
100 * The following parameter is for post alternate-context metaboxes only.
101 *
102 * To output the fields 'naked' (without a postbox wrapper/style), then
103 * add a `'remove_box_wrap' => true` to your metabox registration array.
104 */
105 'remove_box_wrap' => false,
106
107 /*
108 * The following parameter is any additional arguments passed as $callback_args
109 * to add_meta_box, if/when applicable.
110 *
111 * CMB2 does not use these arguments in the add_meta_box callback, however, these args
112 * are parsed for certain special properties, like determining Gutenberg/block-editor
113 * compatibility.
114 *
115 * Examples:
116 *
117 * - Make sure default editor is used as metabox is not compatible with block editor
118 * [ '__block_editor_compatible_meta_box' => false/true ]
119 *
120 * - Or declare this box exists for backwards compatibility
121 * [ '__back_compat_meta_box' => false ]
122 *
123 * More: https://wordpress.org/gutenberg/handbook/extensibility/meta-box/
124 */
125 'mb_callback_args' => null,
126
127 /*
128 * The following parameters are for options-page metaboxes,
129 * and several are passed along to add_menu_page()/add_submenu_page()
130 */
131
132 // 'menu_title' => null, // Falls back to 'title' (above). Do not define here so we can set a fallback.
133 'message_cb' => '', // Optionally define the options-save message (via a callback).
134 'option_key' => '', // The actual option key and admin menu page slug.
135 'parent_slug' => '', // Used as first param in add_submenu_page().
136 'capability' => 'manage_options', // Cap required to view options-page.
137 'icon_url' => '', // Menu icon. Only applicable if 'parent_slug' is left empty.
138 'position' => null, // Menu position. Only applicable if 'parent_slug' is left empty.
139
140 'admin_menu_hook' => 'admin_menu', // Alternately 'network_admin_menu' to add network-level options page.
141 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
142 'save_button' => '', // The text for the options-page save button. Defaults to 'Save'.
143 'disable_settings_errors' => false, // On settings pages (not options-general.php sub-pages), allows disabling.
144 'tab_group' => '', // Tab-group identifier, enables options page tab navigation.
145 // 'tab_title' => null, // Falls back to 'title' (above). Do not define here so we can set a fallback.
146 // 'autoload' => true, // Defaults to true, the options-page option will be autloaded.
147 );
148
149 /**
150 * Metabox field objects
151 *
152 * @var array
153 * @since 2.0.3
154 */
155 protected $fields = array();
156
157 /**
158 * An array of hidden fields to output at the end of the form
159 *
160 * @var array
161 * @since 2.0.0
162 */
163 protected $hidden_fields = array();
164
165 /**
166 * Array of key => value data for saving. Likely $_POST data.
167 *
168 * @var string
169 * @since 2.0.0
170 */
171 protected $generated_nonce = '';
172
173 /**
174 * Whether there are fields to be shown in columns. Set in CMB2::add_field().
175 *
176 * @var bool
177 * @since 2.2.2
178 */
179 protected $has_columns = false;
180
181 /**
182 * If taxonomy field is requesting to remove_default, we store the taxonomy here.
183 *
184 * @var array
185 * @since 2.2.3
186 */
187 protected $tax_metaboxes_to_remove = array();
188
189 /**
190 * Get started
191 *
192 * @since 0.4.0
193 * @param array $config Metabox config array.
194 * @param integer $object_id Optional object id.
195 */
196 public function __construct( $config, $object_id = 0 ) {
197
198 if ( empty( $config['id'] ) ) {
199 wp_die( esc_html__( 'Metabox configuration is required to have an ID parameter.', 'cmb2' ) );
200 }
201
202 $this->cmb_id = $config['id'];
203 $this->meta_box = wp_parse_args( $config, $this->mb_defaults );
204 $this->meta_box['fields'] = array();
205
206 // Ensures object_types is an array.
207 $this->set_prop( 'object_types', $this->box_types() );
208 $this->object_id( $object_id );
209
210 if ( $this->is_options_page_mb() ) {
211
212 // Check initial priority.
213 if ( empty( $config['priority'] ) ) {
214
215 // If not explicitly defined, Reset the priority to 10
216 // Fixes https://github.com/CMB2/CMB2/issues/1410.
217 $this->meta_box['priority'] = 10;
218 }
219
220 $this->init_options_mb();
221 }
222
223 $this->mb_object_type();
224
225 if ( ! empty( $config['fields'] ) && is_array( $config['fields'] ) ) {
226 $this->add_fields( $config['fields'] );
227 }
228
229 CMB2_Boxes::add( $this );
230
231 /**
232 * Hook during initiation of CMB2 object
233 *
234 * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id.
235 *
236 * @param array $cmb This CMB2 object
237 */
238 do_action( "cmb2_init_{$this->cmb_id}", $this );
239
240 // Hook in the hookup... how meta.
241 add_action( "cmb2_init_hookup_{$this->cmb_id}", array( 'CMB2_Hookup', 'maybe_init_and_hookup' ) );
242
243 // Hook in the rest api functionality.
244 add_action( "cmb2_init_hookup_{$this->cmb_id}", array( 'CMB2_REST', 'maybe_init_and_hookup' ) );
245 }
246
247 /**
248 * Loops through and displays fields
249 *
250 * @since 1.0.0
251 * @param int $object_id Object ID.
252 * @param string $object_type Type of object being saved. (e.g., post, user, or comment).
253 *
254 * @return CMB2
255 */
256 public function show_form( $object_id = 0, $object_type = '' ) {
257 $this->render_form_open( $object_id, $object_type );
258
259 foreach ( $this->prop( 'fields' ) as $field_args ) {
260 $this->render_field( $field_args );
261 }
262
263 return $this->render_form_close( $object_id, $object_type );
264 }
265
266 /**
267 * Outputs the opening form markup and runs corresponding hooks:
268 * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}"
269 *
270 * @since 2.2.0
271 * @param integer $object_id Object ID.
272 * @param string $object_type Object type.
273 *
274 * @return CMB2
275 */
276 public function render_form_open( $object_id = 0, $object_type = '' ) {
277 $object_type = $this->object_type( $object_type );
278 $object_id = $this->object_id( $object_id );
279
280 echo "\n<!-- Begin CMB2 Fields -->\n";
281
282 $this->nonce_field();
283
284 /**
285 * Hook before form table begins
286 *
287 * @param array $cmb_id The current box ID.
288 * @param int $object_id The ID of the current object.
289 * @param string $object_type The type of object you are working with.
290 * Usually `post` (this applies to all post-types).
291 * Could also be `comment`, `user` or `options-page`.
292 * @param array $cmb This CMB2 object.
293 */
294 do_action( 'cmb2_before_form', $this->cmb_id, $object_id, $object_type, $this );
295
296 /**
297 * Hook before form table begins
298 *
299 * The first dynamic portion of the hook name, $object_type, is the type of object
300 * you are working with. Usually `post` (this applies to all post-types).
301 * Could also be `comment`, `user` or `options-page`.
302 *
303 * The second dynamic portion of the hook name, $this->cmb_id, is the meta_box id.
304 *
305 * @param array $cmb_id The current box ID
306 * @param int $object_id The ID of the current object
307 * @param array $cmb This CMB2 object
308 */
309 do_action( "cmb2_before_{$object_type}_form_{$this->cmb_id}", $object_id, $this );
310
311 echo '<div class="', esc_attr( $this->box_classes() ), '"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">';
312
313 return $this;
314 }
315
316 /**
317 * Defines the classes for the CMB2 form/wrap.
318 *
319 * @since 2.0.0
320 * @return string Space concatenated list of classes
321 */
322 public function box_classes() {
323
324 $classes = array( 'cmb2-wrap', 'form-table' );
325
326 // Use the callback to fetch classes.
327 if ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) {
328 $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes );
329 $classes = array_merge( $classes, $added_classes );
330 }
331
332 if ( $added_classes = $this->prop( 'classes' ) ) {
333 $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes );
334 $classes = array_merge( $classes, $added_classes );
335 }
336
337 /**
338 * Add our context classes for non-standard metaboxes.
339 *
340 * @since 2.2.4
341 */
342 if ( $this->is_alternate_context_box() ) {
343 $context = array();
344
345 // Include custom class if requesting no title.
346 if ( ! $this->prop( 'title' ) && ! $this->prop( 'remove_box_wrap' ) ) {
347 $context[] = 'cmb2-context-wrap-no-title';
348 }
349
350 // Include a generic context wrapper.
351 $context[] = 'cmb2-context-wrap';
352
353 // Include a context-type based context wrapper.
354 $context[] = 'cmb2-context-wrap-' . $this->prop( 'context' );
355
356 // Include an ID based context wrapper as well.
357 $context[] = 'cmb2-context-wrap-' . $this->prop( 'id' );
358
359 // And merge all the classes back into the array.
360 $classes = array_merge( $classes, $context );
361 }
362
363 /**
364 * Globally filter box wrap classes
365 *
366 * @since 2.2.2
367 *
368 * @param string $classes Array of classes for the cmb2-wrap.
369 * @param CMB2 $cmb This CMB2 object.
370 */
371 $classes = apply_filters( 'cmb2_wrap_classes', $classes, $this );
372
373 $split = array();
374 foreach ( array_filter( $classes ) as $class ) {
375 foreach ( explode( ' ', $class ) as $_class ) {
376 // Clean up & sanitize.
377 $split[] = sanitize_html_class( strip_tags( $_class ) );
378 }
379 }
380 $classes = $split;
381
382 // Remove any duplicates.
383 $classes = array_unique( $classes );
384
385 // Make it a string.
386 return implode( ' ', $classes );
387 }
388
389 /**
390 * Outputs the closing form markup and runs corresponding hooks:
391 * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}"
392 *
393 * @since 2.2.0
394 * @param integer $object_id Object ID.
395 * @param string $object_type Object type.
396 *
397 * @return CMB2
398 */
399 public function render_form_close( $object_id = 0, $object_type = '' ) {
400 $object_type = $this->object_type( $object_type );
401 $object_id = $this->object_id( $object_id );
402
403 echo '</div></div>';
404
405 $this->render_hidden_fields();
406
407 /**
408 * Hook after form form has been rendered
409 *
410 * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id.
411 *
412 * The first dynamic portion of the hook name, $object_type, is the type of object
413 * you are working with. Usually `post` (this applies to all post-types).
414 * Could also be `comment`, `user` or `options-page`.
415 *
416 * @param int $object_id The ID of the current object
417 * @param array $cmb This CMB2 object
418 */
419 do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this );
420
421 /**
422 * Hook after form form has been rendered
423 *
424 * @param array $cmb_id The current box ID.
425 * @param int $object_id The ID of the current object.
426 * @param string $object_type The type of object you are working with.
427 * Usually `post` (this applies to all post-types).
428 * Could also be `comment`, `user` or `options-page`.
429 * @param array $cmb This CMB2 object.
430 */
431 do_action( 'cmb2_after_form', $this->cmb_id, $object_id, $object_type, $this );
432
433 echo "\n<!-- End CMB2 Fields -->\n";
434
435 return $this;
436 }
437
438 /**
439 * Renders a field based on the field type
440 *
441 * @since 2.2.0
442 * @param array $field_args A field configuration array.
443 * @return mixed CMB2_Field object if successful.
444 */
445 public function render_field( $field_args ) {
446 $field_args['context'] = $this->prop( 'context' );
447
448 if ( 'group' === $field_args['type'] ) {
449
450 if ( ! isset( $field_args['show_names'] ) ) {
451 $field_args['show_names'] = $this->prop( 'show_names' );
452 }
453 $field = $this->render_group( $field_args );
454
455 } elseif ( 'hidden' === $field_args['type'] && $this->get_field( $field_args )->should_show() ) {
456 // Save rendering for after the metabox.
457 $field = $this->add_hidden_field( $field_args );
458
459 } else {
460
461 $field_args['show_names'] = $this->prop( 'show_names' );
462
463 // Render default fields.
464 $field = $this->get_field( $field_args )->render_field();
465 }
466
467 return $field;
468 }
469
470 /**
471 * Render a group of fields.
472 *
473 * @param array|CMB2_Field $args Array of field arguments for a group field parent or the group parent field.
474 * @return CMB2_Field|null Group field object.
475 */
476 public function render_group( $args ) {
477 $field_group = false;
478
479 if ( $args instanceof CMB2_Field ) {
480 $field_group = 'group' === $args->type() ? $args : false;
481 } elseif ( isset( $args['id'], $args['fields'] ) && is_array( $args['fields'] ) ) {
482 $field_group = $this->get_field( $args );
483 }
484
485 if ( ! $field_group ) {
486 return;
487 }
488
489 $field_group->render_context = 'edit';
490 $field_group->peform_param_callback( 'render_row_cb' );
491
492 return $field_group;
493 }
494
495 /**
496 * The default callback to render a group of fields.
497 *
498 * @since 2.2.6
499 *
500 * @param array $field_args Array of field arguments for the group field parent.
501 * @param CMB2_Field $field_group The CMB2_Field group object.
502 *
503 * @return CMB2_Field|null Group field object.
504 */
505 public function render_group_callback( $field_args, $field_group ) {
506
507 // If field is requesting to be conditionally shown.
508 if ( ! $field_group || ! $field_group->should_show() ) {
509 return;
510 }
511
512 $field_group->index = 0;
513
514 $field_group->peform_param_callback( 'before_group' );
515
516 $desc = $field_group->args( 'description' );
517 $label = $field_group->args( 'name' );
518 $group_val = (array) $field_group->value();
519
520 echo '<div class="cmb-row cmb-repeat-group-wrap ', esc_attr( $field_group->row_classes() ), '" data-fieldtype="group"><div class="cmb-td"><div data-groupid="', esc_attr( $field_group->id() ), '" id="', esc_attr( $field_group->id() ), '_repeat" ', $this->group_wrap_attributes( $field_group ), '>';
521
522 if ( $desc || $label ) {
523 $class = $desc ? ' cmb-group-description' : '';
524 echo '<div class="cmb-row', $class, '"><div class="cmb-th">';
525 if ( $label ) {
526 echo '<h2 class="cmb-group-name">', $label, '</h2>';
527 }
528 if ( $desc ) {
529 echo '<p class="cmb2-metabox-description">', $desc, '</p>';
530 }
531 echo '</div></div>';
532 }
533
534 if ( ! empty( $group_val ) ) {
535 foreach ( $group_val as $group_key => $field_id ) {
536 $this->render_group_row( $field_group );
537 $field_group->index++;
538 }
539 } else {
540 $this->render_group_row( $field_group );
541 }
542
543 if ( $field_group->args( 'repeatable' ) ) {
544 echo '<div class="cmb-row"><div class="cmb-td"><p class="cmb-add-row"><button type="button" data-selector="', esc_attr( $field_group->id() ), '_repeat" data-grouptitle="', esc_attr( $field_group->options( 'group_title' ) ), '" class="cmb-add-group-row button-secondary">', $field_group->options( 'add_button' ), '</button></p></div></div>';
545 }
546
547 echo '</div></div></div>';
548
549 $field_group->peform_param_callback( 'after_group' );
550
551 return $field_group;
552 }
553
554 /**
555 * Get the group wrap attributes, which are passed through a filter.
556 *
557 * @since 2.2.3
558 * @param CMB2_Field $field_group The group CMB2_Field object.
559 * @return string The attributes string.
560 */
561 public function group_wrap_attributes( $field_group ) {
562 $classes = 'cmb-nested cmb-field-list cmb-repeatable-group';
563 $classes .= $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable';
564 $classes .= $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable';
565
566 $group_wrap_attributes = array(
567 'class' => $classes,
568 'style' => 'width:100%;',
569 );
570
571 /**
572 * Allow for adding additional HTML attributes to a group wrapper.
573 *
574 * The attributes will be an array of key => value pairs for each attribute.
575 *
576 * @since 2.2.2
577 *
578 * @param string $group_wrap_attributes Current attributes array.
579 * @param CMB2_Field $field_group The group CMB2_Field object.
580 */
581 $group_wrap_attributes = apply_filters( 'cmb2_group_wrap_attributes', $group_wrap_attributes, $field_group );
582
583 $atts = array();
584 foreach ( $group_wrap_attributes as $att => $att_value ) {
585 if ( ! CMB2_Utils::is_data_attribute( $att ) ) {
586 $att_value = htmlspecialchars( $att_value );
587 }
588
589 $atts[ sanitize_html_class( $att ) ] = sanitize_text_field( $att_value );
590 }
591
592 return CMB2_Utils::concat_attrs( $atts );
593 }
594
595 /**
596 * Render a repeatable group row
597 *
598 * @since 1.0.2
599 * @param CMB2_Field $field_group CMB2_Field group field object.
600 *
601 * @return CMB2
602 */
603 public function render_group_row( $field_group ) {
604
605 $field_group->peform_param_callback( 'before_group_row' );
606 $closed_class = $field_group->options( 'closed' ) ? ' closed' : '';
607 $confirm_deletion = $field_group->options( 'remove_confirm' );
608 $confirm_deletion = ! empty( $confirm_deletion ) ? $confirm_deletion : '';
609
610 $group_title = isset( $field_group->value[$field_group->index]['id'] ) ? $field_group->value[$field_group->index]['id'] : $field_group->replace_hash( $field_group->options( 'group_title' ) );
611
612 echo '
613 <div id="cmb-group-', $field_group->id(), '-', $field_group->index, '" class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">';
614
615 if ( $field_group->args( 'repeatable' ) ) {
616 echo '<button type="button" data-selector="', $field_group->id(), '_repeat" data-confirm="', esc_attr( $confirm_deletion ), '" class="dashicons-before dashicons-no-alt cmb-remove-group-row" title="', esc_attr( $field_group->options( 'remove_button' ) ), '"></button>';
617 }
618
619 echo '
620 <div class="cmbhandle" title="' , esc_attr__( 'Click to toggle', 'cmb2' ), '"><br></div>
621 <h3 class="cmb-group-title cmbhandle-title"><span>', $group_title, '</span></h3>
622
623 <div class="inside cmb-td cmb-nested cmb-field-list">';
624 // Loop and render repeatable group fields.
625 foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) {
626 if ( 'hidden' === $field_args['type'] ) {
627
628 // Save rendering for after the metabox.
629 $this->add_hidden_field( $field_args, $field_group );
630
631 } else {
632
633 $field_args['show_names'] = $field_group->args( 'show_names' );
634 $field_args['context'] = $field_group->args( 'context' );
635
636 $this->get_field( $field_args, $field_group )->render_field();
637 }
638 }
639
640 if ( $field_group->args( 'repeatable' ) ) {
641 echo '
642 <div class="cmb-row cmb-remove-field-row">
643 <div class="cmb-remove-row">
644 <button type="button" data-selector="', $field_group->id(), '_repeat" data-confirm="', esc_attr( $confirm_deletion ), '" class="cmb-remove-group-row cmb-remove-group-row-button alignright button-secondary">', $field_group->options( 'remove_button' ), '</button>
645 </div>
646 </div>
647 ';
648 }
649 echo '
650 </div>
651 </div>
652 ';
653
654 $field_group->peform_param_callback( 'after_group_row' );
655
656 return $this;
657 }
658
659 /**
660 * Add a hidden field to the list of hidden fields to be rendered later.
661 *
662 * @since 2.0.0
663 *
664 * @param array $field_args Array of field arguments to be passed to CMB2_Field.
665 * @param CMB2_Field|null $field_group CMB2_Field group field object.
666 * @return CMB2_Field
667 */
668 public function add_hidden_field( $field_args, $field_group = null ) {
669 if ( isset( $field_args['field_args'] ) ) {
670 // For back-compatibility.
671 $field = new CMB2_Field( $field_args );
672 } else {
673 $field = $this->get_new_field( $field_args, $field_group );
674 }
675
676 $types = new CMB2_Types( $field );
677
678 if ( $field_group ) {
679 $types->iterator = $field_group->index;
680 }
681
682 $this->hidden_fields[] = $types;
683
684 return $field;
685 }
686
687 /**
688 * Loop through and output hidden fields
689 *
690 * @since 2.0.0
691 *
692 * @return CMB2
693 */
694 public function render_hidden_fields() {
695 if ( ! empty( $this->hidden_fields ) ) {
696 foreach ( $this->hidden_fields as $hidden ) {
697 $hidden->render();
698 }
699 }
700
701 return $this;
702 }
703
704 /**
705 * Returns array of sanitized field values (without saving them)
706 *
707 * @since 2.0.3
708 * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data).
709 * @return mixed
710 */
711 public function get_sanitized_values( array $data_to_sanitize ) {
712 $this->data_to_save = $data_to_sanitize;
713 $stored_id = $this->object_id();
714
715 // We do this So CMB will sanitize our data for us, but not save it.
716 $this->object_id( '_' );
717
718 // Ensure temp. data store is empty.
719 cmb2_options( 0 )->set();
720
721 // We want to get any taxonomy values back.
722 add_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' );
723
724 // Process/save fields.
725 $this->process_fields();
726
727 // Put things back the way they were.
728 remove_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' );
729
730 // Get data from temp. data store.
731 $sanitized_values = cmb2_options( 0 )->get_options();
732
733 // Empty out temp. data store again.
734 cmb2_options( 0 )->set();
735
736 // Reset the object id.
737 $this->object_id( $stored_id );
738
739 return $sanitized_values;
740 }
741
742 /**
743 * Loops through and saves field data
744 *
745 * @since 1.0.0
746 * @param int $object_id Object ID.
747 * @param string $object_type Type of object being saved. (e.g., post, user, or comment).
748 * @param array $data_to_save Array of key => value data for saving. Likely $_POST data.
749 *
750 * @return CMB2
751 */
752 public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) {
753
754 // Fall-back to $_POST data.
755 $this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST;
756 $object_id = $this->object_id( $object_id );
757 $object_type = $this->object_type( $object_type );
758
759 $this->process_fields();
760
761 // If options page, save the updated options.
762 if ( 'options-page' === $object_type ) {
763 cmb2_options( $object_id )->set();
764 }
765
766 return $this->after_save();
767 }
768
769 /**
770 * Process and save form fields
771 *
772 * @since 2.0.0
773 *
774 * @return CMB2
775 */
776 public function process_fields() {
777
778 $this->pre_process();
779
780 // Remove the show_on properties so saving works.
781 $this->prop( 'show_on', array() );
782
783 // save field ids of those that are updated.
784 $this->updated = array();
785
786 foreach ( $this->prop( 'fields' ) as $field_args ) {
787 $this->process_field( $field_args );
788 }
789
790 return $this;
791 }
792
793 /**
794 * Process and save a field
795 *
796 * @since 2.0.0
797 * @param array $field_args Array of field arguments.
798 *
799 * @return CMB2
800 */
801 public function process_field( $field_args ) {
802
803 switch ( $field_args['type'] ) {
804
805 case 'group':
806 if ( $this->save_group( $field_args ) ) {
807 $this->updated[] = $field_args['id'];
808 }
809
810 break;
811
812 case 'title':
813 // Don't process title fields.
814 break;
815
816 default:
817 $field = $this->get_new_field( $field_args );
818
819 if ( $field->save_field_from_data( $this->data_to_save ) ) {
820 $this->updated[] = $field->id();
821 }
822
823 break;
824 }
825
826 return $this;
827 }
828
829 /**
830 * Fires the "cmb2_{$object_type}_process_fields_{$cmb_id}" action hook.
831 *
832 * @since 2.2.2
833 *
834 * @return CMB2
835 */
836 public function pre_process() {
837 $object_type = $this->object_type();
838
839 /**
840 * Fires before fields have been processed/saved.
841 *
842 * The dynamic portion of the hook name, $object_type, refers to the
843 * metabox/form's object type
844 * Usually `post` (this applies to all post-types).
845 * Could also be `comment`, `user` or `options-page`.
846 *
847 * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id.
848 *
849 * @param array $cmb This CMB2 object
850 * @param int $object_id The ID of the current object
851 */
852 do_action( "cmb2_{$object_type}_process_fields_{$this->cmb_id}", $this, $this->object_id() );
853
854 return $this;
855 }
856
857 /**
858 * Fires the "cmb2_save_{$object_type}_fields" and
859 * "cmb2_save_{$object_type}_fields_{$cmb_id}" action hooks.
860 *
861 * @since 2.x.x
862 *
863 * @return CMB2
864 */
865 public function after_save() {
866 $object_type = $this->object_type();
867 $object_id = $this->object_id();
868
869 /**
870 * Fires after all fields have been saved.
871 *
872 * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type
873 * Usually `post` (this applies to all post-types).
874 * Could also be `comment`, `user` or `options-page`.
875 *
876 * @param int $object_id The ID of the current object
877 * @param array $cmb_id The current box ID
878 * @param string $updated Array of field ids that were updated.
879 * Will only include field ids that had values change.
880 * @param array $cmb This CMB2 object
881 */
882 do_action( "cmb2_save_{$object_type}_fields", $object_id, $this->cmb_id, $this->updated, $this );
883
884 /**
885 * Fires after all fields have been saved.
886 *
887 * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id.
888 *
889 * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type
890 * Usually `post` (this applies to all post-types).
891 * Could also be `comment`, `user` or `options-page`.
892 *
893 * @param int $object_id The ID of the current object
894 * @param string $updated Array of field ids that were updated.
895 * Will only include field ids that had values change.
896 * @param array $cmb This CMB2 object
897 */
898 do_action( "cmb2_save_{$object_type}_fields_{$this->cmb_id}", $object_id, $this->updated, $this );
899
900 return $this;
901 }
902
903 /**
904 * Save a repeatable group
905 *
906 * @since 1.x.x
907 * @param array $args Field arguments array.
908 * @return mixed Return of CMB2_Field::update_data().
909 */
910 public function save_group( $args ) {
911 if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) {
912 return;
913 }
914
915 return $this->save_group_field( $this->get_new_field( $args ) );
916 }
917
918 /**
919 * Save a repeatable group
920 *
921 * @since 1.x.x
922 * @param CMB2_Field $field_group CMB2_Field group field object.
923 * @return mixed Return of CMB2_Field::update_data().
924 */
925 public function save_group_field( $field_group ) {
926 $base_id = $field_group->id();
927
928 if ( ! isset( $this->data_to_save[ $base_id ] ) ) {
929 return;
930 }
931
932 $old = $field_group->get_data();
933 // Check if group field has sanitization_cb.
934 $group_vals = $field_group->sanitization_cb( $this->data_to_save[ $base_id ] );
935 $saved = array();
936
937 $field_group->index = 0;
938 $field_group->data_to_save = $this->data_to_save;
939
940 foreach ( array_values( $field_group->fields() ) as $field_args ) {
941 if ( 'title' === $field_args['type'] ) {
942 // Don't process title fields.
943 continue;
944 }
945
946 $field = $this->get_new_field( $field_args, $field_group );
947 $sub_id = $field->id( true );
948 if ( empty( $saved[ $field_group->index ] ) ) {
949 $saved[ $field_group->index ] = array();
950 }
951
952 foreach ( (array) $group_vals as $field_group->index => $post_vals ) {
953
954 // Get value.
955 $new_val = isset( $group_vals[ $field_group->index ][ $sub_id ] )
956 ? $group_vals[ $field_group->index ][ $sub_id ]
957 : false;
958
959 // Sanitize.
960 $new_val = $field->sanitization_cb( $new_val );
961
962 if ( is_array( $new_val ) && $field->args( 'has_supporting_data' ) ) {
963 if ( $field->args( 'repeatable' ) ) {
964 $_new_val = array();
965 foreach ( $new_val as $group_index => $grouped_data ) {
966 // Add the supporting data to the $saved array stack.
967 $saved[ $field_group->index ][ $grouped_data['supporting_field_id'] ][] = $grouped_data['supporting_field_value'];
968 // Reset var to the actual value.
969 $_new_val[ $group_index ] = $grouped_data['value'];
970 }
971 $new_val = $_new_val;
972 } else {
973 // Add the supporting data to the $saved array stack.
974 $saved[ $field_group->index ][ $new_val['supporting_field_id'] ] = $new_val['supporting_field_value'];
975 // Reset var to the actual value.
976 $new_val = $new_val['value'];
977 }
978 }
979
980 // Get old value.
981 $old_val = is_array( $old ) && isset( $old[ $field_group->index ][ $sub_id ] )
982 ? $old[ $field_group->index ][ $sub_id ]
983 : false;
984
985 $is_updated = ( ! CMB2_Utils::isempty( $new_val ) && $new_val !== $old_val );
986 $is_removed = ( CMB2_Utils::isempty( $new_val ) && ! CMB2_Utils::isempty( $old_val ) );
987
988 // Compare values and add to `$updated` array.
989 if ( $is_updated || $is_removed ) {
990 $this->updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id;
991 }
992
993 // Add to `$saved` array.
994 $saved[ $field_group->index ][ $sub_id ] = $new_val;
995
996 }// End foreach.
997
998 $saved[ $field_group->index ] = CMB2_Utils::filter_empty( $saved[ $field_group->index ] );
999 }// End foreach.
1000
1001 $saved = CMB2_Utils::filter_empty( $saved );
1002
1003 return $field_group->update_data( $saved, true );
1004 }
1005
1006 /**
1007 * Get object id from global space if no id is provided
1008 *
1009 * @since 1.0.0
1010 * @param integer|string $object_id Object ID.
1011 * @return integer|string $object_id Object ID.
1012 */
1013 public function object_id( $object_id = 0 ) {
1014 global $pagenow;
1015
1016 if ( $object_id ) {
1017 $this->object_id = $object_id;
1018 return $this->object_id;
1019 }
1020
1021 if ( $this->object_id ) {
1022 return $this->object_id;
1023 }
1024
1025 // Try to get our object ID from the global space.
1026 switch ( $this->object_type() ) {
1027 case 'user':
1028 $object_id = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['user_id'] ) ) : $object_id;
1029 $object_id = ! $object_id && 'user-new.php' !== $pagenow && isset( $GLOBALS['user_ID'] ) ? $GLOBALS['user_ID'] : $object_id;
1030 break;
1031
1032 case 'comment':
1033 $object_id = isset( $_REQUEST['c'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['c'] ) ) : $object_id;
1034 $object_id = ! $object_id && isset( $GLOBALS['comments']->comment_ID ) ? $GLOBALS['comments']->comment_ID : $object_id;
1035 break;
1036
1037 case 'term':
1038 $object_id = isset( $_REQUEST['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tag_ID'] ) ) : $object_id;
1039 break;
1040
1041 case 'options-page':
1042 $key = $this->doing_options_page();
1043 if ( ! empty( $key ) ) {
1044 $object_id = $key;
1045 }
1046 break;
1047
1048 default:
1049 $object_id = isset( $GLOBALS['post']->ID ) ? $GLOBALS['post']->ID : $object_id;
1050 $object_id = isset( $_REQUEST['post'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post'] ) ) : $object_id;
1051 break;
1052 }
1053
1054 // reset to id or 0.
1055 $this->object_id = $object_id ? $object_id : 0;
1056
1057 return $this->object_id;
1058 }
1059
1060 /**
1061 * Sets the $object_type based on metabox settings
1062 *
1063 * @since 1.0.0
1064 * @return string Object type.
1065 */
1066 public function mb_object_type() {
1067 if ( null !== $this->mb_object_type ) {
1068 return $this->mb_object_type;
1069 }
1070
1071 if ( $this->is_options_page_mb() ) {
1072 $this->mb_object_type = 'options-page';
1073 return $this->mb_object_type;
1074 }
1075
1076 $registered_types = $this->box_types();
1077
1078 $type = '';
1079
1080 // if it's an array of one, extract it.
1081 if ( 1 === count( $registered_types ) ) {
1082 $last = end( $registered_types );
1083 if ( is_string( $last ) ) {
1084 $type = $last;
1085 }
1086 } elseif ( ( $curr_type = $this->current_object_type() ) && in_array( $curr_type, $registered_types, true ) ) {
1087 $type = $curr_type;
1088 }
1089
1090 // Get our object type.
1091 switch ( $type ) {
1092
1093 case 'user':
1094 case 'comment':
1095 case 'term':
1096 $this->mb_object_type = $type;
1097 break;
1098
1099 default:
1100 $this->mb_object_type = 'post';
1101 break;
1102 }
1103
1104 return $this->mb_object_type;
1105 }
1106
1107 /**
1108 * Gets the box 'object_types' array based on box settings.
1109 *
1110 * @since 2.2.3
1111 * @param array $fallback Fallback value.
1112 *
1113 * @return array Object types.
1114 */
1115 public function box_types( $fallback = array() ) {
1116 return CMB2_Utils::ensure_array( $this->prop( 'object_types' ), $fallback );
1117 }
1118
1119 /**
1120 * Check if given object_type(s) matches any of the registered object types or
1121 * taxonomies for this box.
1122 *
1123 * @since 2.7.0
1124 * @param string|array $object_types The object type(s) to check.
1125 * @param array $fallback Fallback object_types value.
1126 *
1127 * @return bool Whether given object type(s) are registered to this box.
1128 */
1129 public function is_box_type( $object_types = array(), $fallback = array() ) {
1130 $object_types = (array) $object_types;
1131 $box_types = $this->box_types( $fallback );
1132
1133 if ( in_array( 'term', $box_types, true ) ) {
1134 $taxonomies = CMB2_Utils::ensure_array( $this->prop( 'taxonomies' ) );
1135 $box_types = array_merge( $box_types, $taxonomies );
1136 }
1137
1138 $found = array_intersect( $object_types, $box_types );
1139
1140 return ! empty( $found );
1141 }
1142
1143 /**
1144 * Initates the object types and option key for an options page metabox.
1145 *
1146 * @since 2.2.5
1147 *
1148 * @return void
1149 */
1150 public function init_options_mb() {
1151 $keys = $this->options_page_keys();
1152 $types = $this->box_types();
1153
1154 if ( empty( $keys ) ) {
1155 $keys = '';
1156 $types = $this->deinit_options_mb( $types );
1157 } else {
1158
1159 // Make sure 'options-page' is one of the object types.
1160 $types[] = 'options-page';
1161 }
1162
1163 // Set/Reset the option_key property.
1164 $this->set_prop( 'option_key', $keys );
1165
1166 // Reset the object types.
1167 $this->set_prop( 'object_types', array_unique( $types ) );
1168 }
1169
1170 /**
1171 * If object-page initiation failed, remove traces options page setup.
1172 *
1173 * @since 2.2.5
1174 *
1175 * @param array $types Array of types.
1176 * @return array
1177 */
1178 protected function deinit_options_mb( $types ) {
1179 if ( isset( $this->meta_box['show_on']['key'] ) && 'options-page' === $this->meta_box['show_on']['key'] ) {
1180 unset( $this->meta_box['show_on']['key'] );
1181 }
1182
1183 if ( array_key_exists( 'options-page', $this->meta_box['show_on'] ) ) {
1184 unset( $this->meta_box['show_on']['options-page'] );
1185 }
1186
1187 $index = array_search( 'options-page', $types );
1188
1189 if ( false !== $index ) {
1190 unset( $types[ $index ] );
1191 }
1192
1193 return $types;
1194 }
1195
1196 /**
1197 * Determines if metabox is for an options page
1198 *
1199 * @since 1.0.1
1200 * @return boolean True/False.
1201 */
1202 public function is_options_page_mb() {
1203 return (
1204 // 'show_on' values checked for back-compatibility.
1205 $this->is_old_school_options_page_mb()
1206 || in_array( 'options-page', $this->box_types() )
1207 );
1208 }
1209
1210 /**
1211 * Determines if metabox uses old-schoold options page config.
1212 *
1213 * @since 2.2.5
1214 * @return boolean True/False.
1215 */
1216 public function is_old_school_options_page_mb() {
1217 return (
1218 // 'show_on' values checked for back-compatibility.
1219 isset( $this->meta_box['show_on']['key'] ) && 'options-page' === $this->meta_box['show_on']['key']
1220 || array_key_exists( 'options-page', $this->meta_box['show_on'] )
1221 );
1222 }
1223
1224 /**
1225 * Determine if we are on an options page (or saving the options page).
1226 *
1227 * @since 2.2.5
1228 *
1229 * @return bool
1230 */
1231 public function doing_options_page() {
1232 $found_key = false;
1233 $keys = $this->options_page_keys();
1234
1235 if ( empty( $keys ) ) {
1236 return $found_key;
1237 }
1238
1239 if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], $keys ) ) {
1240 $found_key = $_GET['page'];
1241 }
1242
1243 if ( ! empty( $_POST['action'] ) && in_array( $_POST['action'], $keys ) ) {
1244 $found_key = $_POST['action'];
1245 }
1246
1247 return $found_key ? $found_key : false;
1248 }
1249
1250 /**
1251 * Get the options page key.
1252 *
1253 * @since 2.2.5
1254 * @return string|array
1255 */
1256 public function options_page_keys() {
1257 $key = '';
1258 if ( ! $this->is_options_page_mb() ) {
1259 return $key;
1260 }
1261
1262 $values = null;
1263 if ( ! empty( $this->meta_box['show_on']['value'] ) ) {
1264 $values = $this->meta_box['show_on']['value'];
1265 } elseif ( ! empty( $this->meta_box['show_on']['options-page'] ) ) {
1266 $values = $this->meta_box['show_on']['options-page'];
1267 } elseif ( $this->prop( 'option_key' ) ) {
1268 $values = $this->prop( 'option_key' );
1269 }
1270
1271 if ( $values ) {
1272 $key = $values;
1273 }
1274
1275 if ( ! is_array( $key ) ) {
1276 $key = array( $key );
1277 }
1278
1279 return $key;
1280 }
1281
1282 /**
1283 * Returns the object type
1284 *
1285 * @since 1.0.0
1286 * @param string $object_type Type of object being saved. (e.g., post, user, or comment). Optional.
1287 * @return string Object type.
1288 */
1289 public function object_type( $object_type = '' ) {
1290 if ( $object_type ) {
1291 $this->object_type = $object_type;
1292 return $this->object_type;
1293 }
1294
1295 if ( $this->object_type ) {
1296 return $this->object_type;
1297 }
1298
1299 $this->object_type = $this->current_object_type();
1300
1301 return $this->object_type;
1302 }
1303
1304 /**
1305 * Get the object type for the current page, based on the $pagenow global.
1306 *
1307 * @since 2.2.2
1308 * @return string Page object type name.
1309 */
1310 public function current_object_type() {
1311 global $pagenow;
1312 $type = 'post';
1313
1314 if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) {
1315 $type = 'user';
1316 }
1317
1318 if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) {
1319 $type = 'comment';
1320 }
1321
1322 if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) {
1323 $type = 'term';
1324 }
1325
1326 if ( defined( 'DOING_AJAX' ) && isset( $_POST['action'] ) && 'add-tag' === $_POST['action'] ) {
1327 $type = 'term';
1328 }
1329
1330 if (
1331 in_array( $pagenow, array( 'admin.php', 'admin-post.php' ), true )
1332 && $this->doing_options_page()
1333 ) {
1334 $type = 'options-page';
1335 }
1336
1337 return $type;
1338 }
1339
1340 /**
1341 * Set metabox property.
1342 *
1343 * @since 2.2.2
1344 * @param string $property Metabox config property to retrieve.
1345 * @param mixed $value Value to set if no value found.
1346 * @return mixed Metabox config property value or false.
1347 */
1348 public function set_prop( $property, $value ) {
1349 $this->meta_box[ $property ] = $value;
1350
1351 return $this->prop( $property );
1352 }
1353
1354 /**
1355 * Get metabox property and optionally set a fallback
1356 *
1357 * @since 2.0.0
1358 * @param string $property Metabox config property to retrieve.
1359 * @param mixed $fallback Fallback value to set if no value found.
1360 * @return mixed Metabox config property value or false.
1361 */
1362 public function prop( $property, $fallback = null ) {
1363 if ( array_key_exists( $property, $this->meta_box ) ) {
1364 return $this->meta_box[ $property ];
1365 } elseif ( $fallback ) {
1366 return $this->meta_box[ $property ] = $fallback;
1367 }
1368 }
1369
1370 /**
1371 * Get a field object
1372 *
1373 * @since 2.0.3
1374 * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object.
1375 * @param CMB2_Field|null $field_group (optional) CMB2_Field object (group parent).
1376 * @param bool $reset_cached (optional) Reset the internal cache for this field object.
1377 * Use sparingly.
1378 *
1379 * @return CMB2_Field|false CMB2_Field object (or false).
1380 */
1381 public function get_field( $field, $field_group = null, $reset_cached = false ) {
1382 if ( $field instanceof CMB2_Field ) {
1383 return $field;
1384 }
1385
1386 $field_id = is_string( $field ) ? $field : $field['id'];
1387
1388 $parent_field_id = ! empty( $field_group ) ? $field_group->id() : '';
1389 $ids = $this->get_field_ids( $field_id, $parent_field_id );
1390
1391 if ( ! $ids ) {
1392 return false;
1393 }
1394
1395 list( $field_id, $sub_field_id ) = $ids;
1396
1397 $index = $field_id . ( $sub_field_id ? '|' . $sub_field_id : '' ) . ( $field_group ? '|' . $field_group->index : '' );
1398
1399 if ( array_key_exists( $index, $this->fields ) && ! $reset_cached ) {
1400 return $this->fields[ $index ];
1401 }
1402
1403 $this->fields[ $index ] = new CMB2_Field( $this->get_field_args( $field_id, $field, $sub_field_id, $field_group ) );
1404
1405 return $this->fields[ $index ];
1406 }
1407
1408 /**
1409 * Handles determining which type of arguments to pass to CMB2_Field
1410 *
1411 * @since 2.0.7
1412 * @param mixed $field_id Field (or group field) ID.
1413 * @param mixed $field_args Array of field arguments.
1414 * @param mixed $sub_field_id Sub field ID (if field_group exists).
1415 * @param CMB2_Field|null $field_group If a sub-field, will be the parent group CMB2_Field object.
1416 * @return array Array of CMB2_Field arguments.
1417 */
1418 public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) {
1419
1420 // Check if group is passed and if fields were added in the old-school fields array.
1421 if ( $field_group && ( $sub_field_id || 0 === $sub_field_id ) ) {
1422
1423 // Update the fields array w/ any modified properties inherited from the group field.
1424 $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] = $field_args;
1425
1426 return $this->get_default_args( $field_args, $field_group );
1427 }
1428
1429 if ( is_array( $field_args ) ) {
1430 $this->meta_box['fields'][ $field_id ] = array_merge( $field_args, $this->meta_box['fields'][ $field_id ] );
1431 }
1432
1433 return $this->get_default_args( $this->meta_box['fields'][ $field_id ] );
1434 }
1435
1436 /**
1437 * Get default field arguments specific to this CMB2 object.
1438 *
1439 * @since 2.2.0
1440 * @param array $field_args Metabox field config array.
1441 * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent).
1442 * @return array Array of field arguments.
1443 */
1444 protected function get_default_args( $field_args, $field_group = null ) {
1445 if ( $field_group ) {
1446 $args = array(
1447 'field_args' => $field_args,
1448 'group_field' => $field_group,
1449 );
1450 } else {
1451 $args = array(
1452 'field_args' => $field_args,
1453 'object_type' => $this->object_type(),
1454 'object_id' => $this->object_id(),
1455 'cmb_id' => $this->cmb_id,
1456 );
1457 }
1458
1459 return $args;
1460 }
1461
1462 /**
1463 * When fields are added in the old-school way, intitate them as they should be
1464 *
1465 * @since 2.1.0
1466 * @param array $fields Array of fields to add.
1467 * @param mixed $parent_field_id Parent field id or null.
1468 *
1469 * @return CMB2
1470 */
1471 protected function add_fields( $fields, $parent_field_id = null ) {
1472 foreach ( $fields as $field ) {
1473
1474 $sub_fields = false;
1475 if ( array_key_exists( 'fields', $field ) ) {
1476 $sub_fields = $field['fields'];
1477 unset( $field['fields'] );
1478 }
1479
1480 $field_id = $parent_field_id
1481 ? $this->add_group_field( $parent_field_id, $field )
1482 : $this->add_field( $field );
1483
1484 if ( $sub_fields ) {
1485 $this->add_fields( $sub_fields, $field_id );
1486 }
1487 }
1488
1489 return $this;
1490 }
1491
1492 /**
1493 * Add a field to the metabox
1494 *
1495 * @since 2.0.0
1496 * @param array $field Metabox field config array.
1497 * @param int $position (optional) Position of metabox. 1 for first, etc.
1498 * @return string|false Field id or false.
1499 */
1500 public function add_field( array $field, $position = 0 ) {
1501 if ( ! array_key_exists( 'id', $field ) ) {
1502 return false;
1503 }
1504
1505 $this->_add_field_to_array(
1506 $field,
1507 $this->meta_box['fields'],
1508 $position
1509 );
1510
1511 return $field['id'];
1512 }
1513
1514 /**
1515 * Add a field to a group
1516 *
1517 * @since 2.0.0
1518 * @param string $parent_field_id The field id of the group field to add the field.
1519 * @param array $field Metabox field config array.
1520 * @param int $position (optional) Position of metabox. 1 for first, etc.
1521 * @return mixed Array of parent/field ids or false.
1522 */
1523 public function add_group_field( $parent_field_id, array $field, $position = 0 ) {
1524 if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) {
1525 return false;
1526 }
1527
1528 $parent_field = $this->meta_box['fields'][ $parent_field_id ];
1529
1530 if ( 'group' !== $parent_field['type'] ) {
1531 return false;
1532 }
1533
1534 if ( ! isset( $parent_field['fields'] ) ) {
1535 $this->meta_box['fields'][ $parent_field_id ]['fields'] = array();
1536 }
1537
1538 $this->_add_field_to_array(
1539 $field,
1540 $this->meta_box['fields'][ $parent_field_id ]['fields'],
1541 $position
1542 );
1543
1544 return array( $parent_field_id, $field['id'] );
1545 }
1546
1547 /**
1548 * Perform some field-type-specific initiation actions.
1549 *
1550 * @since 2.7.0
1551 * @param array $field Metabox field config array.
1552 * @return void
1553 */
1554 protected function field_actions( $field ) {
1555 switch ( $field['type'] ) {
1556 case 'file':
1557 case 'file_list':
1558
1559 // Initiate attachment JS hooks.
1560 add_filter( 'wp_prepare_attachment_for_js', array( 'CMB2_Type_File_Base', 'prepare_image_sizes_for_js' ), 10, 3 );
1561 break;
1562
1563 case 'oembed':
1564 // Initiate oembed Ajax hooks.
1565 cmb2_ajax();
1566 break;
1567
1568 case 'group':
1569 if ( empty( $field['render_row_cb'] ) ) {
1570 $field['render_row_cb'] = array( $this, 'render_group_callback' );
1571 }
1572 break;
1573 case 'colorpicker':
1574
1575 // https://github.com/JayWood/CMB2_RGBa_Picker
1576 // Dequeue the rgba_colorpicker custom field script if it is used,
1577 // since we now enqueue our own more current version.
1578 add_action( 'admin_enqueue_scripts', array( 'CMB2_Type_Colorpicker', 'dequeue_rgba_colorpicker_script' ), 99 );
1579 break;
1580 }
1581
1582 if ( isset( $field['column'] ) && false !== $field['column'] ) {
1583 $field = $this->define_field_column( $field );
1584 }
1585
1586 if ( isset( $field['taxonomy'] ) && ! empty( $field['remove_default'] ) ) {
1587 $this->tax_metaboxes_to_remove[ $field['taxonomy'] ] = $field['taxonomy'];
1588 }
1589
1590 return $field;
1591 }
1592
1593 /**
1594 * Defines a field's column if requesting to be show in admin columns.
1595 *
1596 * @since 2.2.3
1597 * @param array $field Metabox field config array.
1598 * @return array Modified metabox field config array.
1599 */
1600 protected function define_field_column( array $field ) {
1601 $this->has_columns = true;
1602
1603 $column = is_array( $field['column'] ) ? $field['column'] : array();
1604
1605 $field['column'] = wp_parse_args( $column, array(
1606 'name' => isset( $field['name'] ) ? $field['name'] : '',
1607 'position' => false,
1608 ) );
1609
1610 return $field;
1611 }
1612
1613 /**
1614 * Add a field array to a fields array in desired position
1615 *
1616 * @since 2.0.2
1617 * @param array $field Metabox field config array.
1618 * @param array $fields Array (passed by reference) to append the field (array) to.
1619 * @param integer $position Optionally specify a position in the array to be inserted.
1620 */
1621 protected function _add_field_to_array( $field, &$fields, $position = 0 ) {
1622 $field = $this->field_actions( $field );
1623
1624 if ( $position ) {
1625 CMB2_Utils::array_insert( $fields, array( $field['id'] => $field ), $position );
1626 } else {
1627 $fields[ $field['id'] ] = $field;
1628 }
1629 }
1630
1631 /**
1632 * Remove a field from the metabox
1633 *
1634 * @since 2.0.0
1635 * @param string $field_id The field id of the field to remove.
1636 * @param string $parent_field_id (optional) The field id of the group field to remove field from.
1637 * @return bool True if field was removed.
1638 */
1639 public function remove_field( $field_id, $parent_field_id = '' ) {
1640 $ids = $this->get_field_ids( $field_id, $parent_field_id );
1641
1642 if ( ! $ids ) {
1643 return false;
1644 }
1645
1646 list( $field_id, $sub_field_id ) = $ids;
1647
1648 unset( $this->fields[ implode( '', $ids ) ] );
1649
1650 if ( ! $sub_field_id ) {
1651 unset( $this->meta_box['fields'][ $field_id ] );
1652 return true;
1653 }
1654
1655 if ( isset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] ) ) {
1656 unset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] );
1657 }
1658 if ( isset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] ) ) {
1659 unset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] );
1660 }
1661
1662 return true;
1663 }
1664
1665 /**
1666 * Update or add a property to a field
1667 *
1668 * @since 2.0.0
1669 * @param string $field_id Field id.
1670 * @param string $property Field property to set/update.
1671 * @param mixed $value Value to set the field property.
1672 * @param string $parent_field_id (optional) The field id of the group field to remove field from.
1673 * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0).
1674 */
1675 public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) {
1676 $ids = $this->get_field_ids( $field_id, $parent_field_id );
1677
1678 if ( ! $ids ) {
1679 return false;
1680 }
1681
1682 list( $field_id, $sub_field_id ) = $ids;
1683
1684 if ( ! $sub_field_id ) {
1685 $this->meta_box['fields'][ $field_id ][ $property ] = $value;
1686 return $field_id;
1687 }
1688
1689 $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ][ $property ] = $value;
1690 return $field_id;
1691 }
1692
1693 /**
1694 * Check if field ids match a field and return the index/field id
1695 *
1696 * @since 2.0.2
1697 * @param string $field_id Field id.
1698 * @param string $parent_field_id (optional) Parent field id.
1699 * @return mixed Array of field/parent ids, or false.
1700 */
1701 public function get_field_ids( $field_id, $parent_field_id = '' ) {
1702 $sub_field_id = $parent_field_id ? $field_id : '';
1703 $field_id = $parent_field_id ? $parent_field_id : $field_id;
1704 $fields =& $this->meta_box['fields'];
1705
1706 if ( ! array_key_exists( $field_id, $fields ) ) {
1707 $field_id = $this->search_old_school_array( $field_id, $fields );
1708 }
1709
1710 if ( false === $field_id ) {
1711 return false;
1712 }
1713
1714 if ( ! $sub_field_id ) {
1715 return array( $field_id, $sub_field_id );
1716 }
1717
1718 if ( 'group' !== $fields[ $field_id ]['type'] ) {
1719 return false;
1720 }
1721
1722 if ( ! array_key_exists( $sub_field_id, $fields[ $field_id ]['fields'] ) ) {
1723 $sub_field_id = $this->search_old_school_array( $sub_field_id, $fields[ $field_id ]['fields'] );
1724 }
1725
1726 return false === $sub_field_id ? false : array( $field_id, $sub_field_id );
1727 }
1728
1729 /**
1730 * When using the old array filter, it is unlikely field array indexes will be the field id.
1731 *
1732 * @since 2.0.2
1733 * @param string $field_id The field id.
1734 * @param array $fields Array of fields to search.
1735 * @return mixed Field index or false.
1736 */
1737 public function search_old_school_array( $field_id, $fields ) {
1738 $ids = wp_list_pluck( $fields, 'id' );
1739 $index = array_search( $field_id, $ids );
1740 return false !== $index ? $index : false;
1741 }
1742
1743 /**
1744 * Handles metabox property callbacks, and passes this $cmb object as property.
1745 *
1746 * @since 2.2.3
1747 * @param callable $cb The callback method/function/closure.
1748 * @param mixed $additional_params Any additoinal parameters which should be passed to the callback.
1749 * @return mixed Return of the callback function.
1750 */
1751 public function do_callback( $cb, $additional_params = null ) {
1752 return call_user_func( $cb, $this, $additional_params );
1753 }
1754
1755 /**
1756 * Generate a unique nonce field for each registered meta_box
1757 *
1758 * @since 2.0.0
1759 * @return void
1760 */
1761 public function nonce_field() {
1762 wp_nonce_field( $this->nonce(), $this->nonce(), false, true );
1763 }
1764
1765 /**
1766 * Generate a unique nonce for each registered meta_box
1767 *
1768 * @since 2.0.0
1769 * @return string unique nonce string.
1770 */
1771 public function nonce() {
1772 if ( ! $this->generated_nonce ) {
1773 $this->generated_nonce = sanitize_html_class( 'nonce_' . basename( __FILE__ ) . $this->cmb_id );
1774 }
1775
1776 return $this->generated_nonce;
1777 }
1778
1779 /**
1780 * Checks if field-saving updated any fields.
1781 *
1782 * @since 2.2.5
1783 *
1784 * @return bool
1785 */
1786 public function was_updated() {
1787 return ! empty( $this->updated );
1788 }
1789
1790 /**
1791 * Whether this box is an "alternate context" box. This means the box has a 'context' property defined as:
1792 * 'form_top', 'before_permalink', 'after_title', or 'after_editor'.
1793 *
1794 * @since 2.2.4
1795 * @return bool
1796 */
1797 public function is_alternate_context_box() {
1798 return $this->prop( 'context' ) && in_array( $this->prop( 'context' ), array( 'form_top', 'before_permalink', 'after_title', 'after_editor' ), true );
1799 }
1800
1801 /**
1802 * Magic getter for our object.
1803 *
1804 * @param string $property Object property.
1805 * @throws Exception Throws an exception if the field is invalid.
1806 * @return mixed
1807 */
1808 public function __get( $property ) {
1809 switch ( $property ) {
1810 case 'updated':
1811 case 'has_columns':
1812 case 'tax_metaboxes_to_remove':
1813 return $this->{$property};
1814 default:
1815 return parent::__get( $property );
1816 }
1817 }
1818
1819 }
1820