Skip to content

Commit

Permalink
Update simulate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukhendu2002 committed Feb 14, 2025
1 parent 011d7ad commit 51ea49d
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions tests/phpunit/tests/cron/wp-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,50 @@ private function simulate_wp_cron() {
define( 'DOING_CRON', true );
}

$doing_cron_transient = get_transient( 'doing_cron' );

if ( ! $doing_cron_transient ) {
$doing_cron_transient = sprintf( '%.22F', microtime( true ) );
set_transient( 'doing_cron', $doing_cron_transient );
}

$crons = wp_get_ready_cron_jobs();

Check failure on line 116 in tests/phpunit/tests/cron/wp-cron.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Whitespace found at end of line
if ( empty( $crons ) ) {
delete_transient( 'doing_cron' );
return;
}

$gmt_time = microtime( true );
$keys = array_keys( $crons );

Check failure on line 124 in tests/phpunit/tests/cron/wp-cron.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Whitespace found at end of line
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
delete_transient( 'doing_cron' );
return;
}

// Process cron jobs.
foreach ( $crons as $timestamp => $cronhooks ) {
if ( $timestamp > $gmt_time ) {
continue;
}

foreach ( $cronhooks as $hook => $keys ) {
foreach ( $keys as $k => $v ) {
$scheduled_args = $v['args'];
wp_unschedule_event( $timestamp, $hook, $scheduled_args );
do_action_ref_array( $hook, $scheduled_args );

if ( isset( $v['schedule'] ) ) {
$next_timestamp = wp_next_scheduled( $hook, $scheduled_args );
if ( ! $next_timestamp ) {
$schedules = wp_get_schedules();
if ( isset( $schedules[ $v['schedule'] ] ) ) {
$interval = $schedules[ $v['schedule'] ]['interval'];
wp_schedule_event( $timestamp + $interval, $v['schedule'], $hook, $scheduled_args );
}
}
$schedule = $v['schedule'];
$args = $v['args'];

do_action_ref_array( $hook, $args );

wp_unschedule_event( $timestamp, $hook, $args );

if ( $schedule ) {
wp_reschedule_event( $timestamp, $schedule, $hook, $args );
}
}
}
}

delete_transient( 'doing_cron' );
}
}

0 comments on commit 51ea49d

Please sign in to comment.