From f04cc253057cbb55a76a2cead1d85e571c5d3e2b Mon Sep 17 00:00:00 2001 From: Jose Eduardo Mascarell Moreno Date: Sat, 10 Dec 2022 10:30:14 +0100 Subject: [PATCH 1/3] When param --due-now get events from wp_get_ready_cron_jobs() --- src/Cron_Event_Command.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index 16cdf7b72..e2447f38f 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -225,7 +225,9 @@ public function run( $args, $assoc_args ) { WP_CLI::error( 'Please specify one or more cron events, or use --due-now/--all.' ); } - $events = self::get_cron_events(); + $is_due_now = Utils\get_flag_value( $assoc_args, 'due-now' ) ? true : false; + + $events = self::get_cron_events( $is_due_now ); if ( is_wp_error( $events ) ) { WP_CLI::error( $events ); @@ -428,12 +430,17 @@ protected static function format_event( stdClass $event ) { * * @return array|WP_Error An array of event objects, or a WP_Error object if there are no events scheduled. */ - protected static function get_cron_events() { + protected static function get_cron_events( $is_due_now = false ) { + + if ( $is_due_now ) { + $crons = wp_get_ready_cron_jobs(); + } else { + $crons = _get_cron_array(); + } - $crons = _get_cron_array(); $events = array(); - if ( empty( $crons ) ) { + if ( empty( $crons ) && ! $is_due_now ) { return new WP_Error( 'no_events', 'You currently have no scheduled cron events.' From 4160b7088f26ed6a132fc4866e063ef65295fd9f Mon Sep 17 00:00:00 2001 From: Jose Eduardo Mascarell Moreno Date: Sat, 10 Dec 2022 10:59:02 +0100 Subject: [PATCH 2/3] Add check version because wp_get_ready_cron_jobs is since 5.1.0 --- src/Cron_Event_Command.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index e2447f38f..ab7a2682a 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -433,7 +433,12 @@ protected static function format_event( stdClass $event ) { protected static function get_cron_events( $is_due_now = false ) { if ( $is_due_now ) { - $crons = wp_get_ready_cron_jobs(); + // wp_get_ready_cron_jobs since 5.1.0 + if ( version_compare( get_bloginfo( 'version' ), '5.1', '>=' ) ) { + $crons = wp_get_ready_cron_jobs(); + } else { + $crons = _get_cron_array(); + } } else { $crons = _get_cron_array(); } From d1794f72195cfb0b69055c91774996f3ec6a4bf9 Mon Sep 17 00:00:00 2001 From: Jose Eduardo Mascarell Moreno Date: Tue, 13 Dec 2022 15:07:58 +0100 Subject: [PATCH 3/3] Changes suggested by danielbachhuber --- src/Cron_Event_Command.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index ab7a2682a..3665b144c 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -225,7 +225,7 @@ public function run( $args, $assoc_args ) { WP_CLI::error( 'Please specify one or more cron events, or use --due-now/--all.' ); } - $is_due_now = Utils\get_flag_value( $assoc_args, 'due-now' ) ? true : false; + $is_due_now = Utils\get_flag_value( $assoc_args, 'due-now' ); $events = self::get_cron_events( $is_due_now ); @@ -432,13 +432,9 @@ protected static function format_event( stdClass $event ) { */ protected static function get_cron_events( $is_due_now = false ) { - if ( $is_due_now ) { - // wp_get_ready_cron_jobs since 5.1.0 - if ( version_compare( get_bloginfo( 'version' ), '5.1', '>=' ) ) { - $crons = wp_get_ready_cron_jobs(); - } else { - $crons = _get_cron_array(); - } + // wp_get_ready_cron_jobs since 5.1.0 + if ( $is_due_now && function_exists( 'wp_get_ready_cron_jobs' ) ) { + $crons = wp_get_ready_cron_jobs(); } else { $crons = _get_cron_array(); }