Complex classes like CMB2_Utils 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_Utils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class CMB2_Utils { |
||
|
|||
14 | |||
15 | /** |
||
16 | * The WordPress ABSPATH constant. |
||
17 | * @var string |
||
18 | * @since 2.2.3 |
||
19 | */ |
||
20 | protected static $ABSPATH = ABSPATH; |
||
21 | |||
22 | /** |
||
23 | * The url which is used to load local resources. |
||
24 | * @var string |
||
25 | * @since 2.0.0 |
||
26 | */ |
||
27 | protected static $url = ''; |
||
28 | |||
29 | /** |
||
30 | * Utility method that attempts to get an attachment's ID by it's url |
||
31 | * @since 1.0.0 |
||
32 | * @param string $img_url Attachment url |
||
33 | * @return int|false Attachment ID or false |
||
34 | */ |
||
35 | 1 | public static function image_id_from_url( $img_url ) { |
|
77 | |||
78 | /** |
||
79 | * Utility method that returns time string offset by timezone |
||
80 | * @since 1.0.0 |
||
81 | * @param string $tzstring Time string |
||
82 | * @return string Offset time string |
||
83 | */ |
||
84 | 2 | public static function timezone_offset( $tzstring ) { |
|
85 | 2 | $tz_offset = 0; |
|
86 | |||
87 | 2 | if ( ! empty( $tzstring ) && is_string( $tzstring ) ) { |
|
88 | 2 | if ( 'UTC' === substr( $tzstring, 0, 3 ) ) { |
|
89 | 1 | $tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring ); |
|
90 | 1 | return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS ); |
|
91 | } |
||
92 | |||
93 | try { |
||
94 | 1 | $date_time_zone_selected = new DateTimeZone( $tzstring ); |
|
95 | 1 | $tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() ); |
|
96 | 1 | } catch ( Exception $e ) { |
|
97 | self::log_if_debug( __METHOD__, __LINE__, $e->getMessage() ); |
||
98 | } |
||
99 | |||
100 | 1 | } |
|
101 | |||
102 | 1 | return $tz_offset; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Utility method that returns a timezone string representing the default timezone for the site. |
||
107 | * |
||
108 | * Roughly copied from WordPress, as get_option('timezone_string') will return |
||
109 | * an empty string if no value has been set on the options page. |
||
110 | * A timezone string is required by the wp_timezone_choice() used by the |
||
111 | * select_timezone field. |
||
112 | * |
||
113 | * @since 1.0.0 |
||
114 | * @return string Timezone string |
||
115 | */ |
||
116 | 1 | public static function timezone_string() { |
|
117 | 1 | $current_offset = get_option( 'gmt_offset' ); |
|
118 | 1 | $tzstring = get_option( 'timezone_string' ); |
|
119 | |||
120 | // Remove old Etc mappings. Fallback to gmt_offset. |
||
121 | 1 | if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) { |
|
122 | $tzstring = ''; |
||
123 | } |
||
124 | |||
125 | 1 | if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists |
|
126 | 1 | if ( 0 == $current_offset ) { |
|
127 | 1 | $tzstring = 'UTC+0'; |
|
128 | 1 | } elseif ( $current_offset < 0 ) { |
|
129 | $tzstring = 'UTC' . $current_offset; |
||
130 | } else { |
||
131 | $tzstring = 'UTC+' . $current_offset; |
||
132 | } |
||
133 | 1 | } |
|
134 | |||
135 | 1 | return $tzstring; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Returns a timestamp, first checking if value already is a timestamp. |
||
140 | * @since 2.0.0 |
||
141 | * @param string|int $string Possible timestamp string |
||
142 | * @return int Time stamp |
||
143 | */ |
||
144 | 10 | public static function make_valid_time_stamp( $string ) { |
|
145 | 10 | if ( ! $string ) { |
|
146 | return 0; |
||
147 | } |
||
148 | |||
149 | 10 | return self::is_valid_time_stamp( $string ) |
|
150 | 10 | ? (int) $string : |
|
151 | 10 | strtotime( (string) $string ); |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Determine if a value is a valid timestamp |
||
156 | * @since 2.0.0 |
||
157 | * @param mixed $timestamp Value to check |
||
158 | * @return boolean Whether value is a valid timestamp |
||
159 | */ |
||
160 | 10 | public static function is_valid_time_stamp( $timestamp ) { |
|
165 | |||
166 | /** |
||
167 | * Checks if a value is 'empty'. Still accepts 0. |
||
168 | * @since 2.0.0 |
||
169 | * @param mixed $value Value to check |
||
170 | * @return bool True or false |
||
171 | */ |
||
172 | 58 | public static function isempty( $value ) { |
|
175 | |||
176 | /** |
||
177 | * Checks if a value is not 'empty'. 0 doesn't count as empty. |
||
178 | * @since 2.2.2 |
||
179 | * @param mixed $value Value to check |
||
180 | * @return bool True or false |
||
181 | */ |
||
182 | 3 | public static function notempty( $value ){ |
|
185 | |||
186 | /** |
||
187 | * Filters out empty values (not including 0). |
||
188 | * @since 2.2.2 |
||
189 | * @param mixed $value Value to check |
||
190 | * @return bool True or false |
||
191 | */ |
||
192 | 2 | public static function filter_empty( $value ) { |
|
195 | |||
196 | /** |
||
197 | * Insert a single array item inside another array at a set position |
||
198 | * @since 2.0.2 |
||
199 | * @param array &$array Array to modify. Is passed by reference, and no return is needed. |
||
200 | * @param array $new New array to insert |
||
201 | * @param int $position Position in the main array to insert the new array |
||
202 | */ |
||
203 | 2 | public static function array_insert( &$array, $new, $position ) { |
|
208 | |||
209 | /** |
||
210 | * Defines the url which is used to load local resources. |
||
211 | * This may need to be filtered for local Window installations. |
||
212 | * If resources do not load, please check the wiki for details. |
||
213 | * @since 1.0.1 |
||
214 | * @return string URL to CMB2 resources |
||
215 | */ |
||
216 | 2 | public static function url( $path = '' ) { |
|
217 | 2 | if ( self::$url ) { |
|
218 | 1 | return self::$url . $path; |
|
219 | } |
||
220 | |||
221 | 1 | $cmb2_url = self::get_url_from_dir( cmb2_dir() ); |
|
222 | |||
223 | /** |
||
224 | * Filter the CMB location url |
||
225 | * |
||
226 | * @param string $cmb2_url Currently registered url |
||
227 | */ |
||
228 | 1 | self::$url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) ); |
|
229 | |||
230 | 1 | return self::$url . $path; |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Converts a system path to a URL |
||
235 | * @since 2.2.2 |
||
236 | * @param string $dir Directory path to convert. |
||
237 | * @return string Converted URL. |
||
238 | */ |
||
239 | 2 | public static function get_url_from_dir( $dir ) { |
|
240 | 2 | $dir = self::normalize_path( $dir ); |
|
241 | |||
242 | // Let's test if We are in the plugins or mu-plugins dir. |
||
243 | 2 | $test_dir = trailingslashit( $dir ) . 'unneeded.php'; |
|
244 | if ( |
||
245 | 2 | 0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) ) |
|
246 | 2 | || 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) ) |
|
247 | 2 | ) { |
|
248 | // Ok, then use plugins_url, as it is more reliable. |
||
249 | 1 | return trailingslashit( plugins_url( '', $test_dir ) ); |
|
250 | } |
||
251 | |||
252 | // Ok, now let's test if we are in the theme dir. |
||
253 | 2 | $theme_root = self::normalize_path( get_theme_root() ); |
|
254 | 2 | if ( 0 === strpos( $dir, $theme_root ) ) { |
|
255 | // Ok, then use get_theme_root_uri. |
||
256 | 1 | return set_url_scheme( |
|
257 | 1 | trailingslashit( |
|
258 | 1 | str_replace( |
|
259 | 1 | untrailingslashit( $theme_root ), |
|
260 | 1 | untrailingslashit( get_theme_root_uri() ), |
|
261 | $dir |
||
262 | 1 | ) |
|
263 | 1 | ) |
|
264 | 1 | ); |
|
265 | } |
||
266 | |||
267 | // Check to see if it's anywhere in the root directory |
||
268 | |||
269 | 2 | $site_dir = self::normalize_path( self::$ABSPATH ); |
|
270 | 2 | $site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() ); |
|
271 | |||
272 | 2 | $url = str_replace( |
|
273 | 2 | array( $site_dir, WP_PLUGIN_DIR ), |
|
274 | 2 | array( $site_url, WP_PLUGIN_URL ), |
|
275 | $dir |
||
276 | 2 | ); |
|
277 | |||
278 | 2 | return set_url_scheme( $url ); |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
||
283 | * |
||
284 | * On windows systems, replaces backslashes with forward slashes |
||
285 | * and forces upper-case drive letters. |
||
286 | * Allows for two leading slashes for Windows network shares, but |
||
287 | * ensures that all other duplicate slashes are reduced to a single. |
||
288 | * |
||
289 | * @since 2.2.0 |
||
290 | * |
||
291 | * @param string $path Path to normalize. |
||
292 | * @return string Normalized path. |
||
293 | */ |
||
294 | 2 | protected static function normalize_path( $path ) { |
|
308 | |||
309 | /** |
||
310 | * Get timestamp from text date |
||
311 | * @since 2.2.0 |
||
312 | * @param string $value Date value |
||
313 | * @param string $date_format Expected date format |
||
314 | * @return mixed Unix timestamp representing the date. |
||
315 | */ |
||
316 | public static function get_timestamp_from_value( $value, $date_format ) { |
||
317 | $date_object = date_create_from_format( $date_format, $value ); |
||
318 | return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value ); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
||
323 | * It will work with only with the following subset ot date() options: |
||
324 | * |
||
325 | * d, j, z, m, n, y, and Y. |
||
326 | * |
||
327 | * A slight effort is made to deal with escaped characters. |
||
328 | * |
||
329 | * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
||
330 | * bring even more translation troubles. |
||
331 | * |
||
332 | * @since 2.2.0 |
||
333 | * @param string $format php date format |
||
334 | * @return string reformatted string |
||
335 | */ |
||
336 | 5 | public static function php_to_js_dateformat( $format ) { |
|
337 | |||
338 | // order is relevant here, since the replacement will be done sequentially. |
||
339 | $supported_options = array( |
||
340 | 5 | 'd' => 'dd', // Day, leading 0 |
|
341 | 5 | 'j' => 'd', // Day, no 0 |
|
342 | 5 | 'z' => 'o', // Day of the year, no leading zeroes, |
|
343 | // 'D' => 'D', // Day name short, not sure how it'll work with translations |
||
344 | // 'l' => 'DD', // Day name full, idem before |
||
345 | 5 | 'm' => 'mm', // Month of the year, leading 0 |
|
346 | 5 | 'n' => 'm', // Month of the year, no leading 0 |
|
347 | // 'M' => 'M', // Month, Short name |
||
348 | // 'F' => 'MM', // Month, full name, |
||
349 | 5 | 'y' => 'y', // Year, two digit |
|
350 | 5 | 'Y' => 'yy', // Year, full |
|
351 | 5 | 'H' => 'HH', // Hour with leading 0 (24 hour) |
|
352 | 5 | 'G' => 'H', // Hour with no leading 0 (24 hour) |
|
353 | 5 | 'h' => 'hh', // Hour with leading 0 (12 hour) |
|
354 | 5 | 'g' => 'h', // Hour with no leading 0 (12 hour), |
|
355 | 5 | 'i' => 'mm', // Minute with leading 0, |
|
356 | 5 | 's' => 'ss', // Second with leading 0, |
|
357 | 5 | 'a' => 'tt', // am/pm |
|
358 | 'A' => 'TT' // AM/PM |
||
359 | 5 | ); |
|
360 | |||
361 | 5 | foreach ( $supported_options as $php => $js ) { |
|
362 | // replaces every instance of a supported option, but skips escaped characters |
||
363 | 5 | $format = preg_replace( "~(?<!\\\\)$php~", $js, $format ); |
|
364 | 5 | } |
|
365 | |||
366 | 5 | $format = preg_replace_callback( '~(?:\\\.)+~', array( __CLASS__, 'wrap_escaped_chars' ), $format ); |
|
367 | |||
368 | 5 | return $format; |
|
369 | } |
||
370 | |||
371 | /** |
||
372 | * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
||
373 | * @since 2.2.0 |
||
374 | * @param $value Value to wrap/escape |
||
375 | * @return string Modified value |
||
376 | */ |
||
377 | 4 | public static function wrap_escaped_chars( $value ) { |
|
380 | |||
381 | /** |
||
382 | * Send to debug.log if WP_DEBUG is defined and true |
||
383 | * |
||
384 | * @since 2.2.0 |
||
385 | * |
||
386 | * @param string $function Function name |
||
387 | * @param int $line Line number |
||
388 | * @param mixed $msg Message to output |
||
389 | * @param mixed $debug Variable to print_r |
||
390 | */ |
||
391 | public static function log_if_debug( $function, $line, $msg, $debug = null ) { |
||
396 | |||
397 | /** |
||
398 | * Determine a file's extension |
||
399 | * @since 1.0.0 |
||
400 | * @param string $file File url |
||
401 | * @return string|false File extension or false |
||
402 | */ |
||
403 | 6 | public static function get_file_ext( $file ) { |
|
407 | |||
408 | /** |
||
409 | * Get the file name from a url |
||
410 | * @since 2.0.0 |
||
411 | * @param string $value File url or path |
||
412 | * @return string File name |
||
413 | */ |
||
414 | 5 | public static function get_file_name_from_path( $value ) { |
|
418 | |||
419 | /** |
||
420 | * Check if WP version is at least $version. |
||
421 | * @since 2.2.2 |
||
422 | * @param string $version WP version string to compare. |
||
423 | * @return bool Result of comparison check. |
||
424 | */ |
||
425 | 6 | public static function wp_at_least( $version ) { |
|
428 | |||
429 | /** |
||
430 | * Combines attributes into a string for a form element. |
||
431 | * @since 1.1.0 |
||
432 | * @param array $attrs Attributes to concatenate. |
||
433 | * @param array $attr_exclude Attributes that should NOT be concatenated. |
||
434 | * @return string String of attributes for form element. |
||
435 | */ |
||
436 | 45 | public static function concat_attrs( $attrs, $attr_exclude = array() ) { |
|
450 | |||
451 | } |
||
452 |
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.