Skip to content

Commit

Permalink
Site Health: Remove use of deprecated function from `wp_is_https_supp…
Browse files Browse the repository at this point in the history
…orted()`.

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
  • Loading branch information
felixarntz committed Dec 16, 2024
1 parent cdc2f25 commit 0530261
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
22 changes: 12 additions & 10 deletions src/wp-includes/https-detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

// 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 = wp_get_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() {
/**
Expand Down
29 changes: 23 additions & 6 deletions tests/phpunit/tests/https-detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,34 @@ public function test_wp_is_using_https() {
* @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' );
}

/**
Expand Down

0 comments on commit 0530261

Please sign in to comment.