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

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