Completed
Pull Request — trunk (#731)
by Rami
06:34
created

CMB2_Utils::log_if_debug()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 3
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 20
rs 9.2
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
	/**
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 that returns time string offset by timezone
80
	 * @since  1.0.0
81
	 * @param  string $tzstring Time string
82
	 * @return string           Offset time string
83
	 */
84 2
	public static function timezone_offset( $tzstring ) {
85 2
		$tz_offset = 0;
86
87 2
		if ( ! empty( $tzstring ) && is_string( $tzstring ) ) {
88 2
			if ( 'UTC' === substr( $tzstring, 0, 3 ) ) {
89 1
				$tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring );
90 1
				return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS );
91
			}
92
93
			try {
94 1
				$date_time_zone_selected = new DateTimeZone( $tzstring );
95 1
				$tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
96 1
			} catch ( Exception $e ) {
97
				self::log_if_debug( __METHOD__, __LINE__, $e->getMessage() );
98
			}
99
100 1
		}
101
102 1
		return $tz_offset;
103
	}
104
105
	/**
106
	 * Utility method that returns a timezone string representing the default timezone for the site.
107
	 *
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 1
	public static function timezone_string() {
117 1
		$current_offset = get_option( 'gmt_offset' );
118 1
		$tzstring       = get_option( 'timezone_string' );
119
120
		// Remove old Etc mappings. Fallback to gmt_offset.
121 1
		if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) {
122
			$tzstring = '';
123
		}
124
125 1
		if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
126 1
			if ( 0 == $current_offset ) {
127 1
				$tzstring = 'UTC+0';
128 1
			} elseif ( $current_offset < 0 ) {
129
				$tzstring = 'UTC' . $current_offset;
130
			} else {
131
				$tzstring = 'UTC+' . $current_offset;
132
			}
133 1
		}
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
	 * @return int   	            Time stamp
143
	 */
144 10
	public static function make_valid_time_stamp( $string ) {
145 10
		if ( ! $string ) {
146
			return 0;
147
		}
148
149 10
		return self::is_valid_time_stamp( $string )
150 10
			? (int) $string :
151 10
			strtotime( (string) $string );
152
	}
153
154
	/**
155
	 * Determine if a value is a valid timestamp
156
	 * @since  2.0.0
157
	 * @param  mixed  $timestamp Value to check
158
	 * @return boolean           Whether value is a valid timestamp
159
	 */
160 10
	public static function is_valid_time_stamp( $timestamp ) {
161 10
		return (string) (int) $timestamp === (string) $timestamp
162 10
			&& $timestamp <= PHP_INT_MAX
163 10
			&& $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 58
	public static function isempty( $value ) {
173 58
		return null === $value || '' === $value || false === $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 3
	public static function notempty( $value ){
183 3
		return null !== $value && '' !== $value && false !== $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 2
	public static function filter_empty( $value ) {
193 2
		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 2
	public static function array_insert( &$array, $new, $position ) {
204 2
		$before = array_slice( $array, 0, $position - 1 );
205 2
		$after  = array_diff_key( $array, $before );
206 2
		$array  = array_merge( $before, $new, $after );
207 2
	}
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 2
	public static function url( $path = '' ) {
217 2
		if ( self::$url ) {
218 1
			return self::$url . $path;
219
		}
220
221 1
		$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 1
		self::$url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) );
229
230 1
		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 2
	public static function get_url_from_dir( $dir ) {
240 2
		$dir = self::normalize_path( $dir );
241
242
		// Let's test if We are in the plugins or mu-plugins dir.
243 2
		$test_dir = trailingslashit( $dir ) . 'unneeded.php';
244
		if (
245 2
			0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) )
246 2
			|| 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) )
247 2
		) {
248
			// Ok, then use plugins_url, as it is more reliable.
249 1
			return trailingslashit( plugins_url( '', $test_dir ) );
250
		}
251
252
		// Ok, now let's test if we are in the theme dir.
253 2
		$theme_root = self::normalize_path( get_theme_root() );
254 2
		if ( 0 === strpos( $dir, $theme_root ) ) {
255
			// Ok, then use get_theme_root_uri.
256 1
			return set_url_scheme(
257 1
				trailingslashit(
258 1
					str_replace(
259 1
						untrailingslashit( $theme_root ),
260 1
						untrailingslashit( get_theme_root_uri() ),
261
						$dir
262 1
					)
263 1
				)
264 1
			);
265
		}
266
267
		// Check to see if it's anywhere in the root directory
268
269 2
		$site_dir = self::normalize_path( self::$ABSPATH );
270 2
		$site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() );
271
272 2
		$url = str_replace(
273 2
			array( $site_dir, WP_PLUGIN_DIR ),
274 2
			array( $site_url, WP_PLUGIN_URL ),
275
			$dir
276 2
		);
277
278 2
		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 2
	protected static function normalize_path( $path ) {
295 2
		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 2
		$path = str_replace( '\\', '/', $path );
301 2
		$path = preg_replace( '|(?<=.)/+|', '/', $path );
302 2
		if ( ':' === substr( $path, 1, 1 ) ) {
303 1
			$path = ucfirst( $path );
304 1
		}
305
306 2
		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 5
	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 5
			'd' => 'dd',  // Day, leading 0
341 5
			'j' => 'd',   // Day, no 0
342 5
			'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 5
			'm' => 'mm',  // Month of the year, leading 0
346 5
			'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 5
			'y' => 'y',   // Year, two digit
350 5
			'Y' => 'yy',  // Year, full
351 5
			'H' => 'HH',  // Hour with leading 0 (24 hour)
352 5
			'G' => 'H',   // Hour with no leading 0 (24 hour)
353 5
			'h' => 'hh',  // Hour with leading 0 (12 hour)
354 5
			'g' => 'h',   // Hour with no leading 0 (12 hour),
355 5
			'i' => 'mm',  // Minute with leading 0,
356 5
			's' => 'ss',  // Second with leading 0,
357 5
			'a' => 'tt',  // am/pm
358
			'A' => 'TT'   // AM/PM
359 5
		);
360
361 5
		foreach ( $supported_options as $php => $js ) {
362
			// replaces every instance of a supported option, but skips escaped characters
363 5
			$format = preg_replace( "~(?<!\\\\)$php~", $js, $format );
364 5
		}
365
366 5
		$format = preg_replace_callback( '~(?:\\\.)+~', array( __CLASS__, 'wrap_escaped_chars' ), $format );
367
368 5
		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 4
	public static function wrap_escaped_chars( $value ) {
378 4
		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 6
	public static function get_file_ext( $file ) {
404 6
		$parsed = @parse_url( $file, PHP_URL_PATH );
405 6
		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 5
	public static function get_file_name_from_path( $value ) {
415 5
		$parts = explode( '/', $value );
416 5
		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 6
	public static function wp_at_least( $version ) {
426 6
		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 45
	public static function concat_attrs( $attrs, $attr_exclude = array() ) {
437 45
		$attr_exclude[] = 'rendered';
438 45
		$attributes = '';
439 45
		foreach ( $attrs as $attr => $val ) {
440 45
			$excluded = in_array( $attr, (array) $attr_exclude, true );
441 45
			$empty    = false === $val && 'value' !== $attr;
442 45
			if ( ! $excluded && ! $empty ) {
443
				// if data attribute, use single quote wraps, else double
444 45
				$quotes = false !== stripos( $attr, 'data-' ) ? "'" : '"';
445 45
				$attributes .= sprintf( ' %1$s=%3$s%2$s%3$s', $attr, $val, $quotes );
446 45
			}
447 45
		}
448 45
		return $attributes;
449
	}
450
451
}
452