Completed
Pull Request — trunk (#836)
by Andrew
23:57 queued 15:47
created

CMB2_Utils   C

Complexity

Total Complexity 69

Size/Duplication

Total Lines 466
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 79.66%

Importance

Changes 0
Metric Value
dl 0
loc 466
rs 5.6445
c 0
b 0
f 0
ccs 47
cts 59
cp 0.7966
wmc 69
lcom 1
cbo 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
C image_id_from_url() 0 42 8
B timezone_offset() 0 20 5
A make_valid_time_stamp() 0 9 3
A is_valid_time_stamp() 0 5 3
A array_insert() 0 5 1
A get_timestamp_from_value() 0 4 2
B php_to_js_dateformat() 0 34 2
A wrap_escaped_chars() 0 3 1
A log_if_debug() 0 5 4
B timezone_string() 0 21 5
A filter_empty() 0 3 1
A normalize_path() 0 14 3
A get_file_ext() 0 4 2
A get_file_name_from_path() 0 4 2
A url() 0 16 2
B get_url_from_dir() 0 41 5
A wp_at_least() 0 3 1
B concat_attrs() 0 14 6
B ensure_array() 0 16 5
A isempty() 0 3 4
A notempty() 0 3 4

How to fix   Complexity   

Complex Class

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
2
/**
3
 * CMB2 Utilities
4
 *
5
 * @since  1.1.0
6
 *
7
 * @category  WordPress_Plugin
8
 * @package   CMB2
9
 * @author    WebDevStudios
10
 * @license   GPL-2.0+
11
 * @link      http://webdevstudios.com
12
 */
13
class CMB2_Utils {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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 ) {
36 1
		$attachment_id = 0;
37 1
		$dir = wp_upload_dir();
38
39
		// Is URL in uploads directory?
40
		if ( false === strpos( $img_url, $dir['baseurl'] . '/' ) ) {
41
			return false;
42
		}
43
44
		$file = basename( $img_url );
45
46
		$query_args = array(
47
			'post_type'   => 'attachment',
48
			'post_status' => 'inherit',
49
			'fields'      => 'ids',
50 2
			'meta_query'  => array(
51 2
				array(
52 1
					'value'   => $file,
53 1
					'compare' => 'LIKE',
54 1
					'key'     => '_wp_attachment_metadata',
55
				),
56
			)
57
		);
58
59
		$query = new WP_Query( $query_args );
60
61
		if ( $query->have_posts() ) {
62
63 1
			foreach ( $query->posts as $post_id ) {
64
				$meta = wp_get_attachment_metadata( $post_id );
65
				$original_file       = basename( $meta['file'] );
66
				$cropped_image_files = isset( $meta['sizes'] ) ? wp_list_pluck( $meta['sizes'], 'file' ) : array();
67
				if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
68
					$attachment_id = $post_id;
69
					break;
70
				}
71
			}
72
73
		}
74
75
		return 0 === $attachment_id ? false : $attachment_id;
76
	}
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 ) {
85
		$tz_offset = 0;
86
87
		if ( ! empty( $tzstring ) && is_string( $tzstring ) ) {
88
			if ( 'UTC' === substr( $tzstring, 0, 3 ) ) {
89 2
				$tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring );
90
				return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS );
91 2
			}
92
93
			try {
94
				$date_time_zone_selected = new DateTimeZone( $tzstring );
95
				$tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
96
			} catch ( Exception $e ) {
97
				self::log_if_debug( __METHOD__, __LINE__, $e->getMessage() );
98
			}
99
100 5
		}
101 5
102
		return $tz_offset;
103
	}
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 ) {
145 1
		if ( ! $string ) {
146 1
			return 0;
147
		}
148
149
		return self::is_valid_time_stamp( $string )
150
			? (int) $string :
151
			strtotime( (string) $string );
152
	}
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 ) {
161
		return (string) (int) $timestamp === (string) $timestamp
162
			&& $timestamp <= PHP_INT_MAX
163
			&& $timestamp >= ~PHP_INT_MAX;
164
	}
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 ) {
204
		$before = array_slice( $array, 0, $position - 1 );
205
		$after  = array_diff_key( $array, $before );
206
		$array  = array_merge( $before, $new, $after );
207
	}
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 ) {
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
	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
			'd' => 'dd',  // Day, leading 0
341
			'j' => 'd',   // Day, no 0
342
			'z' => 'o',   // Day of the year, no leading zeroes,
343
			// 'D' => 'D',   // Day name short, not sure how it'll work with translations
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
344
			// 'l' => 'DD',  // Day name full, idem before
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
345
			'm' => 'mm',  // Month of the year, leading 0
346
			'n' => 'm',   // Month of the year, no leading 0
347
			// 'M' => 'M',   // Month, Short name
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
348
			// 'F' => 'MM',  // Month, full name,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
349
			'y' => 'y',   // Year, two digit
350
			'Y' => 'yy',  // Year, full
351
			'H' => 'HH',  // Hour with leading 0 (24 hour)
352
			'G' => 'H',   // Hour with no leading 0 (24 hour)
353
			'h' => 'hh',  // Hour with leading 0 (12 hour)
354
			'g' => 'h',   // Hour with no leading 0 (12 hour),
355
			'i' => 'mm',  // Minute with leading 0,
356
			's' => 'ss',  // Second with leading 0,
357
			'a' => 'tt',  // am/pm
358
			'A' => 'TT'   // AM/PM
359
		);
360
361
		foreach ( $supported_options as $php => $js ) {
362
			// replaces every instance of a supported option, but skips escaped characters
363
			$format = preg_replace( "~(?<!\\\\)$php~", $js, $format );
364
		}
365
366
		$format = preg_replace_callback( '~(?:\\\.)+~', array( __CLASS__, 'wrap_escaped_chars' ), $format );
367
368
		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
	public static function wrap_escaped_chars( $value ) {
378
		return "&#39;" . str_replace( '\\', '', $value[0] ) . "&#39;";
379
	}
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 ) {
392
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
393
			error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) );
394
		}
395
	}
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 ) {
404
		$parsed = @parse_url( $file, PHP_URL_PATH );
405
		return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
406
	}
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 ) {
415
		$parts = explode( '/', $value );
416
		return is_array( $parts ) ? end( $parts ) : $value;
417
	}
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 ) {
426
		return version_compare( get_bloginfo( 'version' ), $version, '>=' );
427
	}
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() ) {
437
		$attr_exclude[] = 'rendered';
438
		$attributes = '';
439
		foreach ( $attrs as $attr => $val ) {
440
			$excluded = in_array( $attr, (array) $attr_exclude, true );
441
			$empty    = false === $val && 'value' !== $attr;
442
			if ( ! $excluded && ! $empty ) {
443
				// if data attribute, use single quote wraps, else double
444
				$quotes = false !== stripos( $attr, 'data-' ) ? "'" : '"';
445
				$attributes .= sprintf( ' %1$s=%3$s%2$s%3$s', $attr, $val, $quotes );
446
			}
447
		}
448
		return $attributes;
449
	}
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() ) {
462
		if ( empty( $value ) ) {
463
			return $default;
464
		}
465
466
		if ( is_array( $value ) || is_object( $value ) ) {
467
			return (array) $value;
468
		}
469
470
		// Not sure anything would be non-scalar that is not an array or object?
471
		if ( ! is_scalar( $value ) ) {
472
			return $default;
473
		}
474
475
		return (array) $value;
476
	}
477
478
}
479