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 | 1 | /** |
|
| 23 | 1 | * The url which is used to load local resources. |
|
| 24 | * @var string |
||
| 25 | 1 | * @since 2.0.0 |
|
| 26 | */ |
||
| 27 | 1 | protected static $url = ''; |
|
| 28 | 1 | ||
| 29 | 1 | /** |
|
| 30 | 1 | * 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 | 1 | * @return int|false Attachment ID or false |
|
| 34 | */ |
||
| 35 | public static function image_id_from_url( $img_url ) { |
||
| 77 | 2 | ||
| 78 | 2 | /** |
|
| 79 | 2 | * Utility method that returns time string offset by timezone |
|
| 80 | * @since 1.0.0 |
||
| 81 | 2 | * @param string $tzstring Time string |
|
| 82 | 2 | * @return string Offset time string |
|
| 83 | 2 | */ |
|
| 84 | 2 | public static function timezone_offset( $tzstring ) { |
|
| 104 | |||
| 105 | 5 | /** |
|
| 106 | 5 | * Utility method that returns a timezone string representing the default timezone for the site. |
|
| 107 | 5 | * |
|
| 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 | 5 | public static function timezone_string() { |
|
| 117 | 5 | $current_offset = get_option( 'gmt_offset' ); |
|
| 118 | 5 | $tzstring = get_option( 'timezone_string' ); |
|
| 119 | 5 | ||
| 120 | 5 | // Remove old Etc mappings. Fallback to gmt_offset. |
|
| 121 | if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) { |
||
| 122 | $tzstring = ''; |
||
| 123 | } |
||
| 124 | |||
| 125 | if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists |
||
| 126 | if ( 0 == $current_offset ) { |
||
| 127 | $tzstring = 'UTC+0'; |
||
| 128 | } elseif ( $current_offset < 0 ) { |
||
| 129 | $tzstring = 'UTC' . $current_offset; |
||
| 130 | 2 | } else { |
|
| 131 | 2 | $tzstring = 'UTC+' . $current_offset; |
|
| 132 | 1 | } |
|
| 133 | } |
||
| 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 | 1 | * @return int Time stamp |
|
| 143 | 1 | */ |
|
| 144 | 1 | public static function make_valid_time_stamp( $string ) { |
|
| 153 | |||
| 154 | 1 | /** |
|
| 155 | * Determine if a value is a valid timestamp |
||
| 156 | 1 | * @since 2.0.0 |
|
| 157 | * @param mixed $timestamp Value to check |
||
| 158 | * @return boolean Whether value is a valid timestamp |
||
| 159 | */ |
||
| 160 | 1 | 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 | public static function isempty( $value ) { |
||
| 173 | return null === $value || '' === $value || false === $value || array() === $value; |
||
| 174 | } |
||
| 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 | public static function notempty( $value ){ |
||
| 183 | return null !== $value && '' !== $value && false !== $value && array() !== $value; |
||
| 184 | } |
||
| 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 | public static function filter_empty( $value ) { |
||
| 193 | return array_filter( $value, array( __CLASS__, 'notempty' ) ); |
||
| 194 | } |
||
| 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 | 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 | public static function url( $path = '' ) { |
||
| 217 | if ( self::$url ) { |
||
| 218 | return self::$url . $path; |
||
| 219 | } |
||
| 220 | |||
| 221 | $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 | self::$url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) ); |
||
| 229 | |||
| 230 | 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 | public static function get_url_from_dir( $dir ) { |
||
| 240 | $dir = self::normalize_path( $dir ); |
||
| 241 | |||
| 242 | // Let's test if We are in the plugins or mu-plugins dir. |
||
| 243 | $test_dir = trailingslashit( $dir ) . 'unneeded.php'; |
||
| 244 | if ( |
||
| 245 | 0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) ) |
||
| 246 | || 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) ) |
||
| 247 | ) { |
||
| 248 | // Ok, then use plugins_url, as it is more reliable. |
||
| 249 | return trailingslashit( plugins_url( '', $test_dir ) ); |
||
| 250 | } |
||
| 251 | |||
| 252 | // Ok, now let's test if we are in the theme dir. |
||
| 253 | $theme_root = self::normalize_path( get_theme_root() ); |
||
| 254 | if ( 0 === strpos( $dir, $theme_root ) ) { |
||
| 255 | // Ok, then use get_theme_root_uri. |
||
| 256 | return set_url_scheme( |
||
| 257 | trailingslashit( |
||
| 258 | str_replace( |
||
| 259 | untrailingslashit( $theme_root ), |
||
| 260 | untrailingslashit( get_theme_root_uri() ), |
||
| 261 | $dir |
||
| 262 | ) |
||
| 263 | ) |
||
| 264 | ); |
||
| 265 | } |
||
| 266 | |||
| 267 | // Check to see if it's anywhere in the root directory |
||
| 268 | |||
| 269 | $site_dir = self::normalize_path( self::$ABSPATH ); |
||
| 270 | $site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() ); |
||
| 271 | |||
| 272 | $url = str_replace( |
||
| 273 | array( $site_dir, WP_PLUGIN_DIR ), |
||
| 274 | array( $site_url, WP_PLUGIN_URL ), |
||
| 275 | $dir |
||
| 276 | ); |
||
| 277 | |||
| 278 | 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 | protected static function normalize_path( $path ) { |
||
| 295 | if ( function_exists( 'wp_normalize_path' ) ) { |
||
| 296 | return wp_normalize_path( $path ); |
||
| 297 | } |
||
| 298 | |||
| 299 | // Replace newer WP's version of wp_normalize_path. |
||
| 300 | $path = str_replace( '\\', '/', $path ); |
||
| 301 | $path = preg_replace( '|(?<=.)/+|', '/', $path ); |
||
| 302 | if ( ':' === substr( $path, 1, 1 ) ) { |
||
| 303 | $path = ucfirst( $path ); |
||
| 304 | } |
||
| 305 | |||
| 306 | return $path; |
||
| 307 | } |
||
| 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 ) { |
||
| 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 | public static function php_to_js_dateformat( $format ) { |
||
| 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 | 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 | 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 | 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 | 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 | public static function concat_attrs( $attrs, $attr_exclude = array() ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Ensures value is an array. |
||
| 453 | * |
||
| 454 | * @since 2.2.3 |
||
| 455 | * |
||
| 456 | * @param mixed $value Value to ensure is array. |
||
| 457 | * @param array $default Default array. Defaults to empty array. |
||
| 458 | * |
||
| 459 | * @return array The array. |
||
| 460 | */ |
||
| 461 | public static function ensure_array( $value, $default = array() ) { |
||
| 477 | |||
| 478 | } |
||
| 479 |
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.