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 |
||
| 17 | class CMB2 extends CMB2_Base { |
||
|
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * The object properties name. |
||
| 21 | * @var string |
||
| 22 | * @since 2.2.3 |
||
| 23 | */ |
||
| 24 | protected $properties_name = 'meta_box'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Metabox Config array |
||
| 28 | * @var array |
||
| 29 | * @since 0.9.0 |
||
| 30 | */ |
||
| 31 | protected $meta_box = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Type of object registered for metabox. (e.g., post, user, or comment) |
||
| 35 | * @var string |
||
| 36 | * @since 1.0.0 |
||
| 37 | */ |
||
| 38 | protected $mb_object_type = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * List of fields that are changed/updated on save |
||
| 42 | * @var array |
||
| 43 | * @since 1.1.0 |
||
| 44 | */ |
||
| 45 | protected $updated = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Metabox Defaults |
||
| 49 | * @var array |
||
| 50 | * @since 1.0.1 |
||
| 51 | */ |
||
| 52 | protected $mb_defaults = array( |
||
| 53 | 'id' => '', |
||
| 54 | 'title' => '', |
||
| 55 | 'type' => '', |
||
| 56 | 'object_types' => array(), // Post type |
||
| 57 | 'context' => 'normal', |
||
| 58 | 'priority' => 'high', |
||
| 59 | 'show_names' => true, // Show field names on the left |
||
| 60 | 'show_on_cb' => null, // Callback to determine if metabox should display. |
||
| 61 | 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb' |
||
| 62 | 'cmb_styles' => true, // Include CMB2 stylesheet |
||
| 63 | 'enqueue_js' => true, // Include CMB2 JS |
||
| 64 | 'fields' => array(), |
||
| 65 | 'hookup' => true, |
||
| 66 | 'save_fields' => true, // Will not save during hookup if false |
||
| 67 | 'closed' => false, // Default to metabox being closed? |
||
| 68 | 'taxonomies' => array(), |
||
| 69 | 'new_user_section' => 'add-new-user', // or 'add-existing-user' |
||
| 70 | 'new_term_section' => true, |
||
| 71 | ); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Metabox field objects |
||
| 75 | * @var array |
||
| 76 | * @since 2.0.3 |
||
| 77 | */ |
||
| 78 | protected $fields = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * An array of hidden fields to output at the end of the form |
||
| 82 | * @var array |
||
| 83 | * @since 2.0.0 |
||
| 84 | */ |
||
| 85 | protected $hidden_fields = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Array of key => value data for saving. Likely $_POST data. |
||
| 89 | * @var string |
||
| 90 | * @since 2.0.0 |
||
| 91 | */ |
||
| 92 | protected $generated_nonce = ''; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Whether there are fields to be shown in columns. Set in CMB2::add_field(). |
||
| 96 | * @var bool |
||
| 97 | * @since 2.2.2 |
||
| 98 | */ |
||
| 99 | protected $has_columns = false; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * If taxonomy field is requesting to remove_default, we store the taxonomy here. |
||
| 103 | * @var array |
||
| 104 | * @since 2.2.3 |
||
| 105 | */ |
||
| 106 | protected $tax_metaboxes_to_remove = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get started |
||
| 110 | * @since 0.4.0 |
||
| 111 | * @param array $config Metabox config array |
||
| 112 | * @param integer $object_id Optional object id |
||
| 113 | */ |
||
| 114 | 44 | public function __construct( $config, $object_id = 0 ) { |
|
| 115 | |||
| 116 | 44 | if ( empty( $config['id'] ) ) { |
|
| 117 | 1 | wp_die( __( 'Metabox configuration is required to have an ID parameter', 'cmb2' ) ); |
|
| 118 | } |
||
| 119 | |||
| 120 | 44 | $this->meta_box = wp_parse_args( $config, $this->mb_defaults ); |
|
| 121 | 44 | $this->meta_box['fields'] = array(); |
|
| 122 | |||
| 123 | 44 | $this->object_id( $object_id ); |
|
| 124 | 44 | $this->mb_object_type(); |
|
| 125 | 44 | $this->cmb_id = $config['id']; |
|
| 126 | |||
| 127 | 44 | if ( ! empty( $config['fields'] ) && is_array( $config['fields'] ) ) { |
|
| 128 | 41 | $this->add_fields( $config['fields'] ); |
|
| 129 | 41 | } |
|
| 130 | |||
| 131 | 44 | CMB2_Boxes::add( $this ); |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Hook during initiation of CMB2 object |
||
| 135 | * |
||
| 136 | * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. |
||
| 137 | * |
||
| 138 | * @param array $cmb This CMB2 object |
||
| 139 | */ |
||
| 140 | 44 | do_action( "cmb2_init_{$this->cmb_id}", $this ); |
|
| 141 | 44 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Loops through and displays fields |
||
| 145 | * @since 1.0.0 |
||
| 146 | * @param int $object_id Object ID |
||
| 147 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
| 148 | */ |
||
| 149 | 1 | public function show_form( $object_id = 0, $object_type = '' ) { |
|
| 150 | 1 | $this->render_form_open( $object_id, $object_type ); |
|
| 151 | |||
| 152 | 1 | foreach ( $this->prop( 'fields' ) as $field_args ) { |
|
| 153 | 1 | $this->render_field( $field_args ); |
|
| 154 | 1 | } |
|
| 155 | |||
| 156 | 1 | $this->render_form_close( $object_id, $object_type ); |
|
| 157 | 1 | } |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Outputs the opening form markup and runs corresponding hooks: |
||
| 161 | * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
||
| 162 | * @since 2.2.0 |
||
| 163 | * @param integer $object_id Object ID |
||
| 164 | * @param string $object_type Object type |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | 1 | public function render_form_open( $object_id = 0, $object_type = '' ) { |
|
| 168 | 1 | $object_type = $this->object_type( $object_type ); |
|
| 169 | 1 | $object_id = $this->object_id( $object_id ); |
|
| 170 | |||
| 171 | 1 | echo "\n<!-- Begin CMB2 Fields -->\n"; |
|
| 172 | |||
| 173 | 1 | $this->nonce_field(); |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Hook before form table begins |
||
| 177 | * |
||
| 178 | * @param array $cmb_id The current box ID |
||
| 179 | * @param int $object_id The ID of the current object |
||
| 180 | * @param string $object_type The type of object you are working with. |
||
| 181 | * Usually `post` (this applies to all post-types). |
||
| 182 | * Could also be `comment`, `user` or `options-page`. |
||
| 183 | * @param array $cmb This CMB2 object |
||
| 184 | */ |
||
| 185 | 1 | do_action( 'cmb2_before_form', $this->cmb_id, $object_id, $object_type, $this ); |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Hook before form table begins |
||
| 189 | * |
||
| 190 | * The first dynamic portion of the hook name, $object_type, is the type of object |
||
| 191 | * you are working with. Usually `post` (this applies to all post-types). |
||
| 192 | * Could also be `comment`, `user` or `options-page`. |
||
| 193 | * |
||
| 194 | * The second dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
| 195 | * |
||
| 196 | * @param array $cmb_id The current box ID |
||
| 197 | * @param int $object_id The ID of the current object |
||
| 198 | * @param array $cmb This CMB2 object |
||
| 199 | */ |
||
| 200 | 1 | do_action( "cmb2_before_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
|
| 201 | |||
| 202 | 1 | echo '<div class="', $this->box_classes(), '"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">'; |
|
| 203 | |||
| 204 | 1 | } |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Defines the classes for the CMB2 form/wrap. |
||
| 208 | * |
||
| 209 | * @since 2.0.0 |
||
| 210 | * @return string Space concatenated list of classes |
||
| 211 | */ |
||
| 212 | 1 | public function box_classes() { |
|
| 213 | |||
| 214 | 1 | $classes = array( 'cmb2-wrap', 'form-table' ); |
|
| 215 | |||
| 216 | // Use the callback to fetch classes. |
||
| 217 | 1 | View Code Duplication | if ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
| 218 | 1 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
|
| 219 | 1 | $classes = array_merge( $classes, $added_classes ); |
|
| 220 | 1 | } |
|
| 221 | |||
| 222 | 1 | View Code Duplication | if ( $added_classes = $this->prop( 'classes' ) ) { |
| 223 | 1 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
|
| 224 | 1 | $classes = array_merge( $classes, $added_classes ); |
|
| 225 | 1 | } |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Globally filter box wrap classes |
||
| 229 | * |
||
| 230 | * @since 2.2.2 |
||
| 231 | * |
||
| 232 | * @param string $classes Array of classes for the cmb2-wrap. |
||
| 233 | * @param CMB2 $cmb This CMB2 object. |
||
| 234 | */ |
||
| 235 | 1 | $classes = apply_filters( 'cmb2_wrap_classes', $classes, $this ); |
|
| 236 | |||
| 237 | // Clean up. |
||
| 238 | 1 | $classes = array_map( 'strip_tags', array_filter( $classes ) ); |
|
| 239 | |||
| 240 | // Make a string. |
||
| 241 | 1 | return implode( ' ', $classes ); |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Outputs the closing form markup and runs corresponding hooks: |
||
| 246 | * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
||
| 247 | * @since 2.2.0 |
||
| 248 | * @param integer $object_id Object ID |
||
| 249 | * @param string $object_type Object type |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | 1 | public function render_form_close( $object_id = 0, $object_type = '' ) { |
|
| 253 | 1 | $object_type = $this->object_type( $object_type ); |
|
| 254 | 1 | $object_id = $this->object_id( $object_id ); |
|
| 255 | |||
| 256 | 1 | echo '</div></div>'; |
|
| 257 | |||
| 258 | 1 | $this->render_hidden_fields(); |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Hook after form form has been rendered |
||
| 262 | * |
||
| 263 | * @param array $cmb_id The current box ID |
||
| 264 | * @param int $object_id The ID of the current object |
||
| 265 | * @param string $object_type The type of object you are working with. |
||
| 266 | * Usually `post` (this applies to all post-types). |
||
| 267 | * Could also be `comment`, `user` or `options-page`. |
||
| 268 | * @param array $cmb This CMB2 object |
||
| 269 | */ |
||
| 270 | 1 | do_action( 'cmb2_after_form', $this->cmb_id, $object_id, $object_type, $this ); |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Hook after form form has been rendered |
||
| 274 | * |
||
| 275 | * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
| 276 | * |
||
| 277 | * The first dynamic portion of the hook name, $object_type, is the type of object |
||
| 278 | * you are working with. Usually `post` (this applies to all post-types). |
||
| 279 | * Could also be `comment`, `user` or `options-page`. |
||
| 280 | * |
||
| 281 | * @param int $object_id The ID of the current object |
||
| 282 | * @param array $cmb This CMB2 object |
||
| 283 | */ |
||
| 284 | 1 | do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
|
| 285 | |||
| 286 | 1 | echo "\n<!-- End CMB2 Fields -->\n"; |
|
| 287 | |||
| 288 | 1 | } |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Renders a field based on the field type |
||
| 292 | * @since 2.2.0 |
||
| 293 | * @param array $field_args A field configuration array. |
||
| 294 | * @return mixed CMB2_Field object if successful. |
||
| 295 | */ |
||
| 296 | 1 | public function render_field( $field_args ) { |
|
| 297 | 1 | $field_args['context'] = $this->prop( 'context' ); |
|
| 298 | |||
| 299 | 1 | if ( 'group' == $field_args['type'] ) { |
|
| 300 | |||
| 301 | if ( ! isset( $field_args['show_names'] ) ) { |
||
| 302 | $field_args['show_names'] = $this->prop( 'show_names' ); |
||
| 303 | } |
||
| 304 | $field = $this->render_group( $field_args ); |
||
| 305 | |||
| 306 | 1 | } elseif ( 'hidden' == $field_args['type'] && $this->get_field( $field_args )->should_show() ) { |
|
| 307 | // Save rendering for after the metabox |
||
| 308 | $field = $this->add_hidden_field( $field_args ); |
||
| 309 | |||
| 310 | } else { |
||
| 311 | |||
| 312 | 1 | $field_args['show_names'] = $this->prop( 'show_names' ); |
|
| 313 | |||
| 314 | // Render default fields |
||
| 315 | 1 | $field = $this->get_field( $field_args )->render_field(); |
|
| 316 | } |
||
| 317 | |||
| 318 | 1 | return $field; |
|
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Render a repeatable group. |
||
| 323 | * @param array $args Array of field arguments for a group field parent. |
||
| 324 | * @return CMB2_Field|null Group field object. |
||
| 325 | */ |
||
| 326 | 2 | public function render_group( $args ) { |
|
| 327 | |||
| 328 | 2 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
| 329 | return; |
||
| 330 | } |
||
| 331 | |||
| 332 | 2 | $field_group = $this->get_field( $args ); |
|
| 333 | |||
| 334 | // If field is requesting to be conditionally shown |
||
| 335 | 2 | if ( ! $field_group || ! $field_group->should_show() ) { |
|
| 336 | return; |
||
| 337 | } |
||
| 338 | |||
| 339 | 2 | $desc = $field_group->args( 'description' ); |
|
| 340 | 2 | $label = $field_group->args( 'name' ); |
|
| 341 | 2 | $sortable = $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
|
| 342 | 2 | $repeat_class = $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
|
| 343 | 2 | $group_val = (array) $field_group->value(); |
|
| 344 | 2 | $nrows = count( $group_val ); |
|
| 345 | 2 | $remove_disabled = $nrows <= 1 ? 'disabled="disabled" ' : ''; |
|
| 346 | 2 | $field_group->index = 0; |
|
| 347 | |||
| 348 | 2 | $field_group->peform_param_callback( 'before_group' ); |
|
| 349 | |||
| 350 | 2 | echo '<div class="cmb-row cmb-repeat-group-wrap ', $field_group->row_classes(), '" data-fieldtype="group"><div class="cmb-td"><div data-groupid="', $field_group->id(), '" id="', $field_group->id(), '_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group', $sortable, $repeat_class, '" style="width:100%;">'; |
|
| 351 | |||
| 352 | 2 | if ( $desc || $label ) { |
|
| 353 | 2 | $class = $desc ? ' cmb-group-description' : ''; |
|
| 354 | 2 | echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
|
| 355 | 2 | if ( $label ) { |
|
| 356 | 2 | echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
|
| 357 | 2 | } |
|
| 358 | 2 | if ( $desc ) { |
|
| 359 | 1 | echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
|
| 360 | 1 | } |
|
| 361 | 2 | echo '</div></div>'; |
|
| 362 | 2 | } |
|
| 363 | |||
| 364 | 2 | if ( ! empty( $group_val ) ) { |
|
| 365 | |||
| 366 | foreach ( $group_val as $group_key => $field_id ) { |
||
| 367 | $this->render_group_row( $field_group, $remove_disabled ); |
||
| 368 | $field_group->index++; |
||
| 369 | } |
||
| 370 | } else { |
||
| 371 | 2 | $this->render_group_row( $field_group, $remove_disabled ); |
|
| 372 | } |
||
| 373 | |||
| 374 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 375 | 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>'; |
|
| 376 | 1 | } |
|
| 377 | |||
| 378 | 2 | echo '</div></div></div>'; |
|
| 379 | |||
| 380 | 2 | $field_group->peform_param_callback( 'after_group' ); |
|
| 381 | |||
| 382 | 2 | return $field_group; |
|
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Render a repeatable group row |
||
| 387 | * @since 1.0.2 |
||
| 388 | * @param CMB2_Field $field_group CMB2_Field group field object |
||
| 389 | * @param string $remove_disabled Attribute string to disable the remove button |
||
| 390 | */ |
||
| 391 | 2 | public function render_group_row( $field_group, $remove_disabled ) { |
|
| 392 | |||
| 393 | 2 | $field_group->peform_param_callback( 'before_group_row' ); |
|
| 394 | 2 | $closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; |
|
| 395 | |||
| 396 | echo ' |
||
| 397 | 2 | <div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; |
|
| 398 | |||
| 399 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 400 | 1 | echo '<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row"></button>'; |
|
| 401 | 1 | } |
|
| 402 | |||
| 403 | echo ' |
||
| 404 | 2 | <div class="cmbhandle" title="' , __( 'Click to toggle', 'cmb2' ), '"><br></div> |
|
| 405 | 2 | <h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> |
|
| 406 | |||
| 407 | <div class="inside cmb-td cmb-nested cmb-field-list">'; |
||
| 408 | // Loop and render repeatable group fields |
||
| 409 | 2 | foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { |
|
| 410 | 2 | if ( 'hidden' == $field_args['type'] ) { |
|
| 411 | |||
| 412 | // Save rendering for after the metabox |
||
| 413 | $this->add_hidden_field( $field_args, $field_group ); |
||
| 414 | |||
| 415 | } else { |
||
| 416 | |||
| 417 | 2 | $field_args['show_names'] = $field_group->args( 'show_names' ); |
|
| 418 | 2 | $field_args['context'] = $field_group->args( 'context' ); |
|
| 419 | |||
| 420 | 2 | $field = $this->get_field( $field_args, $field_group )->render_field(); |
|
| 421 | } |
||
| 422 | 2 | } |
|
| 423 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 424 | echo ' |
||
| 425 | <div class="cmb-row cmb-remove-field-row"> |
||
| 426 | <div class="cmb-remove-row"> |
||
| 427 | 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> |
|
| 428 | </div> |
||
| 429 | </div> |
||
| 430 | '; |
||
| 431 | 1 | } |
|
| 432 | echo ' |
||
| 433 | </div> |
||
| 434 | </div> |
||
| 435 | 2 | '; |
|
| 436 | |||
| 437 | 2 | $field_group->peform_param_callback( 'after_group_row' ); |
|
| 438 | 2 | } |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Add a hidden field to the list of hidden fields to be rendered later |
||
| 442 | * @since 2.0.0 |
||
| 443 | * @param array $field_args Array of field arguments to be passed to CMB2_Field |
||
| 444 | */ |
||
| 445 | public function add_hidden_field( $field_args, $field_group = null ) { |
||
| 446 | if ( isset( $field_args['field_args'] ) ) { |
||
| 447 | // For back-compatibility. |
||
| 448 | $field = new CMB2_Field( $field_args ); |
||
| 449 | } else { |
||
| 450 | $field = $this->get_new_field( $field_args, $field_group ); |
||
| 451 | } |
||
| 452 | |||
| 453 | $type = new CMB2_Types( $field ); |
||
| 454 | |||
| 455 | if ( $field_group ) { |
||
| 456 | $type->iterator = $field_group->index; |
||
| 457 | } |
||
| 458 | |||
| 459 | $this->hidden_fields[] = $type; |
||
| 460 | |||
| 461 | return $field; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Loop through and output hidden fields |
||
| 466 | * @since 2.0.0 |
||
| 467 | */ |
||
| 468 | 1 | public function render_hidden_fields() { |
|
| 469 | 1 | if ( ! empty( $this->hidden_fields ) ) { |
|
| 470 | foreach ( $this->hidden_fields as $hidden ) { |
||
| 471 | $hidden->render(); |
||
| 472 | } |
||
| 473 | } |
||
| 474 | 1 | } |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Returns array of sanitized field values (without saving them) |
||
| 478 | * @since 2.0.3 |
||
| 479 | * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
||
| 480 | */ |
||
| 481 | 2 | public function get_sanitized_values( array $data_to_sanitize ) { |
|
| 482 | 2 | $this->data_to_save = $data_to_sanitize; |
|
| 483 | 2 | $stored_id = $this->object_id(); |
|
| 484 | |||
| 485 | // We do this So CMB will sanitize our data for us, but not save it |
||
| 486 | 2 | $this->object_id( '_' ); |
|
| 487 | |||
| 488 | // Ensure temp. data store is empty |
||
| 489 | 2 | cmb2_options( 0 )->set(); |
|
| 490 | |||
| 491 | // Process/save fields |
||
| 492 | 2 | $this->process_fields(); |
|
| 493 | |||
| 494 | // Get data from temp. data store |
||
| 495 | 2 | $sanitized_values = cmb2_options( 0 )->get_options(); |
|
| 496 | |||
| 497 | // Empty out temp. data store again |
||
| 498 | 2 | cmb2_options( 0 )->set(); |
|
| 499 | |||
| 500 | // Reset the object id |
||
| 501 | 2 | $this->object_id( $stored_id ); |
|
| 502 | |||
| 503 | 2 | return $sanitized_values; |
|
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Loops through and saves field data |
||
| 508 | * @since 1.0.0 |
||
| 509 | * @param int $object_id Object ID |
||
| 510 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
| 511 | * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
||
| 512 | */ |
||
| 513 | 1 | public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
| 514 | |||
| 515 | // Fall-back to $_POST data |
||
| 516 | 1 | $this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST; |
|
| 517 | 1 | $object_id = $this->object_id( $object_id ); |
|
| 518 | 1 | $object_type = $this->object_type( $object_type ); |
|
| 519 | |||
| 520 | 1 | $this->process_fields(); |
|
| 521 | |||
| 522 | // If options page, save the updated options |
||
| 523 | 1 | if ( 'options-page' == $object_type ) { |
|
| 524 | 1 | cmb2_options( $object_id )->set(); |
|
| 525 | 1 | } |
|
| 526 | |||
| 527 | 1 | $this->after_save(); |
|
| 528 | 1 | } |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Process and save form fields |
||
| 532 | * @since 2.0.0 |
||
| 533 | */ |
||
| 534 | 3 | public function process_fields() { |
|
| 535 | |||
| 536 | 3 | $this->pre_process(); |
|
| 537 | |||
| 538 | // Remove the show_on properties so saving works |
||
| 539 | 3 | $this->prop( 'show_on', array() ); |
|
| 540 | |||
| 541 | // save field ids of those that are updated |
||
| 542 | 3 | $this->updated = array(); |
|
| 543 | |||
| 544 | 3 | foreach ( $this->prop( 'fields' ) as $field_args ) { |
|
| 545 | 3 | $this->process_field( $field_args ); |
|
| 546 | 3 | } |
|
| 547 | 3 | } |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Process and save a field |
||
| 551 | * @since 2.0.0 |
||
| 552 | * @param array $field_args Array of field arguments |
||
| 553 | */ |
||
| 554 | 3 | public function process_field( $field_args ) { |
|
| 581 | |||
| 582 | 3 | public function pre_process() { |
|
| 597 | |||
| 598 | 1 | public function after_save() { |
|
| 599 | 1 | $object_type = $this->object_type(); |
|
| 600 | 1 | $object_id = $this->object_id(); |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Fires after all fields have been saved. |
||
| 604 | * |
||
| 605 | * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
||
| 606 | * Usually `post` (this applies to all post-types). |
||
| 607 | * Could also be `comment`, `user` or `options-page`. |
||
| 608 | * |
||
| 609 | * @param int $object_id The ID of the current object |
||
| 610 | * @param array $cmb_id The current box ID |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Save a repeatable group |
||
| 636 | * @since 1.x.x |
||
| 637 | * @param array $args Field arguments array |
||
| 638 | * @return mixed Return of CMB2_Field::update_data() |
||
| 639 | */ |
||
| 640 | 1 | public function save_group( $args ) { |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Save a repeatable group |
||
| 650 | * @since 1.x.x |
||
| 651 | * @param array $field_group CMB2_Field group field object |
||
| 652 | * @return mixed Return of CMB2_Field::update_data() |
||
| 653 | */ |
||
| 654 | 1 | public function save_group_field( $field_group ) { |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Get object id from global space if no id is provided |
||
| 730 | * @since 1.0.0 |
||
| 731 | * @param integer $object_id Object ID |
||
| 732 | * @return integer $object_id Object ID |
||
| 733 | */ |
||
| 734 | 48 | public function object_id( $object_id = 0 ) { |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Sets the $object_type based on metabox settings |
||
| 776 | * @since 1.0.0 |
||
| 777 | * @return string Object type |
||
| 778 | */ |
||
| 779 | 44 | public function mb_object_type() { |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Determines if metabox is for an options page |
||
| 836 | * @since 1.0.1 |
||
| 837 | * @return boolean True/False |
||
| 838 | */ |
||
| 839 | 44 | public function is_options_page_mb() { |
|
| 842 | |||
| 843 | /** |
||
| 844 | * Returns the object type |
||
| 845 | * @since 1.0.0 |
||
| 846 | * @return string Object type |
||
| 847 | */ |
||
| 848 | 48 | public function object_type( $object_type = '' ) { |
|
| 862 | |||
| 863 | /** |
||
| 864 | * Get the object type for the current page, based on the $pagenow global. |
||
| 865 | * @since 2.2.2 |
||
| 866 | * @return string Page object type name. |
||
| 867 | */ |
||
| 868 | View Code Duplication | public function current_object_type() { |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Set metabox property. |
||
| 889 | * @since 2.2.2 |
||
| 890 | * @param string $property Metabox config property to retrieve |
||
| 891 | * @param mixed $value Value to set if no value found |
||
| 892 | * @return mixed Metabox config property value or false |
||
| 893 | */ |
||
| 894 | 1 | public function set_prop( $property, $value ) { |
|
| 899 | |||
| 900 | /** |
||
| 901 | * Get metabox property and optionally set a fallback |
||
| 902 | * @since 2.0.0 |
||
| 903 | * @param string $property Metabox config property to retrieve |
||
| 904 | * @param mixed $fallback Fallback value to set if no value found |
||
| 905 | * @return mixed Metabox config property value or false |
||
| 906 | */ |
||
| 907 | 44 | public function prop( $property, $fallback = null ) { |
|
| 914 | |||
| 915 | /** |
||
| 916 | * Get a field object |
||
| 917 | * @since 2.0.3 |
||
| 918 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
||
| 919 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 920 | * @return CMB2_Field|false CMB2_Field object (or false) |
||
| 921 | */ |
||
| 922 | 15 | public function get_field( $field, $field_group = null ) { |
|
| 947 | |||
| 948 | /** |
||
| 949 | * Handles determining which type of arguments to pass to CMB2_Field |
||
| 950 | * @since 2.0.7 |
||
| 951 | * @param mixed $field_id Field (or group field) ID |
||
| 952 | * @param mixed $field_args Array of field arguments |
||
| 953 | * @param mixed $sub_field_id Sub field ID (if field_group exists) |
||
| 954 | * @param mixed $field_group If a sub-field, will be the parent group CMB2_Field object |
||
| 955 | * @return array Array of CMB2_Field arguments |
||
| 956 | */ |
||
| 957 | 13 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
|
| 974 | |||
| 975 | /** |
||
| 976 | * Get default field arguments specific to this CMB2 object. |
||
| 977 | * @since 2.2.0 |
||
| 978 | * @param array $field_args Metabox field config array. |
||
| 979 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 980 | * @return array Array of field arguments. |
||
| 981 | */ |
||
| 982 | 17 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
| 999 | |||
| 1000 | /** |
||
| 1001 | * Get a new field object specific to this CMB2 object. |
||
| 1002 | * @since 2.2.0 |
||
| 1003 | * @param array $field_args Metabox field config array. |
||
| 1004 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 1005 | * @return CMB2_Field CMB2_Field object |
||
| 1006 | */ |
||
| 1007 | 5 | protected function get_new_field( $field_args, $field_group = null ) { |
|
| 1010 | |||
| 1011 | /** |
||
| 1012 | * When fields are added in the old-school way, intitate them as they should be |
||
| 1013 | * @since 2.1.0 |
||
| 1014 | * @param array $fields Array of fields to add |
||
| 1015 | * @param mixed $parent_field_id Parent field id or null |
||
| 1016 | */ |
||
| 1017 | 41 | protected function add_fields( $fields, $parent_field_id = null ) { |
|
| 1035 | |||
| 1036 | /** |
||
| 1037 | * Add a field to the metabox |
||
| 1038 | * @since 2.0.0 |
||
| 1039 | * @param array $field Metabox field config array |
||
| 1040 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 1041 | * @return mixed Field id or false |
||
| 1042 | */ |
||
| 1043 | 43 | public function add_field( array $field, $position = 0 ) { |
|
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Defines a field's column if requesting to be show in admin columns. |
||
| 1072 | * @since 2.2.3 |
||
| 1073 | * @param array $field Metabox field config array. |
||
| 1074 | * @return array Modified metabox field config array. |
||
| 1075 | */ |
||
| 1076 | protected function define_field_column( array $field ) { |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Add a field to a group |
||
| 1091 | * @since 2.0.0 |
||
| 1092 | * @param string $parent_field_id The field id of the group field to add the field |
||
| 1093 | * @param array $field Metabox field config array |
||
| 1094 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 1095 | * @return mixed Array of parent/field ids or false |
||
| 1096 | */ |
||
| 1097 | 3 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
|
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Add a field array to a fields array in desired position |
||
| 1123 | * @since 2.0.2 |
||
| 1124 | * @param array $field Metabox field config array |
||
| 1125 | * @param array &$fields Array (passed by reference) to append the field (array) to |
||
| 1126 | * @param integer $position Optionally specify a position in the array to be inserted |
||
| 1127 | */ |
||
| 1128 | 43 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
|
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Remove a field from the metabox |
||
| 1138 | * @since 2.0.0 |
||
| 1139 | * @param string $field_id The field id of the field to remove |
||
| 1140 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1141 | * @return bool True if field was removed |
||
| 1142 | */ |
||
| 1143 | 2 | public function remove_field( $field_id, $parent_field_id = '' ) { |
|
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Update or add a property to a field |
||
| 1170 | * @since 2.0.0 |
||
| 1171 | * @param string $field_id Field id |
||
| 1172 | * @param string $property Field property to set/update |
||
| 1173 | * @param mixed $value Value to set the field property |
||
| 1174 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1175 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
||
| 1176 | */ |
||
| 1177 | 4 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
|
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Check if field ids match a field and return the index/field id |
||
| 1197 | * @since 2.0.2 |
||
| 1198 | * @param string $field_id Field id |
||
| 1199 | * @param string $parent_field_id (optional) Parent field id |
||
| 1200 | * @return mixed Array of field/parent ids, or false |
||
| 1201 | */ |
||
| 1202 | 19 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
|
| 1229 | |||
| 1230 | /** |
||
| 1231 | * When using the old array filter, it is unlikely field array indexes will be the field id |
||
| 1232 | * @since 2.0.2 |
||
| 1233 | * @param string $field_id The field id |
||
| 1234 | * @param array $fields Array of fields to search |
||
| 1235 | * @return mixed Field index or false |
||
| 1236 | */ |
||
| 1237 | 2 | public function search_old_school_array( $field_id, $fields ) { |
|
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Handles metabox property callbacks, and passes this $cmb object as property. |
||
| 1245 | * @since 2.2.3 |
||
| 1246 | * @param callable $cb The callback method/function/closure |
||
| 1247 | * @return mixed Return of the callback function. |
||
| 1248 | */ |
||
| 1249 | 1 | protected function do_callback( $cb ) { |
|
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Generate a unique nonce field for each registered meta_box |
||
| 1255 | * @since 2.0.0 |
||
| 1256 | * @return string unique nonce hidden input |
||
| 1257 | */ |
||
| 1258 | 1 | public function nonce_field() { |
|
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Generate a unique nonce for each registered meta_box |
||
| 1264 | * @since 2.0.0 |
||
| 1265 | * @return string unique nonce string |
||
| 1266 | */ |
||
| 1267 | 1 | public function nonce() { |
|
| 1274 | |||
| 1275 | /** |
||
| 1276 | * Magic getter for our object. |
||
| 1277 | * @param string $field |
||
| 1278 | * @throws Exception Throws an exception if the field is invalid. |
||
| 1279 | * @return mixed |
||
| 1280 | */ |
||
| 1281 | 44 | public function __get( $field ) { |
|
| 1291 | |||
| 1292 | } |
||
| 1293 |
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.