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