Skip to content

Commit

Permalink
Coding Standards: Use strict comparison in wp-includes/cron.php.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SergeyBiryukov committed Aug 12, 2023
1 parent 0c96ba1 commit 6929fd8
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/wp-includes/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 ] );
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ] ) ) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -778,6 +792,7 @@ function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {
break;
}
}

if ( ! $next ) {
return false;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -1009,6 +1033,7 @@ function _wp_cron() {
if ( in_array( false, $results, true ) ) {
return false;
}

return count( $results );
}

Expand Down Expand Up @@ -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(
Expand All @@ -1237,20 +1263,23 @@ 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;
}

$new_cron = array();

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;
}

0 comments on commit 6929fd8

Please sign in to comment.