author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
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 |
* |
|
66 |
* @since 5.7.0 |
|
67 |
* |
|
68 |
* @return bool True if HTTPS is supported, false otherwise. |
|
69 |
*/ |
|
70 |
function wp_is_https_supported() { |
|
71 |
$https_detection_errors = get_option( 'https_detection_errors' ); |
|
72 |
||
73 |
// If option has never been set by the Cron hook before, run it on-the-fly as fallback. |
|
74 |
if ( false === $https_detection_errors ) { |
|
75 |
wp_update_https_detection_errors(); |
|
76 |
||
77 |
$https_detection_errors = get_option( 'https_detection_errors' ); |
|
78 |
} |
|
79 |
||
80 |
// If there are no detection errors, HTTPS is supported. |
|
81 |
return empty( $https_detection_errors ); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors. |
|
86 |
* |
|
87 |
* This internal function is called by a regular Cron hook to ensure HTTPS support is detected and maintained. |
|
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 |
91 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
92 |
function wp_get_https_detection_errors() { |
18 | 93 |
/** |
94 |
* Short-circuits the process of detecting errors related to HTTPS support. |
|
95 |
* |
|
96 |
* Returning a `WP_Error` from the filter will effectively short-circuit the default logic of trying a remote |
|
97 |
* request to the site over HTTPS, storing the errors array from the returned `WP_Error` instead. |
|
98 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
99 |
* @since 6.4.0 |
18 | 100 |
* |
101 |
* @param null|WP_Error $pre Error object to short-circuit detection, |
|
102 |
* or null to continue with the default behavior. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
103 |
* @return null|WP_Error Error object if HTTPS detection errors are found, null otherwise. |
18 | 104 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
$support_errors = apply_filters( 'pre_wp_get_https_detection_errors', null ); |
18 | 106 |
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
|
107 |
return $support_errors->errors; |
18 | 108 |
} |
109 |
||
110 |
$support_errors = new WP_Error(); |
|
111 |
||
112 |
$response = wp_remote_request( |
|
113 |
home_url( '/', 'https' ), |
|
114 |
array( |
|
115 |
'headers' => array( |
|
116 |
'Cache-Control' => 'no-cache', |
|
117 |
), |
|
118 |
'sslverify' => true, |
|
119 |
) |
|
120 |
); |
|
121 |
||
122 |
if ( is_wp_error( $response ) ) { |
|
123 |
$unverified_response = wp_remote_request( |
|
124 |
home_url( '/', 'https' ), |
|
125 |
array( |
|
126 |
'headers' => array( |
|
127 |
'Cache-Control' => 'no-cache', |
|
128 |
), |
|
129 |
'sslverify' => false, |
|
130 |
) |
|
131 |
); |
|
132 |
||
133 |
if ( is_wp_error( $unverified_response ) ) { |
|
134 |
$support_errors->add( |
|
135 |
'https_request_failed', |
|
136 |
__( 'HTTPS request failed.' ) |
|
137 |
); |
|
138 |
} else { |
|
139 |
$support_errors->add( |
|
140 |
'ssl_verification_failed', |
|
141 |
__( 'SSL verification failed.' ) |
|
142 |
); |
|
143 |
} |
|
144 |
||
145 |
$response = $unverified_response; |
|
146 |
} |
|
147 |
||
148 |
if ( ! is_wp_error( $response ) ) { |
|
149 |
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
|
150 |
$support_errors->add( 'bad_response_code', wp_remote_retrieve_response_message( $response ) ); |
|
151 |
} elseif ( false === wp_is_local_html_output( wp_remote_retrieve_body( $response ) ) ) { |
|
152 |
$support_errors->add( 'bad_response_source', __( 'It looks like the response did not come from this site.' ) ); |
|
153 |
} |
|
154 |
} |
|
155 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
return $support_errors->errors; |
18 | 157 |
} |
158 |
||
159 |
/** |
|
160 |
* Checks whether a given HTML string is likely an output from this WordPress site. |
|
161 |
* |
|
162 |
* This function attempts to check for various common WordPress patterns whether they are included in the HTML string. |
|
163 |
* Since any of these actions may be disabled through third-party code, this function may also return null to indicate |
|
164 |
* that it was not possible to determine ownership. |
|
165 |
* |
|
166 |
* @since 5.7.0 |
|
167 |
* @access private |
|
168 |
* |
|
169 |
* @param string $html Full HTML output string, e.g. from a HTTP response. |
|
170 |
* @return bool|null True/false for whether HTML was generated by this site, null if unable to determine. |
|
171 |
*/ |
|
172 |
function wp_is_local_html_output( $html ) { |
|
173 |
// 1. Check if HTML includes the site's Really Simple Discovery link. |
|
174 |
if ( has_action( 'wp_head', 'rsd_link' ) ) { |
|
175 |
$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
|
176 |
return str_contains( $html, $pattern ); |
18 | 177 |
} |
178 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
179 |
// 2. Check if HTML includes the site's REST API link. |
18 | 180 |
if ( has_action( 'wp_head', 'rest_output_link_wp_head' ) ) { |
181 |
// Try both HTTPS and HTTP since the URL depends on context. |
|
182 |
$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
|
183 |
return str_contains( $html, $pattern ); |
18 | 184 |
} |
185 |
||
186 |
// Otherwise the result cannot be determined. |
|
187 |
return null; |
|
188 |
} |