Complex classes like CMB2_Sanitize 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_Sanitize, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class CMB2_Sanitize { |
||
|
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * A CMB field object |
||
| 19 | * @var CMB2_Field object |
||
| 20 | */ |
||
| 21 | public $field; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Field's value |
||
| 25 | * @var mixed |
||
| 26 | */ |
||
| 27 | public $value; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Setup our class vars |
||
| 31 | * @since 1.1.0 |
||
| 32 | * @param CMB2_Field $field A CMB2 field object |
||
| 33 | * @param mixed $value Field value |
||
| 34 | */ |
||
| 35 | 8 | public function __construct( CMB2_Field $field, $value ) { |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Catchall method if field's 'sanitization_cb' is NOT defined, or field type does not have a corresponding validation method |
||
| 42 | * @since 1.0.0 |
||
| 43 | * @param string $name Non-existent method name |
||
| 44 | * @param array $arguments All arguments passed to the method |
||
| 45 | */ |
||
| 46 | 7 | public function __call( $name, $arguments ) { |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Default fallback sanitization method. Applies filters. |
||
| 52 | * @since 1.0.2 |
||
| 53 | */ |
||
| 54 | 7 | public function default_sanitization() { |
|
| 55 | |||
| 56 | /** |
||
| 57 | * This exists for back-compatibility, but validation |
||
| 58 | * is not what happens here. |
||
| 59 | * @deprecated See documentation for "cmb2_sanitize_{$this->type()}". |
||
| 60 | */ |
||
| 61 | 7 | $override_value = apply_filters( "cmb2_validate_{$this->field->type()}", null, $this->value, $this->field->object_id, $this->field->args(), $this ); |
|
| 62 | |||
| 63 | 7 | if ( null !== $override_value ) { |
|
| 64 | return $override_value; |
||
| 65 | } |
||
| 66 | |||
| 67 | 7 | $sanitized_value = ''; |
|
| 68 | 7 | switch ( $this->field->type() ) { |
|
| 69 | 7 | case 'wysiwyg': |
|
| 70 | 7 | case 'textarea_small': |
|
| 71 | 1 | $sanitized_value = $this->textarea(); |
|
| 72 | 1 | break; |
|
| 73 | 7 | case 'taxonomy_select': |
|
| 74 | 7 | case 'taxonomy_radio': |
|
| 75 | 7 | case 'taxonomy_radio_inline': |
|
| 76 | 7 | case 'taxonomy_multicheck': |
|
| 77 | 7 | case 'taxonomy_multicheck_inline': |
|
| 78 | if ( $this->field->args( 'taxonomy' ) ) { |
||
| 79 | wp_set_object_terms( $this->field->object_id, $this->value, $this->field->args( 'taxonomy' ) ); |
||
| 80 | } else { |
||
| 81 | cmb2_utils()->log_if_debug( __METHOD__, __LINE__, "{$this->field->type()} {$this->field->_id()} is missing the 'taxonomy' parameter." ); |
||
| 82 | } |
||
| 83 | break; |
||
| 84 | 7 | case 'multicheck': |
|
| 85 | 7 | case 'multicheck_inline': |
|
| 86 | 7 | case 'file_list': |
|
| 87 | 7 | case 'oembed': |
|
| 88 | 7 | case 'group': |
|
| 89 | // no filtering |
||
| 90 | 2 | $sanitized_value = $this->value; |
|
| 91 | 2 | break; |
|
| 92 | 6 | default: |
|
| 93 | // Handle repeatable fields array |
||
| 94 | // We'll fallback to 'sanitize_text_field' |
||
| 95 | 6 | $sanitized_value = is_array( $this->value ) ? array_map( 'sanitize_text_field', $this->value ) : call_user_func( 'sanitize_text_field', $this->value ); |
|
| 96 | 6 | break; |
|
| 97 | 7 | } |
|
| 98 | |||
| 99 | 7 | return $this->_is_empty_array( $sanitized_value ) ? '' : $sanitized_value; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Simple checkbox validation |
||
| 104 | * @since 1.0.1 |
||
| 105 | * @return string|false 'on' or false |
||
| 106 | */ |
||
| 107 | public function checkbox() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Validate url in a meta value |
||
| 113 | * @since 1.0.1 |
||
| 114 | * @return string Empty string or escaped url |
||
| 115 | */ |
||
| 116 | 1 | public function text_url() { |
|
| 129 | |||
| 130 | public function colorpicker() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Validate email in a meta value |
||
| 148 | * @since 1.0.1 |
||
| 149 | * @return string Empty string or sanitized email |
||
| 150 | */ |
||
| 151 | public function text_email() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Validate money in a meta value |
||
| 168 | * @since 1.0.1 |
||
| 169 | * @return string Empty string or sanitized money value |
||
| 170 | */ |
||
| 171 | public function text_money() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Converts text date to timestamp |
||
| 198 | * @since 1.0.2 |
||
| 199 | * @return string Timestring |
||
| 200 | */ |
||
| 201 | public function text_date_timestamp() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Datetime to timestamp |
||
| 209 | * @since 1.0.1 |
||
| 210 | * @return string Timestring |
||
| 211 | */ |
||
| 212 | public function text_datetime_timestamp( $repeat = false ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Datetime to imestamp with timezone |
||
| 237 | * @since 1.0.1 |
||
| 238 | * @return string Timestring |
||
| 239 | */ |
||
| 240 | 1 | public function text_datetime_timestamp_timezone( $repeat = false ) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Sanitize textareas and wysiwyg fields |
||
| 323 | * @since 1.0.1 |
||
| 324 | * @return string Sanitized data |
||
| 325 | */ |
||
| 326 | 1 | public function textarea() { |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Sanitize code textareas |
||
| 332 | * @since 1.0.2 |
||
| 333 | * @return string Sanitized data |
||
| 334 | */ |
||
| 335 | public function textarea_code( $repeat = false ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Handles saving of attachment post ID and sanitizing file url |
||
| 346 | * @since 1.1.0 |
||
| 347 | * @return string Sanitized url |
||
| 348 | */ |
||
| 349 | 1 | public function file() { |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Gets the values for the `file` field type from the data being saved. |
||
| 365 | * @since 2.2.0 |
||
| 366 | */ |
||
| 367 | 1 | public function _get_group_file_value_array( $id_key ) { |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Peforms saving of `file` attachement's ID |
||
| 386 | * @since 1.1.0 |
||
| 387 | */ |
||
| 388 | public function _save_file_id_value( $file_id_key ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Peforms saving of `text_datetime_timestamp_timezone` utc timestamp |
||
| 406 | * @since 2.2.0 |
||
| 407 | */ |
||
| 408 | public function _save_utc_value( $utc_key, $utc_stamp ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Returns a new, supporting CMB2_Field object based on a new field id. |
||
| 414 | * @since 2.2.0 |
||
| 415 | */ |
||
| 416 | public function _new_supporting_field( $new_field_id ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * If repeating, loop through and re-apply sanitization method |
||
| 434 | * @since 1.1.0 |
||
| 435 | * @param string $method Class method |
||
| 436 | * @param bool $repeat Whether repeating or not |
||
| 437 | * @return mixed Sanitized value |
||
| 438 | */ |
||
| 439 | 1 | public function _check_repeat( $method, $repeat ) { |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Determine if passed value is an empty array |
||
| 463 | * @since 2.0.6 |
||
| 464 | * @param mixed $to_check Value to check |
||
| 465 | * @return boolean Whether value is an array that's empty |
||
| 466 | */ |
||
| 467 | 7 | public function _is_empty_array( $to_check ) { |
|
| 474 | |||
| 475 | } |
||
| 476 |
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.