Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Utils
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 json_encode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 unslash
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 parse_url
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Automattic\Jetpack_Boost\Lib\Minify;
4
5class Utils {
6
7    /**
8     * Indicates whether to use the WordPress functions.
9     *
10     * @var bool $use_wp Whether to use WordPress functions.
11     */
12    private $use_wp;
13
14    /**
15     * Utils constructor.
16     *
17     * @param bool $use_wp Whether to use WordPress functions. Default is true.
18     */
19    public function __construct( $use_wp = true ) {
20        $this->use_wp = $use_wp;
21    }
22
23    /**
24     * Encodes a value to JSON.
25     *
26     * @param mixed $value   The value to encode.
27     * @param int   $flags   Options to be passed to json_encode(). Default 0.
28     * @param int   $depth   Maximum depth to walk through $value. Must be greater than 0.
29     *
30     * @return string|false The JSON-encoded string, or false on failure.
31     */
32    public function json_encode( $value, $flags = 0, $depth = 512 ) {
33        if ( $this->use_wp ) {
34            return wp_json_encode( $value, $flags, $depth );
35        }
36
37        // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
38        return json_encode( $value, $flags, $depth );
39    }
40
41    /**
42     * Removes slashes from a string or an array of strings.
43     *
44     * @param mixed $value The string or array of strings to remove slashes from.
45     *
46     * @return mixed The string or array of strings with slashes removed.
47     */
48    public function unslash( $value ) {
49        if ( $this->use_wp ) {
50            return wp_unslash( $value );
51        }
52
53        return is_string( $value ) ? stripslashes( $value ) : $value;
54    }
55
56    /**
57     * Parses a URL and returns its components.
58     *
59     * @param string $url       The URL to parse.
60     * @param int    $component Optional. The specific component to retrieve.
61     *                          Use one of the PHP_URL_* constants. Default is -1 (all components).
62     *
63     * @return mixed|array|string|null The parsed URL component(s), or the entire URL string if $component is -1.
64     */
65    public function parse_url( $url, $component = -1 ) {
66        if ( $this->use_wp ) {
67            return wp_parse_url( $url, $component );
68        }
69
70        // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
71        return parse_url( $url, $component );
72    }
73}