|
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 url which is used to load local resources. |
|
17
|
|
|
* @var string |
|
18
|
|
|
* @since 2.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $url = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Utility method that attempts to get an attachment's ID by it's url |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
* @param string $img_url Attachment url |
|
26
|
|
|
* @return int|false Attachment ID or false |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function image_id_from_url( $img_url ) { |
|
29
|
1 |
|
$attachment_id = 0; |
|
30
|
1 |
|
$dir = wp_upload_dir(); |
|
31
|
|
|
|
|
32
|
|
|
// Is URL in uploads directory? |
|
33
|
1 |
|
if ( false === strpos( $img_url, $dir['baseurl'] . '/' ) ) { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$file = basename( $img_url ); |
|
38
|
|
|
|
|
39
|
|
|
$query_args = array( |
|
40
|
1 |
|
'post_type' => 'attachment', |
|
41
|
1 |
|
'post_status' => 'inherit', |
|
42
|
1 |
|
'fields' => 'ids', |
|
43
|
|
|
'meta_query' => array( |
|
44
|
|
|
array( |
|
45
|
1 |
|
'value' => $file, |
|
46
|
1 |
|
'compare' => 'LIKE', |
|
47
|
1 |
|
'key' => '_wp_attachment_metadata', |
|
48
|
1 |
|
), |
|
49
|
|
|
) |
|
50
|
1 |
|
); |
|
51
|
|
|
|
|
52
|
1 |
|
$query = new WP_Query( $query_args ); |
|
53
|
|
|
|
|
54
|
1 |
|
if ( $query->have_posts() ) { |
|
55
|
|
|
|
|
56
|
1 |
|
foreach ( $query->posts as $post_id ) { |
|
57
|
1 |
|
$meta = wp_get_attachment_metadata( $post_id ); |
|
58
|
1 |
|
$original_file = basename( $meta['file'] ); |
|
59
|
1 |
|
$cropped_image_files = isset( $meta['sizes'] ) ? wp_list_pluck( $meta['sizes'], 'file' ) : array(); |
|
60
|
1 |
|
if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) { |
|
61
|
1 |
|
$attachment_id = $post_id; |
|
62
|
1 |
|
break; |
|
63
|
|
|
} |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return 0 === $attachment_id ? false : $attachment_id; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Utility method that returns time string offset by timezone |
|
73
|
|
|
* @since 1.0.0 |
|
74
|
|
|
* @param string $tzstring Time string |
|
75
|
|
|
* @return string Offset time string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function timezone_offset( $tzstring ) { |
|
78
|
1 |
|
$tz_offset = 0; |
|
79
|
|
|
|
|
80
|
1 |
|
if ( ! empty( $tzstring ) && is_string( $tzstring ) ) { |
|
81
|
1 |
|
if ( 'UTC' === substr( $tzstring, 0, 3 ) ) { |
|
82
|
|
|
$tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring ); |
|
83
|
|
|
return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
1 |
|
$date_time_zone_selected = new DateTimeZone( $tzstring ); |
|
88
|
1 |
|
$tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() ); |
|
89
|
1 |
|
} catch ( Exception $e ) { |
|
90
|
|
|
$this->log_if_debug( __METHOD__, __LINE__, $e->getMessage() ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return $tz_offset; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Utility method that returns a timezone string representing the default timezone for the site. |
|
100
|
|
|
* |
|
101
|
|
|
* Roughly copied from WordPress, as get_option('timezone_string') will return |
|
102
|
|
|
* an empty string if no value has been set on the options page. |
|
103
|
|
|
* A timezone string is required by the wp_timezone_choice() used by the |
|
104
|
|
|
* select_timezone field. |
|
105
|
|
|
* |
|
106
|
|
|
* @since 1.0.0 |
|
107
|
|
|
* @return string Timezone string |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function timezone_string() { |
|
110
|
1 |
|
$current_offset = get_option( 'gmt_offset' ); |
|
111
|
1 |
|
$tzstring = get_option( 'timezone_string' ); |
|
112
|
|
|
|
|
113
|
|
|
// Remove old Etc mappings. Fallback to gmt_offset. |
|
114
|
1 |
|
if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) { |
|
115
|
|
|
$tzstring = ''; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists |
|
119
|
1 |
|
if ( 0 == $current_offset ) { |
|
120
|
1 |
|
$tzstring = 'UTC+0'; |
|
121
|
1 |
|
} elseif ( $current_offset < 0 ) { |
|
122
|
|
|
$tzstring = 'UTC' . $current_offset; |
|
123
|
|
|
} else { |
|
124
|
|
|
$tzstring = 'UTC+' . $current_offset; |
|
125
|
|
|
} |
|
126
|
1 |
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
return $tzstring; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns a timestamp, first checking if value already is a timestamp. |
|
133
|
|
|
* @since 2.0.0 |
|
134
|
|
|
* @param string|int $string Possible timestamp string |
|
135
|
|
|
* @return int Time stamp |
|
136
|
|
|
*/ |
|
137
|
5 |
|
public function make_valid_time_stamp( $string ) { |
|
138
|
5 |
|
if ( ! $string ) { |
|
139
|
|
|
return 0; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
5 |
|
return $this->is_valid_time_stamp( $string ) |
|
143
|
5 |
|
? (int) $string : |
|
144
|
5 |
|
strtotime( (string) $string ); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Determine if a value is a valid timestamp |
|
149
|
|
|
* @since 2.0.0 |
|
150
|
|
|
* @param mixed $timestamp Value to check |
|
151
|
|
|
* @return boolean Whether value is a valid timestamp |
|
152
|
|
|
*/ |
|
153
|
5 |
|
public function is_valid_time_stamp( $timestamp ) { |
|
154
|
5 |
|
return (string) (int) $timestamp === (string) $timestamp |
|
155
|
5 |
|
&& $timestamp <= PHP_INT_MAX |
|
156
|
5 |
|
&& $timestamp >= ~PHP_INT_MAX; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Checks if a value is 'empty'. Still accepts 0. |
|
161
|
|
|
* @since 2.0.0 |
|
162
|
|
|
* @param mixed $value Value to check |
|
163
|
|
|
* @return bool True or false |
|
164
|
|
|
*/ |
|
165
|
54 |
|
public function isempty( $value ) { |
|
166
|
54 |
|
return null === $value || '' === $value || false === $value; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Checks if a value is not 'empty'. 0 doesn't count as empty. |
|
171
|
|
|
* @since 2.2.2 |
|
172
|
|
|
* @param mixed $value Value to check |
|
173
|
|
|
* @return bool True or false |
|
174
|
|
|
*/ |
|
175
|
3 |
|
public function notempty( $value ){ |
|
176
|
3 |
|
return null !== $value && '' !== $value && false !== $value; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Filters out empty values (not including 0). |
|
181
|
|
|
* @since 2.2.2 |
|
182
|
|
|
* @param mixed $value Value to check |
|
183
|
|
|
* @return bool True or false |
|
184
|
|
|
*/ |
|
185
|
2 |
|
function filter_empty( $value ) { |
|
|
|
|
|
|
186
|
2 |
|
return array_filter( $value, array( $this, 'notempty' ) ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Insert a single array item inside another array at a set position |
|
191
|
|
|
* @since 2.0.2 |
|
192
|
|
|
* @param array &$array Array to modify. Is passed by reference, and no return is needed. |
|
193
|
|
|
* @param array $new New array to insert |
|
194
|
|
|
* @param int $position Position in the main array to insert the new array |
|
195
|
|
|
*/ |
|
196
|
2 |
|
public function array_insert( &$array, $new, $position ) { |
|
197
|
2 |
|
$before = array_slice( $array, 0, $position - 1 ); |
|
198
|
2 |
|
$after = array_diff_key( $array, $before ); |
|
199
|
2 |
|
$array = array_merge( $before, $new, $after ); |
|
200
|
2 |
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Defines the url which is used to load local resources. |
|
204
|
|
|
* This may need to be filtered for local Window installations. |
|
205
|
|
|
* If resources do not load, please check the wiki for details. |
|
206
|
|
|
* @since 1.0.1 |
|
207
|
|
|
* @return string URL to CMB2 resources |
|
208
|
|
|
*/ |
|
209
|
2 |
|
public function url( $path = '' ) { |
|
210
|
2 |
|
if ( $this->url ) { |
|
211
|
1 |
|
return $this->url . $path; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
1 |
|
$cmb2_url = self::get_url_from_dir( cmb2_dir() ); |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Filter the CMB location url |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $cmb2_url Currently registered url |
|
220
|
|
|
*/ |
|
221
|
1 |
|
$this->url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) ); |
|
222
|
|
|
|
|
223
|
1 |
|
return $this->url . $path; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Converts a system path to a URL |
|
228
|
|
|
* @since 2.2.2 |
|
229
|
|
|
* @param string $dir Directory path to convert. |
|
230
|
|
|
* @return string Converted URL. |
|
231
|
|
|
*/ |
|
232
|
2 |
|
public static function get_url_from_dir( $dir ) { |
|
233
|
2 |
|
$dir = self::normalize_path( $dir ); |
|
234
|
|
|
|
|
235
|
|
|
// Let's test if We are in the plugins or mu-plugins dir. |
|
236
|
2 |
|
$test_dir = trailingslashit( $dir ) . 'unneeded.php'; |
|
237
|
|
|
if ( |
|
238
|
2 |
|
0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) ) |
|
239
|
2 |
|
|| 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) ) |
|
240
|
2 |
|
) { |
|
241
|
|
|
// Ok, then use plugins_url, as it is more reliable. |
|
242
|
1 |
|
return trailingslashit( plugins_url( '', $test_dir ) ); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
// Ok, now let's test if we are in the theme dir. |
|
246
|
2 |
|
$theme_root = get_theme_root(); |
|
247
|
2 |
|
if ( 0 === strpos( $dir, $theme_root ) ) { |
|
248
|
|
|
// Ok, then use get_theme_root_uri. |
|
249
|
1 |
|
return set_url_scheme( trailingslashit( str_replace( $theme_root, get_theme_root_uri(), $dir ) ) ); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
// Check to see if it's anywhere in the root directory |
|
253
|
|
|
|
|
254
|
2 |
|
$site_dir = ABSPATH; |
|
255
|
2 |
|
$site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() ); |
|
256
|
|
|
|
|
257
|
2 |
|
$url = str_replace( |
|
258
|
2 |
|
array( $site_dir, WP_PLUGIN_DIR ), |
|
259
|
2 |
|
array( $site_url, WP_PLUGIN_URL ), |
|
260
|
|
|
$dir |
|
261
|
2 |
|
); |
|
262
|
|
|
|
|
263
|
2 |
|
return set_url_scheme( $url ); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
|
268
|
|
|
* |
|
269
|
|
|
* On windows systems, replaces backslashes with forward slashes |
|
270
|
|
|
* and forces upper-case drive letters. |
|
271
|
|
|
* Allows for two leading slashes for Windows network shares, but |
|
272
|
|
|
* ensures that all other duplicate slashes are reduced to a single. |
|
273
|
|
|
* |
|
274
|
|
|
* @since 2.2.0 |
|
275
|
|
|
* |
|
276
|
|
|
* @param string $path Path to normalize. |
|
277
|
|
|
* @return string Normalized path. |
|
278
|
|
|
*/ |
|
279
|
2 |
|
protected static function normalize_path( $path ) { |
|
280
|
2 |
|
if ( function_exists( 'wp_normalize_path' ) ) { |
|
281
|
|
|
return wp_normalize_path( $path ); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
// Replace newer WP's version of wp_normalize_path. |
|
285
|
2 |
|
$path = str_replace( '\\', '/', $path ); |
|
286
|
2 |
|
$path = preg_replace( '|(?<=.)/+|', '/', $path ); |
|
287
|
2 |
|
if ( ':' === substr( $path, 1, 1 ) ) { |
|
288
|
|
|
$path = ucfirst( $path ); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
2 |
|
return $path; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Get timestamp from text date |
|
296
|
|
|
* @since 2.2.0 |
|
297
|
|
|
* @param string $value Date value |
|
298
|
|
|
* @param string $date_format Expected date format |
|
299
|
|
|
* @return mixed Unix timestamp representing the date. |
|
300
|
|
|
*/ |
|
301
|
|
|
public function get_timestamp_from_value( $value, $date_format ) { |
|
302
|
|
|
$date_object = date_create_from_format( $date_format, $value ); |
|
303
|
|
|
return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value ); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
|
308
|
|
|
* It will work with only with the following subset ot date() options: |
|
309
|
|
|
* |
|
310
|
|
|
* d, j, z, m, n, y, and Y. |
|
311
|
|
|
* |
|
312
|
|
|
* A slight effort is made to deal with escaped characters. |
|
313
|
|
|
* |
|
314
|
|
|
* Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
|
315
|
|
|
* bring even more translation troubles. |
|
316
|
|
|
* |
|
317
|
|
|
* @since 2.2.0 |
|
318
|
|
|
* @param string $format php date format |
|
319
|
|
|
* @return string reformatted string |
|
320
|
|
|
*/ |
|
321
|
5 |
|
public function php_to_js_dateformat( $format ) { |
|
322
|
|
|
|
|
323
|
|
|
// order is relevant here, since the replacement will be done sequentially. |
|
324
|
|
|
$supported_options = array( |
|
325
|
5 |
|
'd' => 'dd', // Day, leading 0 |
|
326
|
5 |
|
'j' => 'd', // Day, no 0 |
|
327
|
5 |
|
'z' => 'o', // Day of the year, no leading zeroes, |
|
328
|
|
|
// 'D' => 'D', // Day name short, not sure how it'll work with translations |
|
|
|
|
|
|
329
|
|
|
// 'l' => 'DD', // Day name full, idem before |
|
|
|
|
|
|
330
|
5 |
|
'm' => 'mm', // Month of the year, leading 0 |
|
331
|
5 |
|
'n' => 'm', // Month of the year, no leading 0 |
|
332
|
|
|
// 'M' => 'M', // Month, Short name |
|
|
|
|
|
|
333
|
|
|
// 'F' => 'MM', // Month, full name, |
|
|
|
|
|
|
334
|
5 |
|
'y' => 'y', // Year, two digit |
|
335
|
5 |
|
'Y' => 'yy', // Year, full |
|
336
|
5 |
|
'H' => 'HH', // Hour with leading 0 (24 hour) |
|
337
|
5 |
|
'G' => 'H', // Hour with no leading 0 (24 hour) |
|
338
|
5 |
|
'h' => 'hh', // Hour with leading 0 (12 hour) |
|
339
|
5 |
|
'g' => 'h', // Hour with no leading 0 (12 hour), |
|
340
|
5 |
|
'i' => 'mm', // Minute with leading 0, |
|
341
|
5 |
|
's' => 'ss', // Second with leading 0, |
|
342
|
5 |
|
'a' => 'tt', // am/pm |
|
343
|
|
|
'A' => 'TT' // AM/PM |
|
344
|
5 |
|
); |
|
345
|
|
|
|
|
346
|
5 |
|
foreach ( $supported_options as $php => $js ) { |
|
347
|
|
|
// replaces every instance of a supported option, but skips escaped characters |
|
348
|
5 |
|
$format = preg_replace( "~(?<!\\\\)$php~", $js, $format ); |
|
349
|
5 |
|
} |
|
350
|
|
|
|
|
351
|
5 |
|
$format = preg_replace_callback( '~(?:\\\.)+~', array( $this, 'wrap_escaped_chars' ), $format ); |
|
352
|
|
|
|
|
353
|
5 |
|
return $format; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
|
358
|
|
|
* @since 2.2.0 |
|
359
|
|
|
* @param $value Value to wrap/escape |
|
360
|
|
|
* @return string Modified value |
|
361
|
|
|
*/ |
|
362
|
4 |
|
public function wrap_escaped_chars( $value ) { |
|
363
|
4 |
|
return "'" . str_replace( '\\', '', $value[0] ) . "'"; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* Send to debug.log if WP_DEBUG is defined and true |
|
368
|
|
|
* |
|
369
|
|
|
* @since 2.2.0 |
|
370
|
|
|
* |
|
371
|
|
|
* @param string $function Function name |
|
372
|
|
|
* @param int $line Line number |
|
373
|
|
|
* @param mixed $msg Message to output |
|
374
|
|
|
* @param mixed $debug Variable to print_r |
|
375
|
|
|
*/ |
|
376
|
|
|
public function log_if_debug( $function, $line, $msg, $debug = null ) { |
|
377
|
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
|
378
|
|
|
error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) ); |
|
379
|
|
|
} |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Determine a file's extension |
|
384
|
|
|
* @since 1.0.0 |
|
385
|
|
|
* @param string $file File url |
|
386
|
|
|
* @return string|false File extension or false |
|
387
|
|
|
*/ |
|
388
|
4 |
|
public function get_file_ext( $file ) { |
|
389
|
4 |
|
$parsed = @parse_url( $file, PHP_URL_PATH ); |
|
390
|
4 |
|
return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* Get the file name from a url |
|
395
|
|
|
* @since 2.0.0 |
|
396
|
|
|
* @param string $value File url or path |
|
397
|
|
|
* @return string File name |
|
398
|
|
|
*/ |
|
399
|
3 |
|
public function get_file_name_from_path( $value ) { |
|
400
|
3 |
|
$parts = explode( '/', $value ); |
|
401
|
3 |
|
return is_array( $parts ) ? end( $parts ) : $value; |
|
402
|
|
|
} |
|
403
|
|
|
} |
|
404
|
|
|
|
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.