Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.86% covered (warning)
67.86%
38 / 56
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Urls
67.86% covered (warning)
67.86%
38 / 56
50.00% covered (danger)
50.00%
4 / 8
66.01
0.00% covered (danger)
0.00%
0 / 1
 get_raw_url
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 normalize_www_in_url
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 get_protocol_normalized_url
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
8.09
 is_request_scheme_unknown
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 get_raw_or_filtered_url
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
6.04
 home_url
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 site_url
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 main_network_site_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 maybe_add_origin_site_id
n/a
0 / 0
n/a
0 / 0
1
1<?php
2/**
3 * The Jetpack Connection package Urls class file.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8namespace Automattic\Jetpack\Connection;
9
10use Automattic\Jetpack\Constants;
11
12/**
13 * Provides Url methods for the Connection package.
14 */
15class Urls {
16
17    const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_';
18    const HTTPS_CHECK_HISTORY       = 5;
19
20    /**
21     * Return URL from option or PHP constant.
22     *
23     * @param string $option_name (e.g. 'home').
24     *
25     * @return mixed|null URL.
26     */
27    public static function get_raw_url( $option_name ) {
28        $value    = null;
29        $constant = ( 'home' === $option_name )
30            ? 'WP_HOME'
31            : 'WP_SITEURL';
32
33        // Since we disregard the constant for multisites in ms-default-filters.php,
34        // let's also use the db value if this is a multisite.
35        if ( ! is_multisite() && Constants::is_defined( $constant ) ) {
36            $value = Constants::get_constant( $constant );
37        } else {
38            // Let's get the option from the database so that we can bypass filters. This will help
39            // ensure that we get more uniform values.
40            $value = \Jetpack_Options::get_raw_option( $option_name );
41        }
42
43        return $value;
44    }
45
46    /**
47     * Normalize domains by removing www unless declared in the site's option.
48     *
49     * @param string   $option Option value from the site.
50     * @param callable $url_function Function retrieving the URL to normalize.
51     * @return mixed|string URL.
52     */
53    public static function normalize_www_in_url( $option, $url_function ) {
54        $url        = wp_parse_url( call_user_func( $url_function ) );
55        $option_url = wp_parse_url( get_option( $option ) );
56
57        if ( ! $option_url || ! $url ) {
58            return $url;
59        }
60
61        if ( "www.{$option_url[ 'host' ]}" === $url['host'] ) {
62            // remove www if not present in option URL.
63            $url['host'] = $option_url['host'];
64        }
65        if ( "www.{$url[ 'host' ]}" === $option_url['host'] ) {
66            // add www if present in option URL.
67            $url['host'] = $option_url['host'];
68        }
69
70        $normalized_url = "{$url['scheme']}://{$url['host']}";
71        if ( isset( $url['path'] ) ) {
72            $normalized_url .= "{$url['path']}";
73        }
74
75        if ( isset( $url['query'] ) ) {
76            $normalized_url .= "?{$url['query']}";
77        }
78
79        return $normalized_url;
80    }
81
82    /**
83     * Return URL with a normalized protocol.
84     *
85     * @param string $callable Function name that was used to retrieve URL option.
86     * @param string $new_value URL Protocol to set URLs to.
87     * @return string Normalized URL.
88     */
89    public static function get_protocol_normalized_url( $callable, $new_value ) {
90        $option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
91
92        $parsed_url = wp_parse_url( $new_value );
93
94        if ( ! $parsed_url ) {
95            return $new_value;
96        }
97        if ( array_key_exists( 'scheme', $parsed_url ) ) {
98            $scheme = $parsed_url['scheme'];
99        } else {
100            $scheme = '';
101        }
102        $scheme_history = get_option( $option_key, array() );
103
104        if ( ! is_array( $scheme_history ) ) {
105            $scheme_history = array();
106        }
107
108        // Don't record a scheme that says nothing about whether the site uses https.
109        if ( self::is_request_scheme_unknown() ) {
110            $has_https = 'https' === $scheme || in_array( 'https', $scheme_history, true );
111
112            return set_url_scheme( $new_value, $has_https ? 'https' : 'http' );
113        }
114
115        $scheme_history[] = $scheme;
116
117        // Limit length to self::HTTPS_CHECK_HISTORY.
118        $scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
119
120        update_option( $option_key, $scheme_history );
121
122        $forced_scheme = in_array( 'https', $scheme_history, true ) ? 'https' : 'http';
123
124        return set_url_scheme( $new_value, $forced_scheme );
125    }
126
127    /**
128     * Whether there is no web request to tell us if the site uses https, as during command-line cron.
129     *
130     * @return bool
131     */
132    private static function is_request_scheme_unknown() {
133        return ! is_ssl() && ( Constants::is_true( 'WP_CLI' ) || empty( $_SERVER['HTTP_HOST'] ) );
134    }
135
136    /**
137     * Helper function that is used when getting home or siteurl values. Decides
138     * whether to get the raw or filtered value.
139     *
140     * @param string $url_type URL to get, home or siteurl.
141     * @return string
142     */
143    public static function get_raw_or_filtered_url( $url_type ) {
144        $url_function = ( 'home' === $url_type )
145            ? 'home_url'
146            : 'site_url';
147
148        if (
149            ! Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) ||
150            Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' )
151        ) {
152            $url = (string) self::get_raw_url( $url_type );
153            if ( ! self::is_request_scheme_unknown() ) {
154                $url = set_url_scheme( $url, is_ssl() ? 'https' : 'http' );
155            }
156        } else {
157            $url = self::normalize_www_in_url( $url_type, $url_function );
158        }
159
160        return self::get_protocol_normalized_url( $url_function, $url );
161    }
162
163    /**
164     * Return the escaped home_url.
165     *
166     * @return string
167     */
168    public static function home_url() {
169        $url = self::get_raw_or_filtered_url( 'home' );
170
171        /**
172         * Allows overriding of the home_url value that is synced back to WordPress.com.
173         *
174         * @since 1.7.0
175         * @since-jetpack 5.2.0
176         *
177         * @param string $home_url
178         */
179        return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) );
180    }
181
182    /**
183     * Return the escaped siteurl.
184     *
185     * @return string
186     */
187    public static function site_url() {
188        $url = self::get_raw_or_filtered_url( 'siteurl' );
189
190        /**
191         * Allows overriding of the site_url value that is synced back to WordPress.com.
192         *
193         * @since 1.7.0
194         * @since-jetpack 5.2.0
195         *
196         * @param string $site_url
197         */
198        return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) );
199    }
200
201    /**
202     * Return main site URL with a normalized protocol.
203     *
204     * @return string
205     */
206    public static function main_network_site_url() {
207        return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
208    }
209
210    /**
211     * Formerly added the origin_site_id query parameter to a URL. WordPress.com no longer reads it.
212     *
213     * @since 7.1.0
214     * @deprecated 9.14.0
215     *
216     * @param string $url The URL.
217     * @return string The URL, unchanged.
218     */
219    public static function maybe_add_origin_site_id( $url ) {
220        _deprecated_function( __METHOD__, 'package-9.14.0' );
221        return $url;
222    }
223}