PluginProbe
WPGet API – Connect to any external REST API / 1.9.0
WPGet API – Connect to any external REST API v1.9.0
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 1.9.0, at lib/cmb2/includes/CMB2.php

1,818 lines 52.8 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 echo '
611 <div id="cmb-group-', $field_group->id(), '-', $field_group->index, '" class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">';
612
613 if ( $field_group->args( 'repeatable' ) ) {
614 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>';
615 }
616
617 echo '
618 <div class="cmbhandle" title="' , esc_attr__( 'Click to toggle', 'cmb2' ), '"><br></div>
619 <h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3>
620
621 <div class="inside cmb-td cmb-nested cmb-field-list">';
622 // Loop and render repeatable group fields.
623 foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) {
624 if ( 'hidden' === $field_args['type'] ) {
625
626 // Save rendering for after the metabox.
627 $this->add_hidden_field( $field_args, $field_group );
628
629 } else {
630
631 $field_args['show_names'] = $field_group->args( 'show_names' );
632 $field_args['context'] = $field_group->args( 'context' );
633
634 $this->get_field( $field_args, $field_group )->render_field();
635 }
636 }
637
638 if ( $field_group->args( 'repeatable' ) ) {
639 echo '
640 <div class="cmb-row cmb-remove-field-row">
641 <div class="cmb-remove-row">
642 <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>
643 </div>
644 </div>
645 ';
646 }
647 echo '
648 </div>
649 </div>
650 ';
651
652 $field_group->peform_param_callback( 'after_group_row' );
653
654 return $this;
655 }
656
657 /**
658 * Add a hidden field to the list of hidden fields to be rendered later.
659 *
660 * @since 2.0.0
661 *
662 * @param array $field_args Array of field arguments to be passed to CMB2_Field.
663 * @param CMB2_Field|null $field_group CMB2_Field group field object.
664 * @return CMB2_Field
665 */
666 public function add_hidden_field( $field_args, $field_group = null ) {
667 if ( isset( $field_args['field_args'] ) ) {
668 // For back-compatibility.
669 $field = new CMB2_Field( $field_args );
670 } else {
671 $field = $this->get_new_field( $field_args, $field_group );
672 }
673
674 $types = new CMB2_Types( $field );
675
676 if ( $field_group ) {
677 $types->iterator = $field_group->index;
678 }
679
680 $this->hidden_fields[] = $types;
681
682 return $field;
683 }
684
685 /**
686 * Loop through and output hidden fields
687 *
688 * @since 2.0.0
689 *
690 * @return CMB2
691 */
692 public function render_hidden_fields() {
693 if ( ! empty( $this->hidden_fields ) ) {
694 foreach ( $this->hidden_fields as $hidden ) {
695 $hidden->render();
696 }
697 }
698
699 return $this;
700 }
701
702 /**
703 * Returns array of sanitized field values (without saving them)
704 *
705 * @since 2.0.3
706 * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data).
707 * @return mixed
708 */
709 public function get_sanitized_values( array $data_to_sanitize ) {
710 $this->data_to_save = $data_to_sanitize;
711 $stored_id = $this->object_id();
712
713 // We do this So CMB will sanitize our data for us, but not save it.
714 $this->object_id( '_' );
715
716 // Ensure temp. data store is empty.
717 cmb2_options( 0 )->set();
718
719 // We want to get any taxonomy values back.
720 add_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' );
721
722 // Process/save fields.
723 $this->process_fields();
724
725 // Put things back the way they were.
726 remove_filter( "cmb2_return_taxonomy_values_{$this->cmb_id}", '__return_true' );
727
728 // Get data from temp. data store.
729 $sanitized_values = cmb2_options( 0 )->get_options();
730
731 // Empty out temp. data store again.
732 cmb2_options( 0 )->set();
733
734 // Reset the object id.
735 $this->object_id( $stored_id );
736
737 return $sanitized_values;
738 }
739
740 /**
741 * Loops through and saves field data
742 *
743 * @since 1.0.0
744 * @param int $object_id Object ID.
745 * @param string $object_type Type of object being saved. (e.g., post, user, or comment).
746 * @param array $data_to_save Array of key => value data for saving. Likely $_POST data.
747 *
748 * @return CMB2
749 */
750 public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) {
751
752 // Fall-back to $_POST data.
753 $this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST;
754 $object_id = $this->object_id( $object_id );
755 $object_type = $this->object_type( $object_type );
756
757 $this->process_fields();
758
759 // If options page, save the updated options.
760 if ( 'options-page' === $object_type ) {
761 cmb2_options( $object_id )->set();
762 }
763
764 return $this->after_save();
765 }
766
767 /**
768 * Process and save form fields
769 *
770 * @since 2.0.0
771 *
772 * @return CMB2
773 */
774 public function process_fields() {
775
776 $this->pre_process();
777
778 // Remove the show_on properties so saving works.
779 $this->prop( 'show_on', array() );
780
781 // save field ids of those that are updated.
782 $this->updated = array();
783
784 foreach ( $this->prop( 'fields' ) as $field_args ) {
785 $this->process_field( $field_args );
786 }
787
788 return $this;
789 }
790
791 /**
792 * Process and save a field
793 *
794 * @since 2.0.0
795 * @param array $field_args Array of field arguments.
796 *
797 * @return CMB2
798 */
799 public function process_field( $field_args ) {
800
801 switch ( $field_args['type'] ) {
802
803 case 'group':
804 if ( $this->save_group( $field_args ) ) {
805 $this->updated[] = $field_args['id'];
806 }
807
808 break;
809
810 case 'title':
811 // Don't process title fields.
812 break;
813
814 default:
815 $field = $this->get_new_field( $field_args );
816
817 if ( $field->save_field_from_data( $this->data_to_save ) ) {
818 $this->updated[] = $field->id();
819 }
820
821 break;
822 }
823
824 return $this;
825 }
826
827 /**
828 * Fires the "cmb2_{$object_type}_process_fields_{$cmb_id}" action hook.
829 *
830 * @since 2.2.2
831 *
832 * @return CMB2
833 */
834 public function pre_process() {
835 $object_type = $this->object_type();
836
837 /**
838 * Fires before fields have been processed/saved.
839 *
840 * The dynamic portion of the hook name, $object_type, refers to the
841 * metabox/form's object type
842 * Usually `post` (this applies to all post-types).
843 * Could also be `comment`, `user` or `options-page`.
844 *
845 * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id.
846 *
847 * @param array $cmb This CMB2 object
848 * @param int $object_id The ID of the current object
849 */
850 do_action( "cmb2_{$object_type}_process_fields_{$this->cmb_id}", $this, $this->object_id() );
851
852 return $this;
853 }
854
855 /**
856 * Fires the "cmb2_save_{$object_type}_fields" and
857 * "cmb2_save_{$object_type}_fields_{$cmb_id}" action hooks.
858 *
859 * @since 2.x.x
860 *
861 * @return CMB2
862 */
863 public function after_save() {
864 $object_type = $this->object_type();
865 $object_id = $this->object_id();
866
867 /**
868 * Fires after all fields have been saved.
869 *
870 * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type
871 * Usually `post` (this applies to all post-types).
872 * Could also be `comment`, `user` or `options-page`.
873 *
874 * @param int $object_id The ID of the current object
875 * @param array $cmb_id The current box ID
876 * @param string $updated Array of field ids that were updated.
877 * Will only include field ids that had values change.
878 * @param array $cmb This CMB2 object
879 */
880 do_action( "cmb2_save_{$object_type}_fields", $object_id, $this->cmb_id, $this->updated, $this );
881
882 /**
883 * Fires after all fields have been saved.
884 *
885 * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id.
886 *
887 * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type
888 * Usually `post` (this applies to all post-types).
889 * Could also be `comment`, `user` or `options-page`.
890 *
891 * @param int $object_id The ID of the current object
892 * @param string $updated Array of field ids that were updated.
893 * Will only include field ids that had values change.
894 * @param array $cmb This CMB2 object
895 */
896 do_action( "cmb2_save_{$object_type}_fields_{$this->cmb_id}", $object_id, $this->updated, $this );
897
898 return $this;
899 }
900
901 /**
902 * Save a repeatable group
903 *
904 * @since 1.x.x
905 * @param array $args Field arguments array.
906 * @return mixed Return of CMB2_Field::update_data().
907 */
908 public function save_group( $args ) {
909 if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) {
910 return;
911 }
912
913 return $this->save_group_field( $this->get_new_field( $args ) );
914 }
915
916 /**
917 * Save a repeatable group
918 *
919 * @since 1.x.x
920 * @param CMB2_Field $field_group CMB2_Field group field object.
921 * @return mixed Return of CMB2_Field::update_data().
922 */
923 public function save_group_field( $field_group ) {
924 $base_id = $field_group->id();
925
926 if ( ! isset( $this->data_to_save[ $base_id ] ) ) {
927 return;
928 }
929
930 $old = $field_group->get_data();
931 // Check if group field has sanitization_cb.
932 $group_vals = $field_group->sanitization_cb( $this->data_to_save[ $base_id ] );
933 $saved = array();
934
935 $field_group->index = 0;
936 $field_group->data_to_save = $this->data_to_save;
937
938 foreach ( array_values( $field_group->fields() ) as $field_args ) {
939 if ( 'title' === $field_args['type'] ) {
940 // Don't process title fields.
941 continue;
942 }
943
944 $field = $this->get_new_field( $field_args, $field_group );
945 $sub_id = $field->id( true );
946 if ( empty( $saved[ $field_group->index ] ) ) {
947 $saved[ $field_group->index ] = array();
948 }
949
950 foreach ( (array) $group_vals as $field_group->index => $post_vals ) {
951
952 // Get value.
953 $new_val = isset( $group_vals[ $field_group->index ][ $sub_id ] )
954 ? $group_vals[ $field_group->index ][ $sub_id ]
955 : false;
956
957 // Sanitize.
958 $new_val = $field->sanitization_cb( $new_val );
959
960 if ( is_array( $new_val ) && $field->args( 'has_supporting_data' ) ) {
961 if ( $field->args( 'repeatable' ) ) {
962 $_new_val = array();
963 foreach ( $new_val as $group_index => $grouped_data ) {
964 // Add the supporting data to the $saved array stack.
965 $saved[ $field_group->index ][ $grouped_data['supporting_field_id'] ][] = $grouped_data['supporting_field_value'];
966 // Reset var to the actual value.
967 $_new_val[ $group_index ] = $grouped_data['value'];
968 }
969 $new_val = $_new_val;
970 } else {
971 // Add the supporting data to the $saved array stack.
972 $saved[ $field_group->index ][ $new_val['supporting_field_id'] ] = $new_val['supporting_field_value'];
973 // Reset var to the actual value.
974 $new_val = $new_val['value'];
975 }
976 }
977
978 // Get old value.
979 $old_val = is_array( $old ) && isset( $old[ $field_group->index ][ $sub_id ] )
980 ? $old[ $field_group->index ][ $sub_id ]
981 : false;
982
983 $is_updated = ( ! CMB2_Utils::isempty( $new_val ) && $new_val !== $old_val );
984 $is_removed = ( CMB2_Utils::isempty( $new_val ) && ! CMB2_Utils::isempty( $old_val ) );
985
986 // Compare values and add to `$updated` array.
987 if ( $is_updated || $is_removed ) {
988 $this->updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id;
989 }
990
991 // Add to `$saved` array.
992 $saved[ $field_group->index ][ $sub_id ] = $new_val;
993
994 }// End foreach.
995
996 $saved[ $field_group->index ] = CMB2_Utils::filter_empty( $saved[ $field_group->index ] );
997 }// End foreach.
998
999 $saved = CMB2_Utils::filter_empty( $saved );
1000
1001 return $field_group->update_data( $saved, true );
1002 }
1003
1004 /**
1005 * Get object id from global space if no id is provided
1006 *
1007 * @since 1.0.0
1008 * @param integer|string $object_id Object ID.
1009 * @return integer|string $object_id Object ID.
1010 */
1011 public function object_id( $object_id = 0 ) {
1012 global $pagenow;
1013
1014 if ( $object_id ) {
1015 $this->object_id = $object_id;
1016 return $this->object_id;
1017 }
1018
1019 if ( $this->object_id ) {
1020 return $this->object_id;
1021 }
1022
1023 // Try to get our object ID from the global space.
1024 switch ( $this->object_type() ) {
1025 case 'user':
1026 $object_id = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['user_id'] ) ) : $object_id;
1027 $object_id = ! $object_id && 'user-new.php' !== $pagenow && isset( $GLOBALS['user_ID'] ) ? $GLOBALS['user_ID'] : $object_id;
1028 break;
1029
1030 case 'comment':
1031 $object_id = isset( $_REQUEST['c'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['c'] ) ) : $object_id;
1032 $object_id = ! $object_id && isset( $GLOBALS['comments']->comment_ID ) ? $GLOBALS['comments']->comment_ID : $object_id;
1033 break;
1034
1035 case 'term':
1036 $object_id = isset( $_REQUEST['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tag_ID'] ) ) : $object_id;
1037 break;
1038
1039 case 'options-page':
1040 $key = $this->doing_options_page();
1041 if ( ! empty( $key ) ) {
1042 $object_id = $key;
1043 }
1044 break;
1045
1046 default:
1047 $object_id = isset( $GLOBALS['post']->ID ) ? $GLOBALS['post']->ID : $object_id;
1048 $object_id = isset( $_REQUEST['post'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post'] ) ) : $object_id;
1049 break;
1050 }
1051
1052 // reset to id or 0.
1053 $this->object_id = $object_id ? $object_id : 0;
1054
1055 return $this->object_id;
1056 }
1057
1058 /**
1059 * Sets the $object_type based on metabox settings
1060 *
1061 * @since 1.0.0
1062 * @return string Object type.
1063 */
1064 public function mb_object_type() {
1065 if ( null !== $this->mb_object_type ) {
1066 return $this->mb_object_type;
1067 }
1068
1069 if ( $this->is_options_page_mb() ) {
1070 $this->mb_object_type = 'options-page';
1071 return $this->mb_object_type;
1072 }
1073
1074 $registered_types = $this->box_types();
1075
1076 $type = '';
1077
1078 // if it's an array of one, extract it.
1079 if ( 1 === count( $registered_types ) ) {
1080 $last = end( $registered_types );
1081 if ( is_string( $last ) ) {
1082 $type = $last;
1083 }
1084 } elseif ( ( $curr_type = $this->current_object_type() ) && in_array( $curr_type, $registered_types, true ) ) {
1085 $type = $curr_type;
1086 }
1087
1088 // Get our object type.
1089 switch ( $type ) {
1090
1091 case 'user':
1092 case 'comment':
1093 case 'term':
1094 $this->mb_object_type = $type;
1095 break;
1096
1097 default:
1098 $this->mb_object_type = 'post';
1099 break;
1100 }
1101
1102 return $this->mb_object_type;
1103 }
1104
1105 /**
1106 * Gets the box 'object_types' array based on box settings.
1107 *
1108 * @since 2.2.3
1109 * @param array $fallback Fallback value.
1110 *
1111 * @return array Object types.
1112 */
1113 public function box_types( $fallback = array() ) {
1114 return CMB2_Utils::ensure_array( $this->prop( 'object_types' ), $fallback );
1115 }
1116
1117 /**
1118 * Check if given object_type(s) matches any of the registered object types or
1119 * taxonomies for this box.
1120 *
1121 * @since 2.7.0
1122 * @param string|array $object_types The object type(s) to check.
1123 * @param array $fallback Fallback object_types value.
1124 *
1125 * @return bool Whether given object type(s) are registered to this box.
1126 */
1127 public function is_box_type( $object_types = array(), $fallback = array() ) {
1128 $object_types = (array) $object_types;
1129 $box_types = $this->box_types( $fallback );
1130
1131 if ( in_array( 'term', $box_types, true ) ) {
1132 $taxonomies = CMB2_Utils::ensure_array( $this->prop( 'taxonomies' ) );
1133 $box_types = array_merge( $box_types, $taxonomies );
1134 }
1135
1136 $found = array_intersect( $object_types, $box_types );
1137
1138 return ! empty( $found );
1139 }
1140
1141 /**
1142 * Initates the object types and option key for an options page metabox.
1143 *
1144 * @since 2.2.5
1145 *
1146 * @return void
1147 */
1148 public function init_options_mb() {
1149 $keys = $this->options_page_keys();
1150 $types = $this->box_types();
1151
1152 if ( empty( $keys ) ) {
1153 $keys = '';
1154 $types = $this->deinit_options_mb( $types );
1155 } else {
1156
1157 // Make sure 'options-page' is one of the object types.
1158 $types[] = 'options-page';
1159 }
1160
1161 // Set/Reset the option_key property.
1162 $this->set_prop( 'option_key', $keys );
1163
1164 // Reset the object types.
1165 $this->set_prop( 'object_types', array_unique( $types ) );
1166 }
1167
1168 /**
1169 * If object-page initiation failed, remove traces options page setup.
1170 *
1171 * @since 2.2.5
1172 *
1173 * @param array $types Array of types.
1174 * @return array
1175 */
1176 protected function deinit_options_mb( $types ) {
1177 if ( isset( $this->meta_box['show_on']['key'] ) && 'options-page' === $this->meta_box['show_on']['key'] ) {
1178 unset( $this->meta_box['show_on']['key'] );
1179 }
1180
1181 if ( array_key_exists( 'options-page', $this->meta_box['show_on'] ) ) {
1182 unset( $this->meta_box['show_on']['options-page'] );
1183 }
1184
1185 $index = array_search( 'options-page', $types );
1186
1187 if ( false !== $index ) {
1188 unset( $types[ $index ] );
1189 }
1190
1191 return $types;
1192 }
1193
1194 /**
1195 * Determines if metabox is for an options page
1196 *
1197 * @since 1.0.1
1198 * @return boolean True/False.
1199 */
1200 public function is_options_page_mb() {
1201 return (
1202 // 'show_on' values checked for back-compatibility.
1203 $this->is_old_school_options_page_mb()
1204 || in_array( 'options-page', $this->box_types() )
1205 );
1206 }
1207
1208 /**
1209 * Determines if metabox uses old-schoold options page config.
1210 *
1211 * @since 2.2.5
1212 * @return boolean True/False.
1213 */
1214 public function is_old_school_options_page_mb() {
1215 return (
1216 // 'show_on' values checked for back-compatibility.
1217 isset( $this->meta_box['show_on']['key'] ) && 'options-page' === $this->meta_box['show_on']['key']
1218 || array_key_exists( 'options-page', $this->meta_box['show_on'] )
1219 );
1220 }
1221
1222 /**
1223 * Determine if we are on an options page (or saving the options page).
1224 *
1225 * @since 2.2.5
1226 *
1227 * @return bool
1228 */
1229 public function doing_options_page() {
1230 $found_key = false;
1231 $keys = $this->options_page_keys();
1232
1233 if ( empty( $keys ) ) {
1234 return $found_key;
1235 }
1236
1237 if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], $keys ) ) {
1238 $found_key = $_GET['page'];
1239 }
1240
1241 if ( ! empty( $_POST['action'] ) && in_array( $_POST['action'], $keys ) ) {
1242 $found_key = $_POST['action'];
1243 }
1244
1245 return $found_key ? $found_key : false;
1246 }
1247
1248 /**
1249 * Get the options page key.
1250 *
1251 * @since 2.2.5
1252 * @return string|array
1253 */
1254 public function options_page_keys() {
1255 $key = '';
1256 if ( ! $this->is_options_page_mb() ) {
1257 return $key;
1258 }
1259
1260 $values = null;
1261 if ( ! empty( $this->meta_box['show_on']['value'] ) ) {
1262 $values = $this->meta_box['show_on']['value'];
1263 } elseif ( ! empty( $this->meta_box['show_on']['options-page'] ) ) {
1264 $values = $this->meta_box['show_on']['options-page'];
1265 } elseif ( $this->prop( 'option_key' ) ) {
1266 $values = $this->prop( 'option_key' );
1267 }
1268
1269 if ( $values ) {
1270 $key = $values;
1271 }
1272
1273 if ( ! is_array( $key ) ) {
1274 $key = array( $key );
1275 }
1276
1277 return $key;
1278 }
1279
1280 /**
1281 * Returns the object type
1282 *
1283 * @since 1.0.0
1284 * @param string $object_type Type of object being saved. (e.g., post, user, or comment). Optional.
1285 * @return string Object type.
1286 */
1287 public function object_type( $object_type = '' ) {
1288 if ( $object_type ) {
1289 $this->object_type = $object_type;
1290 return $this->object_type;
1291 }
1292
1293 if ( $this->object_type ) {
1294 return $this->object_type;
1295 }
1296
1297 $this->object_type = $this->current_object_type();
1298
1299 return $this->object_type;
1300 }
1301
1302 /**
1303 * Get the object type for the current page, based on the $pagenow global.
1304 *
1305 * @since 2.2.2
1306 * @return string Page object type name.
1307 */
1308 public function current_object_type() {
1309 global $pagenow;
1310 $type = 'post';
1311
1312 if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) {
1313 $type = 'user';
1314 }
1315
1316 if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) {
1317 $type = 'comment';
1318 }
1319
1320 if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) {
1321 $type = 'term';
1322 }
1323
1324 if ( defined( 'DOING_AJAX' ) && isset( $_POST['action'] ) && 'add-tag' === $_POST['action'] ) {
1325 $type = 'term';
1326 }
1327
1328 if (
1329 in_array( $pagenow, array( 'admin.php', 'admin-post.php' ), true )
1330 && $this->doing_options_page()
1331 ) {
1332 $type = 'options-page';
1333 }
1334
1335 return $type;
1336 }
1337
1338 /**
1339 * Set metabox property.
1340 *
1341 * @since 2.2.2
1342 * @param string $property Metabox config property to retrieve.
1343 * @param mixed $value Value to set if no value found.
1344 * @return mixed Metabox config property value or false.
1345 */
1346 public function set_prop( $property, $value ) {
1347 $this->meta_box[ $property ] = $value;
1348
1349 return $this->prop( $property );
1350 }
1351
1352 /**
1353 * Get metabox property and optionally set a fallback
1354 *
1355 * @since 2.0.0
1356 * @param string $property Metabox config property to retrieve.
1357 * @param mixed $fallback Fallback value to set if no value found.
1358 * @return mixed Metabox config property value or false.
1359 */
1360 public function prop( $property, $fallback = null ) {
1361 if ( array_key_exists( $property, $this->meta_box ) ) {
1362 return $this->meta_box[ $property ];
1363 } elseif ( $fallback ) {
1364 return $this->meta_box[ $property ] = $fallback;
1365 }
1366 }
1367
1368 /**
1369 * Get a field object
1370 *
1371 * @since 2.0.3
1372 * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object.
1373 * @param CMB2_Field|null $field_group (optional) CMB2_Field object (group parent).
1374 * @param bool $reset_cached (optional) Reset the internal cache for this field object.
1375 * Use sparingly.
1376 *
1377 * @return CMB2_Field|false CMB2_Field object (or false).
1378 */
1379 public function get_field( $field, $field_group = null, $reset_cached = false ) {
1380 if ( $field instanceof CMB2_Field ) {
1381 return $field;
1382 }
1383
1384 $field_id = is_string( $field ) ? $field : $field['id'];
1385
1386 $parent_field_id = ! empty( $field_group ) ? $field_group->id() : '';
1387 $ids = $this->get_field_ids( $field_id, $parent_field_id );
1388
1389 if ( ! $ids ) {
1390 return false;
1391 }
1392
1393 list( $field_id, $sub_field_id ) = $ids;
1394
1395 $index = $field_id . ( $sub_field_id ? '|' . $sub_field_id : '' ) . ( $field_group ? '|' . $field_group->index : '' );
1396
1397 if ( array_key_exists( $index, $this->fields ) && ! $reset_cached ) {
1398 return $this->fields[ $index ];
1399 }
1400
1401 $this->fields[ $index ] = new CMB2_Field( $this->get_field_args( $field_id, $field, $sub_field_id, $field_group ) );
1402
1403 return $this->fields[ $index ];
1404 }
1405
1406 /**
1407 * Handles determining which type of arguments to pass to CMB2_Field
1408 *
1409 * @since 2.0.7
1410 * @param mixed $field_id Field (or group field) ID.
1411 * @param mixed $field_args Array of field arguments.
1412 * @param mixed $sub_field_id Sub field ID (if field_group exists).
1413 * @param CMB2_Field|null $field_group If a sub-field, will be the parent group CMB2_Field object.
1414 * @return array Array of CMB2_Field arguments.
1415 */
1416 public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) {
1417
1418 // Check if group is passed and if fields were added in the old-school fields array.
1419 if ( $field_group && ( $sub_field_id || 0 === $sub_field_id ) ) {
1420
1421 // Update the fields array w/ any modified properties inherited from the group field.
1422 $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] = $field_args;
1423
1424 return $this->get_default_args( $field_args, $field_group );
1425 }
1426
1427 if ( is_array( $field_args ) ) {
1428 $this->meta_box['fields'][ $field_id ] = array_merge( $field_args, $this->meta_box['fields'][ $field_id ] );
1429 }
1430
1431 return $this->get_default_args( $this->meta_box['fields'][ $field_id ] );
1432 }
1433
1434 /**
1435 * Get default field arguments specific to this CMB2 object.
1436 *
1437 * @since 2.2.0
1438 * @param array $field_args Metabox field config array.
1439 * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent).
1440 * @return array Array of field arguments.
1441 */
1442 protected function get_default_args( $field_args, $field_group = null ) {
1443 if ( $field_group ) {
1444 $args = array(
1445 'field_args' => $field_args,
1446 'group_field' => $field_group,
1447 );
1448 } else {
1449 $args = array(
1450 'field_args' => $field_args,
1451 'object_type' => $this->object_type(),
1452 'object_id' => $this->object_id(),
1453 'cmb_id' => $this->cmb_id,
1454 );
1455 }
1456
1457 return $args;
1458 }
1459
1460 /**
1461 * When fields are added in the old-school way, intitate them as they should be
1462 *
1463 * @since 2.1.0
1464 * @param array $fields Array of fields to add.
1465 * @param mixed $parent_field_id Parent field id or null.
1466 *
1467 * @return CMB2
1468 */
1469 protected function add_fields( $fields, $parent_field_id = null ) {
1470 foreach ( $fields as $field ) {
1471
1472 $sub_fields = false;
1473 if ( array_key_exists( 'fields', $field ) ) {
1474 $sub_fields = $field['fields'];
1475 unset( $field['fields'] );
1476 }
1477
1478 $field_id = $parent_field_id
1479 ? $this->add_group_field( $parent_field_id, $field )
1480 : $this->add_field( $field );
1481
1482 if ( $sub_fields ) {
1483 $this->add_fields( $sub_fields, $field_id );
1484 }
1485 }
1486
1487 return $this;
1488 }
1489
1490 /**
1491 * Add a field to the metabox
1492 *
1493 * @since 2.0.0
1494 * @param array $field Metabox field config array.
1495 * @param int $position (optional) Position of metabox. 1 for first, etc.
1496 * @return string|false Field id or false.
1497 */
1498 public function add_field( array $field, $position = 0 ) {
1499 if ( ! array_key_exists( 'id', $field ) ) {
1500 return false;
1501 }
1502
1503 $this->_add_field_to_array(
1504 $field,
1505 $this->meta_box['fields'],
1506 $position
1507 );
1508
1509 return $field['id'];
1510 }
1511
1512 /**
1513 * Add a field to a group
1514 *
1515 * @since 2.0.0
1516 * @param string $parent_field_id The field id of the group field to add the field.
1517 * @param array $field Metabox field config array.
1518 * @param int $position (optional) Position of metabox. 1 for first, etc.
1519 * @return mixed Array of parent/field ids or false.
1520 */
1521 public function add_group_field( $parent_field_id, array $field, $position = 0 ) {
1522 if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) {
1523 return false;
1524 }
1525
1526 $parent_field = $this->meta_box['fields'][ $parent_field_id ];
1527
1528 if ( 'group' !== $parent_field['type'] ) {
1529 return false;
1530 }
1531
1532 if ( ! isset( $parent_field['fields'] ) ) {
1533 $this->meta_box['fields'][ $parent_field_id ]['fields'] = array();
1534 }
1535
1536 $this->_add_field_to_array(
1537 $field,
1538 $this->meta_box['fields'][ $parent_field_id ]['fields'],
1539 $position
1540 );
1541
1542 return array( $parent_field_id, $field['id'] );
1543 }
1544
1545 /**
1546 * Perform some field-type-specific initiation actions.
1547 *
1548 * @since 2.7.0
1549 * @param array $field Metabox field config array.
1550 * @return void
1551 */
1552 protected function field_actions( $field ) {
1553 switch ( $field['type'] ) {
1554 case 'file':
1555 case 'file_list':
1556
1557 // Initiate attachment JS hooks.
1558 add_filter( 'wp_prepare_attachment_for_js', array( 'CMB2_Type_File_Base', 'prepare_image_sizes_for_js' ), 10, 3 );
1559 break;
1560
1561 case 'oembed':
1562 // Initiate oembed Ajax hooks.
1563 cmb2_ajax();
1564 break;
1565
1566 case 'group':
1567 if ( empty( $field['render_row_cb'] ) ) {
1568 $field['render_row_cb'] = array( $this, 'render_group_callback' );
1569 }
1570 break;
1571 case 'colorpicker':
1572
1573 // https://github.com/JayWood/CMB2_RGBa_Picker
1574 // Dequeue the rgba_colorpicker custom field script if it is used,
1575 // since we now enqueue our own more current version.
1576 add_action( 'admin_enqueue_scripts', array( 'CMB2_Type_Colorpicker', 'dequeue_rgba_colorpicker_script' ), 99 );
1577 break;
1578 }
1579
1580 if ( isset( $field['column'] ) && false !== $field['column'] ) {
1581 $field = $this->define_field_column( $field );
1582 }
1583
1584 if ( isset( $field['taxonomy'] ) && ! empty( $field['remove_default'] ) ) {
1585 $this->tax_metaboxes_to_remove[ $field['taxonomy'] ] = $field['taxonomy'];
1586 }
1587
1588 return $field;
1589 }
1590
1591 /**
1592 * Defines a field's column if requesting to be show in admin columns.
1593 *
1594 * @since 2.2.3
1595 * @param array $field Metabox field config array.
1596 * @return array Modified metabox field config array.
1597 */
1598 protected function define_field_column( array $field ) {
1599 $this->has_columns = true;
1600
1601 $column = is_array( $field['column'] ) ? $field['column'] : array();
1602
1603 $field['column'] = wp_parse_args( $column, array(
1604 'name' => isset( $field['name'] ) ? $field['name'] : '',
1605 'position' => false,
1606 ) );
1607
1608 return $field;
1609 }
1610
1611 /**
1612 * Add a field array to a fields array in desired position
1613 *
1614 * @since 2.0.2
1615 * @param array $field Metabox field config array.
1616 * @param array $fields Array (passed by reference) to append the field (array) to.
1617 * @param integer $position Optionally specify a position in the array to be inserted.
1618 */
1619 protected function _add_field_to_array( $field, &$fields, $position = 0 ) {
1620 $field = $this->field_actions( $field );
1621
1622 if ( $position ) {
1623 CMB2_Utils::array_insert( $fields, array( $field['id'] => $field ), $position );
1624 } else {
1625 $fields[ $field['id'] ] = $field;
1626 }
1627 }
1628
1629 /**
1630 * Remove a field from the metabox
1631 *
1632 * @since 2.0.0
1633 * @param string $field_id The field id of the field to remove.
1634 * @param string $parent_field_id (optional) The field id of the group field to remove field from.
1635 * @return bool True if field was removed.
1636 */
1637 public function remove_field( $field_id, $parent_field_id = '' ) {
1638 $ids = $this->get_field_ids( $field_id, $parent_field_id );
1639
1640 if ( ! $ids ) {
1641 return false;
1642 }
1643
1644 list( $field_id, $sub_field_id ) = $ids;
1645
1646 unset( $this->fields[ implode( '', $ids ) ] );
1647
1648 if ( ! $sub_field_id ) {
1649 unset( $this->meta_box['fields'][ $field_id ] );
1650 return true;
1651 }
1652
1653 if ( isset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] ) ) {
1654 unset( $this->fields[ $field_id ]->args['fields'][ $sub_field_id ] );
1655 }
1656 if ( isset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] ) ) {
1657 unset( $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ] );
1658 }
1659
1660 return true;
1661 }
1662
1663 /**
1664 * Update or add a property to a field
1665 *
1666 * @since 2.0.0
1667 * @param string $field_id Field id.
1668 * @param string $property Field property to set/update.
1669 * @param mixed $value Value to set the field property.
1670 * @param string $parent_field_id (optional) The field id of the group field to remove field from.
1671 * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0).
1672 */
1673 public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) {
1674 $ids = $this->get_field_ids( $field_id, $parent_field_id );
1675
1676 if ( ! $ids ) {
1677 return false;
1678 }
1679
1680 list( $field_id, $sub_field_id ) = $ids;
1681
1682 if ( ! $sub_field_id ) {
1683 $this->meta_box['fields'][ $field_id ][ $property ] = $value;
1684 return $field_id;
1685 }
1686
1687 $this->meta_box['fields'][ $field_id ]['fields'][ $sub_field_id ][ $property ] = $value;
1688 return $field_id;
1689 }
1690
1691 /**
1692 * Check if field ids match a field and return the index/field id
1693 *
1694 * @since 2.0.2
1695 * @param string $field_id Field id.
1696 * @param string $parent_field_id (optional) Parent field id.
1697 * @return mixed Array of field/parent ids, or false.
1698 */
1699 public function get_field_ids( $field_id, $parent_field_id = '' ) {
1700 $sub_field_id = $parent_field_id ? $field_id : '';
1701 $field_id = $parent_field_id ? $parent_field_id : $field_id;
1702 $fields =& $this->meta_box['fields'];
1703
1704 if ( ! array_key_exists( $field_id, $fields ) ) {
1705 $field_id = $this->search_old_school_array( $field_id, $fields );
1706 }
1707
1708 if ( false === $field_id ) {
1709 return false;
1710 }
1711
1712 if ( ! $sub_field_id ) {
1713 return array( $field_id, $sub_field_id );
1714 }
1715
1716 if ( 'group' !== $fields[ $field_id ]['type'] ) {
1717 return false;
1718 }
1719
1720 if ( ! array_key_exists( $sub_field_id, $fields[ $field_id ]['fields'] ) ) {
1721 $sub_field_id = $this->search_old_school_array( $sub_field_id, $fields[ $field_id ]['fields'] );
1722 }
1723
1724 return false === $sub_field_id ? false : array( $field_id, $sub_field_id );
1725 }
1726
1727 /**
1728 * When using the old array filter, it is unlikely field array indexes will be the field id.
1729 *
1730 * @since 2.0.2
1731 * @param string $field_id The field id.
1732 * @param array $fields Array of fields to search.
1733 * @return mixed Field index or false.
1734 */
1735 public function search_old_school_array( $field_id, $fields ) {
1736 $ids = wp_list_pluck( $fields, 'id' );
1737 $index = array_search( $field_id, $ids );
1738 return false !== $index ? $index : false;
1739 }
1740
1741 /**
1742 * Handles metabox property callbacks, and passes this $cmb object as property.
1743 *
1744 * @since 2.2.3
1745 * @param callable $cb The callback method/function/closure.
1746 * @param mixed $additional_params Any additoinal parameters which should be passed to the callback.
1747 * @return mixed Return of the callback function.
1748 */
1749 public function do_callback( $cb, $additional_params = null ) {
1750 return call_user_func( $cb, $this, $additional_params );
1751 }
1752
1753 /**
1754 * Generate a unique nonce field for each registered meta_box
1755 *
1756 * @since 2.0.0
1757 * @return void
1758 */
1759 public function nonce_field() {
1760 wp_nonce_field( $this->nonce(), $this->nonce(), false, true );
1761 }
1762
1763 /**
1764 * Generate a unique nonce for each registered meta_box
1765 *
1766 * @since 2.0.0
1767 * @return string unique nonce string.
1768 */
1769 public function nonce() {
1770 if ( ! $this->generated_nonce ) {
1771 $this->generated_nonce = sanitize_html_class( 'nonce_' . basename( __FILE__ ) . $this->cmb_id );
1772 }
1773
1774 return $this->generated_nonce;
1775 }
1776
1777 /**
1778 * Checks if field-saving updated any fields.
1779 *
1780 * @since 2.2.5
1781 *
1782 * @return bool
1783 */
1784 public function was_updated() {
1785 return ! empty( $this->updated );
1786 }
1787
1788 /**
1789 * Whether this box is an "alternate context" box. This means the box has a 'context' property defined as:
1790 * 'form_top', 'before_permalink', 'after_title', or 'after_editor'.
1791 *
1792 * @since 2.2.4
1793 * @return bool
1794 */
1795 public function is_alternate_context_box() {
1796 return $this->prop( 'context' ) && in_array( $this->prop( 'context' ), array( 'form_top', 'before_permalink', 'after_title', 'after_editor' ), true );
1797 }
1798
1799 /**
1800 * Magic getter for our object.
1801 *
1802 * @param string $property Object property.
1803 * @throws Exception Throws an exception if the field is invalid.
1804 * @return mixed
1805 */
1806 public function __get( $property ) {
1807 switch ( $property ) {
1808 case 'updated':
1809 case 'has_columns':
1810 case 'tax_metaboxes_to_remove':
1811 return $this->{$property};
1812 default:
1813 return parent::__get( $property );
1814 }
1815 }
1816
1817 }
1818