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 |
||
| 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 | 'hookup' => true, |
||
| 85 | 'save_fields' => true, // Will not save during hookup if false. |
||
| 86 | 'closed' => false, // Default to metabox being closed? |
||
| 87 | 'taxonomies' => array(), |
||
| 88 | 'new_user_section' => 'add-new-user', // or 'add-existing-user'. |
||
| 89 | 'new_term_section' => true, |
||
| 90 | 'show_in_rest' => false, |
||
| 91 | ); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Metabox field objects |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | * @since 2.0.3 |
||
| 98 | */ |
||
| 99 | protected $fields = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * An array of hidden fields to output at the end of the form |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | * @since 2.0.0 |
||
| 106 | */ |
||
| 107 | protected $hidden_fields = array(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Array of key => value data for saving. Likely $_POST data. |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | * @since 2.0.0 |
||
| 114 | */ |
||
| 115 | protected $generated_nonce = ''; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Whether there are fields to be shown in columns. Set in CMB2::add_field(). |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | * @since 2.2.2 |
||
| 122 | */ |
||
| 123 | protected $has_columns = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * If taxonomy field is requesting to remove_default, we store the taxonomy here. |
||
| 127 | * |
||
| 128 | * @var array |
||
| 129 | * @since 2.2.3 |
||
| 130 | */ |
||
| 131 | protected $tax_metaboxes_to_remove = array(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get started |
||
| 135 | * |
||
| 136 | * @since 0.4.0 |
||
| 137 | * @param array $config Metabox config array. |
||
| 138 | * @param integer $object_id Optional object id. |
||
| 139 | */ |
||
| 140 | 48 | public function __construct( $config, $object_id = 0 ) { |
|
| 141 | |||
| 142 | 48 | if ( empty( $config['id'] ) ) { |
|
| 143 | 1 | wp_die( esc_html__( 'Metabox configuration is required to have an ID parameter.', 'cmb2' ) ); |
|
| 144 | } |
||
| 145 | |||
| 146 | 48 | $this->meta_box = wp_parse_args( $config, $this->mb_defaults ); |
|
| 147 | 48 | $this->meta_box['fields'] = array(); |
|
| 148 | |||
| 149 | 48 | $this->object_id( $object_id ); |
|
| 150 | 48 | $this->mb_object_type(); |
|
| 151 | 48 | $this->cmb_id = $config['id']; |
|
| 152 | |||
| 153 | 48 | if ( ! empty( $config['fields'] ) && is_array( $config['fields'] ) ) { |
|
| 154 | 45 | $this->add_fields( $config['fields'] ); |
|
| 155 | 45 | } |
|
| 156 | |||
| 157 | 48 | CMB2_Boxes::add( $this ); |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Hook during initiation of CMB2 object |
||
| 161 | * |
||
| 162 | * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. |
||
| 163 | * |
||
| 164 | * @param array $cmb This CMB2 object |
||
| 165 | */ |
||
| 166 | 48 | do_action( "cmb2_init_{$this->cmb_id}", $this ); |
|
| 167 | 48 | } |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Loops through and displays fields |
||
| 171 | * |
||
| 172 | * @since 1.0.0 |
||
| 173 | * @param int $object_id Object ID. |
||
| 174 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment). |
||
| 175 | */ |
||
| 176 | 1 | public function show_form( $object_id = 0, $object_type = '' ) { |
|
| 177 | 1 | $this->render_form_open( $object_id, $object_type ); |
|
| 178 | |||
| 179 | 1 | foreach ( $this->prop( 'fields' ) as $field_args ) { |
|
| 180 | 1 | $this->render_field( $field_args ); |
|
| 181 | 1 | } |
|
| 182 | |||
| 183 | 1 | $this->render_form_close( $object_id, $object_type ); |
|
| 184 | 1 | } |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Outputs the opening form markup and runs corresponding hooks: |
||
| 188 | * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
||
| 189 | * |
||
| 190 | * @since 2.2.0 |
||
| 191 | * @param integer $object_id Object ID. |
||
| 192 | * @param string $object_type Object type. |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | 1 | public function render_form_open( $object_id = 0, $object_type = '' ) { |
|
| 196 | 1 | $object_type = $this->object_type( $object_type ); |
|
| 197 | 1 | $object_id = $this->object_id( $object_id ); |
|
| 198 | |||
| 199 | 1 | echo "\n<!-- Begin CMB2 Fields -->\n"; |
|
| 200 | |||
| 201 | 1 | $this->nonce_field(); |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Hook before form table begins |
||
| 205 | * |
||
| 206 | * @param array $cmb_id The current box ID. |
||
| 207 | * @param int $object_id The ID of the current object. |
||
| 208 | * @param string $object_type The type of object you are working with. |
||
| 209 | * Usually `post` (this applies to all post-types). |
||
| 210 | * Could also be `comment`, `user` or `options-page`. |
||
| 211 | * @param array $cmb This CMB2 object. |
||
| 212 | */ |
||
| 213 | 1 | do_action( 'cmb2_before_form', $this->cmb_id, $object_id, $object_type, $this ); |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Hook before form table begins |
||
| 217 | * |
||
| 218 | * The first dynamic portion of the hook name, $object_type, is the type of object |
||
| 219 | * you are working with. Usually `post` (this applies to all post-types). |
||
| 220 | * Could also be `comment`, `user` or `options-page`. |
||
| 221 | * |
||
| 222 | * The second dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
| 223 | * |
||
| 224 | * @param array $cmb_id The current box ID |
||
| 225 | * @param int $object_id The ID of the current object |
||
| 226 | * @param array $cmb This CMB2 object |
||
| 227 | */ |
||
| 228 | 1 | do_action( "cmb2_before_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
|
| 229 | |||
| 230 | 1 | echo '<div class="', esc_attr( $this->box_classes() ), '"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">'; |
|
| 231 | |||
| 232 | 1 | } |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Defines the classes for the CMB2 form/wrap. |
||
| 236 | * |
||
| 237 | * @since 2.0.0 |
||
| 238 | * @return string Space concatenated list of classes |
||
| 239 | */ |
||
| 240 | 1 | public function box_classes() { |
|
| 241 | |||
| 242 | 1 | $classes = array( 'cmb2-wrap', 'form-table' ); |
|
| 243 | |||
| 244 | // Use the callback to fetch classes. |
||
| 245 | 1 | View Code Duplication | if ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
| 246 | 1 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
|
| 247 | 1 | $classes = array_merge( $classes, $added_classes ); |
|
| 248 | 1 | } |
|
| 249 | |||
| 250 | 1 | View Code Duplication | if ( $added_classes = $this->prop( 'classes' ) ) { |
| 251 | 1 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
|
| 252 | 1 | $classes = array_merge( $classes, $added_classes ); |
|
| 253 | 1 | } |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Add our context classes for non-standard metaboxes. |
||
| 257 | * |
||
| 258 | * @since 2.2.4 |
||
| 259 | */ |
||
| 260 | 1 | if ( $this->is_alternate_context_box() ) { |
|
| 261 | |||
| 262 | // Include custom class if requesting no title. |
||
| 263 | if ( ! $this->prop( 'title' ) && ! $this->prop( 'remove_box_wrap' ) ) { |
||
| 264 | $context[] = 'cmb2-context-wrap-no-title'; |
||
| 265 | } |
||
| 266 | |||
| 267 | // Include a generic context wrapper. |
||
| 268 | $context[] = 'cmb2-context-wrap'; |
||
| 269 | |||
| 270 | // Include a context-type based context wrapper. |
||
| 271 | $context[] = 'cmb2-context-wrap-' . $this->prop( 'context' ); |
||
| 272 | |||
| 273 | // Include an ID based context wrapper as well. |
||
| 274 | $context[] = 'cmb2-context-wrap-' . $this->prop( 'id' ); |
||
| 275 | |||
| 276 | // And merge all the classes back into the array. |
||
| 277 | $classes = array_merge( $classes, $context ); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Globally filter box wrap classes |
||
| 282 | * |
||
| 283 | * @since 2.2.2 |
||
| 284 | * |
||
| 285 | * @param string $classes Array of classes for the cmb2-wrap. |
||
| 286 | * @param CMB2 $cmb This CMB2 object. |
||
| 287 | */ |
||
| 288 | 1 | $classes = apply_filters( 'cmb2_wrap_classes', $classes, $this ); |
|
| 289 | |||
| 290 | // Clean up. |
||
| 291 | 1 | $classes = array_map( 'strip_tags', array_filter( $classes ) ); |
|
| 292 | |||
| 293 | // Remove any duplicates. |
||
| 294 | 1 | $classes = array_unique( $classes ); |
|
| 295 | |||
| 296 | // Make a string. |
||
| 297 | 1 | return implode( ' ', $classes ); |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Outputs the closing form markup and runs corresponding hooks: |
||
| 302 | * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
||
| 303 | * |
||
| 304 | * @since 2.2.0 |
||
| 305 | * @param integer $object_id Object ID. |
||
| 306 | * @param string $object_type Object type. |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 1 | public function render_form_close( $object_id = 0, $object_type = '' ) { |
|
| 310 | 1 | $object_type = $this->object_type( $object_type ); |
|
| 311 | 1 | $object_id = $this->object_id( $object_id ); |
|
| 312 | |||
| 313 | 1 | echo '</div></div>'; |
|
| 314 | |||
| 315 | 1 | $this->render_hidden_fields(); |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Hook after form form has been rendered |
||
| 319 | * |
||
| 320 | * @param array $cmb_id The current box ID. |
||
| 321 | * @param int $object_id The ID of the current object. |
||
| 322 | * @param string $object_type The type of object you are working with. |
||
| 323 | * Usually `post` (this applies to all post-types). |
||
| 324 | * Could also be `comment`, `user` or `options-page`. |
||
| 325 | * @param array $cmb This CMB2 object. |
||
| 326 | */ |
||
| 327 | 1 | do_action( 'cmb2_after_form', $this->cmb_id, $object_id, $object_type, $this ); |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Hook after form form has been rendered |
||
| 331 | * |
||
| 332 | * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
| 333 | * |
||
| 334 | * The first dynamic portion of the hook name, $object_type, is the type of object |
||
| 335 | * you are working with. Usually `post` (this applies to all post-types). |
||
| 336 | * Could also be `comment`, `user` or `options-page`. |
||
| 337 | * |
||
| 338 | * @param int $object_id The ID of the current object |
||
| 339 | * @param array $cmb This CMB2 object |
||
| 340 | */ |
||
| 341 | 1 | do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
|
| 342 | |||
| 343 | 1 | echo "\n<!-- End CMB2 Fields -->\n"; |
|
| 344 | |||
| 345 | 1 | } |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Renders a field based on the field type |
||
| 349 | * |
||
| 350 | * @since 2.2.0 |
||
| 351 | * @param array $field_args A field configuration array. |
||
| 352 | * @return mixed CMB2_Field object if successful. |
||
| 353 | */ |
||
| 354 | 1 | public function render_field( $field_args ) { |
|
| 355 | 1 | $field_args['context'] = $this->prop( 'context' ); |
|
| 356 | |||
| 357 | 1 | if ( 'group' === $field_args['type'] ) { |
|
| 358 | |||
| 359 | if ( ! isset( $field_args['show_names'] ) ) { |
||
| 360 | $field_args['show_names'] = $this->prop( 'show_names' ); |
||
| 361 | } |
||
| 362 | $field = $this->render_group( $field_args ); |
||
| 363 | |||
| 364 | 1 | } elseif ( 'hidden' === $field_args['type'] && $this->get_field( $field_args )->should_show() ) { |
|
| 365 | // Save rendering for after the metabox. |
||
| 366 | $field = $this->add_hidden_field( $field_args ); |
||
| 367 | |||
| 368 | } else { |
||
| 369 | |||
| 370 | 1 | $field_args['show_names'] = $this->prop( 'show_names' ); |
|
| 371 | |||
| 372 | // Render default fields. |
||
| 373 | 1 | $field = $this->get_field( $field_args )->render_field(); |
|
| 374 | } |
||
| 375 | |||
| 376 | 1 | return $field; |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Render a repeatable group. |
||
| 381 | * |
||
| 382 | * @param array $args Array of field arguments for a group field parent. |
||
| 383 | * @return CMB2_Field|null Group field object. |
||
| 384 | */ |
||
| 385 | 2 | public function render_group( $args ) { |
|
| 386 | |||
| 387 | 2 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
| 388 | return; |
||
| 389 | } |
||
| 390 | |||
| 391 | 2 | $field_group = $this->get_field( $args ); |
|
| 392 | |||
| 393 | // If field is requesting to be conditionally shown. |
||
| 394 | 2 | if ( ! $field_group || ! $field_group->should_show() ) { |
|
| 395 | return; |
||
| 396 | } |
||
| 397 | |||
| 398 | 2 | $desc = $field_group->args( 'description' ); |
|
| 399 | 2 | $label = $field_group->args( 'name' ); |
|
| 400 | 2 | $group_val = (array) $field_group->value(); |
|
| 401 | 2 | $remove_disabled = count( $group_val ) <= 1 ? 'disabled="disabled" ' : ''; |
|
| 402 | 2 | $field_group->index = 0; |
|
| 403 | |||
| 404 | 2 | $field_group->peform_param_callback( 'before_group' ); |
|
| 405 | |||
| 406 | 2 | 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 ), '>'; |
|
| 407 | |||
| 408 | 2 | if ( $desc || $label ) { |
|
| 409 | 2 | $class = $desc ? ' cmb-group-description' : ''; |
|
| 410 | 2 | echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
|
| 411 | 2 | if ( $label ) { |
|
| 412 | 2 | echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
|
| 413 | 2 | } |
|
| 414 | 2 | if ( $desc ) { |
|
| 415 | 1 | echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
|
| 416 | 1 | } |
|
| 417 | 2 | echo '</div></div>'; |
|
| 418 | 2 | } |
|
| 419 | |||
| 420 | 2 | if ( ! empty( $group_val ) ) { |
|
| 421 | foreach ( $group_val as $group_key => $field_id ) { |
||
| 422 | $this->render_group_row( $field_group, $remove_disabled ); |
||
| 423 | $field_group->index++; |
||
| 424 | } |
||
| 425 | } else { |
||
| 426 | 2 | $this->render_group_row( $field_group, $remove_disabled ); |
|
| 427 | } |
||
| 428 | |||
| 429 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 430 | 1 | 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">', $field_group->options( 'add_button' ), '</button></p></div></div>'; |
|
| 431 | 1 | } |
|
| 432 | |||
| 433 | 2 | echo '</div></div></div>'; |
|
| 434 | |||
| 435 | 2 | $field_group->peform_param_callback( 'after_group' ); |
|
| 436 | |||
| 437 | 2 | return $field_group; |
|
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get the group wrap attributes, which are passed through a filter. |
||
| 442 | * |
||
| 443 | * @since 2.2.3 |
||
| 444 | * @param CMB2_Field $field_group The group CMB2_Field object. |
||
| 445 | * @return string The attributes string. |
||
| 446 | */ |
||
| 447 | 2 | public function group_wrap_attributes( $field_group ) { |
|
| 448 | 2 | $classes = 'cmb-nested cmb-field-list cmb-repeatable-group'; |
|
| 449 | 2 | $classes .= $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
|
| 450 | 2 | $classes .= $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
|
| 451 | |||
| 452 | $group_wrap_attributes = array( |
||
| 453 | 2 | 'class' => $classes, |
|
| 454 | 2 | 'style' => 'width:100%;', |
|
| 455 | 2 | ); |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Allow for adding additional HTML attributes to a group wrapper. |
||
| 459 | * |
||
| 460 | * The attributes will be an array of key => value pairs for each attribute. |
||
| 461 | * |
||
| 462 | * @since 2.2.2 |
||
| 463 | * |
||
| 464 | * @param string $group_wrap_attributes Current attributes array. |
||
| 465 | * |
||
| 466 | * @param CMB2_Field $field_group The group CMB2_Field object. |
||
| 467 | */ |
||
| 468 | 2 | $group_wrap_attributes = apply_filters( 'cmb2_group_wrap_attributes', $group_wrap_attributes, $field_group ); |
|
| 469 | |||
| 470 | 2 | return CMB2_Utils::concat_attrs( $group_wrap_attributes ); |
|
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Render a repeatable group row |
||
| 475 | * |
||
| 476 | * @since 1.0.2 |
||
| 477 | * @param CMB2_Field $field_group CMB2_Field group field object. |
||
| 478 | * @param string $remove_disabled Attribute string to disable the remove button. |
||
| 479 | */ |
||
| 480 | 2 | public function render_group_row( $field_group, $remove_disabled ) { |
|
| 481 | |||
| 482 | 2 | $field_group->peform_param_callback( 'before_group_row' ); |
|
| 483 | 2 | $closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; |
|
| 484 | |||
| 485 | echo ' |
||
| 486 | 2 | <div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; |
|
| 487 | |||
| 488 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 489 | 1 | echo '<button type="button" ', $remove_disabled, '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>'; |
|
| 490 | 1 | } |
|
| 491 | |||
| 492 | echo ' |
||
| 493 | 2 | <div class="cmbhandle" title="' , esc_attr__( 'Click to toggle', 'cmb2' ), '"><br></div> |
|
| 494 | 2 | <h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> |
|
| 495 | |||
| 496 | <div class="inside cmb-td cmb-nested cmb-field-list">'; |
||
| 497 | // Loop and render repeatable group fields. |
||
| 498 | 2 | foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { |
|
| 499 | 2 | if ( 'hidden' === $field_args['type'] ) { |
|
| 500 | |||
| 501 | // Save rendering for after the metabox. |
||
| 502 | $this->add_hidden_field( $field_args, $field_group ); |
||
| 503 | |||
| 504 | } else { |
||
| 505 | |||
| 506 | 2 | $field_args['show_names'] = $field_group->args( 'show_names' ); |
|
| 507 | 2 | $field_args['context'] = $field_group->args( 'context' ); |
|
| 508 | |||
| 509 | 2 | $field = $this->get_field( $field_args, $field_group )->render_field(); |
|
| 510 | } |
||
| 511 | 2 | } |
|
| 512 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 513 | echo ' |
||
| 514 | <div class="cmb-row cmb-remove-field-row"> |
||
| 515 | <div class="cmb-remove-row"> |
||
| 516 | 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> |
|
| 517 | </div> |
||
| 518 | </div> |
||
| 519 | '; |
||
| 520 | 1 | } |
|
| 521 | echo ' |
||
| 522 | </div> |
||
| 523 | </div> |
||
| 524 | 2 | '; |
|
| 525 | |||
| 526 | 2 | $field_group->peform_param_callback( 'after_group_row' ); |
|
| 527 | 2 | } |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Add a hidden field to the list of hidden fields to be rendered later |
||
| 531 | * |
||
| 532 | * @since 2.0.0 |
||
| 533 | * @param array $field_args Array of field arguments to be passed to CMB2_Field. |
||
| 534 | * @param CMB2_Field|null $field_group CMB2_Field group field object. |
||
| 535 | */ |
||
| 536 | public function add_hidden_field( $field_args, $field_group = null ) { |
||
| 537 | if ( isset( $field_args['field_args'] ) ) { |
||
| 538 | // For back-compatibility. |
||
| 539 | $field = new CMB2_Field( $field_args ); |
||
| 540 | } else { |
||
| 541 | $field = $this->get_new_field( $field_args, $field_group ); |
||
| 542 | } |
||
| 543 | |||
| 544 | $types = new CMB2_Types( $field ); |
||
| 545 | |||
| 546 | if ( $field_group ) { |
||
| 547 | $types->iterator = $field_group->index; |
||
| 548 | } |
||
| 549 | |||
| 550 | $this->hidden_fields[] = $types; |
||
| 551 | |||
| 552 | return $field; |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Loop through and output hidden fields |
||
| 557 | * |
||
| 558 | * @since 2.0.0 |
||
| 559 | */ |
||
| 560 | 1 | public function render_hidden_fields() { |
|
| 561 | 1 | if ( ! empty( $this->hidden_fields ) ) { |
|
| 562 | foreach ( $this->hidden_fields as $hidden ) { |
||
| 563 | $hidden->render(); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | 1 | } |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Returns array of sanitized field values (without saving them) |
||
| 570 | * |
||
| 571 | * @since 2.0.3 |
||
| 572 | * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
||
| 573 | */ |
||
| 574 | 4 | public function get_sanitized_values( array $data_to_sanitize ) { |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Loops through and saves field data |
||
| 607 | * |
||
| 608 | * @since 1.0.0 |
||
| 609 | * @param int $object_id Object ID. |
||
| 610 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment). |
||
| 611 | * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
||
| 612 | */ |
||
| 613 | 1 | public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
| 614 | |||
| 615 | // Fall-back to $_POST data. |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Process and save form fields |
||
| 632 | * |
||
| 633 | * @since 2.0.0 |
||
| 634 | */ |
||
| 635 | 5 | public function process_fields() { |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Process and save a field |
||
| 652 | * |
||
| 653 | * @since 2.0.0 |
||
| 654 | * @param array $field_args Array of field arguments. |
||
| 655 | */ |
||
| 656 | 5 | public function process_field( $field_args ) { |
|
| 683 | |||
| 684 | /** |
||
| 685 | * Fires the "cmb2_{$object_type}_process_fields_{$cmb_id}" action hook. |
||
| 686 | * |
||
| 687 | * @since 2.x.x |
||
| 688 | * |
||
| 689 | * @return void |
||
| 690 | */ |
||
| 691 | 5 | public function pre_process() { |
|
| 706 | |||
| 707 | /** |
||
| 708 | * Fires the "cmb2_save_{$object_type}_fields" and |
||
| 709 | * "cmb2_save_{$object_type}_fields_{$cmb_id}" action hooks. |
||
| 710 | * |
||
| 711 | * @since 2.x.x |
||
| 712 | * |
||
| 713 | * @return void |
||
| 714 | */ |
||
| 715 | 1 | public function after_save() { |
|
| 750 | |||
| 751 | /** |
||
| 752 | * Save a repeatable group |
||
| 753 | * |
||
| 754 | * @since 1.x.x |
||
| 755 | * @param array $args Field arguments array. |
||
| 756 | * @return mixed Return of CMB2_Field::update_data(). |
||
| 757 | */ |
||
| 758 | 2 | public function save_group( $args ) { |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Save a repeatable group |
||
| 768 | * |
||
| 769 | * @since 1.x.x |
||
| 770 | * @param CMB2_Field $field_group CMB2_Field group field object. |
||
| 771 | * @return mixed Return of CMB2_Field::update_data(). |
||
| 772 | */ |
||
| 773 | 2 | public function save_group_field( $field_group ) { |
|
| 850 | |||
| 851 | /** |
||
| 852 | * Get object id from global space if no id is provided |
||
| 853 | * |
||
| 854 | * @since 1.0.0 |
||
| 855 | * @param integer $object_id Object ID. |
||
| 856 | * @return integer $object_id Object ID. |
||
| 857 | */ |
||
| 858 | 52 | public function object_id( $object_id = 0 ) { |
|
| 897 | |||
| 898 | /** |
||
| 899 | * Sets the $object_type based on metabox settings |
||
| 900 | * |
||
| 901 | * @since 1.0.0 |
||
| 902 | * @return string Object type. |
||
| 903 | */ |
||
| 904 | 48 | public function mb_object_type() { |
|
| 944 | |||
| 945 | /** |
||
| 946 | * Gets the box 'object_types' array based on box settings. |
||
| 947 | * |
||
| 948 | * @since 2.2.3 |
||
| 949 | * @return array Object types. |
||
| 950 | */ |
||
| 951 | 47 | public function box_types() { |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Determines if metabox is for an options page |
||
| 957 | * |
||
| 958 | * @since 1.0.1 |
||
| 959 | * @return boolean True/False. |
||
| 960 | */ |
||
| 961 | 48 | public function is_options_page_mb() { |
|
| 964 | |||
| 965 | /** |
||
| 966 | * Returns the object type |
||
| 967 | * |
||
| 968 | * @since 1.0.0 |
||
| 969 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment). Optional. |
||
| 970 | * @return string Object type. |
||
| 971 | */ |
||
| 972 | 52 | public function object_type( $object_type = '' ) { |
|
| 986 | |||
| 987 | /** |
||
| 988 | * Get the object type for the current page, based on the $pagenow global. |
||
| 989 | * |
||
| 990 | * @since 2.2.2 |
||
| 991 | * @return string Page object type name. |
||
| 992 | */ |
||
| 993 | 47 | public function current_object_type() { |
|
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Set metabox property. |
||
| 1018 | * |
||
| 1019 | * @since 2.2.2 |
||
| 1020 | * @param string $property Metabox config property to retrieve. |
||
| 1021 | * @param mixed $value Value to set if no value found. |
||
| 1022 | * @return mixed Metabox config property value or false. |
||
| 1023 | */ |
||
| 1024 | 1 | public function set_prop( $property, $value ) { |
|
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Get metabox property and optionally set a fallback |
||
| 1032 | * |
||
| 1033 | * @since 2.0.0 |
||
| 1034 | * @param string $property Metabox config property to retrieve. |
||
| 1035 | * @param mixed $fallback Fallback value to set if no value found. |
||
| 1036 | * @return mixed Metabox config property value or false. |
||
| 1037 | */ |
||
| 1038 | 48 | public function prop( $property, $fallback = null ) { |
|
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Get a field object |
||
| 1048 | * |
||
| 1049 | * @since 2.0.3 |
||
| 1050 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object. |
||
| 1051 | * @param CMB2_Field|null $field_group (optional) CMB2_Field object (group parent). |
||
| 1052 | * @return CMB2_Field|false CMB2_Field object (or false). |
||
| 1053 | */ |
||
| 1054 | 15 | public function get_field( $field, $field_group = null ) { |
|
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Handles determining which type of arguments to pass to CMB2_Field |
||
| 1082 | * |
||
| 1083 | * @since 2.0.7 |
||
| 1084 | * @param mixed $field_id Field (or group field) ID. |
||
| 1085 | * @param mixed $field_args Array of field arguments. |
||
| 1086 | * @param mixed $sub_field_id Sub field ID (if field_group exists). |
||
| 1087 | * @param CMB2_Field|null $field_group If a sub-field, will be the parent group CMB2_Field object. |
||
| 1088 | * @return array Array of CMB2_Field arguments. |
||
| 1089 | */ |
||
| 1090 | 13 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
|
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Get default field arguments specific to this CMB2 object. |
||
| 1110 | * |
||
| 1111 | * @since 2.2.0 |
||
| 1112 | * @param array $field_args Metabox field config array. |
||
| 1113 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent). |
||
| 1114 | * @return array Array of field arguments. |
||
| 1115 | */ |
||
| 1116 | 19 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Get a new field object specific to this CMB2 object. |
||
| 1136 | * |
||
| 1137 | * @since 2.2.0 |
||
| 1138 | * @param array $field_args Metabox field config array. |
||
| 1139 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent). |
||
| 1140 | * @return CMB2_Field CMB2_Field object. |
||
| 1141 | */ |
||
| 1142 | 7 | protected function get_new_field( $field_args, $field_group = null ) { |
|
| 1145 | |||
| 1146 | /** |
||
| 1147 | * When fields are added in the old-school way, intitate them as they should be |
||
| 1148 | * |
||
| 1149 | * @since 2.1.0 |
||
| 1150 | * @param array $fields Array of fields to add. |
||
| 1151 | * @param mixed $parent_field_id Parent field id or null. |
||
| 1152 | */ |
||
| 1153 | 45 | protected function add_fields( $fields, $parent_field_id = null ) { |
|
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Add a field to the metabox |
||
| 1174 | * |
||
| 1175 | * @since 2.0.0 |
||
| 1176 | * @param array $field Metabox field config array. |
||
| 1177 | * @param int $position (optional) Position of metabox. 1 for first, etc. |
||
| 1178 | * @return string|false Field id or false. |
||
| 1179 | */ |
||
| 1180 | 47 | public function add_field( array $field, $position = 0 ) { |
|
| 1217 | |||
| 1218 | /** |
||
| 1219 | * Defines a field's column if requesting to be show in admin columns. |
||
| 1220 | * |
||
| 1221 | * @since 2.2.3 |
||
| 1222 | * @param array $field Metabox field config array. |
||
| 1223 | * @return array Modified metabox field config array. |
||
| 1224 | */ |
||
| 1225 | protected function define_field_column( array $field ) { |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * Add a field to a group |
||
| 1240 | * |
||
| 1241 | * @since 2.0.0 |
||
| 1242 | * @param string $parent_field_id The field id of the group field to add the field. |
||
| 1243 | * @param array $field Metabox field config array. |
||
| 1244 | * @param int $position (optional) Position of metabox. 1 for first, etc. |
||
| 1245 | * @return mixed Array of parent/field ids or false. |
||
| 1246 | */ |
||
| 1247 | 5 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
|
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Add a field array to a fields array in desired position |
||
| 1273 | * |
||
| 1274 | * @since 2.0.2 |
||
| 1275 | * @param array $field Metabox field config array. |
||
| 1276 | * @param array $fields Array (passed by reference) to append the field (array) to. |
||
| 1277 | * @param integer $position Optionally specify a position in the array to be inserted. |
||
| 1278 | */ |
||
| 1279 | 47 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
|
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Remove a field from the metabox |
||
| 1289 | * |
||
| 1290 | * @since 2.0.0 |
||
| 1291 | * @param string $field_id The field id of the field to remove. |
||
| 1292 | * @param string $parent_field_id (optional) The field id of the group field to remove field from. |
||
| 1293 | * @return bool True if field was removed. |
||
| 1294 | */ |
||
| 1295 | 2 | public function remove_field( $field_id, $parent_field_id = '' ) { |
|
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Update or add a property to a field |
||
| 1322 | * |
||
| 1323 | * @since 2.0.0 |
||
| 1324 | * @param string $field_id Field id. |
||
| 1325 | * @param string $property Field property to set/update. |
||
| 1326 | * @param mixed $value Value to set the field property. |
||
| 1327 | * @param string $parent_field_id (optional) The field id of the group field to remove field from. |
||
| 1328 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0). |
||
| 1329 | */ |
||
| 1330 | 4 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
|
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Check if field ids match a field and return the index/field id |
||
| 1350 | * |
||
| 1351 | * @since 2.0.2 |
||
| 1352 | * @param string $field_id Field id. |
||
| 1353 | * @param string $parent_field_id (optional) Parent field id. |
||
| 1354 | * @return mixed Array of field/parent ids, or false. |
||
| 1355 | */ |
||
| 1356 | 19 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
|
| 1383 | |||
| 1384 | /** |
||
| 1385 | * When using the old array filter, it is unlikely field array indexes will be the field id. |
||
| 1386 | * |
||
| 1387 | * @since 2.0.2 |
||
| 1388 | * @param string $field_id The field id. |
||
| 1389 | * @param array $fields Array of fields to search. |
||
| 1390 | * @return mixed Field index or false. |
||
| 1391 | */ |
||
| 1392 | 2 | public function search_old_school_array( $field_id, $fields ) { |
|
| 1397 | |||
| 1398 | /** |
||
| 1399 | * Handles metabox property callbacks, and passes this $cmb object as property. |
||
| 1400 | * |
||
| 1401 | * @since 2.2.3 |
||
| 1402 | * @param callable $cb The callback method/function/closure. |
||
| 1403 | * @return mixed Return of the callback function. |
||
| 1404 | */ |
||
| 1405 | 1 | protected function do_callback( $cb ) { |
|
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Generate a unique nonce field for each registered meta_box |
||
| 1411 | * |
||
| 1412 | * @since 2.0.0 |
||
| 1413 | * @return void |
||
| 1414 | */ |
||
| 1415 | 1 | public function nonce_field() { |
|
| 1418 | |||
| 1419 | /** |
||
| 1420 | * Generate a unique nonce for each registered meta_box |
||
| 1421 | * |
||
| 1422 | * @since 2.0.0 |
||
| 1423 | * @return string unique nonce string. |
||
| 1424 | */ |
||
| 1425 | 1 | public function nonce() { |
|
| 1432 | |||
| 1433 | /** |
||
| 1434 | * Whether this box is an "alternate context" box. This means the box has a 'context' property defined as: |
||
| 1435 | * 'form_top', 'before_permalink', 'after_title', or 'after_editor'. |
||
| 1436 | * |
||
| 1437 | * @since 2.2.4 |
||
| 1438 | * @return bool |
||
| 1439 | */ |
||
| 1440 | 1 | public function is_alternate_context_box() { |
|
| 1443 | |||
| 1444 | /** |
||
| 1445 | * Magic getter for our object. |
||
| 1446 | * |
||
| 1447 | * @param string $property Object property. |
||
| 1448 | * @throws Exception Throws an exception if the field is invalid. |
||
| 1449 | * @return mixed |
||
| 1450 | */ |
||
| 1451 | 48 | public function __get( $property ) { |
|
| 1461 | |||
| 1462 | } |
||
| 1463 |
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.