Skip to content

Commit

Permalink
Introduce wp_cron_isolate_events function for reduce cron side effects
Browse files Browse the repository at this point in the history
Instead of a bunch of check for updates and 3rd party crons running
during testing, allow to isolate the available crons to just ones
limited to a selector.
  • Loading branch information
lipemat committed Oct 21, 2023
1 parent 1c20084 commit 61b6e16
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions includes/template-tags/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Run all crons in queue during Testing
*
* @see wp/wp-cron.php
* @see wp_cron_isolate_events()
*
* @return void
*/
Expand Down Expand Up @@ -50,3 +51,32 @@ function wp_cron_run_event( $hook ) {
}
}
}

/**
* Limit the cron events to actions which include a specific selector.
*
* Used to prevent a bunch of core and third party crons from running when
* using `wp_cron_run_all`.
*
* @since 3.5.0
*
* @notice Must be used after `setUp` is called to prevent affecting the database.
*
* @example wp_cron_isolate_events( 'lipe' );
*
*
* @param string $selector - Selector to limit cron events to.
* Will match any cron event which includes this string.
*
* @return void
*/
function wp_cron_isolate_events( $selector ) {
update_option( 'cron', \array_filter( \array_map( function( $timed_events ) use ( $selector ) {
if ( ! is_array( $timed_events ) ) {
return $timed_events;
}
return \array_filter( $timed_events, function( $action ) use ( $selector ) {
return false !== \strpos( $action, $selector );
}, ARRAY_FILTER_USE_KEY );
}, get_option( 'cron', [] ) ) ) );
}

0 comments on commit 61b6e16

Please sign in to comment.