Cron API: Add a new `wp_doing_cron()` helper function.

This replaces `DOING_CRON` checks via the constant.

Props tfrommen.
Fixes #39591.

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


git-svn-id: http://core.svn.wordpress.org/trunk@40445 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2017-05-06 14:30:40 +00:00
parent e1d5fd1c6b
commit 064e62cbea
6 changed files with 35 additions and 15 deletions

View File

@ -406,7 +406,7 @@ class Plugin_Upgrader extends WP_Upgrader {
return $return; return $return;
// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
if ( defined( 'DOING_CRON' ) && DOING_CRON ) if ( wp_doing_cron() )
return $return; return $return;
$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : ''; $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';

View File

@ -663,7 +663,7 @@ function _unzip_file_ziparchive($file, $to, $needed_dirs = array() ) {
* A disk that has zero free bytes has bigger problems. * A disk that has zero free bytes has bigger problems.
* Require we have enough space to unzip the file and copy its contents, with a 10% buffer. * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
*/ */
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( wp_doing_cron() ) {
$available_space = @disk_free_space( WP_CONTENT_DIR ); $available_space = @disk_free_space( WP_CONTENT_DIR );
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space )
return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) ); return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );
@ -769,7 +769,7 @@ function _unzip_file_pclzip($file, $to, $needed_dirs = array()) {
* A disk that has zero free bytes has bigger problems. * A disk that has zero free bytes has bigger problems.
* Require we have enough space to unzip the file and copy its contents, with a 10% buffer. * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
*/ */
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( wp_doing_cron() ) {
$available_space = @disk_free_space( WP_CONTENT_DIR ); $available_space = @disk_free_space( WP_CONTENT_DIR );
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space )
return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) ); return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );

View File

@ -107,7 +107,7 @@ function get_core_checksums( $version, $locale ) {
$url = set_url_scheme( $url, 'https' ); $url = set_url_scheme( $url, 'https' );
$options = array( $options = array(
'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ), 'timeout' => wp_doing_cron() ? 30 : 3,
); );
$response = wp_remote_get( $url, $options ); $response = wp_remote_get( $url, $options );

View File

@ -1059,6 +1059,24 @@ function wp_doing_ajax() {
return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ); return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
} }
/**
* Determines whether the current request is a WordPress cron request.
*
* @since 4.8.0
*
* @return bool True if it's a WordPress cron request, false otherwise.
*/
function wp_doing_cron() {
/**
* Filters whether the current request is a WordPress cron request.
*
* @since 4.8.0
*
* @param bool $wp_doing_cron Whether the current request is a WordPress cron request.
*/
return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON );
}
/** /**
* Check whether variable is a WordPress Error. * Check whether variable is a WordPress Error.
* *

View File

@ -108,8 +108,10 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
$url = set_url_scheme( $url, 'https' ); $url = set_url_scheme( $url, 'https' );
$doing_cron = wp_doing_cron();
$options = array( $options = array(
'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ), 'timeout' => $doing_cron ? 30 : 3,
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ), 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
'headers' => array( 'headers' => array(
'wp_install' => $wp_install, 'wp_install' => $wp_install,
@ -177,7 +179,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
} }
// Trigger background updates if running non-interactively, and we weren't called from the update handler. // Trigger background updates if running non-interactively, and we weren't called from the update handler.
if ( defined( 'DOING_CRON' ) && DOING_CRON && ! doing_action( 'wp_maybe_auto_update' ) ) { if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
do_action( 'wp_maybe_auto_update' ); do_action( 'wp_maybe_auto_update' );
} }
} }
@ -217,6 +219,8 @@ function wp_update_plugins( $extra_stats = array() ) {
$new_option = new stdClass; $new_option = new stdClass;
$new_option->last_checked = time(); $new_option->last_checked = time();
$doing_cron = wp_doing_cron();
// Check for update on a different schedule, depending on the page. // Check for update on a different schedule, depending on the page.
switch ( current_filter() ) { switch ( current_filter() ) {
case 'upgrader_process_complete' : case 'upgrader_process_complete' :
@ -230,7 +234,7 @@ function wp_update_plugins( $extra_stats = array() ) {
$timeout = HOUR_IN_SECONDS; $timeout = HOUR_IN_SECONDS;
break; break;
default : default :
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( $doing_cron ) {
$timeout = 0; $timeout = 0;
} else { } else {
$timeout = 12 * HOUR_IN_SECONDS; $timeout = 12 * HOUR_IN_SECONDS;
@ -282,7 +286,7 @@ function wp_update_plugins( $extra_stats = array() ) {
$locales = apply_filters( 'plugins_update_check_locales', $locales ); $locales = apply_filters( 'plugins_update_check_locales', $locales );
$locales = array_unique( $locales ); $locales = array_unique( $locales );
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( $doing_cron ) {
$timeout = 30; $timeout = 30;
} else { } else {
// Three seconds, plus one extra second for every 10 plugins // Three seconds, plus one extra second for every 10 plugins
@ -400,6 +404,8 @@ function wp_update_themes( $extra_stats = array() ) {
); );
} }
$doing_cron = wp_doing_cron();
// Check for update on a different schedule, depending on the page. // Check for update on a different schedule, depending on the page.
switch ( current_filter() ) { switch ( current_filter() ) {
case 'upgrader_process_complete' : case 'upgrader_process_complete' :
@ -413,11 +419,7 @@ function wp_update_themes( $extra_stats = array() ) {
$timeout = HOUR_IN_SECONDS; $timeout = HOUR_IN_SECONDS;
break; break;
default : default :
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { $timeout = $doing_cron ? 0 : 12 * HOUR_IN_SECONDS;
$timeout = 0;
} else {
$timeout = 12 * HOUR_IN_SECONDS;
}
} }
$time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked ); $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
@ -463,7 +465,7 @@ function wp_update_themes( $extra_stats = array() ) {
$locales = apply_filters( 'themes_update_check_locales', $locales ); $locales = apply_filters( 'themes_update_check_locales', $locales );
$locales = array_unique( $locales ); $locales = array_unique( $locales );
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( $doing_cron ) {
$timeout = 30; $timeout = 30;
} else { } else {
// Three seconds, plus one extra second for every 10 themes // Three seconds, plus one extra second for every 10 themes

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.8-alpha-40574'; $wp_version = '4.8-alpha-40575';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.