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 | 14 | 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 | 11 | public function __call( $name, $arguments ) { |
|
49 | |||
50 | /** |
||
51 | * Default fallback sanitization method. Applies filters. |
||
52 | * @since 1.0.2 |
||
53 | */ |
||
54 | 11 | 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 | 11 | if ( function_exists( 'apply_filters_deprecated' ) ) { |
|
62 | $override_value = apply_filters_deprecated( "cmb2_validate_{$this->field->type()}", array( null, $this->value, $this->field->object_id, $this->field->args(), $this ), '2.0.0', "cmb2_sanitize_{$this->field->type()}" ); |
||
63 | } else { |
||
64 | 11 | $override_value = apply_filters( "cmb2_validate_{$this->field->type()}", null, $this->value, $this->field->object_id, $this->field->args(), $this ); |
|
65 | } |
||
66 | |||
67 | 11 | if ( null !== $override_value ) { |
|
68 | return $override_value; |
||
69 | } |
||
70 | |||
71 | 11 | $sanitized_value = ''; |
|
72 | 11 | switch ( $this->field->type() ) { |
|
73 | 11 | case 'wysiwyg': |
|
74 | 11 | case 'textarea_small': |
|
75 | 11 | case 'oembed': |
|
76 | 2 | $sanitized_value = $this->textarea(); |
|
77 | 2 | break; |
|
78 | 11 | case 'taxonomy_select': |
|
79 | 11 | case 'taxonomy_radio': |
|
80 | 11 | case 'taxonomy_radio_inline': |
|
81 | 11 | case 'taxonomy_multicheck': |
|
82 | 11 | case 'taxonomy_multicheck_inline': |
|
83 | 1 | $sanitized_value = $this->_taxonomy(); |
|
84 | 1 | break; |
|
85 | 11 | case 'multicheck': |
|
86 | 11 | case 'multicheck_inline': |
|
87 | 11 | case 'file_list': |
|
88 | 11 | case 'group': |
|
89 | // no filtering |
||
90 | 2 | $sanitized_value = $this->value; |
|
91 | 2 | break; |
|
92 | 10 | default: |
|
93 | // Handle repeatable fields array |
||
94 | // We'll fallback to 'sanitize_text_field' |
||
95 | 10 | $sanitized_value = $this->_default_sanitization(); |
|
96 | 10 | break; |
|
97 | 11 | } |
|
98 | |||
99 | 11 | return $this->_is_empty_array( $sanitized_value ) ? '' : $sanitized_value; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Default sanitization method, sanitize_text_field. Checks if value is array. |
||
104 | * @since 2.2.4 |
||
105 | * @return mixed Sanitized value. |
||
106 | */ |
||
107 | 10 | protected function _default_sanitization() { |
|
111 | |||
112 | /** |
||
113 | * Sets the object terms to the object (if not options-page) and optionally returns the sanitized term values. |
||
114 | * @since 2.2.4 |
||
115 | * @return mixed Blank value, or sanitized term values if "cmb2_return_taxonomy_values_{$cmb_id}" is true. |
||
116 | */ |
||
117 | 1 | protected function _taxonomy() { |
|
118 | 1 | $sanitized_value = ''; |
|
119 | |||
120 | 1 | if ( ! $this->field->args( 'taxonomy' ) ) { |
|
121 | CMB2_Utils::log_if_debug( __METHOD__, __LINE__, "{$this->field->type()} {$this->field->_id()} is missing the 'taxonomy' parameter." ); |
||
122 | } else { |
||
123 | |||
124 | 1 | if ( 'options-page' !== $this->field->object_type ) { |
|
125 | 1 | $return_values = true; |
|
126 | 1 | } else { |
|
127 | wp_set_object_terms( $this->field->object_id, $this->value, $this->field->args( 'taxonomy' ) ); |
||
128 | $return_values = false; |
||
129 | } |
||
130 | |||
131 | 1 | $cmb_id = $this->field->cmb_id; |
|
132 | |||
133 | /** |
||
134 | * Filter whether 'taxonomy_*' fields should return their value when being sanitized. |
||
135 | * |
||
136 | * By default, these fields do not return a value as we do not want them stored to meta |
||
137 | * (as they are stored as terms). This allows overriding that and is used by CMB2::get_sanitized_values(). |
||
138 | * |
||
139 | * The dynamic portion of the hook, $cmb_id, refers to the this field's CMB2 box id. |
||
140 | * |
||
141 | * @since 2.2.4 |
||
142 | * |
||
143 | * @param bool $return_values By default, this is only true for 'options-page' boxes. To enable: |
||
144 | * `add_filter( "cmb2_return_taxonomy_values_{$cmb_id}", '__return_true' );` |
||
145 | * @param CMB2_Sanitize $sanitizer This object. |
||
146 | */ |
||
147 | 1 | if ( apply_filters( "cmb2_return_taxonomy_values_{$cmb_id}", $return_values, $this ) ) { |
|
148 | 1 | $sanitized_value = $this->_default_sanitization(); |
|
149 | 1 | } |
|
150 | } |
||
151 | |||
152 | 1 | return $sanitized_value; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Simple checkbox validation |
||
157 | * @since 1.0.1 |
||
158 | * @return string|false 'on' or false |
||
159 | */ |
||
160 | public function checkbox() { |
||
163 | |||
164 | /** |
||
165 | * Validate url in a meta value |
||
166 | * @since 1.0.1 |
||
167 | * @return string Empty string or escaped url |
||
168 | */ |
||
169 | 1 | public function text_url() { |
|
182 | |||
183 | public function colorpicker() { |
||
198 | |||
199 | /** |
||
200 | * Validate email in a meta value |
||
201 | * @since 1.0.1 |
||
202 | * @return string Empty string or sanitized email |
||
203 | */ |
||
204 | public function text_email() { |
||
218 | |||
219 | /** |
||
220 | * Validate money in a meta value |
||
221 | * @since 1.0.1 |
||
222 | * @return string Empty string or sanitized money value |
||
223 | */ |
||
224 | 1 | public function text_money() { |
|
247 | |||
248 | /** |
||
249 | * Converts text date to timestamp |
||
250 | * @since 1.0.2 |
||
251 | * @return string Timestring |
||
252 | */ |
||
253 | public function text_date_timestamp() { |
||
258 | |||
259 | /** |
||
260 | * Datetime to timestamp |
||
261 | * @since 1.0.1 |
||
262 | * @return string|array Timestring |
||
263 | */ |
||
264 | public function text_datetime_timestamp( $repeat = false ) { |
||
286 | |||
287 | /** |
||
288 | * Datetime to timestamp with timezone |
||
289 | * @since 1.0.1 |
||
290 | * @return string Timestring |
||
291 | */ |
||
292 | 2 | public function text_datetime_timestamp_timezone( $repeat = false ) { |
|
372 | |||
373 | /** |
||
374 | * Sanitize textareas and wysiwyg fields |
||
375 | * @since 1.0.1 |
||
376 | * @return string Sanitized data |
||
377 | */ |
||
378 | 2 | public function textarea() { |
|
381 | |||
382 | /** |
||
383 | * Sanitize code textareas |
||
384 | * @since 1.0.2 |
||
385 | * @return string Sanitized data |
||
386 | */ |
||
387 | public function textarea_code( $repeat = false ) { |
||
395 | |||
396 | /** |
||
397 | * Handles saving of attachment post ID and sanitizing file url |
||
398 | * @since 1.1.0 |
||
399 | * @return string Sanitized url |
||
400 | */ |
||
401 | 1 | public function file() { |
|
414 | |||
415 | /** |
||
416 | * Gets the values for the `file` field type from the data being saved. |
||
417 | * @since 2.2.0 |
||
418 | */ |
||
419 | 1 | public function _get_group_file_value_array( $id_key ) { |
|
435 | |||
436 | /** |
||
437 | * Peforms saving of `file` attachement's ID |
||
438 | * @since 1.1.0 |
||
439 | */ |
||
440 | public function _save_file_id_value( $file_id_key ) { |
||
455 | |||
456 | /** |
||
457 | * Peforms saving of `text_datetime_timestamp_timezone` utc timestamp |
||
458 | * @since 2.2.0 |
||
459 | */ |
||
460 | 1 | public function _save_utc_value( $utc_key, $utc_stamp ) { |
|
463 | |||
464 | /** |
||
465 | * Returns a new, supporting, CMB2_Field object based on a new field id. |
||
466 | * @since 2.2.0 |
||
467 | */ |
||
468 | 1 | public function _new_supporting_field( $new_field_id ) { |
|
474 | |||
475 | /** |
||
476 | * If repeating, loop through and re-apply sanitization method |
||
477 | * @since 1.1.0 |
||
478 | * @param string $method Class method |
||
479 | * @param bool $repeat Whether repeating or not |
||
480 | * @return mixed Sanitized value |
||
481 | */ |
||
482 | 2 | public function _check_repeat( $method, $repeat ) { |
|
503 | |||
504 | /** |
||
505 | * Determine if passed value is an empty array |
||
506 | * @since 2.0.6 |
||
507 | * @param mixed $to_check Value to check |
||
508 | * @return boolean Whether value is an array that's empty |
||
509 | */ |
||
510 | 11 | public function _is_empty_array( $to_check ) { |
|
517 | |||
518 | } |
||
519 |
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.