PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.8
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.8
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / functions / compat.php

compat.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.9.8, at inc/functions/compat.php

546 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3
4 /** --------------------------------------------------------------------------------------------- */
5 /** PHP ========================================================================================= */
6 /** --------------------------------------------------------------------------------------------- */
7
8 if ( ! function_exists( 'curl_file_create' ) ) :
9 /**
10 * PHP-agnostic version of curl_file_create(): create a CURLFile object.
11 *
12 * @since 1.0
13 * @since PHP 5.5
14 * @source http://dk2.php.net/manual/en/function.curl-file-create.php
15 *
16 * @param string $filename Path to the file which will be uploaded.
17 * @param string $mimetype Mimetype of the file.
18 * @param string $postname Name of the file to be used in the upload data.
19 * @return string The CURLFile object.
20 */
21 function curl_file_create( $filename, $mimetype = '', $postname = '' ) {
22 return "@$filename;filename="
23 . ( $postname ? $postname : basename( $filename ) )
24 . ( $mimetype ? ";type=$mimetype" : '' );
25 }
26 endif;
27
28 /** --------------------------------------------------------------------------------------------- */
29 /** WORDPRESS =================================================================================== */
30 /** --------------------------------------------------------------------------------------------- */
31
32 if ( ! function_exists( 'wp_json_encode' ) ) :
33 /**
34 * Encode a variable into JSON, with some sanity checks.
35 *
36 * @since 1.6.5
37 * @since WP 4.1.0
38 *
39 * @param mixed $data Variable (usually an array or object) to encode as JSON.
40 * @param int $options Optional. Options to be passed to json_encode(). Default 0.
41 * @param int $depth Optional. Maximum depth to walk through $data. Must be greater than 0. Default 512.
42 * @return string|false The JSON encoded string, or false if it cannot be encoded.
43 */
44 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
45 /*
46 * json_encode() has had extra params added over the years.
47 * $options was added in 5.3, and $depth in 5.5.
48 * We need to make sure we call it with the correct arguments.
49 */
50 if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
51 $args = array( $data, $options, $depth );
52 } elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
53 $args = array( $data, $options );
54 } else {
55 $args = array( $data );
56 }
57
58 // Prepare the data for JSON serialization.
59 $args[0] = _wp_json_prepare_data( $data );
60
61 $json = @call_user_func_array( 'json_encode', $args );
62
63 // If json_encode() was successful, no need to do more sanity checking.
64 // ... unless we're in an old version of PHP, and json_encode() returned
65 // a string containing 'null'. Then we need to do more sanity checking.
66 if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) {
67 return $json;
68 }
69
70 try {
71 $args[0] = _wp_json_sanity_check( $data, $depth );
72 } catch ( Exception $e ) {
73 return false;
74 }
75
76 return call_user_func_array( 'json_encode', $args );
77 }
78 endif;
79
80 if ( ! function_exists( '_wp_json_prepare_data' ) ) :
81 /**
82 * Prepares response data to be serialized to JSON.
83 *
84 * This supports the JsonSerializable interface for PHP 5.2-5.3 as well.
85 *
86 * @since 1.6.5
87 * @since WP 4.4.0
88 * @access private
89 *
90 * @param mixed $data Native representation.
91 * @return bool|int|float|null|string|array Data ready for `json_encode()`.
92 */
93 function _wp_json_prepare_data( $data ) {
94 if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) {
95 return $data;
96 }
97
98 switch ( gettype( $data ) ) {
99 case 'boolean':
100 case 'integer':
101 case 'double':
102 case 'string':
103 case 'NULL':
104 // These values can be passed through.
105 return $data;
106
107 case 'array':
108 // Arrays must be mapped in case they also return objects.
109 return array_map( '_wp_json_prepare_data', $data );
110
111 case 'object':
112 // If this is an incomplete object (__PHP_Incomplete_Class), bail.
113 if ( ! is_object( $data ) ) {
114 return null;
115 }
116
117 if ( $data instanceof JsonSerializable ) {
118 $data = $data->jsonSerialize();
119 } else {
120 $data = get_object_vars( $data );
121 }
122
123 // Now, pass the array (or whatever was returned from jsonSerialize through).
124 return _wp_json_prepare_data( $data );
125
126 default:
127 return null;
128 }
129 }
130 endif;
131
132 if ( ! function_exists( '_wp_json_sanity_check' ) ) :
133 /**
134 * Perform sanity checks on data that shall be encoded to JSON.
135 *
136 * @since 1.6.5
137 * @since WP 4.1.0
138 * @access private
139 * @throws Exception If the depth limit is reached.
140 *
141 * @see wp_json_encode()
142 *
143 * @param mixed $data Variable (usually an array or object) to encode as JSON.
144 * @param int $depth Maximum depth to walk through $data. Must be greater than 0.
145 * @return mixed The sanitized data that shall be encoded to JSON.
146 */
147 function _wp_json_sanity_check( $data, $depth ) {
148 if ( $depth < 0 ) {
149 throw new Exception( 'Reached depth limit' );
150 }
151
152 if ( is_array( $data ) ) {
153 $output = array();
154 foreach ( $data as $id => $el ) {
155 // Don't forget to sanitize the ID!
156 if ( is_string( $id ) ) {
157 $clean_id = _wp_json_convert_string( $id );
158 } else {
159 $clean_id = $id;
160 }
161
162 // Check the element type, so that we're only recursing if we really have to.
163 if ( is_array( $el ) || is_object( $el ) ) {
164 $output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 );
165 } elseif ( is_string( $el ) ) {
166 $output[ $clean_id ] = _wp_json_convert_string( $el );
167 } else {
168 $output[ $clean_id ] = $el;
169 }
170 }
171 } elseif ( is_object( $data ) ) {
172 $output = new stdClass();
173 foreach ( $data as $id => $el ) {
174 if ( is_string( $id ) ) {
175 $clean_id = _wp_json_convert_string( $id );
176 } else {
177 $clean_id = $id;
178 }
179
180 if ( is_array( $el ) || is_object( $el ) ) {
181 $output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 );
182 } elseif ( is_string( $el ) ) {
183 $output->$clean_id = _wp_json_convert_string( $el );
184 } else {
185 $output->$clean_id = $el;
186 }
187 }
188 } elseif ( is_string( $data ) ) {
189 return _wp_json_convert_string( $data );
190 } else {
191 return $data;
192 } // End if().
193
194 return $output;
195 }
196 endif;
197
198 if ( ! function_exists( '_wp_json_convert_string' ) ) :
199 /**
200 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
201 *
202 * @since 1.6.5
203 * @since WP 4.1.0
204 * @access private
205 *
206 * @see _wp_json_sanity_check()
207 *
208 * @staticvar bool $use_mb
209 *
210 * @param string $string The string which is to be converted.
211 * @return string The checked string.
212 */
213 function _wp_json_convert_string( $string ) {
214 static $use_mb = null;
215 if ( is_null( $use_mb ) ) {
216 $use_mb = function_exists( 'mb_convert_encoding' );
217 }
218
219 if ( $use_mb ) {
220 $encoding = mb_detect_encoding( $string, mb_detect_order(), true );
221 if ( $encoding ) {
222 return mb_convert_encoding( $string, 'UTF-8', $encoding );
223 } else {
224 return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' );
225 }
226 } else {
227 return wp_check_invalid_utf8( $string, true );
228 }
229 }
230 endif;
231
232 if ( ! function_exists( 'wp_parse_url' ) ) :
233 /**
234 * A wrapper for PHP's parse_url() function that handles consistency in the return
235 * values across PHP versions.
236 *
237 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
238 * schemeless and relative url's with :// in the path. This function works around
239 * those limitations providing a standard output on PHP 5.2~5.4+.
240 *
241 * Secondly, across various PHP versions, schemeless URLs starting containing a ":"
242 * in the query are being handled inconsistently. This function works around those
243 * differences as well.
244 *
245 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
246 * when URL parsing failed.
247 *
248 * @since 1.6.9
249 * @since WP 4.4.0
250 * @since WP 4.7.0 The $component parameter was added for parity with PHP's parse_url().
251 *
252 * @param (string) $url The URL to parse.
253 * @param (int) $component The specific component to retrieve. Use one of the PHP
254 * predefined constants to specify which one.
255 * Defaults to -1 (= return all parts as an array).
256 * @see http://php.net/manual/en/function.parse-url.php
257 *
258 * @return (mixed) False on parse failure; Array of URL components on success;
259 * When a specific component has been requested: null if the component
260 * doesn't exist in the given URL; a sting or - in the case of
261 * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
262 */
263 function wp_parse_url( $url, $component = -1 ) {
264 $to_unset = array();
265 $url = strval( $url );
266
267 if ( '//' === substr( $url, 0, 2 ) ) {
268 $to_unset[] = 'scheme';
269 $url = 'placeholder:' . $url;
270 } elseif ( '/' === substr( $url, 0, 1 ) ) {
271 $to_unset[] = 'scheme';
272 $to_unset[] = 'host';
273 $url = 'placeholder://placeholder' . $url;
274 }
275
276 $parts = @parse_url( $url );
277
278 if ( false === $parts ) {
279 // Parsing failure.
280 return $parts;
281 }
282
283 // Remove the placeholder values.
284 if ( $to_unset ) {
285 foreach ( $to_unset as $key ) {
286 unset( $parts[ $key ] );
287 }
288 }
289
290 return _get_component_from_parsed_url_array( $parts, $component );
291 }
292 endif;
293
294 if ( ! function_exists( '_get_component_from_parsed_url_array' ) ) :
295 /**
296 * Retrieve a specific component from a parsed URL array.
297 *
298 * @since 1.6.9
299 * @since WP 4.7.0
300 *
301 * @param (array|false) $url_parts The parsed URL. Can be false if the URL failed to parse.
302 * @param (int) $component The specific component to retrieve. Use one of the PHP
303 * predefined constants to specify which one.
304 * Defaults to -1 (= return all parts as an array).
305 * @see http://php.net/manual/en/function.parse-url.php
306 *
307 * @return (mixed) False on parse failure; Array of URL components on success;
308 * When a specific component has been requested: null if the component
309 * doesn't exist in the given URL; a sting or - in the case of
310 * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
311 */
312 function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
313 if ( -1 === $component ) {
314 return $url_parts;
315 }
316
317 $key = _wp_translate_php_url_constant_to_key( $component );
318
319 if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) {
320 return $url_parts[ $key ];
321 } else {
322 return null;
323 }
324 }
325 endif;
326
327 if ( ! function_exists( '_wp_translate_php_url_constant_to_key' ) ) :
328 /**
329 * Translate a PHP_URL_* constant to the named array keys PHP uses.
330 *
331 * @since 1.6.9
332 * @since WP 4.7.0
333 * @see http://php.net/manual/en/url.constants.php
334 *
335 * @param (int) $constant PHP_URL_* constant.
336 *
337 * @return (string|bool) The named key or false.
338 */
339 function _wp_translate_php_url_constant_to_key( $constant ) {
340 $translation = array(
341 PHP_URL_SCHEME => 'scheme',
342 PHP_URL_HOST => 'host',
343 PHP_URL_PORT => 'port',
344 PHP_URL_USER => 'user',
345 PHP_URL_PASS => 'pass',
346 PHP_URL_PATH => 'path',
347 PHP_URL_QUERY => 'query',
348 PHP_URL_FRAGMENT => 'fragment',
349 );
350
351 if ( isset( $translation[ $constant ] ) ) {
352 return $translation[ $constant ];
353 } else {
354 return false;
355 }
356 }
357 endif;
358
359 if ( ! function_exists( 'wp_get_additional_image_sizes' ) ) :
360 /**
361 * Retrieve additional image sizes.
362 *
363 * @since 1.6.10
364 * @since WP 4.7.0
365 *
366 * @global array $_wp_additional_image_sizes
367 *
368 * @return array Additional images size data.
369 */
370 function wp_get_additional_image_sizes() {
371 global $_wp_additional_image_sizes;
372 if ( ! $_wp_additional_image_sizes ) {
373 $_wp_additional_image_sizes = array(); // WPCS: override ok.
374 }
375 return $_wp_additional_image_sizes;
376 }
377 endif;
378
379 if ( ! function_exists( 'wp_scripts' ) ) :
380 /**
381 * Initialize $wp_scripts if it has not been set.
382 *
383 * @global WP_Scripts $wp_scripts
384 *
385 * @since 1.6.11
386 * @since WP 4.2.0
387 *
388 * @return WP_Scripts WP_Scripts instance.
389 */
390 function wp_scripts() {
391 global $wp_scripts;
392 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
393 $wp_scripts = new WP_Scripts(); // WPCS: override ok.
394 }
395 return $wp_scripts;
396 }
397 endif;
398
399 if ( ! function_exists( 'wp_doing_ajax' ) ) :
400 /**
401 * Determines whether the current request is a WordPress Ajax request.
402 *
403 * @since 1.7
404 * @since WP 4.7.0
405 *
406 * @return bool True if it's a WordPress Ajax request, false otherwise.
407 */
408 function wp_doing_ajax() {
409 /**
410 * Filters whether the current request is a WordPress Ajax request.
411 *
412 * @since 1.7
413 * @since WP 4.7.0
414 *
415 * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
416 */
417 return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
418 }
419 endif;
420
421 if ( ! function_exists( '_deprecated_hook' ) ) :
422 /**
423 * Marks a deprecated action or filter hook as deprecated and throws a notice.
424 *
425 * Use the {@see 'deprecated_hook_run'} action to get the backtrace describing where
426 * the deprecated hook was called.
427 *
428 * Default behavior is to trigger a user error if `WP_DEBUG` is true.
429 *
430 * This function is called by the do_action_deprecated() and apply_filters_deprecated()
431 * functions, and so generally does not need to be called directly.
432 *
433 * @since 1.7
434 * @since WP 4.6.0
435 * @access private
436 *
437 * @param string $hook The hook that was used.
438 * @param string $version The version of WordPress that deprecated the hook.
439 * @param string $replacement Optional. The hook that should have been used.
440 * @param string $message Optional. A message regarding the change.
441 */
442 function _deprecated_hook( $hook, $version, $replacement = null, $message = null ) {
443 /**
444 * Fires when a deprecated hook is called.
445 *
446 * @since 1.7
447 * @since WP 4.6.0
448 *
449 * @param string $hook The hook that was called.
450 * @param string $replacement The hook that should be used as a replacement.
451 * @param string $version The version of WordPress that deprecated the argument used.
452 * @param string $message A message regarding the change.
453 */
454 do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message );
455
456 /**
457 * Filters whether to trigger deprecated hook errors.
458 *
459 * @since 1.7
460 * @since WP 4.6.0
461 *
462 * @param bool $trigger Whether to trigger deprecated hook errors. Requires
463 * `WP_DEBUG` to be defined true.
464 */
465 if ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) {
466 $message = empty( $message ) ? '' : ' ' . $message;
467 if ( ! is_null( $replacement ) ) {
468 /* translators: 1: WordPress hook name, 2: version number, 3: alternative hook name */
469 trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', 'imagify' ), $hook, $version, $replacement ) . $message );
470 } else {
471 /* translators: 1: WordPress hook name, 2: version number */
472 trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', 'imagify' ), $hook, $version ) . $message );
473 }
474 }
475 }
476 endif;
477
478 if ( ! function_exists( 'do_action_deprecated' ) ) :
479 /**
480 * Fires functions attached to a deprecated action hook.
481 *
482 * When an action hook is deprecated, the do_action() call is replaced with
483 * do_action_deprecated(), which triggers a deprecation notice and then fires
484 * the original hook.
485 *
486 * @since 1.9
487 * @since WP 4.6.0
488 *
489 * @see _deprecated_hook()
490 *
491 * @param string $tag The name of the action hook.
492 * @param array $args Array of additional function arguments to be passed to do_action().
493 * @param string $version The version of WordPress that deprecated the hook.
494 * @param string $replacement Optional. The hook that should have been used.
495 * @param string $message Optional. A message regarding the change.
496 */
497 function do_action_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
498 if ( ! has_action( $tag ) ) {
499 return;
500 }
501
502 _deprecated_hook( $tag, $version, $replacement, $message );
503
504 do_action_ref_array( $tag, $args );
505 }
506 endif;
507
508 if ( ! function_exists( 'apply_filters_deprecated' ) ) :
509 /**
510 * Fires functions attached to a deprecated filter hook.
511 *
512 * When a filter hook is deprecated, the apply_filters() call is replaced with
513 * apply_filters_deprecated(), which triggers a deprecation notice and then fires
514 * the original filter hook.
515 *
516 * Note: the value and extra arguments passed to the original apply_filters() call
517 * must be passed here to `$args` as an array. For example:
518 *
519 * // Old filter.
520 * return apply_filters( 'wpdocs_filter', $value, $extra_arg );
521 *
522 * // Deprecated.
523 * return apply_filters_deprecated( 'wpdocs_filter', array( $value, $extra_arg ), '4.9', 'wpdocs_new_filter' );
524 *
525 * @since 1.7
526 * @since WP 4.6.0
527 *
528 * @see _deprecated_hook()
529 *
530 * @param string $tag The name of the filter hook.
531 * @param array $args Array of additional function arguments to be passed to apply_filters().
532 * @param string $version The version of WordPress that deprecated the hook.
533 * @param string $replacement Optional. The hook that should have been used. Default false.
534 * @param string $message Optional. A message regarding the change. Default null.
535 */
536 function apply_filters_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
537 if ( ! has_filter( $tag ) ) {
538 return $args[0];
539 }
540
541 _deprecated_hook( $tag, $version, $replacement, $message );
542
543 return apply_filters_ref_array( $tag, $args );
544 }
545 endif;
546