Site Health: Update loopback test to POST to wp-cron.

Replace loopback health check URL with a `POST` request to `wp-cron.php` in the WordPress install directory. This more closely represents how WordPress uses loopback requests.

A `POST` request is used to cause `wp-cron.php` to exit prior to running any cron jobs. This allows the test to indicate success quicker and accounts for sites that set the `DISABLE_WP_CRON` constant to avoid running cron jobs on loopback requests.

Props clorith, peterwilsoncc, TimothyBlynJacobs.
Fixes #52547.


Built from https://develop.svn.wordpress.org/trunk@50399


git-svn-id: http://core.svn.wordpress.org/trunk@50010 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2021-02-22 00:51:09 +00:00
parent 17fa3f6b6d
commit 0b9c7b7cdf
2 changed files with 14 additions and 3 deletions

View File

@ -2647,6 +2647,7 @@ class WP_Site_Health {
* @return object The test results.
*/
function can_perform_loopback() {
$body = array( 'site-health' => 'loopback-test' );
$cookies = wp_unslash( $_COOKIE );
$timeout = 10;
$headers = array(
@ -2660,9 +2661,19 @@ class WP_Site_Health {
$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
}
$url = home_url();
$url = site_url( 'wp-cron.php' );
$r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
/*
* A post request is used for the wp-cron.php loopback test to cause the file
* to finish early without triggering cron jobs. This has two benefits:
* - cron jobs are not triggered a second time on the site health page,
* - the loopback request finishes sooner providing a quicker result.
*
* Using a POST request causes the loopback to differ slightly to the standard
* GET request WordPress uses for wp-cron.php loopback requests but is close
* enough. See https://core.trac.wordpress.org/ticket/52547
*/
$r = wp_remote_post( $url, compact( 'body', 'cookies', 'headers', 'timeout', 'sslverify' ) );
if ( is_wp_error( $r ) ) {
return (object) array(

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.7-beta3-50398';
$wp_version = '5.7-beta3-50399';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.