From 6929fd876f743b022a96032e3885f531070cd74d Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 12 Aug 2023 12:00:27 +0000 Subject: [PATCH] Coding Standards: Use strict comparison in `wp-includes/cron.php`. Includes minor code layout fixes for better readability. Follow-up to [3634], [4189]. Props aristath, poena, afercia, SergeyBiryukov. See #58831. git-svn-id: https://develop.svn.wordpress.org/trunk@56394 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/cron.php | 43 +++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/cron.php b/src/wp-includes/cron.php index 3ebd854537f43..c89d7376aa850 100644 --- a/src/wp-includes/cron.php +++ b/src/wp-includes/cron.php @@ -139,9 +139,11 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error if ( $event_timestamp < $min_timestamp ) { continue; } + if ( $event_timestamp > $max_timestamp ) { break; } + if ( isset( $cron[ $event->hook ][ $key ] ) ) { $duplicate = true; break; @@ -359,6 +361,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $ // Now we try to get it from the saved interval in case the schedule disappears. if ( 0 === $interval ) { $scheduled_event = wp_get_scheduled_event( $hook, $args, $timestamp ); + if ( $scheduled_event && isset( $scheduled_event->interval ) ) { $interval = $scheduled_event->interval; } @@ -414,7 +417,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $ } // Now we assume something is wrong and fail to schedule. - if ( 0 == $interval ) { + if ( 0 === $interval ) { if ( $wp_error ) { return new WP_Error( 'invalid_schedule', @@ -506,10 +509,13 @@ function wp_unschedule_event( $timestamp, $hook, $args = array(), $wp_error = fa $crons = _get_cron_array(); $key = md5( serialize( $args ) ); + unset( $crons[ $timestamp ][ $hook ][ $key ] ); + if ( empty( $crons[ $timestamp ][ $hook ] ) ) { unset( $crons[ $timestamp ][ $hook ] ); } + if ( empty( $crons[ $timestamp ] ) ) { unset( $crons[ $timestamp ] ); } @@ -546,7 +552,12 @@ function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) { * Previously, this function took the arguments as discrete vars rather than an array like the rest of the API. */ if ( ! is_array( $args ) ) { - _deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) ); + _deprecated_argument( + __FUNCTION__, + '3.0.0', + __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) + ); + $args = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection $wp_error = false; } @@ -681,10 +692,12 @@ function wp_unschedule_hook( $hook, $wp_error = false ) { } $results = array(); + foreach ( $crons as $timestamp => $args ) { if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) { $results[] = count( $crons[ $timestamp ][ $hook ] ); } + unset( $crons[ $timestamp ][ $hook ] ); if ( empty( $crons[ $timestamp ] ) ) { @@ -754,6 +767,7 @@ function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) { * @param int|null $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event. */ $pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp ); + if ( null !== $pre ) { return $pre; } @@ -778,6 +792,7 @@ function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) { break; } } + if ( ! $next ) { return false; } @@ -815,6 +830,7 @@ function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) { */ function wp_next_scheduled( $hook, $args = array() ) { $next_event = wp_get_scheduled_event( $hook, $args ); + if ( ! $next_event ) { return false; } @@ -930,6 +946,7 @@ function spawn_cron( $gmt_time = 0 ) { ); $result = wp_remote_post( $cron_request['url'], $cron_request['args'] ); + return ! is_wp_error( $result ); } @@ -976,7 +993,9 @@ function wp_cron() { */ function _wp_cron() { // Prevent infinite loops caused by lack of wp-cron.php. - if ( str_contains( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) { + if ( str_contains( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) + || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) + ) { return 0; } @@ -993,14 +1012,19 @@ function _wp_cron() { $schedules = wp_get_schedules(); $results = array(); + foreach ( $crons as $timestamp => $cronhooks ) { if ( $timestamp > $gmt_time ) { break; } + foreach ( (array) $cronhooks as $hook => $args ) { - if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) { + if ( isset( $schedules[ $hook ]['callback'] ) + && ! call_user_func( $schedules[ $hook ]['callback'] ) + ) { continue; } + $results[] = spawn_cron( $gmt_time ); break 2; } @@ -1009,6 +1033,7 @@ function _wp_cron() { if ( in_array( false, $results, true ) ) { return false; } + return count( $results ); } @@ -1213,7 +1238,8 @@ function _set_cron_array( $cron, $wp_error = false ) { } $cron['version'] = 2; - $result = update_option( 'cron', $cron ); + + $result = update_option( 'cron', $cron ); if ( $wp_error && ! $result ) { return new WP_Error( @@ -1237,7 +1263,7 @@ function _set_cron_array( $cron, $wp_error = false ) { * @return array An upgraded cron info array. */ function _upgrade_cron_array( $cron ) { - if ( isset( $cron['version'] ) && 2 == $cron['version'] ) { + if ( isset( $cron['version'] ) && 2 === $cron['version'] ) { return $cron; } @@ -1245,12 +1271,15 @@ function _upgrade_cron_array( $cron ) { foreach ( (array) $cron as $timestamp => $hooks ) { foreach ( (array) $hooks as $hook => $args ) { - $key = md5( serialize( $args['args'] ) ); + $key = md5( serialize( $args['args'] ) ); + $new_cron[ $timestamp ][ $hook ][ $key ] = $args; } } $new_cron['version'] = 2; + update_option( 'cron', $new_cron ); + return $new_cron; }