Skip to content

Commit 38531c0

Browse files
Merge pull request #2 from pressidium/next
1.0.0
2 parents 66a2a1a + 3939575 commit 38531c0

File tree

7 files changed

+132
-124
lines changed

7 files changed

+132
-124
lines changed

composer.lock

Lines changed: 64 additions & 119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/API/Sanitizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function sanitize_email( $email ): string {
4949
* @return string
5050
*/
5151
public static function sanitize_url( $url ): string {
52-
return sanitize_url( $url );
52+
return esc_url_raw( $url );
5353
}
5454

5555
/**

includes/Files/File.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
namespace Pressidium\WP\Performance\Files;
1010

1111
use Pressidium\WP\Performance\Logging\Logger;
12-
use Pressidium\WP\Performance\Utils\URL_Utils;
1312
use Pressidium\WP\Performance\Utils\WP_Utils;
13+
use Pressidium\WP\Performance\Utils\URL_Utils;
1414

1515
if ( ! defined( 'ABSPATH' ) ) {
1616
die( 'Forbidden' );
@@ -154,6 +154,10 @@ private function compute_hash(): string {
154154
)
155155
);
156156

157+
if ( URL_Utils::is_url( $location ) ) {
158+
$location = URL_Utils::normalize_url( $location );
159+
}
160+
157161
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
158162
$contents = file_get_contents( $location );
159163

includes/Files/File_Reader.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Pressidium\WP\Performance\Exceptions\Filesystem_Exception;
1212
use Pressidium\WP\Performance\Logging\Logger;
1313
use Pressidium\WP\Performance\Utils\WP_Utils;
14+
use Pressidium\WP\Performance\Utils\URL_Utils;
1415

1516
use const Pressidium\WP\Performance\VERSION;
1617

@@ -25,6 +26,11 @@
2526
*/
2627
final class File_Reader {
2728

29+
/**
30+
* @var string User agent for remote requests.
31+
*/
32+
const USER_AGENT = 'Pressidium Performance Plugin';
33+
2834
/**
2935
* File_Reader constructor.
3036
*
@@ -49,6 +55,20 @@ private function maybe_read_local( string $file_path ): string {
4955
return $this->filesystem->read( $file_path );
5056
}
5157

58+
/**
59+
* Determine if the given URL is protocol-relative.
60+
*
61+
* Protocol-relative URLs start with '//' and inherit
62+
* the protocol (http or https) from the current context.
63+
*
64+
* @param string $url URL to check.
65+
*
66+
* @return bool `true` if the URL is protocol-relative, `false` otherwise.
67+
*/
68+
private function is_protocol_relative_url( string $url ): bool {
69+
return str_starts_with( $url, '//' );
70+
}
71+
5272
/**
5373
* Fetch and return the contents of the file at the given URI.
5474
*
@@ -59,11 +79,13 @@ private function maybe_read_local( string $file_path ): string {
5979
* @return string Contents of the file.
6080
*/
6181
private function maybe_fetch_remote( string $file_uri ): string {
82+
$file_uri = URL_Utils::normalize_url( $file_uri );
83+
6284
$response = wp_safe_remote_request(
6385
$file_uri,
6486
array(
6587
'timeout' => 10, // seconds
66-
'user-agent' => sprintf( 'Pressidium Performance Plugin/%s', VERSION ),
88+
'user-agent' => sprintf( '%s/%s', self::USER_AGENT, VERSION ),
6789
'sslverify' => ! WP_Utils::is_local_or_development_env(), // do not verify SSL in local/dev env
6890
),
6991
);

includes/Utils/URL_Utils.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@
1919
*/
2020
final class URL_Utils {
2121

22+
/**
23+
* Normalize the given URL.
24+
*
25+
* @param string $url URL to normalize.
26+
*
27+
* @return string
28+
*/
29+
public static function normalize_url( string $url ): string {
30+
// Handle protocol-relative URLs
31+
$is_protocol_relative = str_starts_with( $url, '//' );
32+
33+
if ( $is_protocol_relative ) {
34+
$protocol = WP_Utils::get_site_protocol();
35+
$url = $protocol . ':' . $url;
36+
}
37+
38+
return $url;
39+
}
40+
2241
/**
2342
* Whether the given value is a valid URL.
2443
*
@@ -27,7 +46,9 @@ final class URL_Utils {
2746
* @return bool
2847
*/
2948
public static function is_url( string $value ): bool {
30-
return (bool) filter_var( $value, FILTER_VALIDATE_URL );
49+
$normalized_value = self::normalize_url( $value );
50+
51+
return (bool) filter_var( $normalized_value, FILTER_VALIDATE_URL );
3152
}
3253

3354
/**

includes/Utils/WP_Utils.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public static function get_request_uri(): string {
4848
return esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
4949
}
5050

51+
/**
52+
* Return the protocol of this WordPress website.
53+
*
54+
* @return string
55+
*/
56+
public static function get_site_protocol(): string {
57+
$parsed_url = wp_parse_url( get_home_url() );
58+
$scheme = $parsed_url['scheme'] ?? 'https';
59+
60+
if ( $scheme === 'http' ) {
61+
return 'http';
62+
}
63+
64+
return 'https';
65+
}
66+
5167
/**
5268
* Return the environment type of this WordPress website.
5369
*

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ If you have spotted any bugs, or would like to request additional features from
157157

158158
== Changelog ==
159159

160-
= 1.0.0: Nov 22, 2025 =
160+
= 1.0.0: Jan 9, 2026 =
161161

162162
* Initial version

0 commit comments

Comments
 (0)