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

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