Completed
Pull Request — trunk (#848)
by
unknown
04:35
created

CMB2_Utils::url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 16
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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 to get a combined list of default and custom registered image sizes
80
	 * @since  2.x.x.x
81 2
	 * @link   http://core.trac.wordpress.org/ticket/18947
82 2
	 * @global array $_wp_additional_image_sizes
83 2
	 * @return array $image_sizes The image sizes
84 2
	 */
85
	static function get_available_image_sizes() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
86
		global $_wp_additional_image_sizes;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
87
88
		$default_image_sizes = array( 'thumbnail', 'medium', 'large' );
89 2
		foreach ( $default_image_sizes as $size ) {
90
			$image_sizes[ $size ] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$image_sizes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $image_sizes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
91 2
				'height' => intval( get_option( "{$size}_size_h" ) ),
92
				'width'  => intval( get_option( "{$size}_size_w" ) ),
93
				'crop'   => get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false,
94
			);
95
		}
96
97
		if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) {
98
			$image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
0 ignored issues
show
Bug introduced by
The variable $image_sizes does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
99
		}
100 5
101 5
		return $image_sizes;
102
	}
103
104
	/**
105 5
	 * Utility method to return the closest named size from an array of values
106 5
	 *
107 5
	 * Based off of WordPress's image_get_intermediate_size()
108
	 * If the size matches an existing size then it will be used. If there is no
109
	 * direct match, then the nearest image size larger than the specified size
110
	 * will be used. If nothing is found, then the function will return false.
111
	 * Uses get_available_image_sizes() to get all available sizes.
112
	 *
113
	 * @since  2.x.x.x
114
	 * @param  array|string $size Image size. Accepts an array of width and height (in that order).
115
	 * @return false|string $data Named image size e.g. 'thumbnail'.
116 5
	 */
117 5
	public static function get_named_size( $size ) {
118 5
		$image_sizes = self::get_available_image_sizes();
119 5
		$data = array();
120 5
121
		// Find the best match when '$size' is an array.
122
		if ( is_array( $size ) ) {
123
			$candidates = array();
124
125
			foreach ( $image_sizes as $_size => $data ) {
126
127
				// If there's an exact match to an existing image size, short circuit.
128 View Code Duplication
				if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
					$candidates[ $data['width'] * $data['height'] ] = array( $_size, $data );
130 2
					break;
131 2
				}
132 1
133
				// If it's not an exact match, consider larger sizes with the same aspect ratio.
134
				if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
135 1 View Code Duplication
					if ( wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
						$candidates[ $data['width'] * $data['height'] ] = array( $_size, $data );
137
					}
138
				}
139
			}
140
141
			if ( ! empty( $candidates ) ) {
142 1
				// Sort the array by size if we have more than one candidate.
143 1
				if ( 1 < count( $candidates ) ) {
144 1
					ksort( $candidates );
145 1
				}
146 1
147
				$data = array_shift( $candidates );
148
				$data = $data[0];
149
			/*
150
			 * When the size requested is smaller than the thumbnail dimensions, we
151
			 * fall back to the thumbnail size.
152
			 */
153
			} elseif ( ! empty( $image_sizes['thumbnail'] ) && $image_sizes['thumbnail']['width'] >= $size[0] && $image_sizes['thumbnail']['width'] >= $size[1] ) {
154 1
				$data = 'thumbnail';
155
			} else {
156 1
				return false;
157
			}
158
159
		} elseif ( ! empty( $image_sizes[ $size ] ) ) {
160 1
			$data = $size;
161
		}
162
163
		// If we still don't have a match at this point, return false.
164
		if ( empty( $data ) ) {
165
			return false;
166
		}
167
168
		return $data;
169
	}
170
171
	/**
172
	 * Utility method that returns time string offset by timezone
173
	 * @since  1.0.0
174
	 * @param  string $tzstring Time string
175
	 * @return string           Offset time string
176
	 */
177
	public static function timezone_offset( $tzstring ) {
178
		$tz_offset = 0;
179
180
		if ( ! empty( $tzstring ) && is_string( $tzstring ) ) {
181
			if ( 'UTC' === substr( $tzstring, 0, 3 ) ) {
182
				$tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring );
183
				return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS );
184
			}
185
186
			try {
187
				$date_time_zone_selected = new DateTimeZone( $tzstring );
188
				$tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
189
			} catch ( Exception $e ) {
190
				self::log_if_debug( __METHOD__, __LINE__, $e->getMessage() );
191
			}
192
193
		}
194
195
		return $tz_offset;
196
	}
197
198
	/**
199
	 * Utility method that returns a timezone string representing the default timezone for the site.
200
	 *
201
	 * Roughly copied from WordPress, as get_option('timezone_string') will return
202
	 * an empty string if no value has been set on the options page.
203
	 * A timezone string is required by the wp_timezone_choice() used by the
204
	 * select_timezone field.
205
	 *
206
	 * @since  1.0.0
207
	 * @return string Timezone string
208
	 */
209
	public static function timezone_string() {
210
		$current_offset = get_option( 'gmt_offset' );
211
		$tzstring       = get_option( 'timezone_string' );
212
213
		// Remove old Etc mappings. Fallback to gmt_offset.
214
		if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) {
215
			$tzstring = '';
216
		}
217
218
		if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
219
			if ( 0 == $current_offset ) {
220
				$tzstring = 'UTC+0';
221
			} elseif ( $current_offset < 0 ) {
222
				$tzstring = 'UTC' . $current_offset;
223
			} else {
224
				$tzstring = 'UTC+' . $current_offset;
225
			}
226
		}
227
228
		return $tzstring;
229
	}
230
231
	/**
232
	 * Returns a timestamp, first checking if value already is a timestamp.
233
	 * @since  2.0.0
234
	 * @param  string|int $string Possible timestamp string
235
	 * @return int   	            Time stamp
236
	 */
237
	public static function make_valid_time_stamp( $string ) {
238
		if ( ! $string ) {
239
			return 0;
240
		}
241
242
		return self::is_valid_time_stamp( $string )
243
			? (int) $string :
244
			strtotime( (string) $string );
245
	}
246
247
	/**
248
	 * Determine if a value is a valid timestamp
249
	 * @since  2.0.0
250
	 * @param  mixed  $timestamp Value to check
251
	 * @return boolean           Whether value is a valid timestamp
252
	 */
253
	public static function is_valid_time_stamp( $timestamp ) {
254
		return (string) (int) $timestamp === (string) $timestamp
255
			&& $timestamp <= PHP_INT_MAX
256
			&& $timestamp >= ~PHP_INT_MAX;
257
	}
258
259
	/**
260
	 * Checks if a value is 'empty'. Still accepts 0.
261
	 * @since  2.0.0
262
	 * @param  mixed $value Value to check
263
	 * @return bool         True or false
264
	 */
265
	public static function isempty( $value ) {
266
		return null === $value || '' === $value || false === $value || array() === $value;
267
	}
268
269
	/**
270
	 * Checks if a value is not 'empty'. 0 doesn't count as empty.
271
	 * @since  2.2.2
272
	 * @param  mixed $value Value to check
273
	 * @return bool         True or false
274
	 */
275
	public static function notempty( $value ){
276
		return null !== $value && '' !== $value && false !== $value && array() !== $value;
277
	}
278
279
	/**
280
	 * Filters out empty values (not including 0).
281
	 * @since  2.2.2
282
	 * @param  mixed $value Value to check
283
	 * @return bool         True or false
284
	 */
285
	public static function filter_empty( $value ) {
286
		return array_filter( $value, array( __CLASS__, 'notempty' ) );
287
	}
288
289
	/**
290
	 * Insert a single array item inside another array at a set position
291
	 * @since  2.0.2
292
	 * @param  array &$array   Array to modify. Is passed by reference, and no return is needed.
293
	 * @param  array $new      New array to insert
294
	 * @param  int   $position Position in the main array to insert the new array
295
	 */
296
	public static function array_insert( &$array, $new, $position ) {
297
		$before = array_slice( $array, 0, $position - 1 );
298
		$after  = array_diff_key( $array, $before );
299
		$array  = array_merge( $before, $new, $after );
300
	}
301
302
	/**
303
	 * Defines the url which is used to load local resources.
304
	 * This may need to be filtered for local Window installations.
305
	 * If resources do not load, please check the wiki for details.
306
	 * @since  1.0.1
307
	 * @return string URL to CMB2 resources
308
	 */
309
	public static function url( $path = '' ) {
310
		if ( self::$url ) {
311
			return self::$url . $path;
312
		}
313
314
		$cmb2_url = self::get_url_from_dir( cmb2_dir() );
315
316
		/**
317
		 * Filter the CMB location url
318
		 *
319
		 * @param string $cmb2_url Currently registered url
320
		 */
321
		self::$url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) );
322
323
		return self::$url . $path;
324
	}
325
326
	/**
327
	 * Converts a system path to a URL
328
	 * @since  2.2.2
329
	 * @param  string $dir Directory path to convert.
330
	 * @return string      Converted URL.
331
	 */
332
	public static function get_url_from_dir( $dir ) {
333
		$dir = self::normalize_path( $dir );
334
335
		// Let's test if We are in the plugins or mu-plugins dir.
336
		$test_dir = trailingslashit( $dir ) . 'unneeded.php';
337
		if (
338
			0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) )
339
			|| 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) )
340
		) {
341
			// Ok, then use plugins_url, as it is more reliable.
342
			return trailingslashit( plugins_url( '', $test_dir ) );
343
		}
344
345
		// Ok, now let's test if we are in the theme dir.
346
		$theme_root = self::normalize_path( get_theme_root() );
347
		if ( 0 === strpos( $dir, $theme_root ) ) {
348
			// Ok, then use get_theme_root_uri.
349
			return set_url_scheme(
350
				trailingslashit(
351
					str_replace(
352
						untrailingslashit( $theme_root ),
353
						untrailingslashit( get_theme_root_uri() ),
354
						$dir
355
					)
356
				)
357
			);
358
		}
359
360
		// Check to see if it's anywhere in the root directory
361
362
		$site_dir = self::normalize_path( self::$ABSPATH );
363
		$site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() );
364
365
		$url = str_replace(
366
			array( $site_dir, WP_PLUGIN_DIR ),
367
			array( $site_url, WP_PLUGIN_URL ),
368
			$dir
369
		);
370
371
		return set_url_scheme( $url );
372
	}
373
374
	/**
375
	 * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path.
376
	 *
377
	 * On windows systems, replaces backslashes with forward slashes
378
	 * and forces upper-case drive letters.
379
	 * Allows for two leading slashes for Windows network shares, but
380
	 * ensures that all other duplicate slashes are reduced to a single.
381
	 *
382
	 * @since 2.2.0
383
	 *
384
	 * @param string $path Path to normalize.
385
	 * @return string Normalized path.
386
	 */
387
	protected static function normalize_path( $path ) {
388
		if ( function_exists( 'wp_normalize_path' ) ) {
389
			return wp_normalize_path( $path );
390
		}
391
392
		// Replace newer WP's version of wp_normalize_path.
393
		$path = str_replace( '\\', '/', $path );
394
		$path = preg_replace( '|(?<=.)/+|', '/', $path );
395
		if ( ':' === substr( $path, 1, 1 ) ) {
396
			$path = ucfirst( $path );
397
		}
398
399
		return $path;
400
	}
401
402
	/**
403
	 * Get timestamp from text date
404
	 * @since  2.2.0
405
	 * @param  string $value       Date value
406
	 * @param  string $date_format Expected date format
407
	 * @return mixed               Unix timestamp representing the date.
408
	 */
409
	public static function get_timestamp_from_value( $value, $date_format ) {
410
		$date_object = date_create_from_format( $date_format, $value );
411
		return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value );
412
	}
413
414
	/**
415
	 * Takes a php date() format string and returns a string formatted to suit for the date/time pickers
416
	 * It will work with only with the following subset ot date() options:
417
	 *
418
	 *  d, j, z, m, n, y, and Y.
419
	 *
420
	 * A slight effort is made to deal with escaped characters.
421
	 *
422
	 * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or
423
	 * bring even more translation troubles.
424
	 *
425
	 * @since 2.2.0
426
	 * @param string $format php date format
427
	 * @return string reformatted string
428
	 */
429
	public static function php_to_js_dateformat( $format ) {
430
431
		// order is relevant here, since the replacement will be done sequentially.
432
		$supported_options = array(
433
			'd' => 'dd',  // Day, leading 0
434
			'j' => 'd',   // Day, no 0
435
			'z' => 'o',   // Day of the year, no leading zeroes,
436
			// '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...
437
			// '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...
438
			'm' => 'mm',  // Month of the year, leading 0
439
			'n' => 'm',   // Month of the year, no leading 0
440
			// '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...
441
			// '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...
442
			'y' => 'y',   // Year, two digit
443
			'Y' => 'yy',  // Year, full
444
			'H' => 'HH',  // Hour with leading 0 (24 hour)
445
			'G' => 'H',   // Hour with no leading 0 (24 hour)
446
			'h' => 'hh',  // Hour with leading 0 (12 hour)
447
			'g' => 'h',   // Hour with no leading 0 (12 hour),
448
			'i' => 'mm',  // Minute with leading 0,
449
			's' => 'ss',  // Second with leading 0,
450
			'a' => 'tt',  // am/pm
451
			'A' => 'TT'   // AM/PM
452
		);
453
454
		foreach ( $supported_options as $php => $js ) {
455
			// replaces every instance of a supported option, but skips escaped characters
456
			$format = preg_replace( "~(?<!\\\\)$php~", $js, $format );
457
		}
458
459
		$format = preg_replace_callback( '~(?:\\\.)+~', array( __CLASS__, 'wrap_escaped_chars' ), $format );
460
461
		return $format;
462
	}
463
464
	/**
465
	 * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded.
466
	 * @since  2.2.0
467
	 * @param  $value Value to wrap/escape
468
	 * @return string Modified value
469
	 */
470
	public static function wrap_escaped_chars( $value ) {
471
		return "&#39;" . str_replace( '\\', '', $value[0] ) . "&#39;";
472
	}
473
474
	/**
475
	 * Send to debug.log if WP_DEBUG is defined and true
476
	 *
477
	 * @since  2.2.0
478
	 *
479
	 * @param  string  $function Function name
480
	 * @param  int     $line     Line number
481
	 * @param  mixed   $msg      Message to output
482
	 * @param  mixed   $debug    Variable to print_r
483
	 */
484
	public static function log_if_debug( $function, $line, $msg, $debug = null ) {
485
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
486
			error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) );
487
		}
488
	}
489
490
	/**
491
	 * Determine a file's extension
492
	 * @since  1.0.0
493
	 * @param  string       $file File url
494
	 * @return string|false       File extension or false
495
	 */
496
	public static function get_file_ext( $file ) {
497
		$parsed = @parse_url( $file, PHP_URL_PATH );
498
		return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
499
	}
500
501
	/**
502
	 * Get the file name from a url
503
	 * @since  2.0.0
504
	 * @param  string $value File url or path
505
	 * @return string        File name
506
	 */
507
	public static function get_file_name_from_path( $value ) {
508
		$parts = explode( '/', $value );
509
		return is_array( $parts ) ? end( $parts ) : $value;
510
	}
511
512
	/**
513
	 * Check if WP version is at least $version.
514
	 * @since  2.2.2
515
	 * @param  string  $version WP version string to compare.
516
	 * @return bool             Result of comparison check.
517
	 */
518
	public static function wp_at_least( $version ) {
519
		return version_compare( get_bloginfo( 'version' ), $version, '>=' );
520
	}
521
522
	/**
523
	 * Combines attributes into a string for a form element.
524
	 * @since  1.1.0
525
	 * @param  array  $attrs        Attributes to concatenate.
526
	 * @param  array  $attr_exclude Attributes that should NOT be concatenated.
527
	 * @return string               String of attributes for form element.
528
	 */
529
	public static function concat_attrs( $attrs, $attr_exclude = array() ) {
530
		$attr_exclude[] = 'rendered';
531
		$attributes = '';
532
		foreach ( $attrs as $attr => $val ) {
533
			$excluded = in_array( $attr, (array) $attr_exclude, true );
534
			$empty    = false === $val && 'value' !== $attr;
535
			if ( ! $excluded && ! $empty ) {
536
				// if data attribute, use single quote wraps, else double
537
				$quotes = false !== stripos( $attr, 'data-' ) ? "'" : '"';
538
				$attributes .= sprintf( ' %1$s=%3$s%2$s%3$s', $attr, $val, $quotes );
539
			}
540
		}
541
		return $attributes;
542
	}
543
544
	/**
545
	 * Ensures value is an array.
546
	 *
547
	 * @since  2.2.3
548
	 *
549
	 * @param  mixed $value   Value to ensure is array.
550
	 * @param  array $default Default array. Defaults to empty array.
551
	 *
552
	 * @return array          The array.
553
	 */
554
	public static function ensure_array( $value, $default = array() ) {
555
		if ( empty( $value ) ) {
556
			return $default;
557
		}
558
559
		if ( is_array( $value ) || is_object( $value ) ) {
560
			return (array) $value;
561
		}
562
563
		// Not sure anything would be non-scalar that is not an array or object?
564
		if ( ! is_scalar( $value ) ) {
565
			return $default;
566
		}
567
568
		return (array) $value;
569
	}
570
571
}
572