@@ -87,4 +87,131 @@ function apply_filters_deprecated( $tag, $args, $version, $replacement = false,
8787
8888 return apply_filters_ref_array ( $ tag , $ args );
8989 }
90- }
90+ }
91+
92+
93+ /**
94+ * Copied from core for compatibility with WP < 4.4
95+ * A wrapper for PHP's parse_url() function that handles consistency in the return
96+ * values across PHP versions.
97+ *
98+ * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
99+ * schemeless and relative url's with :// in the path. This function works around
100+ * those limitations providing a standard output on PHP 5.2~5.4+.
101+ *
102+ * Secondly, across various PHP versions, schemeless URLs starting containing a ":"
103+ * in the query are being handled inconsistently. This function works around those
104+ * differences as well.
105+ *
106+ * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
107+ * when URL parsing failed.
108+ *
109+ * @since 4.4.0
110+ * @since 4.7.0 The $component parameter was added for parity with PHP's parse_url().
111+ *
112+ * @param string $url The URL to parse.
113+ * @param int $component The specific component to retrieve. Use one of the PHP
114+ * predefined constants to specify which one.
115+ * Defaults to -1 (= return all parts as an array).
116+ * @see http://php.net/manual/en/function.parse-url.php
117+ * @return mixed False on parse failure; Array of URL components on success;
118+ * When a specific component has been requested: null if the component
119+ * doesn't exist in the given URL; a sting or - in the case of
120+ * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
121+ */
122+ if ( ! function_exists ( 'wp_parse_url ' ) ) {
123+ function wp_parse_url ( $ url , $ component = -1 ) {
124+ $ to_unset = array ();
125+ $ url = strval ( $ url );
126+
127+ if ( '// ' === substr ( $ url , 0 , 2 ) ) {
128+ $ to_unset [] = 'scheme ' ;
129+ $ url = 'placeholder: ' . $ url ;
130+ } elseif ( '/ ' === substr ( $ url , 0 , 1 ) ) {
131+ $ to_unset [] = 'scheme ' ;
132+ $ to_unset [] = 'host ' ;
133+ $ url = 'placeholder://placeholder ' . $ url ;
134+ }
135+
136+ $ parts = @parse_url ( $ url );
137+
138+ if ( false === $ parts ) {
139+ // Parsing failure.
140+ return $ parts ;
141+ }
142+
143+ // Remove the placeholder values.
144+ foreach ( $ to_unset as $ key ) {
145+ unset( $ parts [ $ key ] );
146+ }
147+
148+ return _get_component_from_parsed_url_array ( $ parts , $ component );
149+ }
150+ }
151+
152+ /**
153+ * Copied from core for compatibility with WP < 4.7
154+ * Retrieve a specific component from a parsed URL array.
155+ *
156+ * @internal
157+ *
158+ * @since 4.7.0
159+ *
160+ * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse.
161+ * @param int $component The specific component to retrieve. Use one of the PHP
162+ * predefined constants to specify which one.
163+ * Defaults to -1 (= return all parts as an array).
164+ * @see http://php.net/manual/en/function.parse-url.php
165+ * @return mixed False on parse failure; Array of URL components on success;
166+ * When a specific component has been requested: null if the component
167+ * doesn't exist in the given URL; a sting or - in the case of
168+ * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
169+ */
170+ if ( ! function_exists ( '_get_component_from_parsed_url_array ' ) ) {
171+ function _get_component_from_parsed_url_array ( $ url_parts , $ component = -1 ) {
172+ if ( -1 === $ component ) {
173+ return $ url_parts ;
174+ }
175+
176+ $ key = _wp_translate_php_url_constant_to_key ( $ component );
177+ if ( false !== $ key && is_array ( $ url_parts ) && isset ( $ url_parts [ $ key ] ) ) {
178+ return $ url_parts [ $ key ];
179+ } else {
180+ return null ;
181+ }
182+ }
183+ }
184+
185+ /**
186+ * Copied from core for compatibility with WP < 4.7
187+ * Translate a PHP_URL_* constant to the named array keys PHP uses.
188+ *
189+ * @internal
190+ *
191+ * @since 4.7.0
192+ *
193+ * @see http://php.net/manual/en/url.constants.php
194+ *
195+ * @param int $constant PHP_URL_* constant.
196+ * @return string|bool The named key or false.
197+ */
198+ if ( ! function_exists ( '_wp_translate_php_url_constant_to_key ' ) ) {
199+ function _wp_translate_php_url_constant_to_key ( $ constant ) {
200+ $ translation = array (
201+ PHP_URL_SCHEME => 'scheme ' ,
202+ PHP_URL_HOST => 'host ' ,
203+ PHP_URL_PORT => 'port ' ,
204+ PHP_URL_USER => 'user ' ,
205+ PHP_URL_PASS => 'pass ' ,
206+ PHP_URL_PATH => 'path ' ,
207+ PHP_URL_QUERY => 'query ' ,
208+ PHP_URL_FRAGMENT => 'fragment ' ,
209+ );
210+
211+ if ( isset ( $ translation [ $ constant ] ) ) {
212+ return $ translation [ $ constant ];
213+ } else {
214+ return false ;
215+ }
216+ }
217+ }
0 commit comments