Site Health: Remove use of deprecated function from wp_is_https_supported().

Follow up to [56664].

Props peter8nss, debarghyabanerjee, sebastienserre, geekofshire, swissspidy, desrosj.
Fixes #62252.
See #58494.


git-svn-id: https://develop.svn.wordpress.org/trunk@59517 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2024-12-16 14:06:05 +00:00
parent cdc2f255ac
commit 0530261e7d
2 changed files with 35 additions and 16 deletions

View File

@ -63,31 +63,33 @@ function wp_is_site_url_using_https() {
/**
* Checks whether HTTPS is supported for the server and domain.
*
* This function makes an HTTP request through `wp_get_https_detection_errors()`
* to check for HTTPS support. As this process can be resource-intensive,
* it should be used cautiously, especially in performance-sensitive environments,
* to avoid potential latency issues.
*
* @since 5.7.0
*
* @return bool True if HTTPS is supported, false otherwise.
*/
function wp_is_https_supported() {
$https_detection_errors = get_option( 'https_detection_errors' );
$https_detection_errors = wp_get_https_detection_errors();
// If option has never been set by the Cron hook before, run it on-the-fly as fallback.
if ( false === $https_detection_errors ) {
wp_update_https_detection_errors();
$https_detection_errors = get_option( 'https_detection_errors' );
}
// If there are no detection errors, HTTPS is supported.
// If there are errors, HTTPS is not supported.
return empty( $https_detection_errors );
}
/**
* Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors.
*
* This internal function is called by a regular Cron hook to ensure HTTPS support is detected and maintained.
* This function checks for HTTPS support by making an HTTP request. As this process can be resource-intensive,
* it should be used cautiously, especially in performance-sensitive environments.
* It is called when HTTPS support needs to be validated.
*
* @since 6.4.0
* @access private
*
* @return array An array containing potential detection errors related to HTTPS, or an empty array if no errors are found.
*/
function wp_get_https_detection_errors() {
/**

View File

@ -41,17 +41,34 @@ class Tests_HTTPS_Detection extends WP_UnitTestCase {
* @ticket 47577
*/
public function test_wp_is_https_supported() {
// The function works with cached errors, so only test that here.
$wp_error = new WP_Error();
// Simulate that HTTPS is supported by returning an empty error array.
add_filter(
'pre_wp_get_https_detection_errors',
function () {
return new WP_Error(); // No errors means HTTPS is supported.
}
);
// No errors, so HTTPS is supported.
update_option( 'https_detection_errors', $wp_error->errors );
$this->assertTrue( wp_is_https_supported() );
// Errors, so HTTPS is not supported.
$wp_error->add( 'ssl_verification_failed', 'SSL verification failed.' );
update_option( 'https_detection_errors', $wp_error->errors );
// Now we simulate that HTTPS is not supported by returning errors.
$support_errors = new WP_Error();
$support_errors->add( 'ssl_verification_failed', 'SSL verification failed.' );
// Short-circuit the detection logic to return our simulated errors.
add_filter(
'pre_wp_get_https_detection_errors',
function () use ( $support_errors ) {
return $support_errors;
}
);
// Test that HTTPS is not supported due to the simulated errors.
$this->assertFalse( wp_is_https_supported() );
// Remove the filter to avoid affecting other tests.
remove_filter( 'pre_wp_get_https_detection_errors', '__return_null' );
}
/**