Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CMB2 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CMB2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class CMB2 { |
||
|
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * Current field's ID |
||
| 19 | * @var string |
||
| 20 | * @since 2.0.0 |
||
| 21 | */ |
||
| 22 | protected $cmb_id = ''; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Metabox Config array |
||
| 26 | * @var array |
||
| 27 | * @since 0.9.0 |
||
| 28 | */ |
||
| 29 | protected $meta_box = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Object ID for metabox meta retrieving/saving |
||
| 33 | * @var mixed |
||
| 34 | * @since 1.0.0 |
||
| 35 | */ |
||
| 36 | protected $object_id = 0; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Type of object being saved. (e.g., post, user, or comment) |
||
| 40 | * @var string |
||
| 41 | * @since 1.0.0 |
||
| 42 | */ |
||
| 43 | protected $object_type = 'post'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Type of object registered for metabox. (e.g., post, user, or comment) |
||
| 47 | * @var string |
||
| 48 | * @since 1.0.0 |
||
| 49 | */ |
||
| 50 | protected $mb_object_type = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * List of fields that are changed/updated on save |
||
| 54 | * @var array |
||
| 55 | * @since 1.1.0 |
||
| 56 | */ |
||
| 57 | protected $updated = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Metabox Defaults |
||
| 61 | * @var array |
||
| 62 | * @since 1.0.1 |
||
| 63 | */ |
||
| 64 | protected $mb_defaults = array( |
||
| 65 | 'id' => '', |
||
| 66 | 'title' => '', |
||
| 67 | 'type' => '', |
||
| 68 | 'object_types' => array(), // Post type |
||
| 69 | 'context' => 'normal', |
||
| 70 | 'priority' => 'high', |
||
| 71 | 'show_names' => true, // Show field names on the left |
||
| 72 | 'show_on_cb' => null, // Callback to determine if metabox should display. |
||
| 73 | 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb' |
||
| 74 | 'cmb_styles' => true, // Include CMB2 stylesheet |
||
| 75 | 'enqueue_js' => true, // Include CMB2 JS |
||
| 76 | 'fields' => array(), |
||
| 77 | 'hookup' => true, |
||
| 78 | 'save_fields' => true, // Will not save during hookup if false |
||
| 79 | 'closed' => false, // Default to metabox being closed? |
||
| 80 | 'taxonomies' => array(), |
||
| 81 | 'new_user_section' => 'add-new-user', // or 'add-existing-user' |
||
| 82 | 'new_term_section' => true, |
||
| 83 | 'show_in_rest' => false, |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Metabox field objects |
||
| 88 | * @var array |
||
| 89 | * @since 2.0.3 |
||
| 90 | */ |
||
| 91 | protected $fields = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * An array of hidden fields to output at the end of the form |
||
| 95 | * @var array |
||
| 96 | * @since 2.0.0 |
||
| 97 | */ |
||
| 98 | protected $hidden_fields = array(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Array of key => value data for saving. Likely $_POST data. |
||
| 102 | * @var array |
||
| 103 | * @since 2.0.0 |
||
| 104 | */ |
||
| 105 | public $data_to_save = array(); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Array of key => value data for saving. Likely $_POST data. |
||
| 109 | * @var string |
||
| 110 | * @since 2.0.0 |
||
| 111 | */ |
||
| 112 | protected $generated_nonce = ''; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get started |
||
| 116 | * @since 0.4.0 |
||
| 117 | * @param array $meta_box Metabox config array |
||
| 118 | * @param integer $object_id Optional object id |
||
| 119 | */ |
||
| 120 | 43 | public function __construct( $meta_box, $object_id = 0 ) { |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Loops through and displays fields |
||
| 151 | * @since 1.0.0 |
||
| 152 | * @param int $object_id Object ID |
||
| 153 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
| 154 | */ |
||
| 155 | 1 | public function show_form( $object_id = 0, $object_type = '' ) { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Outputs the opening form markup and runs corresponding hooks: |
||
| 167 | * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
||
| 168 | * @since 2.2.0 |
||
| 169 | * @param integer $object_id Object ID |
||
| 170 | * @param string $object_type Object type |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | 1 | View Code Duplication | public function render_form_open( $object_id = 0, $object_type = '' ) { |
| 211 | |||
| 212 | /** |
||
| 213 | * Outputs the closing form markup and runs corresponding hooks: |
||
| 214 | * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
||
| 215 | * @since 2.2.0 |
||
| 216 | * @param integer $object_id Object ID |
||
| 217 | * @param string $object_type Object type |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | 1 | View Code Duplication | public function render_form_close( $object_id = 0, $object_type = '' ) { |
| 257 | |||
| 258 | /** |
||
| 259 | * Renders a field based on the field type |
||
| 260 | * @since 2.2.0 |
||
| 261 | * @param array $field_args A field configuration array. |
||
| 262 | * @return mixed CMB2_Field object if successful. |
||
| 263 | */ |
||
| 264 | 1 | public function render_field( $field_args ) { |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Render a repeatable group. |
||
| 295 | * @param array $args Array of field arguments for a group field parent. |
||
| 296 | * @return CMB2_Field|null Group field object. |
||
| 297 | */ |
||
| 298 | 2 | public function render_group( $args ) { |
|
| 299 | |||
| 300 | 2 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
| 301 | return; |
||
| 302 | } |
||
| 303 | |||
| 304 | 2 | $field_group = $this->get_field( $args ); |
|
| 305 | |||
| 306 | // If field is requesting to be conditionally shown |
||
| 307 | 2 | if ( ! $field_group || ! $field_group->should_show() ) { |
|
| 308 | return; |
||
| 309 | } |
||
| 310 | |||
| 311 | 2 | $desc = $field_group->args( 'description' ); |
|
| 312 | 2 | $label = $field_group->args( 'name' ); |
|
| 313 | 2 | $sortable = $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
|
| 314 | 2 | $repeat_class = $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
|
| 315 | 2 | $group_val = (array) $field_group->value(); |
|
| 316 | 2 | $nrows = count( $group_val ); |
|
| 317 | 2 | $remove_disabled = $nrows <= 1 ? 'disabled="disabled" ' : ''; |
|
| 318 | 2 | $field_group->index = 0; |
|
| 319 | |||
| 320 | 2 | $field_group->peform_param_callback( 'before_group' ); |
|
| 321 | |||
| 322 | 2 | echo '<div class="cmb-row cmb-repeat-group-wrap" data-fieldtype="group"><div class="cmb-td"><div id="', $field_group->id(), '_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group', $sortable, $repeat_class, '" style="width:100%;">'; |
|
| 323 | |||
| 324 | 2 | if ( $desc || $label ) { |
|
| 325 | 2 | $class = $desc ? ' cmb-group-description' : ''; |
|
| 326 | 2 | echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
|
| 327 | 2 | if ( $label ) { |
|
| 328 | 2 | echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
|
| 329 | 2 | } |
|
| 330 | 2 | if ( $desc ) { |
|
| 331 | 1 | echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
|
| 332 | 1 | } |
|
| 333 | 2 | echo '</div></div>'; |
|
| 334 | 2 | } |
|
| 335 | |||
| 336 | 2 | if ( ! empty( $group_val ) ) { |
|
| 337 | |||
| 338 | foreach ( $group_val as $group_key => $field_id ) { |
||
| 339 | $this->render_group_row( $field_group, $remove_disabled ); |
||
| 340 | $field_group->index++; |
||
| 341 | } |
||
| 342 | } else { |
||
| 343 | 2 | $this->render_group_row( $field_group, $remove_disabled ); |
|
| 344 | } |
||
| 345 | |||
| 346 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 347 | 1 | echo '<div class="cmb-row"><div class="cmb-td"><p class="cmb-add-row"><button type="button" data-selector="', $field_group->id(), '_repeat" data-grouptitle="', $field_group->options( 'group_title' ), '" class="cmb-add-group-row button">', $field_group->options( 'add_button' ), '</button></p></div></div>'; |
|
| 348 | 1 | } |
|
| 349 | |||
| 350 | 2 | echo '</div></div></div>'; |
|
| 351 | |||
| 352 | 2 | $field_group->peform_param_callback( 'after_group' ); |
|
| 353 | |||
| 354 | 2 | return $field_group; |
|
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Render a repeatable group row |
||
| 359 | * @since 1.0.2 |
||
| 360 | * @param CMB2_Field $field_group CMB2_Field group field object |
||
| 361 | * @param string $remove_disabled Attribute string to disable the remove button |
||
| 362 | */ |
||
| 363 | 2 | public function render_group_row( $field_group, $remove_disabled ) { |
|
| 364 | |||
| 365 | 2 | $field_group->peform_param_callback( 'before_group_row' ); |
|
| 366 | 2 | $closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; |
|
| 367 | |||
| 368 | echo ' |
||
| 369 | 2 | <div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; |
|
| 370 | |||
| 371 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 372 | 1 | echo '<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row"></button>'; |
|
| 373 | 1 | } |
|
| 374 | |||
| 375 | echo ' |
||
| 376 | 2 | <div class="cmbhandle" title="' , __( 'Click to toggle', 'cmb2' ), '"><br></div> |
|
| 377 | 2 | <h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> |
|
| 378 | |||
| 379 | <div class="inside cmb-td cmb-nested cmb-field-list">'; |
||
| 380 | // Loop and render repeatable group fields |
||
| 381 | 2 | foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { |
|
| 382 | 2 | if ( 'hidden' == $field_args['type'] ) { |
|
| 383 | |||
| 384 | // Save rendering for after the metabox |
||
| 385 | $this->add_hidden_field( array( |
||
| 386 | 'field_args' => $field_args, |
||
| 387 | 'group_field' => $field_group, |
||
| 388 | ) ); |
||
| 389 | |||
| 390 | } else { |
||
| 391 | |||
| 392 | 2 | $field_args['show_names'] = $field_group->args( 'show_names' ); |
|
| 393 | 2 | $field_args['context'] = $field_group->args( 'context' ); |
|
| 394 | |||
| 395 | 2 | $field = $this->get_field( $field_args, $field_group )->render_field(); |
|
| 396 | } |
||
| 397 | 2 | } |
|
| 398 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 399 | echo ' |
||
| 400 | <div class="cmb-row cmb-remove-field-row"> |
||
| 401 | <div class="cmb-remove-row"> |
||
| 402 | 1 | <button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="button cmb-remove-group-row alignright">', $field_group->options( 'remove_button' ), '</button> |
|
| 403 | </div> |
||
| 404 | </div> |
||
| 405 | '; |
||
| 406 | 1 | } |
|
| 407 | echo ' |
||
| 408 | </div> |
||
| 409 | </div> |
||
| 410 | 2 | '; |
|
| 411 | |||
| 412 | 2 | $field_group->peform_param_callback( 'after_group_row' ); |
|
| 413 | 2 | } |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Add a hidden field to the list of hidden fields to be rendered later |
||
| 417 | * @since 2.0.0 |
||
| 418 | * @param array $args Array of arguments to be passed to CMB2_Field |
||
| 419 | */ |
||
| 420 | public function add_hidden_field( $args ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Loop through and output hidden fields |
||
| 429 | * @since 2.0.0 |
||
| 430 | */ |
||
| 431 | 1 | public function render_hidden_fields() { |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Returns array of sanitized field values (without saving them) |
||
| 441 | * @since 2.0.3 |
||
| 442 | * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
||
| 443 | */ |
||
| 444 | 2 | public function get_sanitized_values( array $data_to_sanitize ) { |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Loops through and saves field data |
||
| 471 | * @since 1.0.0 |
||
| 472 | * @param int $object_id Object ID |
||
| 473 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
| 474 | * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
||
| 475 | */ |
||
| 476 | 1 | public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
| 477 | |||
| 478 | // Fall-back to $_POST data |
||
| 479 | 1 | $this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST; |
|
| 480 | 1 | $object_id = $this->object_id( $object_id ); |
|
| 481 | 1 | $object_type = $this->object_type( $object_type ); |
|
| 482 | |||
| 483 | 1 | $this->process_fields(); |
|
| 484 | |||
| 485 | // If options page, save the updated options |
||
| 486 | 1 | if ( 'options-page' == $object_type ) { |
|
| 487 | 1 | cmb2_options( $object_id )->set(); |
|
| 488 | 1 | } |
|
| 489 | |||
| 490 | 1 | $this->after_save(); |
|
| 491 | 1 | } |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Process and save form fields |
||
| 495 | * @since 2.0.0 |
||
| 496 | */ |
||
| 497 | 3 | public function process_fields() { |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Process and save a field |
||
| 514 | * @since 2.0.0 |
||
| 515 | * @param array $field_args Array of field arguments |
||
| 516 | */ |
||
| 517 | 3 | public function process_field( $field_args ) { |
|
| 549 | |||
| 550 | 3 | public function pre_process() { |
|
| 565 | |||
| 566 | 1 | public function after_save() { |
|
| 567 | 1 | $object_type = $this->object_type(); |
|
| 568 | 1 | $object_id = $this->object_id(); |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Fires after all fields have been saved. |
||
| 572 | * |
||
| 573 | * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
||
| 574 | * Usually `post` (this applies to all post-types). |
||
| 575 | * Could also be `comment`, `user` or `options-page`. |
||
| 576 | * |
||
| 577 | * @param int $object_id The ID of the current object |
||
| 578 | * @param array $cmb_id The current box ID |
||
| 579 | * @param string $updated Array of field ids that were updated. |
||
| 580 | * Will only include field ids that had values change. |
||
| 581 | * @param array $cmb This CMB2 object |
||
| 582 | */ |
||
| 583 | 1 | do_action( "cmb2_save_{$object_type}_fields", $object_id, $this->cmb_id, $this->updated, $this ); |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Fires after all fields have been saved. |
||
| 587 | * |
||
| 588 | * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
| 589 | * |
||
| 590 | * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
||
| 591 | * Usually `post` (this applies to all post-types). |
||
| 592 | * Could also be `comment`, `user` or `options-page`. |
||
| 593 | * |
||
| 594 | * @param int $object_id The ID of the current object |
||
| 595 | * @param string $updated Array of field ids that were updated. |
||
| 596 | * Will only include field ids that had values change. |
||
| 597 | * @param array $cmb This CMB2 object |
||
| 598 | */ |
||
| 599 | 1 | do_action( "cmb2_save_{$object_type}_fields_{$this->cmb_id}", $object_id, $this->updated, $this ); |
|
| 600 | 1 | } |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Save a repeatable group |
||
| 604 | * @since 1.x.x |
||
| 605 | * @param array $args Field arguments array |
||
| 606 | * @return mixed Return of CMB2_Field::update_data() |
||
| 607 | */ |
||
| 608 | 1 | public function save_group( $args ) { |
|
| 609 | 1 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
| 610 | return; |
||
| 611 | } |
||
| 612 | |||
| 613 | 1 | return $this->save_group_field( new CMB2_Field( array( |
|
| 614 | 1 | 'field_args' => $args, |
|
| 615 | 1 | 'object_type' => $this->object_type(), |
|
| 616 | 1 | 'object_id' => $this->object_id(), |
|
| 617 | 1 | ) ) ); |
|
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Save a repeatable group |
||
| 622 | * @since 1.x.x |
||
| 623 | * @param array $field_group CMB2_Field group field object |
||
| 624 | * @return mixed Return of CMB2_Field::update_data() |
||
| 625 | */ |
||
| 626 | 1 | public function save_group_field( $field_group ) { |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Get object id from global space if no id is provided |
||
| 701 | * @since 1.0.0 |
||
| 702 | * @param integer $object_id Object ID |
||
| 703 | * @return integer $object_id Object ID |
||
| 704 | */ |
||
| 705 | 47 | public function object_id( $object_id = 0 ) { |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Sets the $object_type based on metabox settings |
||
| 747 | * @since 1.0.0 |
||
| 748 | * @return string Object type |
||
| 749 | */ |
||
| 750 | 43 | public function mb_object_type() { |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Determines if metabox is for an options page |
||
| 803 | * @since 1.0.1 |
||
| 804 | * @return boolean True/False |
||
| 805 | */ |
||
| 806 | 43 | public function is_options_page_mb() { |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Returns the object type |
||
| 812 | * @since 1.0.0 |
||
| 813 | * @return string Object type |
||
| 814 | */ |
||
| 815 | 47 | public function object_type( $object_type = '' ) { |
|
| 842 | |||
| 843 | /** |
||
| 844 | * Set metabox property. |
||
| 845 | * @since 2.2.0 |
||
| 846 | * @param string $property Metabox config property to retrieve |
||
| 847 | * @param mixed $value Value to set if no value found |
||
| 848 | * @return mixed Metabox config property value or false |
||
| 849 | */ |
||
| 850 | 1 | public function set_prop( $property, $value ) { |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Get metabox property and optionally set a fallback |
||
| 858 | * @since 2.0.0 |
||
| 859 | * @param string $property Metabox config property to retrieve |
||
| 860 | * @param mixed $fallback Fallback value to set if no value found |
||
| 861 | * @return mixed Metabox config property value or false |
||
| 862 | */ |
||
| 863 | 43 | public function prop( $property, $fallback = null ) { |
|
| 870 | |||
| 871 | /** |
||
| 872 | * Get a field object |
||
| 873 | * |
||
| 874 | * @since 2.0.3 |
||
| 875 | * |
||
| 876 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
||
| 877 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 878 | * |
||
| 879 | * @return CMB2_Field|false CMB2_Field object (or false) |
||
| 880 | */ |
||
| 881 | 15 | public function get_field( $field, $field_group = null ) { |
|
| 906 | |||
| 907 | /** |
||
| 908 | * Handles determining which type of arguments to pass to CMB2_Field |
||
| 909 | * @since 2.0.7 |
||
| 910 | * @param mixed $field_id Field (or group field) ID |
||
| 911 | * @param mixed $field_args Array of field arguments |
||
| 912 | * @param mixed $sub_field_id Sub field ID (if field_group exists) |
||
| 913 | * @param mixed $field_group If a sub-field, will be the parent group CMB2_Field object |
||
| 914 | * @return array Array of CMB2_Field arguments |
||
| 915 | */ |
||
| 916 | 13 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
|
| 941 | |||
| 942 | /** |
||
| 943 | * When fields are added in the old-school way, intitate them as they should be |
||
| 944 | * @since 2.1.0 |
||
| 945 | * @param array $fields Array of fields to add |
||
| 946 | * @param mixed $parent_field_id Parent field id or null |
||
| 947 | */ |
||
| 948 | 41 | protected function add_fields( $fields, $parent_field_id = null ) { |
|
| 966 | |||
| 967 | /** |
||
| 968 | * Add a field to the metabox |
||
| 969 | * @since 2.0.0 |
||
| 970 | * @param array $field Metabox field config array |
||
| 971 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 972 | * @return mixed Field id or false |
||
| 973 | */ |
||
| 974 | 43 | public function add_field( array $field, $position = 0 ) { |
|
| 992 | |||
| 993 | /** |
||
| 994 | * Add a field to a group |
||
| 995 | * @since 2.0.0 |
||
| 996 | * @param string $parent_field_id The field id of the group field to add the field |
||
| 997 | * @param array $field Metabox field config array |
||
| 998 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 999 | * @return mixed Array of parent/field ids or false |
||
| 1000 | */ |
||
| 1001 | 3 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
|
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Add a field array to a fields array in desired position |
||
| 1027 | * @since 2.0.2 |
||
| 1028 | * @param array $field Metabox field config array |
||
| 1029 | * @param array &$fields Array (passed by reference) to append the field (array) to |
||
| 1030 | * @param integer $position Optionally specify a position in the array to be inserted |
||
| 1031 | */ |
||
| 1032 | 43 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
|
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Remove a field from the metabox |
||
| 1042 | * @since 2.0.0 |
||
| 1043 | * @param string $field_id The field id of the field to remove |
||
| 1044 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1045 | * @return bool True if field was removed |
||
| 1046 | */ |
||
| 1047 | 2 | public function remove_field( $field_id, $parent_field_id = '' ) { |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Update or add a property to a field |
||
| 1074 | * @since 2.0.0 |
||
| 1075 | * @param string $field_id Field id |
||
| 1076 | * @param string $property Field property to set/update |
||
| 1077 | * @param mixed $value Value to set the field property |
||
| 1078 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1079 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
||
| 1080 | */ |
||
| 1081 | 4 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
|
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Check if field ids match a field and return the index/field id |
||
| 1101 | * @since 2.0.2 |
||
| 1102 | * @param string $field_id Field id |
||
| 1103 | * @param string $parent_field_id (optional) Parent field id |
||
| 1104 | * @return mixed Array of field/parent ids, or false |
||
| 1105 | */ |
||
| 1106 | 19 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
|
| 1133 | |||
| 1134 | /** |
||
| 1135 | * When using the old array filter, it is unlikely field array indexes will be the field id |
||
| 1136 | * @since 2.0.2 |
||
| 1137 | * @param string $field_id The field id |
||
| 1138 | * @param array $fields Array of fields to search |
||
| 1139 | * @return mixed Field index or false |
||
| 1140 | */ |
||
| 1141 | 2 | public function search_old_school_array( $field_id, $fields ) { |
|
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
||
| 1149 | * |
||
| 1150 | * @since 2.0.9 |
||
| 1151 | * |
||
| 1152 | * @return bool Whether this cmb should be shown. |
||
| 1153 | */ |
||
| 1154 | View Code Duplication | public function should_show() { |
|
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Generate a unique nonce field for each registered meta_box |
||
| 1168 | * @since 2.0.0 |
||
| 1169 | * @return string unique nonce hidden input |
||
| 1170 | */ |
||
| 1171 | 1 | public function nonce_field() { |
|
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Generate a unique nonce for each registered meta_box |
||
| 1177 | * @since 2.0.0 |
||
| 1178 | * @return string unique nonce string |
||
| 1179 | */ |
||
| 1180 | 1 | public function nonce() { |
|
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Magic getter for our object. |
||
| 1190 | * @param string $field |
||
| 1191 | * @throws Exception Throws an exception if the field is invalid. |
||
| 1192 | * @return mixed |
||
| 1193 | */ |
||
| 1194 | 43 | public function __get( $field ) { |
|
| 1206 | |||
| 1207 | } |
||
| 1208 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.