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