author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
/** |
|
3 |
* HTTPS detection functions. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @since 5.7.0 |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Checks whether the website is using HTTPS. |
|
11 |
* |
|
12 |
* This is based on whether both the home and site URL are using HTTPS. |
|
13 |
* |
|
14 |
* @since 5.7.0 |
|
15 |
* @see wp_is_home_url_using_https() |
|
16 |
* @see wp_is_site_url_using_https() |
|
17 |
* |
|
18 |
* @return bool True if using HTTPS, false otherwise. |
|
19 |
*/ |
|
20 |
function wp_is_using_https() { |
|
21 |
if ( ! wp_is_home_url_using_https() ) { |
|
22 |
return false; |
|
23 |
} |
|
24 |
||
25 |
return wp_is_site_url_using_https(); |
|
26 |
} |
|
27 |
||
28 |
/** |
|
29 |
* Checks whether the current site URL is using HTTPS. |
|
30 |
* |
|
31 |
* @since 5.7.0 |
|
32 |
* @see home_url() |
|
33 |
* |
|
34 |
* @return bool True if using HTTPS, false otherwise. |
|
35 |
*/ |
|
36 |
function wp_is_home_url_using_https() { |
|
37 |
return 'https' === wp_parse_url( home_url(), PHP_URL_SCHEME ); |
|
38 |
} |
|
39 |
||
40 |
/** |
|
41 |
* Checks whether the current site's URL where WordPress is stored is using HTTPS. |
|
42 |
* |
|
43 |
* This checks the URL where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) |
|
44 |
* are accessible. |
|
45 |
* |
|
46 |
* @since 5.7.0 |
|
47 |
* @see site_url() |
|
48 |
* |
|
49 |
* @return bool True if using HTTPS, false otherwise. |
|
50 |
*/ |
|
51 |
function wp_is_site_url_using_https() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
52 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
* Use direct option access for 'siteurl' and manually run the 'site_url' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
* filter because `site_url()` will adjust the scheme based on what the |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
* current request is using. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
56 |
*/ |
18 | 57 |
/** This filter is documented in wp-includes/link-template.php */ |
58 |
$site_url = apply_filters( 'site_url', get_option( 'siteurl' ), '', null, null ); |
|
59 |
||
60 |
return 'https' === wp_parse_url( $site_url, PHP_URL_SCHEME ); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Checks whether HTTPS is supported for the server and domain. |
|
65 |
* |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
66 |
* This function makes an HTTP request through `wp_get_https_detection_errors()` |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
67 |
* to check for HTTPS support. As this process can be resource-intensive, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
68 |
* it should be used cautiously, especially in performance-sensitive environments, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
69 |
* to avoid potential latency issues. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
70 |
* |
18 | 71 |
* @since 5.7.0 |
72 |
* |
|
73 |
* @return bool True if HTTPS is supported, false otherwise. |
|
74 |
*/ |
|
75 |
function wp_is_https_supported() { |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
76 |
$https_detection_errors = wp_get_https_detection_errors(); |
18 | 77 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
78 |
// If there are errors, HTTPS is not supported. |
18 | 79 |
return empty( $https_detection_errors ); |
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors. |
|
84 |
* |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
85 |
* This function checks for HTTPS support by making an HTTP request. As this process can be resource-intensive, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
86 |
* it should be used cautiously, especially in performance-sensitive environments. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
87 |
* It is called when HTTPS support needs to be validated. |
18 | 88 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
89 |
* @since 6.4.0 |
18 | 90 |
* @access private |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
91 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
92 |
* @return array An array containing potential detection errors related to HTTPS, or an empty array if no errors are found. |
18 | 93 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
function wp_get_https_detection_errors() { |
18 | 95 |
/** |
96 |
* Short-circuits the process of detecting errors related to HTTPS support. |
|
97 |
* |
|
98 |
* Returning a `WP_Error` from the filter will effectively short-circuit the default logic of trying a remote |
|
99 |
* request to the site over HTTPS, storing the errors array from the returned `WP_Error` instead. |
|
100 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
101 |
* @since 6.4.0 |
18 | 102 |
* |
103 |
* @param null|WP_Error $pre Error object to short-circuit detection, |
|
104 |
* or null to continue with the default behavior. |
|
105 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
$support_errors = apply_filters( 'pre_wp_get_https_detection_errors', null ); |
18 | 107 |
if ( is_wp_error( $support_errors ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
return $support_errors->errors; |
18 | 109 |
} |
110 |
||
111 |
$support_errors = new WP_Error(); |
|
112 |
||
113 |
$response = wp_remote_request( |
|
114 |
home_url( '/', 'https' ), |
|
115 |
array( |
|
116 |
'headers' => array( |
|
117 |
'Cache-Control' => 'no-cache', |
|
118 |
), |
|
119 |
'sslverify' => true, |
|
120 |
) |
|
121 |
); |
|
122 |
||
123 |
if ( is_wp_error( $response ) ) { |
|
124 |
$unverified_response = wp_remote_request( |
|
125 |
home_url( '/', 'https' ), |
|
126 |
array( |
|
127 |
'headers' => array( |
|
128 |
'Cache-Control' => 'no-cache', |
|
129 |
), |
|
130 |
'sslverify' => false, |
|
131 |
) |
|
132 |
); |
|
133 |
||
134 |
if ( is_wp_error( $unverified_response ) ) { |
|
135 |
$support_errors->add( |
|
136 |
'https_request_failed', |
|
137 |
__( 'HTTPS request failed.' ) |
|
138 |
); |
|
139 |
} else { |
|
140 |
$support_errors->add( |
|
141 |
'ssl_verification_failed', |
|
142 |
__( 'SSL verification failed.' ) |
|
143 |
); |
|
144 |
} |
|
145 |
||
146 |
$response = $unverified_response; |
|
147 |
} |
|
148 |
||
149 |
if ( ! is_wp_error( $response ) ) { |
|
150 |
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
|
151 |
$support_errors->add( 'bad_response_code', wp_remote_retrieve_response_message( $response ) ); |
|
152 |
} elseif ( false === wp_is_local_html_output( wp_remote_retrieve_body( $response ) ) ) { |
|
153 |
$support_errors->add( 'bad_response_source', __( 'It looks like the response did not come from this site.' ) ); |
|
154 |
} |
|
155 |
} |
|
156 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
return $support_errors->errors; |
18 | 158 |
} |
159 |
||
160 |
/** |
|
161 |
* Checks whether a given HTML string is likely an output from this WordPress site. |
|
162 |
* |
|
163 |
* This function attempts to check for various common WordPress patterns whether they are included in the HTML string. |
|
164 |
* Since any of these actions may be disabled through third-party code, this function may also return null to indicate |
|
165 |
* that it was not possible to determine ownership. |
|
166 |
* |
|
167 |
* @since 5.7.0 |
|
168 |
* @access private |
|
169 |
* |
|
170 |
* @param string $html Full HTML output string, e.g. from a HTTP response. |
|
171 |
* @return bool|null True/false for whether HTML was generated by this site, null if unable to determine. |
|
172 |
*/ |
|
173 |
function wp_is_local_html_output( $html ) { |
|
174 |
// 1. Check if HTML includes the site's Really Simple Discovery link. |
|
175 |
if ( has_action( 'wp_head', 'rsd_link' ) ) { |
|
176 |
$pattern = preg_replace( '#^https?:(?=//)#', '', esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ) ); // See rsd_link(). |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
177 |
return str_contains( $html, $pattern ); |
18 | 178 |
} |
179 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
180 |
// 2. Check if HTML includes the site's REST API link. |
18 | 181 |
if ( has_action( 'wp_head', 'rest_output_link_wp_head' ) ) { |
182 |
// Try both HTTPS and HTTP since the URL depends on context. |
|
183 |
$pattern = preg_replace( '#^https?:(?=//)#', '', esc_url( get_rest_url() ) ); // See rest_output_link_wp_head(). |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
184 |
return str_contains( $html, $pattern ); |
18 | 185 |
} |
186 |
||
187 |
// Otherwise the result cannot be determined. |
|
188 |
return null; |
|
189 |
} |