PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
← All changes | src/helpers/url-helper.php +84 -7 18.228.5 View file →
@@ -29,9 +29,9 @@
29 29 if ( $home_path === '/' ) { // Home at site root, already slashed.
30 30 return $home_url;
31 31 }
32 32
33 - if ( \is_null( $home_path ) ) { // Home at site root, always slash.
33 + if ( $home_path === null ) { // Home at site root, always slash.
34 34 return \trailingslashit( $home_url );
35 35 }
36 36
37 37 if ( \is_string( $home_path ) ) { // Home in subdirectory, slash if permalink structure has slash.
@@ -99,19 +99,40 @@
99 99
100 100 /**
101 101 * Gets the path from the passed URL.
102 102 *
103 - * @codeCoverageIgnore It only wraps a WordPress function.
104 - *
105 103 * @param string $url The URL to get the path from.
106 104 *
107 105 * @return string The path of the URL. Returns an empty string if URL parsing fails.
108 106 */
109 107 public function get_url_path( $url ) {
108 + if ( \is_string( $url ) === false
109 + && ( \is_object( $url ) === false || \method_exists( $url, '__toString' ) === false )
110 + ) {
111 + return '';
112 + }
113 +
110 114 return (string) \wp_parse_url( $url, \PHP_URL_PATH );
111 115 }
112 116
113 117 /**
118 + * Gets the host from the passed URL.
119 + *
120 + * @param string $url The URL to get the host from.
121 + *
122 + * @return string The host of the URL. Returns an empty string if URL parsing fails.
123 + */
124 + public function get_url_host( $url ) {
125 + if ( \is_string( $url ) === false
126 + && ( \is_object( $url ) === false || \method_exists( $url, '__toString' ) === false )
127 + ) {
128 + return '';
129 + }
130 +
131 + return (string) \wp_parse_url( $url, \PHP_URL_HOST );
132 + }
133 +
134 + /**
114 135 * Determines the file extension of the given url.
115 136 *
116 137 * @param string $url The URL.
117 138 *
@@ -163,9 +184,9 @@
163 184 $url_parts = \wp_parse_url( \home_url() );
164 185
165 186 $base_url = \trailingslashit( $url_parts['scheme'] . '://' . $url_parts['host'] );
166 187
167 - if ( ! \is_null( $path ) ) {
188 + if ( \is_string( $path ) ) {
168 189 $base_url .= \ltrim( $path, '/' );
169 190 }
170 191
171 192 return $base_url;
@@ -191,11 +212,9 @@
191 212 if ( \array_key_exists( 'scheme', $url ) && ! \in_array( $url['scheme'], [ 'http', 'https' ], true ) ) {
192 213 return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
193 214 }
194 215
195 - if ( \is_null( $home_url ) ) {
196 - $home_url = \wp_parse_url( \home_url() );
197 - }
216 + $home_url ??= \wp_parse_url( \home_url() );
198 217
199 218 // When the base host is equal to the host.
200 219 if ( isset( $url['host'] ) && $url['host'] !== $home_url['host'] ) {
201 220 return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
@@ -211,6 +230,64 @@
211 230 return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
212 231 }
213 232
214 233 return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
234 + }
235 +
236 + /**
237 + * Recreate current URL.
238 + *
239 + * @param bool $with_request_uri Whether we want the REQUEST_URI appended.
240 + *
241 + * @return string
242 + */
243 + public function recreate_current_url( $with_request_uri = true ) {
244 + $current_url = 'http';
245 + if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
246 + $current_url .= 's';
247 + }
248 + $current_url .= '://';
249 +
250 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
251 + $suffix = ( $with_request_uri && isset( $_SERVER['REQUEST_URI'] ) ) ? $_SERVER['REQUEST_URI'] : '';
252 +
253 + if ( isset( $_SERVER['SERVER_NAME'] ) && ! empty( $_SERVER['SERVER_NAME'] ) ) {
254 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
255 + $server_name = $_SERVER['SERVER_NAME'];
256 + }
257 + else {
258 + // Early return with just the path.
259 + return $suffix;
260 + }
261 +
262 + $server_port = '';
263 + if ( isset( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] !== '80' && $_SERVER['SERVER_PORT'] !== '443' ) {
264 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
265 + $server_port = $_SERVER['SERVER_PORT'];
266 + }
267 +
268 + if ( ! empty( $server_port ) ) {
269 + $current_url .= $server_name . ':' . $server_port . $suffix;
270 + }
271 + else {
272 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
273 + $current_url .= $server_name . $suffix;
274 + }
275 +
276 + return $current_url;
277 + }
278 +
279 + /**
280 + * Parses a URL and returns its components, this wrapper function was created to support unit tests.
281 + *
282 + * @param string $parsed_url The URL to parse.
283 + * @return array The parsed components of the URL.
284 + */
285 + public function parse_str_params( $parsed_url ) {
286 + $array = [];
287 +
288 + // @todo parse_str changes spaces in param names into `_`, we should find a better way to support them.
289 + \wp_parse_str( $parsed_url, $array );
290 +
291 + return $array;
215 292 }
216 293 }