From 163dd7a7ccbc167f2dd65a5b3d2dd53665a0d374 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 19 Jun 2024 10:44:29 -0400 Subject: [PATCH 01/52] add files --- classes/WP_CLI/Action/Cancel.php | 72 ++++ classes/WP_CLI/Action/Create.php | 121 ++++++ classes/WP_CLI/Action/Delete.php | 67 ++++ classes/WP_CLI/Action/Generate.php | 117 ++++++ classes/WP_CLI/Action/Get.php | 44 +++ classes/WP_CLI/Action/List.php | 100 +++++ classes/WP_CLI/Action/Next.php | 120 ++++++ classes/WP_CLI/Action/Run.php | 137 +++++++ .../ActionScheduler_WPCLI_Action_Command.php | 350 ++++++++++++++++++ .../ActionScheduler_WPCLI_System_Command.php | 205 ++++++++++ classes/abstracts/ActionScheduler.php | 11 +- .../ActionScheduler_WPCLI_Command.php | 78 ++++ 12 files changed, 1419 insertions(+), 3 deletions(-) create mode 100644 classes/WP_CLI/Action/Cancel.php create mode 100644 classes/WP_CLI/Action/Create.php create mode 100644 classes/WP_CLI/Action/Delete.php create mode 100644 classes/WP_CLI/Action/Generate.php create mode 100644 classes/WP_CLI/Action/Get.php create mode 100644 classes/WP_CLI/Action/List.php create mode 100644 classes/WP_CLI/Action/Next.php create mode 100644 classes/WP_CLI/Action/Run.php create mode 100644 classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php create mode 100644 classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php create mode 100644 classes/abstracts/ActionScheduler_WPCLI_Command.php diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel.php new file mode 100644 index 000000000..88fa10bf1 --- /dev/null +++ b/classes/WP_CLI/Action/Cancel.php @@ -0,0 +1,72 @@ +print_error() + * @uses $this->print_success() + * @return void + */ + public function execute() : void { + $hook = $this->args[0]; + $group = $this->args[1] ?? null; + $callback_args = get_flag_value( $this->assoc_args, 'args', null ); + $all = get_flag_value( $this->assoc_args, 'all' ); + + if ( ! empty( $callback_args ) ) { + $callback_args = json_decode( $callback_args, true ); + } + + $function_name = 'as_unschedule_action'; + $multiple = false; + + if ( $all ) { + $function_name = 'as_unschedule_all_actions'; + $multiple = true; + } + + try { + call_user_func( $function_name, $hook, $callback_args, $group ); + } catch ( \Exception $e ) { + $this->print_error( $e, $multiple ); + } + + $this->print_success( $multiple ); + } + + /** + * Print a success message. + * + * @param bool $multiple + * @return void + */ + protected function print_success( bool $multiple ) : void { + \WP_CLI::success( _n( 'Scheduled action cancelled.', 'All scheduled actions cancelled.', $multiple ? 2 : 1, 'action-scheduler' ) ); + } + + /** + * Convert an exception into a WP CLI error. + * + * @param \Exception $e The error object. + * @param bool $multiple + * @throws \WP_CLI\ExitException + * @return void + */ + protected function print_error( \Exception $e, bool $multiple ) : void { + \WP_CLI::error( + sprintf( + /* translators: %s refers to the exception error message. */ + __( 'There was an error cancelling the scheduled %s: %s', 'action-scheduler' ), + $multiple ? 'actions' : 'action', + $e->getMessage() + ) + ); + } + +} diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php new file mode 100644 index 000000000..6d108ef1d --- /dev/null +++ b/classes/WP_CLI/Action/Create.php @@ -0,0 +1,121 @@ +print_error() + * @uses $this->print_success() + * @return void + */ + public function execute() : void { + $hook = $this->args[0]; + $schedule_start = $this->args[1]; + $callback_args = get_flag_value( $this->assoc_args, 'args', array() ); + $group = get_flag_value( $this->assoc_args, 'group', '' ); + $interval = absint( get_flag_value( $this->assoc_args, 'interval', 0 ) ); + $cron = get_flag_value( $this->assoc_args, 'cron', '' ); + + if ( !empty( $callback_args ) ) + $callback_args = json_decode( $callback_args, true ); + + $function_args = array( + 'start' => 'async', + 'cron' => $cron, + 'interval' => $interval, + 'hook' => $hook, + 'callback_args' => $callback_args, + 'group' => $group, + ); + + // Generate schedule start if appropriate. + if ( ! in_array( $schedule_start, static::ASYNC_OPTS ) ) { + $schedule_start = as_get_datetime_object( $schedule_start ); + $function_args['start'] = $schedule_start->format( 'U' ); + } + + // Default to creating single action. + $action_type = 'single'; + $function = 'as_schedule_single_action'; + + // Enqueue async action. + if ( 'async' === $function_args['start'] ) { + $action_type = 'async'; + $function = 'as_enqueue_async_action'; + + $function_args = array_filter( $function_args, static function( string $key ) : bool { + return in_array( $key, array( 'hook', 'callback_args', 'group' ) ); + }, ARRAY_FILTER_USE_KEY ); + + // Creating recurring action. + } else if ( !empty( $interval ) ) { + $action_type = 'recurring'; + $function = 'as_schedule_recurring_action'; + + // Creating cron action. + } else if ( !empty( $cron ) ) { + $action_type = 'cron'; + $function = 'as_schedule_cron_action'; + } + + $function_args = array_values( array_filter( $function_args ) ); + + try { + $actions_added = call_user_func_array( $function, $function_args ); + } catch ( \Exception $e ) { + $this->print_error( $e ); + } + + $num_actions_added = count( ( array ) $actions_added ); + + $this->print_success( $num_actions_added, $action_type ); + } + + /** + * Print a success message with the action ID. + * + * @param int $action_added + * @param string $action_type + * + * @return void + */ + protected function print_success( $actions_added, $action_type ) : void { + \WP_CLI::success( + sprintf( + /* translators: %d refers to the total number of taskes added */ + _n( '%d %s action scheduled.', '%d %s actions scheduled.', $actions_added, 'action-scheduler' ), + number_format_i18n( $actions_added ), + $action_type + ) + ); + } + + /** + * Convert an exception into a WP CLI error. + * + * @param \Exception $e The error object. + * + * @throws \WP_CLI\ExitException + * + * @return void + */ + protected function print_error( \Exception $e ) : void { + \WP_CLI::error( + sprintf( + /* translators: %s refers to the exception error message. */ + __( 'There was an error creating the scheduled action: %s', 'action-scheduler' ), + $e->getMessage() + ) + ); + } + +} diff --git a/classes/WP_CLI/Action/Delete.php b/classes/WP_CLI/Action/Delete.php new file mode 100644 index 000000000..fc82910a3 --- /dev/null +++ b/classes/WP_CLI/Action/Delete.php @@ -0,0 +1,67 @@ + 0, + 'total' => 0, + ); + + function __construct( array $args, array $assoc_args ) { + parent::__construct( $args, $assoc_args ); + + $this->action_ids = array_map( 'absint', $args ); + $this->action_counts['total'] = count( $this->action_ids ); + + add_action( 'action_scheduler_deleted_action', array( $this, 'action__deleted' ) ); + } + + /** + * Execute. + * + * @uses \ActionScheduler_Store::delete_action() + * @uses \WP_CLI::warning() + * @uses \WP_CLI::success() + * @return void + */ + function execute() : void { + $store = \ActionScheduler::store(); + + $progress_bar = \WP_CLI\Utils\make_progress_bar( + sprintf( + _n( 'Deleting %d action', 'Deleting %d actions', $this->action_counts['total'], 'action-scheduler' ), + number_format_i18n( $this->action_counts['total'] ) + ), + $this->action_counts['total'] + ); + + foreach ( $this->action_ids as $action_id ) { + $store->delete_action( $action_id ); + $progress_bar->tick(); + } + + $progress_bar->finish(); + + \WP_CLI::success( sprintf( + _n( 'Deleted %d action.', 'Deleted %d actions.', $this->action_counts['deleted'], 'action-scheduler' ), + number_format_i18n( $this->action_counts['deleted'] ) + ) ); + } + + /** + * Action: action_scheduler_deleted_action + * + * @param int $action_id + * @uses \WP_CLI::debug() + * @return void + */ + function action__deleted( int $action_id ) : void { + if ( !in_array( $action_id, $this->action_ids ) ) + return; + + $this->action_counts['deleted']++; + \WP_CLI::debug( sprintf( 'Action %d was deleted.', $action_id ) ); + } + +} diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php new file mode 100644 index 000000000..34b3a843c --- /dev/null +++ b/classes/WP_CLI/Action/Generate.php @@ -0,0 +1,117 @@ +generate() + * @uses $this->print_error() + * @uses $this->print_success() + * @return void + */ + public function execute() : void { + $hook = $this->args[0]; + $schedule_start = $this->args[1]; + $callback_args = get_flag_value( $this->assoc_args, 'args', array() ); + $group = get_flag_value( $this->assoc_args, 'group', '' ); + $interval = absint( get_flag_value( $this->assoc_args, 'interval', 0 ) ); + $count = absint( get_flag_value( $this->assoc_args, 'count', 1 ) ); + + if ( !empty( $callback_args ) ) { + $callback_args = json_decode( $callback_args, true ); + } + + $schedule_start = as_get_datetime_object( $schedule_start ); + + $function_args = array( + 'start' => absint( $schedule_start->format( 'U' ) ), + 'interval' => $interval, + 'count' => $count, + 'hook' => $hook, + 'callback_args' => $callback_args, + 'group' => $group, + ); + + $action_type = 'single'; + $function_args = array_values( array_filter( $function_args ) ); + + try { + $actions_added = $this->generate( ...$function_args ); + } catch ( \Exception $e ) { + $this->print_error( $e ); + } + + $num_actions_added = count( ( array ) $actions_added ); + + $this->print_success( $num_actions_added, $action_type ); + } + + /** + * Schedule multiple single actions. + * + * @param int $schedule_start Starting timestamp of first action. + * @param int $interval How long to wait between runs. + * @param int $count Limit number of actions to schedule. + * @param string $hook The hook to trigger. + * @param array $args Arguments to pass when the hook triggers. + * @param string $group The group to assign this job to. + * @uses as_schedule_single_action() + * @return int[] IDs of actions added. + */ + protected function generate( int $schedule_start, int $interval, int $count, string $hook, array $args = array(), string $group = '' ) : array { + $actions_added = array(); + + $progress_bar = \WP_CLI\Utils\make_progress_bar( + sprintf( _n( 'Creating %d action', 'Creating %d actions', $count, 'action-scheduler' ), number_format_i18n( $count ) ), + $count + ); + + for ( $i = 0; $i < $count; $i++ ) { + $actions_added[] = as_schedule_single_action( $schedule_start + ( $i * $interval ), $hook, $args, $group ); + $progress_bar->tick(); + } + + $progress_bar->finish(); + + return $actions_added; + } + + /** + * Print a success message with the action ID. + * + * @param int $action_added + * @param string $action_type + * @return void + */ + protected function print_success( $actions_added, $action_type ) : void { + \WP_CLI::success( + sprintf( + /* translators: %d refers to the total number of taskes added */ + _n( '%d %s action scheduled.', '%d %s actions scheduled.', $actions_added, 'action-scheduler' ), + number_format_i18n( $actions_added ), + $action_type + ) + ); + } + + /** + * Convert an exception into a WP CLI error. + * + * @param \Exception $e The error object. + * @throws \WP_CLI\ExitException + * @return void + */ + protected function print_error( \Exception $e ) : void { + \WP_CLI::error( + sprintf( + /* translators: %s refers to the exception error message. */ + __( 'There was an error creating the scheduled action: %s', 'action-scheduler' ), + $e->getMessage() + ) + ); + } + +} diff --git a/classes/WP_CLI/Action/Get.php b/classes/WP_CLI/Action/Get.php new file mode 100644 index 000000000..5ad9f8b97 --- /dev/null +++ b/classes/WP_CLI/Action/Get.php @@ -0,0 +1,44 @@ +args[0]; + $store = \ActionScheduler::store(); + $logger = \ActionScheduler::logger(); + $action = $store->fetch_action( $action_id ); + + $action_arr = array( + 'id' => $this->args[0], + 'hook' => $action->get_hook(), + 'status' => $store->get_status( $action_id ), + 'args' => $action->get_args(), + 'group' => $action->get_group(), + 'recurring' => $action->get_schedule()->is_recurring() ? 'yes' : 'no', + 'scheduled_date' => $this->get_schedule_display_string( $action->get_schedule() ), + 'log_entries' => array(), + ); + + foreach ( $logger->get_logs( $action_id ) as $log_entry ) { + $action_arr['log_entries'][] = array( + 'date' => $log_entry->get_date()->format( static::DATE_FORMAT ), + 'message' => $log_entry->get_message(), + ); + } + + $fields = array_keys( $action_arr ); + + if ( !empty( $this->assoc_args['fields'] ) ) + $fields = explode( ',', $this->assoc_args['fields'] ); + + $formatter = new \WP_CLI\Formatter( $this->assoc_args, $fields ); + $formatter->display_item( $action_arr ); + } + +} diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List.php new file mode 100644 index 000000000..e6dd020ca --- /dev/null +++ b/classes/WP_CLI/Action/List.php @@ -0,0 +1,100 @@ +process_csv_arguments_to_arrays(); + + if ( ! empty( $this->assoc_args['fields'] ) ) { + $fields = $this->assoc_args['fields']; + } + + $formatter = new \WP_CLI\Formatter( $this->assoc_args, $fields ); + + $query_args = array_filter( $this->assoc_args, static function ( string $key ) : bool { + return in_array( $key, static::PARAMETERS ); + }, ARRAY_FILTER_USE_KEY ); + + if ( ! empty( $query_args['args'] ) ) { + $query_args['args'] = json_decode( $query_args['args'], true ); + } + + switch ( $formatter->format ) { + + case 'ids': + $actions = as_get_scheduled_actions( $query_args, 'ids' ); + echo implode( ' ', $actions ); + break; + + case 'count': + $actions = as_get_scheduled_actions( $query_args, 'ids' ); + $formatter->display_items( $actions ); + break; + + default: + $actions = as_get_scheduled_actions( $query_args ); + + $actions_arr = array(); + + foreach ( $actions as $action_id => $action ) { + $action_arr = array( + 'id' => $action_id, + 'hook' => $action->get_hook(), + 'status' => $store->get_status( $action_id ), + 'args' => $action->get_args(), + 'group' => $action->get_group(), + 'recurring' => $action->get_schedule()->is_recurring() ? 'yes' : 'no', + 'scheduled_date' => $this->get_schedule_display_string( $action->get_schedule() ), + 'log_entries' => array(), + ); + + foreach ( $logger->get_logs( $action_id ) as $log_entry ) { + $action_arr['log_entries'][] = array( + 'date' => $log_entry->get_date()->format( static::DATE_FORMAT ), + 'message' => $log_entry->get_message(), + ); + } + + $actions_arr[] = $action_arr; + } + + $formatter->display_items( $actions_arr ); + break; + + } + } + +} diff --git a/classes/WP_CLI/Action/Next.php b/classes/WP_CLI/Action/Next.php new file mode 100644 index 000000000..e7e328d0f --- /dev/null +++ b/classes/WP_CLI/Action/Next.php @@ -0,0 +1,120 @@ +args[0]; + $group = get_flag_value( $this->assoc_args, 'group', '' ); + $callback_args = get_flag_value( $this->assoc_args, 'args', null ); + + if ( !empty( $callback_args ) ) + $callback_args = json_decode( $callback_args, true ); + + $next_action_id = $this->as_next_scheduled_action( $hook, $callback_args, $group ); + + if ( empty( $next_action_id ) ) { + \WP_CLI::warning( 'No matching next action.' ); + return; + } + + $fields = array( + 'id', + 'hook', + 'status', + 'group', + 'recurring', + 'scheduled_date', + ); + + $this->process_csv_arguments_to_arrays(); + + if ( !empty( $this->assoc_args['fields'] ) ) + $fields = $this->assoc_args['fields']; + + $store = \ActionScheduler::store(); + $logger = \ActionScheduler::logger(); + $formatter = new \WP_CLI\Formatter( $this->assoc_args, $fields ); + + if ( 'ids' === $formatter->format ) { + echo $next_action_id; + return; + } + + $action = $store->fetch_action( $next_action_id ); + + $action_arr = array( + 'id' => $next_action_id, + 'hook' => $action->get_hook(), + 'status' => $store->get_status( $next_action_id ), + 'args' => $action->get_args(), + 'group' => $action->get_group(), + 'recurring' => $action->get_schedule()->is_recurring() ? 'yes' : 'no', + 'scheduled_date' => $this->get_schedule_display_string( $action->get_schedule() ), + 'log_entries' => array(), + ); + + foreach ( $logger->get_logs( $next_action_id ) as $log_entry ) { + $action_arr['log_entries'][] = array( + 'date' => $log_entry->get_date()->format( static::DATE_FORMAT ), + 'message' => $log_entry->get_message(), + ); + } + + if ( !empty( $this->assoc_args['fields'] ) ) + $fields = explode( ',', $this->assoc_args['fields'] ); + + $formatter->display_item( $action_arr ); + } + + /** + * Get next scheduled action. + * + * @see as_next_scheduled_action() + * @param string $hook + * @param null|array $args + * @param string $group + * @return int + */ + protected function as_next_scheduled_action( $hook, $args = null, $group = '' ) : int { + if ( ! \ActionScheduler::is_initialized( 'as_next_scheduled_action' ) ) { + return 0; + } + $params = array(); + if ( is_array($args) ) { + $params['args'] = $args; + } + if ( !empty($group) ) { + $params['group'] = $group; + } + + $params['status'] = \ActionScheduler_Store::STATUS_RUNNING; + $job_id = absint( \ActionScheduler::store()->find_action( $hook, $params ) ); + if ( ! empty( $job_id ) ) { + return $job_id; + } + + $params['status'] = \ActionScheduler_Store::STATUS_PENDING; + $job_id = absint( \ActionScheduler::store()->find_action( $hook, $params ) ); + if ( empty($job_id) ) { + return 0; + } + $job = \ActionScheduler::store()->fetch_action( $job_id ); + $scheduled_date = $job->get_schedule()->get_date(); + if ( $scheduled_date ) { + return $job_id; + } elseif ( NULL === $scheduled_date ) { // pending async action with NullSchedule + return $job_id; + } + return 0; + } + +} diff --git a/classes/WP_CLI/Action/Run.php b/classes/WP_CLI/Action/Run.php new file mode 100644 index 000000000..ae89d75af --- /dev/null +++ b/classes/WP_CLI/Action/Run.php @@ -0,0 +1,137 @@ + 0, + 'failed' => 0, + 'ignored' => 0, + 'invalid' => 0, + 'total' => 0, + ); + + function __construct( array $args, array $assoc_args ) { + parent::__construct( $args, $assoc_args ); + + $this->action_ids = array_map( 'absint', $args ); + $this->action_counts['total'] = count( $this->action_ids ); + + add_action( 'action_scheduler_execution_ignored', array( $this, 'action__ignored' ) ); + add_action( 'action_scheduler_after_execute', array( $this, 'action__executed' ) ); + add_action( 'action_scheduler_failed_execution', array( $this, 'action__failed' ), 10, 2 ); + add_action( 'action_scheduler_failed_validation', array( $this, 'action__invalid' ), 10, 2 ); + } + + /** + * Execute. + * + * @uses \ActionScheduler_Abstract_QueueRunner::process_action() + * @uses \WP_CLI::warning() + * @uses \WP_CLI::success() + * @return void + */ + function execute() : void { + $runner = \ActionScheduler::runner(); + + $progress_bar = \WP_CLI\Utils\make_progress_bar( + sprintf( + _n( 'Executing %d action', 'Executing %d actions', $this->action_counts['total'], 'action-scheduler' ), + number_format_i18n( $this->action_counts['total'] ) + ), + $this->action_counts['total'] + ); + + foreach ( $this->action_ids as $action_id ) { + $runner->process_action( $action_id, 'Action Scheduler CLI' ); + $progress_bar->tick(); + } + + $progress_bar->finish(); + + foreach ( array( + 'ignored', + 'invalid', + 'failed', + ) as $type ) { + $count = $this->action_counts[ $type ]; + + if ( empty( $count ) ) + continue; + + \WP_CLI::warning( sprintf( + _n( '%d action %s.', '%d actions %s.', $count, 'action-scheduler' ), + number_format_i18n( $count ), + $type + ) ); + } + + \WP_CLI::success( sprintf( + _n( 'Executed %d action.', 'Executed %d actions.', $this->action_counts['executed'], 'action-scheduler' ), + number_format_i18n( $this->action_counts['executed'] ) + ) ); + } + + /** + * Action: action_scheduler_execution_ignored + * + * @param int $action_id + * @uses \WP_CLI::debug() + * @return void + */ + function action__ignored( int $action_id ) : void { + if ( !in_array( $action_id, $this->action_ids ) ) + return; + + $this->action_counts['ignored']++; + \WP_CLI::debug( sprintf( 'Action %d was ignored.', $action_id ) ); + } + + /** + * Action: action_scheduler_after_execute + * + * @param int $action_id + * @uses \WP_CLI::success() + * @return void + */ + function action__executed( int $action_id ) : void { + if ( !in_array( $action_id, $this->action_ids ) ) + return; + + $this->action_counts['executed']++; + \WP_CLI::debug( sprintf( 'Action %d was executed.', $action_id ) ); + } + + /** + * Action: action_scheduler_failed_execution + * + * @param int $action_id + * @param \Exception $e + * @uses \WP_CLI::debug() + * @return void + */ + function action__failed( int $action_id, \Exception $e ) : void { + if ( !in_array( $action_id, $this->action_ids ) ) + return; + + $this->action_counts['failed']++; + \WP_CLI::debug( sprintf( 'Action %d failed execution: %s', $action_id, $e->getMessage() ) ); + } + + /** + * Action: action_scheduler_failed_validation + * + * @param int $action_id + * @param \Exception $e + * @uses \WP_CLI::debug() + * @return void + */ + function action__invalid( int $action_id, \Exception $e ) : void { + if ( !in_array( $action_id, $this->action_ids ) ) + return; + + $this->action_counts['invalid']++; + \WP_CLI::debug( sprintf( 'Action %d failed validation: %s', $action_id, $e->getMessage() ) ); + } + +} diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php new file mode 100644 index 000000000..d88d9e53e --- /dev/null +++ b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php @@ -0,0 +1,350 @@ + + * : Name of the action hook. + * + * [] + * : The group the job is assigned to. + * + * [--args=] + * : JSON object of arguments assigned to the job. + * --- + * default: [] + * --- + * + * [--all] + * : Cancel all occurrences of a scheduled action. + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function cancel( array $args, array $assoc_args ) : void { + require_once 'Action/Cancel.php'; + $command = new ActionScheduler_WPCLI_Action_Cancel_Command( $args, $assoc_args ); + $command->execute(); + } + + /** + * Creates a new scheduled action. + * + * ## OPTIONS + * + * + * : Name of the action hook. + * + * + * : A unix timestamp representing the date you want the action to start. Also 'async' or 'now' to enqueue an async action. + * + * [--args=] + * : JSON object of arguments to pass to callbacks when the hook triggers. + * --- + * default: [] + * --- + * + * [--cron=] + * : A cron-like schedule string (https://crontab.guru/). + * --- + * default: '' + * --- + * + * [--group=] + * : The group to assign this job to. + * --- + * default: '' + * --- + * + * [--interval=] + * : Number of seconds to wait between runs. + * --- + * default: 0 + * --- + * + * ## EXAMPLES + * + * wp action-scheduler action create hook_async async + * wp action-scheduler action create hook_single 1627147598 + * wp action-scheduler action create hook_recurring 1627148188 --interval=5 + * wp action-scheduler action create hook_cron 1627147655 --cron='5 4 * * *' + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function create( array $args, array $assoc_args ) : void { + require_once 'Action/Create.php'; + $command = new ActionScheduler_WPCLI_Action_Create_Command( $args, $assoc_args ); + $command->execute(); + } + + /** + * Delete existing scheduled action(s). + * + * ## OPTIONS + * + * ... + * : One or more IDs of actions to delete. + * --- + * default: 0 + * --- + * + * ## EXAMPLES + * + * # Delete the action with id 100 + * $ wp action-scheduler action delete 100 + * + * # Delete the actions with ids 100 and 200 + * $ wp action-scheduler action delete 100 200 + * + * # Delete the first five pending actions in 'action-scheduler' group + * $ wp action-scheduler action delete $( wp action-scheduler action list --status=pending --group=action-scheduler --format=ids ) + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function delete( array $args, array $assoc_args ) : void { + require_once 'Action/Delete.php'; + $command = new ActionScheduler_WPCLI_Action_Delete_Command( $args, $assoc_args ); + $command->execute(); + } + + /** + * Generates some scheduled actions. + * + * ## OPTIONS + * + * + * : Name of the action hook. + * + * + * : The Unix timestamp representing the date you want the action to start. + * + * [--count=] + * : Number of actions to create. + * --- + * default: 1 + * --- + * + * [--interval=] + * : Number of seconds to wait between runs. + * --- + * default: 0 + * --- + * + * [--args=] + * : JSON object of arguments to pass to callbacks when the hook triggers. + * --- + * default: [] + * --- + * + * [--group=] + * : The group to assign this job to. + * --- + * default: '' + * --- + * + * ## EXAMPLES + * + * wp action-scheduler action generate test_multiple 1627147598 --count=5 --interval=5 + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function generate( array $args, array $assoc_args ) : void { + require_once 'Action/Generate.php'; + $command = new ActionScheduler_WPCLI_Action_Generate_Command( $args, $assoc_args ); + $command->execute(); + } + + /** + * Get details about a scheduled action. + * + * ## OPTIONS + * + * + * : The ID of the action to get. + * --- + * default: 0 + * --- + * + * [--field=] + * : Instead of returning the whole action, returns the value of a single field. + * + * [--fields=] + * : Limit the output to specific fields. Defaults to all fields. + * + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - csv + * - json + * - yaml + * --- + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function get( array $args, array $assoc_args ) : void { + require_once 'Action/Get.php'; + $command = new ActionScheduler_WPCLI_Action_Get_Command( $args, $assoc_args ); + $command->execute(); + } + + /** + * Get a list of scheduled actions. + * + * Display actions based on all arguments supported by + * [as_get_scheduled_actions()](https://actionscheduler.org/api/#function-reference--as_get_scheduled_actions). + * + * ## OPTIONS + * + * [--=] + * : One or more arguments to pass to as_get_scheduled_actions(). + * + * [--field=] + * : Prints the value of a single property for each action. + * + * [--fields=] + * : Limit the output to specific object properties. + * + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - csv + * - ids + * - json + * - count + * - yaml + * --- + * + * ## AVAILABLE FIELDS + * + * These fields will be displayed by default for each action: + * + * * id + * * hook + * * status + * * group + * * recurring + * * scheduled_date + * + * These fields are optionally available: + * + * * args + * * log_entries + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function list( array $args, array $assoc_args ) : void { + require_once 'Action/List.php'; + $command = new ActionScheduler_WPCLI_Action_List_Command( $args, $assoc_args ); + $command->execute(); + } + + /** + * Get the next scheduled action. + * + * ## OPTIONS + * + * + * : The hook of the next scheduled action. + * + * [--field=] + * : Prints the value of a single property for the action. + * + * [--fields=] + * : Limit the output to specific object properties. + * + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - csv + * - ids + * - json + * - count + * - yaml + * --- + * + * ## AVAILABLE FIELDS + * + * These fields will be displayed by default for the action: + * + * * id + * * hook + * * status + * * group + * * recurring + * * scheduled_date + * + * These fields are optionally available: + * + * * args + * * log_entries + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function next( array $args, array $assoc_args ) : void { + require_once 'Action/Next.php'; + $command = new ActionScheduler_WPCLI_Action_Next_Command( $args, $assoc_args ); + $command->execute(); + } + + /** + * Run existing scheduled action(s). + * + * ## OPTIONS + * + * ... + * : One or more IDs of actions to run. + * --- + * default: 0 + * --- + * + * ## EXAMPLES + * + * # Run the action with id 100 + * $ wp action-scheduler action run 100 + * + * # Run the actions with ids 100 and 200 + * $ wp action-scheduler action run 100 200 + * + * # Run the first five pending actions in 'action-scheduler' group + * $ wp action-scheduler action run $( wp action-scheduler action list --status=pending --group=action-scheduler --format=ids ) + * + * @param array $args + * @param array $assoc_args + * @return void + */ + function run( array $args, array $assoc_args ) : void { + require_once 'Action/Run.php'; + $command = new ActionScheduler_WPCLI_Action_Run_Command( $args, $assoc_args ); + $command->execute(); + } + +} diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php new file mode 100644 index 000000000..440729970 --- /dev/null +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -0,0 +1,205 @@ +store = \ActionScheduler::store(); + } + + /** + * Print in-use data store class. + * + * @param array $args + * @param array $assoc_args + * @uses $this->get_current_datastore() + * @return void + * + * @subcommand data-store + */ + function datastore( array $args, array $assoc_args ) : void { + echo $this->get_current_datastore(); + } + + /** + * Print in-use runner class. + * + * @param array $args + * @param array $assoc_args + * @uses $this->get_current_runner() + * @return void + */ + function runner( array $args, array $assoc_args ) : void { + echo $this->get_current_runner(); + } + + /** + * Get system status. + * + * @param array $args + * @param array $assoc_args + * @uses $this->get_current_datastore() + * @uses $this->get_latest_version() + * @uses $this->print_statuses() + * @return void + */ + function status( array $args, array $assoc_args ) : void { + /** + * Get runner status. + * + * @link https://github.com/woocommerce/action-scheduler-disable-default-runner + */ + $runner_enabled = has_action( 'action_scheduler_run_queue', array( ActionScheduler::runner(), 'run' ) ); + + \WP_CLI::line( sprintf( 'Data store: %s', $this->get_current_datastore() ) ); + \WP_CLI::line( sprintf( 'Runner: %s%s', $this->get_current_runner(), ( $runner_enabled ? '' : ' (disabled)' ) ) ); + \WP_CLI::line( sprintf( 'Version: %s', $this->get_latest_version() ) ); + + $rows = array(); + $action_counts = $this->store->action_counts(); + $oldest_and_newest = $this->get_oldest_and_newest( array_keys( $action_counts ) ); + + foreach( $action_counts as $status => $count ) + $rows[] = array( + 'status' => $status, + 'count' => $count, + 'oldest' => $oldest_and_newest[ $status ]['oldest'], + 'newest' => $oldest_and_newest[ $status ]['newest'], + ); + + $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'status', 'count', 'oldest', 'newest' ) ); + $formatter->display_items( $rows ); + } + + /** + * Get latest or all system versions. + * + * ## OPTIONS + * + * [--all] + * : Get all system versions. + * + * @param array $args + * @param array $assoc_args + * @uses \ActionScheduler_Versions::get_versions() + * @uses \WP_CLI\Formatter::display_items() + * @uses $this->get_latest_version() + * @return void + */ + function version( array $args, array $assoc_args ) : void { + $all = ( bool ) get_flag_value( $assoc_args, 'all' ); + $instance = \ActionScheduler_Versions::instance(); + + if ( $all ) { + $versions = $instance->get_versions(); + + $rows = array(); + + foreach ( $versions as $version => $callback ) + $rows[ $version ] = array( + 'version' => $version, + 'callback' => $callback, + ); + + ksort( $rows, SORT_NUMERIC ); + + $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback' ) ); + $formatter->display_items( $rows ); + + return; + } + + echo $this->get_latest_version( $instance ); + } + + /** + * Get current data store. + * + * @return string + */ + protected function get_current_datastore() : string { + return get_class( $this->store ); + } + + /** + * Get latest version. + * + * @param null|\ActionScheduler_Versions + * @uses \ActionScheduler_Versions::latest_version() + * @return string + */ + protected function get_latest_version( $instance = null ) : string { + if ( is_null( $instance ) ) + $instance = \ActionScheduler_Versions::instance(); + + return $instance->latest_version(); + } + + /** + * Get current runner. + * + * @uses \ActionScheduler::runner() + * @return string + */ + protected function get_current_runner() : string { + return get_class( \ActionScheduler::runner() ); + } + + /** + * Get oldest and newest scheduled dates for a given set of statuses. + * + * @param array $status_keys Set of statuses to find oldest & newest action for. + * @return array + */ + protected function get_oldest_and_newest( $status_keys ) : array { + $oldest_and_newest = array(); + + foreach ( $status_keys as $status ) { + $oldest_and_newest[ $status ] = array( + 'oldest' => '–', + 'newest' => '–', + ); + + if ( 'in-progress' === $status ) { + continue; + } + + $oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' ); + $oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' ); + } + + return $oldest_and_newest; + } + + /** + * Get oldest or newest scheduled date for a given status. + * + * @param string $status Action status label/name string. + * @param string $date_type Oldest or Newest. + * @return string + */ + protected function get_action_status_date( $status, $date_type = 'oldest' ) : string { + $order = 'oldest' === $date_type ? 'ASC' : 'DESC'; + + $action = $this->store->query_actions( array( + 'claimed' => false, + 'status' => $status, + 'per_page' => 1, + 'order' => $order, + ) ); + + if ( !empty( $action ) ) { + $date_object = $this->store->get_date( $action[0] ); + $action_date = $date_object->format( 'Y-m-d H:i:s O' ); + } else { + $action_date = '–'; + } + + return $action_date; + } + +} diff --git a/classes/abstracts/ActionScheduler.php b/classes/abstracts/ActionScheduler.php index 0163f7072..bb5487762 100644 --- a/classes/abstracts/ActionScheduler.php +++ b/classes/abstracts/ActionScheduler.php @@ -197,6 +197,8 @@ function () { if ( defined( 'WP_CLI' ) && WP_CLI ) { WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' ); WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Clean_Command' ); + WP_CLI::add_command( 'action-scheduler action', 'ActionScheduler_WPCLI_Action_Command' ); + WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_System_Command' ); if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) { $command = new Migration_Command(); $command->register(); @@ -253,6 +255,7 @@ protected static function is_class_abstract( $class ) { 'ActionScheduler_Abstract_Schema' => true, 'ActionScheduler_Store' => true, 'ActionScheduler_TimezoneHelper' => true, + 'ActionScheduler_WPCLI_Command' => true, ); return isset( $abstracts[ $class ] ) && $abstracts[ $class ]; @@ -297,9 +300,11 @@ protected static function is_class_migration( $class ) { */ protected static function is_class_cli( $class ) { static $cli_segments = array( - 'QueueRunner' => true, - 'Command' => true, - 'ProgressBar' => true, + 'QueueRunner' => true, + 'Command' => true, + 'ProgressBar' => true, + 'ActionScheduler_WPCLI_Action_Command' => true, + 'ActionScheduler_WPCLI_System_Command' => true, ); $segments = explode( '_', $class ); diff --git a/classes/abstracts/ActionScheduler_WPCLI_Command.php b/classes/abstracts/ActionScheduler_WPCLI_Command.php new file mode 100644 index 000000000..6e7c1e0ec --- /dev/null +++ b/classes/abstracts/ActionScheduler_WPCLI_Command.php @@ -0,0 +1,78 @@ +args = $args; + $this->assoc_args = $assoc_args; + } + + abstract public function execute() : void; + + /** + * Get the scheduled date in a human friendly format. + * + * @see \ActionScheduler_ListTable::get_schedule_display_string() + * @param ActionScheduler_Schedule $schedule + * @return string + */ + protected function get_schedule_display_string( \ActionScheduler_Schedule $schedule ) { + + $schedule_display_string = ''; + + if ( ! $schedule->get_date() ) { + return '0000-00-00 00:00:00'; + } + + $next_timestamp = $schedule->get_date()->getTimestamp(); + + $schedule_display_string .= $schedule->get_date()->format( static::DATE_FORMAT ); + + return $schedule_display_string; + } + + /** + * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. + * + * @see \ActionScheduler_ListTable::get_recurrence() + * @param ActionScheduler_Action $action + * + * @return string + */ + protected function get_recurrence( $action ) { + $schedule = $action->get_schedule(); + if ( $schedule->is_recurring() ) { + $recurrence = $schedule->get_recurrence(); + + if ( is_numeric( $recurrence ) ) { + /* translators: %s: time interval */ + return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); + } else { + return $recurrence; + } + } + + return __( 'Non-repeating', 'action-scheduler' ); + } + + /** + * Transforms arguments with '__' from CSV into expected arrays. + * + * @see \WP_CLI\CommandWithDBObject::process_csv_arguments_to_arrays() + * @link https://github.com/wp-cli/entity-command/blob/6e0e77a297eefa3329b94bec16c15cf7528d343f/src/WP_CLI/CommandWithDBObject.php + * @return void + */ + protected function process_csv_arguments_to_arrays() : void { + foreach ( $this->assoc_args as $k => $v ) { + if ( false !== strpos( $k, '__' ) ) { + $this->assoc_args[ $k ] = explode( ',', $v ); + } + } + } + +} From a34d670f54c3590d15c883b4f82474db87948fe8 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 19 Jun 2024 12:09:20 -0400 Subject: [PATCH 02/52] eliminate code smells --- classes/WP_CLI/Action/Cancel.php | 25 ++-- classes/WP_CLI/Action/Create.php | 47 ++++--- classes/WP_CLI/Action/Delete.php | 45 +++++-- classes/WP_CLI/Action/Generate.php | 39 +++--- classes/WP_CLI/Action/Get.php | 11 +- classes/WP_CLI/Action/List.php | 17 ++- classes/WP_CLI/Action/Next.php | 68 +++++----- classes/WP_CLI/Action/Run.php | 120 +++++++++++++----- .../ActionScheduler_WPCLI_Action_Command.php | 50 ++++---- .../ActionScheduler_WPCLI_System_Command.php | 70 +++++----- .../ActionScheduler_WPCLI_Command.php | 31 +++-- 11 files changed, 332 insertions(+), 191 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel.php index 88fa10bf1..16962a0b6 100644 --- a/classes/WP_CLI/Action/Cancel.php +++ b/classes/WP_CLI/Action/Cancel.php @@ -1,7 +1,10 @@ -print_success() * @return void */ - public function execute() : void { + public function execute() { $hook = $this->args[0]; - $group = $this->args[1] ?? null; + $group = null; $callback_args = get_flag_value( $this->assoc_args, 'args', null ); $all = get_flag_value( $this->assoc_args, 'all' ); + if ( ! empty( $this->args[1] ) ) { + $group = $this->args[1]; + } + if ( ! empty( $callback_args ) ) { $callback_args = json_decode( $callback_args, true ); } @@ -43,10 +50,10 @@ public function execute() : void { /** * Print a success message. * - * @param bool $multiple + * @param bool $multiple Boolean if multiple actions. * @return void */ - protected function print_success( bool $multiple ) : void { + protected function print_success( $multiple ) { \WP_CLI::success( _n( 'Scheduled action cancelled.', 'All scheduled actions cancelled.', $multiple ? 2 : 1, 'action-scheduler' ) ); } @@ -54,15 +61,15 @@ protected function print_success( bool $multiple ) : void { * Convert an exception into a WP CLI error. * * @param \Exception $e The error object. - * @param bool $multiple + * @param bool $multiple Boolean if multiple actions. * @throws \WP_CLI\ExitException * @return void */ - protected function print_error( \Exception $e, bool $multiple ) : void { + protected function print_error( \Exception $e, $multiple ) { \WP_CLI::error( sprintf( - /* translators: %s refers to the exception error message. */ - __( 'There was an error cancelling the scheduled %s: %s', 'action-scheduler' ), + /* translators: %1$s: singular or plural %2$s: refers to the exception error message. */ + __( 'There was an error cancelling the scheduled %1$s: %2$s', 'action-scheduler' ), $multiple ? 'actions' : 'action', $e->getMessage() ) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index 6d108ef1d..d8bedab71 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -1,7 +1,10 @@ -print_success() * @return void */ - public function execute() : void { + public function execute() { $hook = $this->args[0]; $schedule_start = $this->args[1]; $callback_args = get_flag_value( $this->assoc_args, 'args', array() ); @@ -25,8 +28,9 @@ public function execute() : void { $interval = absint( get_flag_value( $this->assoc_args, 'interval', 0 ) ); $cron = get_flag_value( $this->assoc_args, 'cron', '' ); - if ( !empty( $callback_args ) ) + if ( ! empty( $callback_args ) ) { $callback_args = json_decode( $callback_args, true ); + } $function_args = array( 'start' => 'async', @@ -38,8 +42,8 @@ public function execute() : void { ); // Generate schedule start if appropriate. - if ( ! in_array( $schedule_start, static::ASYNC_OPTS ) ) { - $schedule_start = as_get_datetime_object( $schedule_start ); + if ( ! in_array( $schedule_start, static::ASYNC_OPTS, true ) ) { + $schedule_start = as_get_datetime_object( $schedule_start ); $function_args['start'] = $schedule_start->format( 'U' ); } @@ -47,22 +51,23 @@ public function execute() : void { $action_type = 'single'; $function = 'as_schedule_single_action'; - // Enqueue async action. - if ( 'async' === $function_args['start'] ) { + if ( 'async' === $function_args['start'] ) { // Enqueue async action. $action_type = 'async'; $function = 'as_enqueue_async_action'; - $function_args = array_filter( $function_args, static function( string $key ) : bool { - return in_array( $key, array( 'hook', 'callback_args', 'group' ) ); - }, ARRAY_FILTER_USE_KEY ); + $function_args = array_filter( + $function_args, + static function( $key ) { + return in_array( $key, array( 'hook', 'callback_args', 'group' ), true ); + }, + ARRAY_FILTER_USE_KEY + ); - // Creating recurring action. - } else if ( !empty( $interval ) ) { + } else if ( ! empty( $interval ) ) { // Creating recurring action. $action_type = 'recurring'; $function = 'as_schedule_recurring_action'; - // Creating cron action. - } else if ( !empty( $cron ) ) { + } else if ( ! empty( $cron ) ) { // Creating cron action. $action_type = 'cron'; $function = 'as_schedule_cron_action'; } @@ -75,7 +80,7 @@ public function execute() : void { $this->print_error( $e ); } - $num_actions_added = count( ( array ) $actions_added ); + $num_actions_added = count( (array) $actions_added ); $this->print_success( $num_actions_added, $action_type ); } @@ -83,16 +88,16 @@ public function execute() : void { /** * Print a success message with the action ID. * - * @param int $action_added - * @param string $action_type + * @param int $actions_added Number actions added. + * @param string $action_type Type of action. * * @return void */ - protected function print_success( $actions_added, $action_type ) : void { + protected function print_success( $actions_added, $action_type ) { \WP_CLI::success( sprintf( - /* translators: %d refers to the total number of taskes added */ - _n( '%d %s action scheduled.', '%d %s actions scheduled.', $actions_added, 'action-scheduler' ), + /* translators: %1$d: refers to the total number of tasks added, %2$s: type of action */ + _n( '%1$d %2$s action scheduled.', '%1$d %2$s actions scheduled.', $actions_added, 'action-scheduler' ), number_format_i18n( $actions_added ), $action_type ) @@ -108,7 +113,7 @@ protected function print_success( $actions_added, $action_type ) : void { * * @return void */ - protected function print_error( \Exception $e ) : void { + protected function print_error( \Exception $e ) { \WP_CLI::error( sprintf( /* translators: %s refers to the exception error message. */ diff --git a/classes/WP_CLI/Action/Delete.php b/classes/WP_CLI/Action/Delete.php index fc82910a3..d42cb4946 100644 --- a/classes/WP_CLI/Action/Delete.php +++ b/classes/WP_CLI/Action/Delete.php @@ -1,17 +1,29 @@ - */ protected $action_counts = array( 'deleted' => 0, 'total' => 0, ); - function __construct( array $args, array $assoc_args ) { + /** + * Construct. + * + * @param string[] $args Positional arguments. + * @param array $assoc_args Keyed arguments. + */ + public function __construct( array $args, array $assoc_args ) { parent::__construct( $args, $assoc_args ); - $this->action_ids = array_map( 'absint', $args ); + $this->action_ids = array_map( 'absint', $args ); $this->action_counts['total'] = count( $this->action_ids ); add_action( 'action_scheduler_deleted_action', array( $this, 'action__deleted' ) ); @@ -25,11 +37,12 @@ function __construct( array $args, array $assoc_args ) { * @uses \WP_CLI::success() * @return void */ - function execute() : void { + public function execute() { $store = \ActionScheduler::store(); $progress_bar = \WP_CLI\Utils\make_progress_bar( sprintf( + /* translators: %d: number of actions to be deleted */ _n( 'Deleting %d action', 'Deleting %d actions', $this->action_counts['total'], 'action-scheduler' ), number_format_i18n( $this->action_counts['total'] ) ), @@ -43,22 +56,32 @@ function execute() : void { $progress_bar->finish(); - \WP_CLI::success( sprintf( - _n( 'Deleted %d action.', 'Deleted %d actions.', $this->action_counts['deleted'], 'action-scheduler' ), - number_format_i18n( $this->action_counts['deleted'] ) - ) ); + \WP_CLI::success( + sprintf( + /* translators: %d: number of actions deleted */ + _n( 'Deleted %d action.', 'Deleted %d actions.', $this->action_counts['deleted'], 'action-scheduler' ), + number_format_i18n( $this->action_counts['deleted'] ) + ) + ); } /** * Action: action_scheduler_deleted_action * - * @param int $action_id + * @param int $action_id Action ID. * @uses \WP_CLI::debug() * @return void */ - function action__deleted( int $action_id ) : void { - if ( !in_array( $action_id, $this->action_ids ) ) + public function action__deleted( $action_id ) { + if ( 'action_scheduler_deleted_action' !== current_action() ) { return; + } + + $action_id = absint( $action_id ); + + if ( ! in_array( $action_id, $this->action_ids, true ) ) { + return; + } $this->action_counts['deleted']++; \WP_CLI::debug( sprintf( 'Action %d was deleted.', $action_id ) ); diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php index 34b3a843c..c4417d1e6 100644 --- a/classes/WP_CLI/Action/Generate.php +++ b/classes/WP_CLI/Action/Generate.php @@ -1,7 +1,10 @@ -print_success() * @return void */ - public function execute() : void { + public function execute() { $hook = $this->args[0]; $schedule_start = $this->args[1]; $callback_args = get_flag_value( $this->assoc_args, 'args', array() ); @@ -20,7 +23,7 @@ public function execute() : void { $interval = absint( get_flag_value( $this->assoc_args, 'interval', 0 ) ); $count = absint( get_flag_value( $this->assoc_args, 'count', 1 ) ); - if ( !empty( $callback_args ) ) { + if ( ! empty( $callback_args ) ) { $callback_args = json_decode( $callback_args, true ); } @@ -44,7 +47,7 @@ public function execute() : void { $this->print_error( $e ); } - $num_actions_added = count( ( array ) $actions_added ); + $num_actions_added = count( (array) $actions_added ); $this->print_success( $num_actions_added, $action_type ); } @@ -52,20 +55,24 @@ public function execute() : void { /** * Schedule multiple single actions. * - * @param int $schedule_start Starting timestamp of first action. - * @param int $interval How long to wait between runs. - * @param int $count Limit number of actions to schedule. + * @param int $schedule_start Starting timestamp of first action. + * @param int $interval How long to wait between runs. + * @param int $count Limit number of actions to schedule. * @param string $hook The hook to trigger. - * @param array $args Arguments to pass when the hook triggers. + * @param array $args Arguments to pass when the hook triggers. * @param string $group The group to assign this job to. * @uses as_schedule_single_action() * @return int[] IDs of actions added. */ - protected function generate( int $schedule_start, int $interval, int $count, string $hook, array $args = array(), string $group = '' ) : array { + protected function generate( $schedule_start, $interval, $count, $hook, array $args = array(), $group = '' ) { $actions_added = array(); $progress_bar = \WP_CLI\Utils\make_progress_bar( - sprintf( _n( 'Creating %d action', 'Creating %d actions', $count, 'action-scheduler' ), number_format_i18n( $count ) ), + sprintf( + /* translators: %d is number of actions to create */ + _n( 'Creating %d action', 'Creating %d actions', $count, 'action-scheduler' ), + number_format_i18n( $count ) + ), $count ); @@ -82,15 +89,15 @@ protected function generate( int $schedule_start, int $interval, int $count, str /** * Print a success message with the action ID. * - * @param int $action_added - * @param string $action_type + * @param int $actions_added Number of actions generated. + * @param string $action_type Type of actions scheduled. * @return void */ - protected function print_success( $actions_added, $action_type ) : void { + protected function print_success( $actions_added, $action_type ) { \WP_CLI::success( sprintf( - /* translators: %d refers to the total number of taskes added */ - _n( '%d %s action scheduled.', '%d %s actions scheduled.', $actions_added, 'action-scheduler' ), + /* translators: %1$d refers to the total number of tasks added, %2$s is the action type */ + _n( '%1$d %2$s action scheduled.', '%1$d %2$s actions scheduled.', $actions_added, 'action-scheduler' ), number_format_i18n( $actions_added ), $action_type ) @@ -104,7 +111,7 @@ protected function print_success( $actions_added, $action_type ) : void { * @throws \WP_CLI\ExitException * @return void */ - protected function print_error( \Exception $e ) : void { + protected function print_error( \Exception $e ) { \WP_CLI::error( sprintf( /* translators: %s refers to the exception error message. */ diff --git a/classes/WP_CLI/Action/Get.php b/classes/WP_CLI/Action/Get.php index 5ad9f8b97..1c0560b46 100644 --- a/classes/WP_CLI/Action/Get.php +++ b/classes/WP_CLI/Action/Get.php @@ -1,6 +1,8 @@ -args[0]; $store = \ActionScheduler::store(); $logger = \ActionScheduler::logger(); @@ -34,8 +36,9 @@ public function execute() : void { $fields = array_keys( $action_arr ); - if ( !empty( $this->assoc_args['fields'] ) ) + if ( ! empty( $this->assoc_args['fields'] ) ) { $fields = explode( ',', $this->assoc_args['fields'] ); + } $formatter = new \WP_CLI\Formatter( $this->assoc_args, $fields ); $formatter->display_item( $action_arr ); diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List.php index e6dd020ca..5980f1755 100644 --- a/classes/WP_CLI/Action/List.php +++ b/classes/WP_CLI/Action/List.php @@ -1,5 +1,8 @@ -assoc_args, $fields ); - $query_args = array_filter( $this->assoc_args, static function ( string $key ) : bool { - return in_array( $key, static::PARAMETERS ); - }, ARRAY_FILTER_USE_KEY ); + $query_args = array_filter( + $this->assoc_args, + static function ( $key ) { + return in_array( $key, static::PARAMETERS, true ); + }, + ARRAY_FILTER_USE_KEY + ); if ( ! empty( $query_args['args'] ) ) { $query_args['args'] = json_decode( $query_args['args'], true ); diff --git a/classes/WP_CLI/Action/Next.php b/classes/WP_CLI/Action/Next.php index e7e328d0f..3f27fa250 100644 --- a/classes/WP_CLI/Action/Next.php +++ b/classes/WP_CLI/Action/Next.php @@ -1,9 +1,10 @@ -args[0]; $group = get_flag_value( $this->assoc_args, 'group', '' ); $callback_args = get_flag_value( $this->assoc_args, 'args', null ); - if ( !empty( $callback_args ) ) + if ( ! empty( $callback_args ) ) { $callback_args = json_decode( $callback_args, true ); + } $next_action_id = $this->as_next_scheduled_action( $hook, $callback_args, $group ); @@ -37,8 +39,9 @@ function execute() : void { $this->process_csv_arguments_to_arrays(); - if ( !empty( $this->assoc_args['fields'] ) ) + if ( ! empty( $this->assoc_args['fields'] ) ) { $fields = $this->assoc_args['fields']; + } $store = \ActionScheduler::store(); $logger = \ActionScheduler::logger(); @@ -69,8 +72,9 @@ function execute() : void { ); } - if ( !empty( $this->assoc_args['fields'] ) ) + if ( ! empty( $this->assoc_args['fields'] ) ) { $fields = explode( ',', $this->assoc_args['fields'] ); + } $formatter->display_item( $action_arr ); } @@ -79,41 +83,47 @@ function execute() : void { * Get next scheduled action. * * @see as_next_scheduled_action() - * @param string $hook - * @param null|array $args - * @param string $group + * @param string $hook Name of the hook to search for. + * @param array $args Arguments of the action to be searched. + * @param string $group Group of the action to be searched. * @return int */ - protected function as_next_scheduled_action( $hook, $args = null, $group = '' ) : int { - if ( ! \ActionScheduler::is_initialized( 'as_next_scheduled_action' ) ) { + protected function as_next_scheduled_action( $hook, $args = null, $group = '' ) { + if ( ! ActionScheduler::is_initialized( 'as_next_scheduled_action' ) ) { return 0; } - $params = array(); - if ( is_array($args) ) { + + $params = array( + 'hook' => $hook, + 'orderby' => 'date', + 'order' => 'ASC', + 'group' => $group, + ); + + if ( is_array( $args ) ) { $params['args'] = $args; } - if ( !empty($group) ) { - $params['group'] = $group; - } - $params['status'] = \ActionScheduler_Store::STATUS_RUNNING; - $job_id = absint( \ActionScheduler::store()->find_action( $hook, $params ) ); - if ( ! empty( $job_id ) ) { - return $job_id; + $params['status'] = ActionScheduler_Store::STATUS_RUNNING; + $action_id = ActionScheduler::store()->query_action( $params ); + if ( $action_id ) { + return $action_id; } - $params['status'] = \ActionScheduler_Store::STATUS_PENDING; - $job_id = absint( \ActionScheduler::store()->find_action( $hook, $params ) ); - if ( empty($job_id) ) { + $params['status'] = ActionScheduler_Store::STATUS_PENDING; + $action_id = ActionScheduler::store()->query_action( $params ); + if ( null === $action_id ) { return 0; } - $job = \ActionScheduler::store()->fetch_action( $job_id ); - $scheduled_date = $job->get_schedule()->get_date(); + + $action = ActionScheduler::store()->fetch_action( $action_id ); + $scheduled_date = $action->get_schedule()->get_date(); if ( $scheduled_date ) { - return $job_id; - } elseif ( NULL === $scheduled_date ) { // pending async action with NullSchedule - return $job_id; + return (int) $scheduled_date->format( 'U' ); + } elseif ( null === $scheduled_date ) { // pending async action with NullSchedule. + return $action_id; } + return 0; } diff --git a/classes/WP_CLI/Action/Run.php b/classes/WP_CLI/Action/Run.php index ae89d75af..7b248ef1d 100644 --- a/classes/WP_CLI/Action/Run.php +++ b/classes/WP_CLI/Action/Run.php @@ -1,8 +1,14 @@ - */ protected $action_counts = array( 'executed' => 0, 'failed' => 0, @@ -11,16 +17,22 @@ class ActionScheduler_WPCLI_Action_Run_Command extends ActionScheduler_WPCLI_Com 'total' => 0, ); - function __construct( array $args, array $assoc_args ) { + /** + * Construct. + * + * @param string[] $args Positional arguments. + * @param array $assoc_args Keyed arguments. + */ + public function __construct( array $args, array $assoc_args ) { parent::__construct( $args, $assoc_args ); - $this->action_ids = array_map( 'absint', $args ); + $this->action_ids = array_map( 'absint', $args ); $this->action_counts['total'] = count( $this->action_ids ); - add_action( 'action_scheduler_execution_ignored', array( $this, 'action__ignored' ) ); - add_action( 'action_scheduler_after_execute', array( $this, 'action__executed' ) ); - add_action( 'action_scheduler_failed_execution', array( $this, 'action__failed' ), 10, 2 ); - add_action( 'action_scheduler_failed_validation', array( $this, 'action__invalid' ), 10, 2 ); + add_action( 'action_scheduler_execution_ignored', array( $this, 'action__ignored' ) ); + add_action( 'action_scheduler_after_execute', array( $this, 'action__executed' ) ); + add_action( 'action_scheduler_failed_execution', array( $this, 'action__failed' ), 10, 2 ); + add_action( 'action_scheduler_failed_validation', array( $this, 'action__invalid' ), 10, 2 ); } /** @@ -31,11 +43,12 @@ function __construct( array $args, array $assoc_args ) { * @uses \WP_CLI::success() * @return void */ - function execute() : void { + public function execute() { $runner = \ActionScheduler::runner(); $progress_bar = \WP_CLI\Utils\make_progress_bar( sprintf( + /* translators: %d: number of actions */ _n( 'Executing %d action', 'Executing %d actions', $this->action_counts['total'], 'action-scheduler' ), number_format_i18n( $this->action_counts['total'] ) ), @@ -56,32 +69,52 @@ function execute() : void { ) as $type ) { $count = $this->action_counts[ $type ]; - if ( empty( $count ) ) + if ( empty( $count ) ) { continue; - - \WP_CLI::warning( sprintf( - _n( '%d action %s.', '%d actions %s.', $count, 'action-scheduler' ), - number_format_i18n( $count ), - $type - ) ); + } + + /* + * translators: + * %1$d: count of actions evaluated. + * %2$s: type of action evaluated. + */ + $format = _n( '%1$d action %2$s.', '%1$d actions %2$s.', $count, 'action-scheduler' ), + + \WP_CLI::warning( + sprintf( + $format, + number_format_i18n( $count ), + $type + ) + ); } - \WP_CLI::success( sprintf( - _n( 'Executed %d action.', 'Executed %d actions.', $this->action_counts['executed'], 'action-scheduler' ), - number_format_i18n( $this->action_counts['executed'] ) - ) ); + \WP_CLI::success( + sprintf( + /* translators: %d: number of executed actions */ + _n( 'Executed %d action.', 'Executed %d actions.', $this->action_counts['executed'], 'action-scheduler' ), + number_format_i18n( $this->action_counts['executed'] ) + ) + ); } /** * Action: action_scheduler_execution_ignored * - * @param int $action_id + * @param int $action_id Action ID. * @uses \WP_CLI::debug() * @return void */ - function action__ignored( int $action_id ) : void { - if ( !in_array( $action_id, $this->action_ids ) ) + public function action__ignored( $action_id ) { + if ( 'action_scheduler_execution_ignored' !== current_action() ) { return; + } + + $action_id = absint( $action_id ); + + if ( ! in_array( $action_id, $this->action_ids, true ) ) { + return; + } $this->action_counts['ignored']++; \WP_CLI::debug( sprintf( 'Action %d was ignored.', $action_id ) ); @@ -90,13 +123,20 @@ function action__ignored( int $action_id ) : void { /** * Action: action_scheduler_after_execute * - * @param int $action_id + * @param int $action_id Action ID. * @uses \WP_CLI::success() * @return void */ - function action__executed( int $action_id ) : void { - if ( !in_array( $action_id, $this->action_ids ) ) + public function action__executed( $action_id ) { + if ( 'action_scheduler_after_execute' !== current_action() ) { return; + } + + $action_id = absint( $action_id ); + + if ( ! in_array( $action_id, $this->action_ids, true ) ) { + return; + } $this->action_counts['executed']++; \WP_CLI::debug( sprintf( 'Action %d was executed.', $action_id ) ); @@ -105,14 +145,21 @@ function action__executed( int $action_id ) : void { /** * Action: action_scheduler_failed_execution * - * @param int $action_id - * @param \Exception $e + * @param int $action_id Action ID. + * @param \Exception $e Exception. * @uses \WP_CLI::debug() * @return void */ - function action__failed( int $action_id, \Exception $e ) : void { - if ( !in_array( $action_id, $this->action_ids ) ) + public function action__failed( $action_id, \Exception $e ) { + if ( 'action_scheduler_failed_execution' !== current_action() ) { return; + } + + $action_id = absint( $action_id ); + + if ( ! in_array( $action_id, $this->action_ids, true ) ) { + return; + } $this->action_counts['failed']++; \WP_CLI::debug( sprintf( 'Action %d failed execution: %s', $action_id, $e->getMessage() ) ); @@ -121,14 +168,21 @@ function action__failed( int $action_id, \Exception $e ) : void { /** * Action: action_scheduler_failed_validation * - * @param int $action_id - * @param \Exception $e + * @param int $action_id Action ID. + * @param \Exception $e Exception. * @uses \WP_CLI::debug() * @return void */ - function action__invalid( int $action_id, \Exception $e ) : void { - if ( !in_array( $action_id, $this->action_ids ) ) + public function action__invalid( $action_id, \Exception $e ) { + if ( 'action_scheduler_failed_validation' !== current_action() ) { + return; + } + + $action_id = absint( $action_id ); + + if ( ! in_array( $action_id, $this->action_ids, true ) ) { return; + } $this->action_counts['invalid']++; \WP_CLI::debug( sprintf( 'Action %d failed validation: %s', $action_id, $e->getMessage() ) ); diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php index d88d9e53e..de4c74be7 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php @@ -25,11 +25,11 @@ class ActionScheduler_WPCLI_Action_Command extends WP_CLI_Command { * [--all] * : Cancel all occurrences of a scheduled action. * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void */ - function cancel( array $args, array $assoc_args ) : void { + public function cancel( array $args, array $assoc_args ) { require_once 'Action/Cancel.php'; $command = new ActionScheduler_WPCLI_Action_Cancel_Command( $args, $assoc_args ); $command->execute(); @@ -77,11 +77,11 @@ function cancel( array $args, array $assoc_args ) : void { * wp action-scheduler action create hook_recurring 1627148188 --interval=5 * wp action-scheduler action create hook_cron 1627147655 --cron='5 4 * * *' * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void */ - function create( array $args, array $assoc_args ) : void { + public function create( array $args, array $assoc_args ) { require_once 'Action/Create.php'; $command = new ActionScheduler_WPCLI_Action_Create_Command( $args, $assoc_args ); $command->execute(); @@ -109,11 +109,11 @@ function create( array $args, array $assoc_args ) : void { * # Delete the first five pending actions in 'action-scheduler' group * $ wp action-scheduler action delete $( wp action-scheduler action list --status=pending --group=action-scheduler --format=ids ) * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void */ - function delete( array $args, array $assoc_args ) : void { + public function delete( array $args, array $assoc_args ) { require_once 'Action/Delete.php'; $command = new ActionScheduler_WPCLI_Action_Delete_Command( $args, $assoc_args ); $command->execute(); @@ -158,11 +158,11 @@ function delete( array $args, array $assoc_args ) : void { * * wp action-scheduler action generate test_multiple 1627147598 --count=5 --interval=5 * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void */ - function generate( array $args, array $assoc_args ) : void { + public function generate( array $args, array $assoc_args ) { require_once 'Action/Generate.php'; $command = new ActionScheduler_WPCLI_Action_Generate_Command( $args, $assoc_args ); $command->execute(); @@ -196,11 +196,11 @@ function generate( array $args, array $assoc_args ) : void { * - yaml * --- * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void */ - function get( array $args, array $assoc_args ) : void { + public function get( array $args, array $assoc_args ) { require_once 'Action/Get.php'; $command = new ActionScheduler_WPCLI_Action_Get_Command( $args, $assoc_args ); $command->execute(); @@ -252,11 +252,13 @@ function get( array $args, array $assoc_args ) : void { * * args * * log_entries * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void + * + * @subcommand list */ - function list( array $args, array $assoc_args ) : void { + public function subcommand_list( array $args, array $assoc_args ) { require_once 'Action/List.php'; $command = new ActionScheduler_WPCLI_Action_List_Command( $args, $assoc_args ); $command->execute(); @@ -305,11 +307,11 @@ function list( array $args, array $assoc_args ) : void { * * args * * log_entries * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void */ - function next( array $args, array $assoc_args ) : void { + public function next( array $args, array $assoc_args ) { require_once 'Action/Next.php'; $command = new ActionScheduler_WPCLI_Action_Next_Command( $args, $assoc_args ); $command->execute(); @@ -337,11 +339,11 @@ function next( array $args, array $assoc_args ) : void { * # Run the first five pending actions in 'action-scheduler' group * $ wp action-scheduler action run $( wp action-scheduler action list --status=pending --group=action-scheduler --format=ids ) * - * @param array $args - * @param array $assoc_args + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. * @return void */ - function run( array $args, array $assoc_args ) : void { + public function run( array $args, array $assoc_args ) { require_once 'Action/Run.php'; $command = new ActionScheduler_WPCLI_Action_Run_Command( $args, $assoc_args ); $command->execute(); diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index 440729970..59e6c35d5 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -1,53 +1,56 @@ -store = \ActionScheduler::store(); } /** * Print in-use data store class. * - * @param array $args - * @param array $assoc_args + * @param array $args Positional args. + * @param array $assoc_args Keyed args. * @uses $this->get_current_datastore() * @return void * * @subcommand data-store */ - function datastore( array $args, array $assoc_args ) : void { + public function datastore( array $args, array $assoc_args ) { echo $this->get_current_datastore(); } /** * Print in-use runner class. * - * @param array $args - * @param array $assoc_args + * @param array $args Positional args. + * @param array $assoc_args Keyed args. * @uses $this->get_current_runner() * @return void */ - function runner( array $args, array $assoc_args ) : void { + public function runner( array $args, array $assoc_args ) { echo $this->get_current_runner(); } /** * Get system status. * - * @param array $args - * @param array $assoc_args + * @param array $args Positional args. + * @param array $assoc_args Keyed args. * @uses $this->get_current_datastore() * @uses $this->get_latest_version() * @uses $this->print_statuses() * @return void */ - function status( array $args, array $assoc_args ) : void { + public function status( array $args, array $assoc_args ) { /** * Get runner status. * @@ -56,20 +59,21 @@ function status( array $args, array $assoc_args ) : void { $runner_enabled = has_action( 'action_scheduler_run_queue', array( ActionScheduler::runner(), 'run' ) ); \WP_CLI::line( sprintf( 'Data store: %s', $this->get_current_datastore() ) ); - \WP_CLI::line( sprintf( 'Runner: %s%s', $this->get_current_runner(), ( $runner_enabled ? '' : ' (disabled)' ) ) ); - \WP_CLI::line( sprintf( 'Version: %s', $this->get_latest_version() ) ); + \WP_CLI::line( sprintf( 'Runner: %s%s', $this->get_current_runner(), ( $runner_enabled ? '' : ' (disabled)' ) ) ); + \WP_CLI::line( sprintf( 'Version: %s', $this->get_latest_version() ) ); - $rows = array(); - $action_counts = $this->store->action_counts(); + $rows = array(); + $action_counts = $this->store->action_counts(); $oldest_and_newest = $this->get_oldest_and_newest( array_keys( $action_counts ) ); - foreach( $action_counts as $status => $count ) + foreach ( $action_counts as $status => $count ) { $rows[] = array( 'status' => $status, 'count' => $count, 'oldest' => $oldest_and_newest[ $status ]['oldest'], 'newest' => $oldest_and_newest[ $status ]['newest'], ); + } $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'status', 'count', 'oldest', 'newest' ) ); $formatter->display_items( $rows ); @@ -83,15 +87,15 @@ function status( array $args, array $assoc_args ) : void { * [--all] * : Get all system versions. * - * @param array $args - * @param array $assoc_args + * @param array $args Positional args. + * @param array $assoc_args Keyed args. * @uses \ActionScheduler_Versions::get_versions() * @uses \WP_CLI\Formatter::display_items() * @uses $this->get_latest_version() * @return void */ - function version( array $args, array $assoc_args ) : void { - $all = ( bool ) get_flag_value( $assoc_args, 'all' ); + public function version( array $args, array $assoc_args ) { + $all = (bool) get_flag_value( $assoc_args, 'all' ); $instance = \ActionScheduler_Versions::instance(); if ( $all ) { @@ -99,11 +103,12 @@ function version( array $args, array $assoc_args ) : void { $rows = array(); - foreach ( $versions as $version => $callback ) + foreach ( $versions as $version => $callback ) { $rows[ $version ] = array( 'version' => $version, 'callback' => $callback, ); + } ksort( $rows, SORT_NUMERIC ); @@ -121,20 +126,21 @@ function version( array $args, array $assoc_args ) : void { * * @return string */ - protected function get_current_datastore() : string { + protected function get_current_datastore() { return get_class( $this->store ); } /** * Get latest version. * - * @param null|\ActionScheduler_Versions + * @param null|\ActionScheduler_Versions $instance Versions. * @uses \ActionScheduler_Versions::latest_version() * @return string */ - protected function get_latest_version( $instance = null ) : string { - if ( is_null( $instance ) ) + protected function get_latest_version( $instance = null ) { + if ( is_null( $instance ) ) { $instance = \ActionScheduler_Versions::instance(); + } return $instance->latest_version(); } @@ -145,7 +151,7 @@ protected function get_latest_version( $instance = null ) : string { * @uses \ActionScheduler::runner() * @return string */ - protected function get_current_runner() : string { + protected function get_current_runner() { return get_class( \ActionScheduler::runner() ); } @@ -155,7 +161,7 @@ protected function get_current_runner() : string { * @param array $status_keys Set of statuses to find oldest & newest action for. * @return array */ - protected function get_oldest_and_newest( $status_keys ) : array { + protected function get_oldest_and_newest( $status_keys ) { $oldest_and_newest = array(); foreach ( $status_keys as $status ) { @@ -182,17 +188,19 @@ protected function get_oldest_and_newest( $status_keys ) : array { * @param string $date_type Oldest or Newest. * @return string */ - protected function get_action_status_date( $status, $date_type = 'oldest' ) : string { + protected function get_action_status_date( $status, $date_type = 'oldest' ) { $order = 'oldest' === $date_type ? 'ASC' : 'DESC'; - $action = $this->store->query_actions( array( + $args = array( 'claimed' => false, 'status' => $status, 'per_page' => 1, 'order' => $order, - ) ); + ); + + $action = $this->store->query_actions( $args ); - if ( !empty( $action ) ) { + if ( ! empty( $action ) ) { $date_object = $this->store->get_date( $action[0] ); $action_date = $date_object->format( 'Y-m-d H:i:s O' ); } else { diff --git a/classes/abstracts/ActionScheduler_WPCLI_Command.php b/classes/abstracts/ActionScheduler_WPCLI_Command.php index 6e7c1e0ec..31ed3994c 100644 --- a/classes/abstracts/ActionScheduler_WPCLI_Command.php +++ b/classes/abstracts/ActionScheduler_WPCLI_Command.php @@ -1,27 +1,42 @@ - */ protected $assoc_args; + /** + * Construct. + * + * @param string[] $args Positional arguments. + * @param array $assoc_args Keyed arguments. + */ public function __construct( array $args, array $assoc_args ) { - $this->args = $args; + $this->args = $args; $this->assoc_args = $assoc_args; } - abstract public function execute() : void; + /** + * Execute command. + */ + abstract public function execute(); /** * Get the scheduled date in a human friendly format. * - * @see \ActionScheduler_ListTable::get_schedule_display_string() - * @param ActionScheduler_Schedule $schedule + * @see ActionScheduler_ListTable::get_schedule_display_string() + * @param ActionScheduler_Schedule $schedule Schedule. * @return string */ - protected function get_schedule_display_string( \ActionScheduler_Schedule $schedule ) { + protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { $schedule_display_string = ''; @@ -40,7 +55,7 @@ protected function get_schedule_display_string( \ActionScheduler_Schedule $sched * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. * * @see \ActionScheduler_ListTable::get_recurrence() - * @param ActionScheduler_Action $action + * @param ActionScheduler_Action $action Action. * * @return string */ @@ -67,7 +82,7 @@ protected function get_recurrence( $action ) { * @link https://github.com/wp-cli/entity-command/blob/6e0e77a297eefa3329b94bec16c15cf7528d343f/src/WP_CLI/CommandWithDBObject.php * @return void */ - protected function process_csv_arguments_to_arrays() : void { + protected function process_csv_arguments_to_arrays() { foreach ( $this->assoc_args as $k => $v ) { if ( false !== strpos( $k, '__' ) ) { $this->assoc_args[ $k ] = explode( ',', $v ); From 3e3246f7604f1b1120e8b23188cc43631530cfa6 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 21 Jun 2024 14:08:50 -0400 Subject: [PATCH 03/52] add logs command --- classes/WP_CLI/Action/Get.php | 28 +++++++++++++------ .../ActionScheduler_WPCLI_Action_Command.php | 20 +++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/classes/WP_CLI/Action/Get.php b/classes/WP_CLI/Action/Get.php index 1c0560b46..0c1bc57e9 100644 --- a/classes/WP_CLI/Action/Get.php +++ b/classes/WP_CLI/Action/Get.php @@ -16,6 +16,25 @@ public function execute() { $logger = \ActionScheduler::logger(); $action = $store->fetch_action( $action_id ); + $only_logs = ! empty( $this->assoc_args['field'] ) && 'log_entries' === $this->assoc_args['field']; + $log_entries = array(); + + foreach ( $logger->get_logs( $action_id ) as $log_entry ) { + if ( $only_logs ) { + WP_CLI::line( sprintf( '%s %s', $log_entry->get_date()->format( static::DATE_FORMAT ), $log_entry->get_message() ) ); + continue; + } + + $log_entries[] = array( + 'date' => $log_entry->get_date()->format( static::DATE_FORMAT ), + 'message' => $log_entry->get_message(), + ); + } + + if ( $only_logs ) { + return; + } + $action_arr = array( 'id' => $this->args[0], 'hook' => $action->get_hook(), @@ -24,16 +43,9 @@ public function execute() { 'group' => $action->get_group(), 'recurring' => $action->get_schedule()->is_recurring() ? 'yes' : 'no', 'scheduled_date' => $this->get_schedule_display_string( $action->get_schedule() ), - 'log_entries' => array(), + 'log_entries' => $log_entries, ); - foreach ( $logger->get_logs( $action_id ) as $log_entry ) { - $action_arr['log_entries'][] = array( - 'date' => $log_entry->get_date()->format( static::DATE_FORMAT ), - 'message' => $log_entry->get_message(), - ); - } - $fields = array_keys( $action_arr ); if ( ! empty( $this->assoc_args['fields'] ) ) { diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php index de4c74be7..b0dfc71b8 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php @@ -264,6 +264,26 @@ public function subcommand_list( array $args, array $assoc_args ) { $command->execute(); } + /** + * Get logs for a scheduled action. + * + * ## OPTIONS + * + * + * : The ID of the action to get. + * --- + * default: 0 + * --- + * + * @param array $args Positional arguments. + * @param array $assoc_args Keyed arguments. + * @return void + */ + public function logs( array $args ) { + $command = sprintf( 'action-scheduler action get %d --field=log_entries', $args[0] ); + WP_CLI::runcommand( $command ); + } + /** * Get the next scheduled action. * From b947a6eee6449d03dd510c752e906a669e7471e0 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 3 Jul 2024 19:24:34 -0400 Subject: [PATCH 04/52] add unique and priority parameters --- classes/WP_CLI/Action/Create.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index d8bedab71..ed045ac22 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -27,6 +27,8 @@ public function execute() { $group = get_flag_value( $this->assoc_args, 'group', '' ); $interval = absint( get_flag_value( $this->assoc_args, 'interval', 0 ) ); $cron = get_flag_value( $this->assoc_args, 'cron', '' ); + $unique = get_flag_value( $this->assoc_args, 'unique', false ); + $priority = absint( get_flag_value( $this->assoc_args, 'priority', 10 ) ); if ( ! empty( $callback_args ) ) { $callback_args = json_decode( $callback_args, true ); @@ -39,6 +41,8 @@ public function execute() { 'hook' => $hook, 'callback_args' => $callback_args, 'group' => $group, + 'unique' => $unique, + 'priority' => $priority, ); // Generate schedule start if appropriate. @@ -58,7 +62,7 @@ public function execute() { $function_args = array_filter( $function_args, static function( $key ) { - return in_array( $key, array( 'hook', 'callback_args', 'group' ), true ); + return in_array( $key, array( 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); }, ARRAY_FILTER_USE_KEY ); From d824cbc4d1b52ca60831d62958aa990e4b01f6ac Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 3 Jul 2024 19:25:02 -0400 Subject: [PATCH 05/52] use accurate variable names and language --- classes/WP_CLI/Action/Create.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index ed045ac22..62ce03f3c 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -79,11 +79,16 @@ static function( $key ) { $function_args = array_values( array_filter( $function_args ) ); try { - $actions_added = call_user_func_array( $function, $function_args ); + $action_id = call_user_func_array( $function, $function_args ); } catch ( \Exception $e ) { $this->print_error( $e ); } + if ( 0 === $action_id ) { + $e = new \Exception( __( 'Unable to create a scheduled action.', 'action-scheduler' ) ); + $this->print_error( $e ); + } + $num_actions_added = count( (array) $actions_added ); $this->print_success( $num_actions_added, $action_type ); @@ -100,10 +105,10 @@ static function( $key ) { protected function print_success( $actions_added, $action_type ) { \WP_CLI::success( sprintf( - /* translators: %1$d: refers to the total number of tasks added, %2$s: type of action */ - _n( '%1$d %2$s action scheduled.', '%1$d %2$s actions scheduled.', $actions_added, 'action-scheduler' ), - number_format_i18n( $actions_added ), - $action_type + /* translators: %1$s: type of action, %2$d: ID of the created action */ + __( '%1$s action (%2$d) scheduled.', 'action-scheduler' ), + ucfirst( $action_type ), + $action_id ) ); } From d3e910689be376eb0a342769f2c6970695949ffe Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 3 Jul 2024 19:52:55 -0400 Subject: [PATCH 06/52] improve fail detection and improve unscheduling of multiple actions --- classes/WP_CLI/Action/Cancel.php | 73 +++++++++++++++---- .../ActionScheduler_WPCLI_Action_Command.php | 4 +- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel.php index 16962a0b6..f358affd1 100644 --- a/classes/WP_CLI/Action/Cancel.php +++ b/classes/WP_CLI/Action/Cancel.php @@ -17,44 +17,87 @@ class ActionScheduler_WPCLI_Action_Cancel_Command extends ActionScheduler_WPCLI_ * @return void */ public function execute() { - $hook = $this->args[0]; - $group = null; + $hook = ''; + $group = get_flag_value( $this->assoc_args, 'group', '' ); $callback_args = get_flag_value( $this->assoc_args, 'args', null ); - $all = get_flag_value( $this->assoc_args, 'all' ); + $all = get_flag_value( $this->assoc_args, 'all', false ); - if ( ! empty( $this->args[1] ) ) { - $group = $this->args[1]; + if ( ! empty( $this->args[0] ) ) { + $hook = $this->args[0]; } if ( ! empty( $callback_args ) ) { $callback_args = json_decode( $callback_args, true ); } - $function_name = 'as_unschedule_action'; - $multiple = false; - if ( $all ) { - $function_name = 'as_unschedule_all_actions'; - $multiple = true; + $this->cancel_all( $hook, $callback_args, $group ); + return; + } + + $this->cancel_single( $hook, $callback_args, $group ); + } + + /** + * Cancel single action. + * + * @param string $hook + * @param array $callback_args + * @param string $group + * @return void + */ + protected function cancel_single( $hook, $callback_args, $group ) { + if ( empty( $hook ) ) { + \WP_CLI::error( __( 'Please specify hook of action to cancel.', 'action-scheduler' ) ); + } + + try { + $result = call_user_func( 'as_unschedule_action', $hook, $callback_args, $group ); + } catch ( \Exception $e ) { + $this->print_error( $e, false ); + } + + if ( null === $result ) { + $e = new \Exception( __( 'Unable to cancel scheduled action: check the logs.', 'action-scheduler' ) ); + $this->print_error( $e, false ); + } + + $this->print_success( false ); + } + + /** + * Cancel all actions. + * + * @param string $hook + * @param array $callback_args + * @param string $group + * @return void + */ + protected function cancel_all( $hook, $callback_args, $group ) { + if ( empty( $hook ) && empty( $group ) ) { + \WP_CLI::error( __( 'Please specify hook and/or group of actions to cancel.', 'action-scheduler' ) ); } try { - call_user_func( $function_name, $hook, $callback_args, $group ); + $result = call_user_func( 'as_unschedule_all_actions', $hook, $callback_args, $group ); } catch ( \Exception $e ) { $this->print_error( $e, $multiple ); } - $this->print_success( $multiple ); + /** + * Because as_unschedule_all_actions() does not provide a result, + * neither confirm or deny actions cancelled. + */ + \WP_CLI::success( __( 'Request to cancel scheduled actions completed.', 'action-scheduler' ) ); } /** * Print a success message. * - * @param bool $multiple Boolean if multiple actions. * @return void */ - protected function print_success( $multiple ) { - \WP_CLI::success( _n( 'Scheduled action cancelled.', 'All scheduled actions cancelled.', $multiple ? 2 : 1, 'action-scheduler' ) ); + protected function print_success() { + \WP_CLI::success( __( 'Scheduled action cancelled.', 'action-scheduler' ) ); } /** diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php index b0dfc71b8..05cfa9490 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php @@ -10,10 +10,10 @@ class ActionScheduler_WPCLI_Action_Command extends WP_CLI_Command { * * ## OPTIONS * - * + * [] * : Name of the action hook. * - * [] + * [--group=] * : The group the job is assigned to. * * [--args=] From bfbea086180bf75c78325168a7002bd0cc526312 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 3 Jul 2024 19:53:09 -0400 Subject: [PATCH 07/52] redundancy to ensure in wp cli environment --- classes/abstracts/ActionScheduler_WPCLI_Command.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/classes/abstracts/ActionScheduler_WPCLI_Command.php b/classes/abstracts/ActionScheduler_WPCLI_Command.php index 31ed3994c..c60a9a343 100644 --- a/classes/abstracts/ActionScheduler_WPCLI_Command.php +++ b/classes/abstracts/ActionScheduler_WPCLI_Command.php @@ -20,6 +20,11 @@ abstract class ActionScheduler_WPCLI_Command extends \WP_CLI_Command { * @param array $assoc_args Keyed arguments. */ public function __construct( array $args, array $assoc_args ) { + if ( ! defined( 'WP_CLI' ) || ! constant( 'WP_CLI' ) ) { + /* translators: %s php class name */ + throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); + } + $this->args = $args; $this->assoc_args = $assoc_args; } From 9da188748473c3c779f29776875917d345e4d434 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 5 Jul 2024 21:08:41 -0400 Subject: [PATCH 08/52] catch deletion failures --- classes/WP_CLI/Action/Delete.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/classes/WP_CLI/Action/Delete.php b/classes/WP_CLI/Action/Delete.php index d42cb4946..12f630e92 100644 --- a/classes/WP_CLI/Action/Delete.php +++ b/classes/WP_CLI/Action/Delete.php @@ -11,6 +11,7 @@ class ActionScheduler_WPCLI_Action_Delete_Command extends ActionScheduler_WPCLI_ /** @var array */ protected $action_counts = array( 'deleted' => 0, + 'failed' => 0, 'total' => 0, ); @@ -50,17 +51,28 @@ public function execute() { ); foreach ( $this->action_ids as $action_id ) { - $store->delete_action( $action_id ); + try { + $store->delete_action( $action_id ); + } catch ( \Exception $e ) { + $this->action_counts['failed']++; + \WP_CLI::warning( $e->getMessage() ); + } + $progress_bar->tick(); } $progress_bar->finish(); + /* translators: %1$d: number of actions deleted */ + $format = _n( 'Deleted %1$d action', 'Deleted %1$d actions', $this->action_counts['deleted'], 'action-scheduler' ) .', '; + /* translators: %2$d: number of actions deletions failed */ + $format .= _n( '%2$d failure.', '%2$d failures.', $this->action_counts['failed'], 'action-scheduler' ); + \WP_CLI::success( sprintf( - /* translators: %d: number of actions deleted */ - _n( 'Deleted %d action.', 'Deleted %d actions.', $this->action_counts['deleted'], 'action-scheduler' ), - number_format_i18n( $this->action_counts['deleted'] ) + $format, + number_format_i18n( $this->action_counts['deleted'] ), + number_format_i18n( $this->action_counts['failed'] ) ) ); } From 2e6aec6fecfb3aabe5ae8130af160b40001efb99 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 5 Jul 2024 21:18:55 -0400 Subject: [PATCH 09/52] catch action retrieval errors --- classes/WP_CLI/Action/Get.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Get.php b/classes/WP_CLI/Action/Get.php index 0c1bc57e9..263865c9f 100644 --- a/classes/WP_CLI/Action/Get.php +++ b/classes/WP_CLI/Action/Get.php @@ -16,6 +16,11 @@ public function execute() { $logger = \ActionScheduler::logger(); $action = $store->fetch_action( $action_id ); + if ( is_a( $action, ActionScheduler_NullAction::class ) ) { + /* translators: %d is action ID. */ + \WP_CLI::error( sprintf( esc_html__( 'Unable to retrieve action %d.', 'action-scheduler' ), $action_id ) ); + } + $only_logs = ! empty( $this->assoc_args['field'] ) && 'log_entries' === $this->assoc_args['field']; $log_entries = array(); @@ -35,10 +40,16 @@ public function execute() { return; } + try { + $status = $store->get_status( $action_id ); + } catch ( \Exception $e ) { + \WP_CLI::error( $e->getMessage() ); + } + $action_arr = array( 'id' => $this->args[0], 'hook' => $action->get_hook(), - 'status' => $store->get_status( $action_id ), + 'status' => $status, 'args' => $action->get_args(), 'group' => $action->get_group(), 'recurring' => $action->get_schedule()->is_recurring() ? 'yes' : 'no', From 9b6ac43674261891bd5c212ecbbdd35015474e8c Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 5 Jul 2024 21:34:04 -0400 Subject: [PATCH 10/52] catch exception when creating action --- classes/WP_CLI/Action/Create.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index 62ce03f3c..99a18aba3 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -45,10 +45,14 @@ public function execute() { 'priority' => $priority, ); - // Generate schedule start if appropriate. - if ( ! in_array( $schedule_start, static::ASYNC_OPTS, true ) ) { - $schedule_start = as_get_datetime_object( $schedule_start ); - $function_args['start'] = $schedule_start->format( 'U' ); + try { + // Generate schedule start if appropriate. + if ( ! in_array( $schedule_start, static::ASYNC_OPTS, true ) ) { + $schedule_start = as_get_datetime_object( $schedule_start ); + $function_args['start'] = $schedule_start->format( 'U' ); + } + } catch( \Exception $e ) { + \WP_CLI::error( $e->getMessage() ); } // Default to creating single action. From 22654eefa49ffba1dabd293c60b33ca2bc1c3d3d Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 5 Jul 2024 21:34:24 -0400 Subject: [PATCH 11/52] must allow empty function parameters --- classes/WP_CLI/Action/Create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index 99a18aba3..ac7bfb441 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -80,7 +80,7 @@ static function( $key ) { $function = 'as_schedule_cron_action'; } - $function_args = array_values( array_filter( $function_args ) ); + $function_args = array_values( $function_args ); try { $action_id = call_user_func_array( $function, $function_args ); From 1544e279493440becaa7550c05ec1e423cfe6f35 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 5 Jul 2024 21:34:36 -0400 Subject: [PATCH 12/52] cleanup creation success --- classes/WP_CLI/Action/Create.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index ac7bfb441..b7d373ebf 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -93,20 +93,18 @@ static function( $key ) { $this->print_error( $e ); } - $num_actions_added = count( (array) $actions_added ); - - $this->print_success( $num_actions_added, $action_type ); + $this->print_success( $action_id, $action_type ); } /** * Print a success message with the action ID. * - * @param int $actions_added Number actions added. - * @param string $action_type Type of action. + * @param int $action_id Created action ID. + * @param string $action_type Type of action. * * @return void */ - protected function print_success( $actions_added, $action_type ) { + protected function print_success( $action_id, $action_type ) { \WP_CLI::success( sprintf( /* translators: %1$s: type of action, %2$d: ID of the created action */ From 23dc21e818b234963f418f6b4514b8167791dab6 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 5 Jul 2024 21:36:58 -0400 Subject: [PATCH 13/52] permit empty function parameters --- classes/WP_CLI/Action/Generate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php index c4417d1e6..a0d97e5df 100644 --- a/classes/WP_CLI/Action/Generate.php +++ b/classes/WP_CLI/Action/Generate.php @@ -39,7 +39,7 @@ public function execute() { ); $action_type = 'single'; - $function_args = array_values( array_filter( $function_args ) ); + $function_args = array_values( $function_args ); try { $actions_added = $this->generate( ...$function_args ); From 645c858e5614e2cb3f2f5e63bea2ffdcadf73124 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 5 Jul 2024 21:37:07 -0400 Subject: [PATCH 14/52] cleanup --- classes/WP_CLI/Action/Generate.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php index a0d97e5df..2db8dd9df 100644 --- a/classes/WP_CLI/Action/Generate.php +++ b/classes/WP_CLI/Action/Generate.php @@ -38,7 +38,6 @@ public function execute() { 'group' => $group, ); - $action_type = 'single'; $function_args = array_values( $function_args ); try { @@ -49,7 +48,7 @@ public function execute() { $num_actions_added = count( (array) $actions_added ); - $this->print_success( $num_actions_added, $action_type ); + $this->print_success( $num_actions_added, 'single' ); } /** From 1565b5ea70dfdabca7c10273e1e0ec50decf1a92 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 8 Jul 2024 11:46:08 -0400 Subject: [PATCH 15/52] improve display of action log entries --- classes/WP_CLI/Action/Get.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/classes/WP_CLI/Action/Get.php b/classes/WP_CLI/Action/Get.php index 263865c9f..3485dc93a 100644 --- a/classes/WP_CLI/Action/Get.php +++ b/classes/WP_CLI/Action/Get.php @@ -25,11 +25,6 @@ public function execute() { $log_entries = array(); foreach ( $logger->get_logs( $action_id ) as $log_entry ) { - if ( $only_logs ) { - WP_CLI::line( sprintf( '%s %s', $log_entry->get_date()->format( static::DATE_FORMAT ), $log_entry->get_message() ) ); - continue; - } - $log_entries[] = array( 'date' => $log_entry->get_date()->format( static::DATE_FORMAT ), 'message' => $log_entry->get_message(), @@ -37,6 +32,13 @@ public function execute() { } if ( $only_logs ) { + $args = array( + 'format' => \WP_CLI\Utils\get_flag_value( $this->assoc_args, 'format', 'table' ), + ); + + $formatter = new \WP_CLI\Formatter( $args, array( 'date', 'message' ) ); + $formatter->display_items( $log_entries ); + return; } From b56f2555f3d747cac745eebeb84fe8b16ab466a9 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 17 Jul 2024 20:54:33 -0400 Subject: [PATCH 16/52] fix version sorting --- classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index 59e6c35d5..b402d1ffe 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -110,7 +110,7 @@ public function version( array $args, array $assoc_args ) { ); } - ksort( $rows, SORT_NUMERIC ); + uksort( $rows, 'version_compare' ); $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback' ) ); $formatter->display_items( $rows ); From 830738f96a2ad4b51e0f2bb268e43d7efdc55271 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 17 Jul 2024 21:31:58 -0400 Subject: [PATCH 17/52] fix 'claimed' parameter --- classes/WP_CLI/Action/List.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List.php index 5980f1755..796995233 100644 --- a/classes/WP_CLI/Action/List.php +++ b/classes/WP_CLI/Action/List.php @@ -55,6 +55,16 @@ static function ( $key ) { ARRAY_FILTER_USE_KEY ); + /** + * The `claimed` parameter expects a boolean or integer: + * check for string 'false', and set explicitly to `false` boolean. + */ + if ( array_key_exists( 'claimed', $query_args ) && 'false' === strtolower( $query_args['claimed'] ) ) { + $query_args['claimed'] = false; + } + + WP_CLI::debug( 'as_get_scheduled_actions( ' . var_export( $query_args, true ) . ' )' ); + if ( ! empty( $query_args['args'] ) ) { $query_args['args'] = json_decode( $query_args['args'], true ); } From 673c2b718c5aa273764c652816cf16fbdd50d897 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 17 Jul 2024 21:48:06 -0400 Subject: [PATCH 18/52] explicitly define return format and add to debug info --- classes/WP_CLI/Action/List.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List.php index 796995233..e877748e4 100644 --- a/classes/WP_CLI/Action/List.php +++ b/classes/WP_CLI/Action/List.php @@ -63,7 +63,19 @@ static function ( $key ) { $query_args['claimed'] = false; } - WP_CLI::debug( 'as_get_scheduled_actions( ' . var_export( $query_args, true ) . ' )' ); + $return_format = 'OBJECT'; + + if ( in_array( $formatter->format, array( 'ids', 'count' ) ) ) { + $return_format = '\'ids\''; + } + + WP_CLI::debug( + sprintf( + 'as_get_scheduled_actions( %s, %s )', + var_export( $query_args, true ), + $return_format + ) + ); if ( ! empty( $query_args['args'] ) ) { $query_args['args'] = json_decode( $query_args['args'], true ); @@ -82,7 +94,7 @@ static function ( $key ) { break; default: - $actions = as_get_scheduled_actions( $query_args ); + $actions = as_get_scheduled_actions( $query_args, OBJECT ); $actions_arr = array(); From 46eb6cb3dd1e8e5cd3eec6c660c311cc02ae3eaf Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 17 Jul 2024 22:00:26 -0400 Subject: [PATCH 19/52] improve debug info --- classes/WP_CLI/Action/List.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List.php index e877748e4..037faf079 100644 --- a/classes/WP_CLI/Action/List.php +++ b/classes/WP_CLI/Action/List.php @@ -69,10 +69,16 @@ static function ( $key ) { $return_format = '\'ids\''; } + $params = var_export( $query_args, true ); + + if ( empty( $query_args ) ) { + $params = 'array()'; + } + WP_CLI::debug( sprintf( 'as_get_scheduled_actions( %s, %s )', - var_export( $query_args, true ), + $params, $return_format ) ); From 633a800418add0db868aa8dab9f819c2a47b8ccc Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 17 Jul 2024 23:04:18 -0400 Subject: [PATCH 20/52] cleanup the next command why was i repeating myself? do this instead: wp action-scheduler action get $(wp action-scheduler action next hook_name) --- classes/WP_CLI/Action/Next.php | 96 +++---------------- .../ActionScheduler_WPCLI_Action_Command.php | 44 +++------ 2 files changed, 26 insertions(+), 114 deletions(-) diff --git a/classes/WP_CLI/Action/Next.php b/classes/WP_CLI/Action/Next.php index 3f27fa250..6bb800df1 100644 --- a/classes/WP_CLI/Action/Next.php +++ b/classes/WP_CLI/Action/Next.php @@ -16,83 +16,17 @@ public function execute() { $hook = $this->args[0]; $group = get_flag_value( $this->assoc_args, 'group', '' ); $callback_args = get_flag_value( $this->assoc_args, 'args', null ); + $raw = (bool) get_flag_value( $this->assoc_args, 'raw', false ); if ( ! empty( $callback_args ) ) { $callback_args = json_decode( $callback_args, true ); } - $next_action_id = $this->as_next_scheduled_action( $hook, $callback_args, $group ); - - if ( empty( $next_action_id ) ) { - \WP_CLI::warning( 'No matching next action.' ); + if ( $raw ) { + WP_CLI::line( as_next_scheduled_action( $hook, $callback_args, $group ) ); return; } - $fields = array( - 'id', - 'hook', - 'status', - 'group', - 'recurring', - 'scheduled_date', - ); - - $this->process_csv_arguments_to_arrays(); - - if ( ! empty( $this->assoc_args['fields'] ) ) { - $fields = $this->assoc_args['fields']; - } - - $store = \ActionScheduler::store(); - $logger = \ActionScheduler::logger(); - $formatter = new \WP_CLI\Formatter( $this->assoc_args, $fields ); - - if ( 'ids' === $formatter->format ) { - echo $next_action_id; - return; - } - - $action = $store->fetch_action( $next_action_id ); - - $action_arr = array( - 'id' => $next_action_id, - 'hook' => $action->get_hook(), - 'status' => $store->get_status( $next_action_id ), - 'args' => $action->get_args(), - 'group' => $action->get_group(), - 'recurring' => $action->get_schedule()->is_recurring() ? 'yes' : 'no', - 'scheduled_date' => $this->get_schedule_display_string( $action->get_schedule() ), - 'log_entries' => array(), - ); - - foreach ( $logger->get_logs( $next_action_id ) as $log_entry ) { - $action_arr['log_entries'][] = array( - 'date' => $log_entry->get_date()->format( static::DATE_FORMAT ), - 'message' => $log_entry->get_message(), - ); - } - - if ( ! empty( $this->assoc_args['fields'] ) ) { - $fields = explode( ',', $this->assoc_args['fields'] ); - } - - $formatter->display_item( $action_arr ); - } - - /** - * Get next scheduled action. - * - * @see as_next_scheduled_action() - * @param string $hook Name of the hook to search for. - * @param array $args Arguments of the action to be searched. - * @param string $group Group of the action to be searched. - * @return int - */ - protected function as_next_scheduled_action( $hook, $args = null, $group = '' ) { - if ( ! ActionScheduler::is_initialized( 'as_next_scheduled_action' ) ) { - return 0; - } - $params = array( 'hook' => $hook, 'orderby' => 'date', @@ -105,26 +39,24 @@ protected function as_next_scheduled_action( $hook, $args = null, $group = '' ) } $params['status'] = ActionScheduler_Store::STATUS_RUNNING; - $action_id = ActionScheduler::store()->query_action( $params ); + WP_CLI::debug( 'ActionScheduler()::store()->query_action( ' . var_export( $params, true ) . ' )' ); + + $action_id = ActionScheduler::store()->query_action( $params ); if ( $action_id ) { - return $action_id; + echo $action_id; + return; } $params['status'] = ActionScheduler_Store::STATUS_PENDING; - $action_id = ActionScheduler::store()->query_action( $params ); - if ( null === $action_id ) { - return 0; - } + WP_CLI::debug( 'ActionScheduler()::store()->query_action( ' . var_export( $params, true ) . ' )' ); - $action = ActionScheduler::store()->fetch_action( $action_id ); - $scheduled_date = $action->get_schedule()->get_date(); - if ( $scheduled_date ) { - return (int) $scheduled_date->format( 'U' ); - } elseif ( null === $scheduled_date ) { // pending async action with NullSchedule. - return $action_id; + $action_id = ActionScheduler::store()->query_action( $params ); + if ( $action_id ) { + echo $action_id; + return; } - return 0; + WP_CLI::warning( 'No matching next action.' ); } } diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php index 05cfa9490..720c045f5 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php @@ -285,47 +285,27 @@ public function logs( array $args ) { } /** - * Get the next scheduled action. + * Get the ID or timestamp of the next scheduled action. * * ## OPTIONS * - * + * * : The hook of the next scheduled action. * - * [--field=] - * : Prints the value of a single property for the action. - * - * [--fields=] - * : Limit the output to specific object properties. - * - * [--format=] - * : Render output in a particular format. + * [--args=] + * : JSON object of arguments to search for next scheduled action. * --- - * default: table - * options: - * - table - * - csv - * - ids - * - json - * - count - * - yaml + * default: [] * --- * - * ## AVAILABLE FIELDS - * - * These fields will be displayed by default for the action: - * - * * id - * * hook - * * status - * * group - * * recurring - * * scheduled_date - * - * These fields are optionally available: + * [--group=] + * : The group to which the next scheduled action is assigned. + * --- + * default: '' + * --- * - * * args - * * log_entries + * [--raw] + * : Display the raw output of as_next_scheduled_action() (timestamp or boolean). * * @param array $args Positional arguments. * @param array $assoc_args Keyed arguments. From a2c5313608e64467c9795ff2a16a5e3bebbbb7c8 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 29 Jul 2024 10:00:30 -0400 Subject: [PATCH 21/52] replace comma with semicolon --- classes/WP_CLI/Action/Run.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Run.php b/classes/WP_CLI/Action/Run.php index 7b248ef1d..de39ec572 100644 --- a/classes/WP_CLI/Action/Run.php +++ b/classes/WP_CLI/Action/Run.php @@ -78,7 +78,7 @@ public function execute() { * %1$d: count of actions evaluated. * %2$s: type of action evaluated. */ - $format = _n( '%1$d action %2$s.', '%1$d actions %2$s.', $count, 'action-scheduler' ), + $format = _n( '%1$d action %2$s.', '%1$d actions %2$s.', $count, 'action-scheduler' ); \WP_CLI::warning( sprintf( From bcce8366c50215a6fc86db4521311a2119da4c6f Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 29 Jul 2024 10:33:31 -0400 Subject: [PATCH 22/52] save --- classes/WP_CLI/Action/Create.php | 35 +++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index b7d373ebf..b3be742b7 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -7,7 +7,7 @@ */ class ActionScheduler_WPCLI_Action_Create_Command extends ActionScheduler_WPCLI_Command { - const ASYNC_OPTS = array( 'async', 'now', 0 ); + const ASYNC_OPTS = array( 'async', 0 ); /** * Execute command. @@ -35,7 +35,7 @@ public function execute() { } $function_args = array( - 'start' => 'async', + 'start' => $schedule_start, 'cron' => $cron, 'interval' => $interval, 'hook' => $hook, @@ -59,7 +59,29 @@ public function execute() { $action_type = 'single'; $function = 'as_schedule_single_action'; - if ( 'async' === $function_args['start'] ) { // Enqueue async action. + if ( ! empty( $interval ) ) { // Creating recurring action. + $action_type = 'recurring'; + $function = 'as_schedule_recurring_action'; + + $function_args = array_filter( + $function_args, + static function( $key ) { + return in_array( $key, array( 'start', 'interval', 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); + }, + ARRAY_FILTER_USE_KEY + ); + } else if ( ! empty( $cron ) ) { // Creating cron action. + $action_type = 'cron'; + $function = 'as_schedule_cron_action'; + + $function_args = array_filter( + $function_args, + static function( $key ) { + return in_array( $key, array( 'start', 'cron', 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); + }, + ARRAY_FILTER_USE_KEY + ); + } else if ( in_array( $function_args['start'], static::ASYNC_OPTS ) ) { // Enqueue async action. $action_type = 'async'; $function = 'as_enqueue_async_action'; @@ -71,13 +93,6 @@ static function( $key ) { ARRAY_FILTER_USE_KEY ); - } else if ( ! empty( $interval ) ) { // Creating recurring action. - $action_type = 'recurring'; - $function = 'as_schedule_recurring_action'; - - } else if ( ! empty( $cron ) ) { // Creating cron action. - $action_type = 'cron'; - $function = 'as_schedule_cron_action'; } $function_args = array_values( $function_args ); From 123f8f1126518304ea0c4f317672ace2a6ef7b31 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Tue, 30 Jul 2024 19:24:44 -0400 Subject: [PATCH 23/52] save --- classes/WP_CLI/Action/Create.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index b3be742b7..59a22c0d6 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -92,7 +92,14 @@ static function( $key ) { }, ARRAY_FILTER_USE_KEY ); - + } else { // Enqueue single action. + $function_args = array_filter( + $function_args, + static function( $key ) { + return in_array( $key, array( 'start', 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); + }, + ARRAY_FILTER_USE_KEY + ); } $function_args = array_values( $function_args ); From 815bf156e2df3f0919e4208b4a4cf076f5b53ddd Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Tue, 30 Jul 2024 19:51:08 -0400 Subject: [PATCH 24/52] add help text --- classes/ActionScheduler_AdminView.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/ActionScheduler_AdminView.php b/classes/ActionScheduler_AdminView.php index ed30950a7..e3e8a992a 100644 --- a/classes/ActionScheduler_AdminView.php +++ b/classes/ActionScheduler_AdminView.php @@ -228,6 +228,10 @@ public function add_help_tabs() { '

' . sprintf( __( 'About Action Scheduler %s', 'action-scheduler' ), $as_version ) . '

' . '

' . __( 'Action Scheduler is a scalable, traceable job queue for background processing large sets of actions. Action Scheduler works by triggering an action hook to run at some time in the future. Scheduled actions can also be scheduled to run on a recurring schedule.', 'action-scheduler' ) . + '

' . + '

' . __( 'WP CLI', 'action-scheduler' ) . '

' . + '

' . + __( 'WP CLI commands are available: execute wp help action-scheduler for a list of available commands.' ) . '

', ) ); From 262694804aa6f1a836f1575d89d8163fd90c97cc Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 19:48:58 -0400 Subject: [PATCH 25/52] improve translatability --- classes/ActionScheduler_AdminView.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/ActionScheduler_AdminView.php b/classes/ActionScheduler_AdminView.php index e3e8a992a..9dbdf62e4 100644 --- a/classes/ActionScheduler_AdminView.php +++ b/classes/ActionScheduler_AdminView.php @@ -231,7 +231,11 @@ public function add_help_tabs() { '

' . '

' . __( 'WP CLI', 'action-scheduler' ) . '

' . '

' . - __( 'WP CLI commands are available: execute wp help action-scheduler for a list of available commands.' ) . + sprintf( + /* translators: %1$s is WP CLI command (not translatable) */ + __( 'WP CLI commands are available: execute %1$s for a list of available commands.', 'action-scheduler' ), + 'wp help action-scheduler', + ) . '

', ) ); From 44bb388df1578d300b54caab816b79429773e227 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 19:49:47 -0400 Subject: [PATCH 26/52] remove @uses phpdoc statements --- classes/WP_CLI/Action/Cancel.php | 4 ---- classes/WP_CLI/Action/Create.php | 6 ------ classes/WP_CLI/Action/Delete.php | 4 ---- classes/WP_CLI/Action/Generate.php | 4 ---- classes/WP_CLI/Action/Run.php | 7 ------- .../WP_CLI/ActionScheduler_WPCLI_System_Command.php | 10 ---------- 6 files changed, 35 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel.php index f358affd1..8497e6e17 100644 --- a/classes/WP_CLI/Action/Cancel.php +++ b/classes/WP_CLI/Action/Cancel.php @@ -10,10 +10,6 @@ class ActionScheduler_WPCLI_Action_Cancel_Command extends ActionScheduler_WPCLI_ /** * Execute command. * - * @uses as_unschedule_action() - * @uses as_unschedule_all_actions() - * @uses $this->print_error() - * @uses $this->print_success() * @return void */ public function execute() { diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index 59a22c0d6..71df0165e 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -12,12 +12,6 @@ class ActionScheduler_WPCLI_Action_Create_Command extends ActionScheduler_WPCLI_ /** * Execute command. * - * @uses as_schedule_single_action() - * @uses as_enqueue_async_action() - * @uses as_schedule_recurring_action() - * @uses as_schedule_cron_action() - * @uses $this->print_error() - * @uses $this->print_success() * @return void */ public function execute() { diff --git a/classes/WP_CLI/Action/Delete.php b/classes/WP_CLI/Action/Delete.php index 12f630e92..252fd95e1 100644 --- a/classes/WP_CLI/Action/Delete.php +++ b/classes/WP_CLI/Action/Delete.php @@ -33,9 +33,6 @@ public function __construct( array $args, array $assoc_args ) { /** * Execute. * - * @uses \ActionScheduler_Store::delete_action() - * @uses \WP_CLI::warning() - * @uses \WP_CLI::success() * @return void */ public function execute() { @@ -81,7 +78,6 @@ public function execute() { * Action: action_scheduler_deleted_action * * @param int $action_id Action ID. - * @uses \WP_CLI::debug() * @return void */ public function action__deleted( $action_id ) { diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php index 2db8dd9df..1858ed9dc 100644 --- a/classes/WP_CLI/Action/Generate.php +++ b/classes/WP_CLI/Action/Generate.php @@ -10,9 +10,6 @@ class ActionScheduler_WPCLI_Action_Generate_Command extends ActionScheduler_WPCL /** * Execute command. * - * @uses $this->generate() - * @uses $this->print_error() - * @uses $this->print_success() * @return void */ public function execute() { @@ -60,7 +57,6 @@ public function execute() { * @param string $hook The hook to trigger. * @param array $args Arguments to pass when the hook triggers. * @param string $group The group to assign this job to. - * @uses as_schedule_single_action() * @return int[] IDs of actions added. */ protected function generate( $schedule_start, $interval, $count, $hook, array $args = array(), $group = '' ) { diff --git a/classes/WP_CLI/Action/Run.php b/classes/WP_CLI/Action/Run.php index de39ec572..a9a7fa19c 100644 --- a/classes/WP_CLI/Action/Run.php +++ b/classes/WP_CLI/Action/Run.php @@ -38,9 +38,6 @@ public function __construct( array $args, array $assoc_args ) { /** * Execute. * - * @uses \ActionScheduler_Abstract_QueueRunner::process_action() - * @uses \WP_CLI::warning() - * @uses \WP_CLI::success() * @return void */ public function execute() { @@ -102,7 +99,6 @@ public function execute() { * Action: action_scheduler_execution_ignored * * @param int $action_id Action ID. - * @uses \WP_CLI::debug() * @return void */ public function action__ignored( $action_id ) { @@ -124,7 +120,6 @@ public function action__ignored( $action_id ) { * Action: action_scheduler_after_execute * * @param int $action_id Action ID. - * @uses \WP_CLI::success() * @return void */ public function action__executed( $action_id ) { @@ -147,7 +142,6 @@ public function action__executed( $action_id ) { * * @param int $action_id Action ID. * @param \Exception $e Exception. - * @uses \WP_CLI::debug() * @return void */ public function action__failed( $action_id, \Exception $e ) { @@ -170,7 +164,6 @@ public function action__failed( $action_id, \Exception $e ) { * * @param int $action_id Action ID. * @param \Exception $e Exception. - * @uses \WP_CLI::debug() * @return void */ public function action__invalid( $action_id, \Exception $e ) { diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index b402d1ffe..ae72d2cc2 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -19,7 +19,6 @@ public function __construct() { * * @param array $args Positional args. * @param array $assoc_args Keyed args. - * @uses $this->get_current_datastore() * @return void * * @subcommand data-store @@ -33,7 +32,6 @@ public function datastore( array $args, array $assoc_args ) { * * @param array $args Positional args. * @param array $assoc_args Keyed args. - * @uses $this->get_current_runner() * @return void */ public function runner( array $args, array $assoc_args ) { @@ -45,9 +43,6 @@ public function runner( array $args, array $assoc_args ) { * * @param array $args Positional args. * @param array $assoc_args Keyed args. - * @uses $this->get_current_datastore() - * @uses $this->get_latest_version() - * @uses $this->print_statuses() * @return void */ public function status( array $args, array $assoc_args ) { @@ -89,9 +84,6 @@ public function status( array $args, array $assoc_args ) { * * @param array $args Positional args. * @param array $assoc_args Keyed args. - * @uses \ActionScheduler_Versions::get_versions() - * @uses \WP_CLI\Formatter::display_items() - * @uses $this->get_latest_version() * @return void */ public function version( array $args, array $assoc_args ) { @@ -134,7 +126,6 @@ protected function get_current_datastore() { * Get latest version. * * @param null|\ActionScheduler_Versions $instance Versions. - * @uses \ActionScheduler_Versions::latest_version() * @return string */ protected function get_latest_version( $instance = null ) { @@ -148,7 +139,6 @@ protected function get_latest_version( $instance = null ) { /** * Get current runner. * - * @uses \ActionScheduler::runner() * @return string */ protected function get_current_runner() { From 29872c7da49889fed17cc87f4ab902655501bbc2 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 19:57:17 -0400 Subject: [PATCH 27/52] remove parameter restriction --- classes/WP_CLI/Action/List.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List.php index 037faf079..8a23dccec 100644 --- a/classes/WP_CLI/Action/List.php +++ b/classes/WP_CLI/Action/List.php @@ -45,15 +45,8 @@ public function execute() { $fields = $this->assoc_args['fields']; } - $formatter = new \WP_CLI\Formatter( $this->assoc_args, $fields ); - - $query_args = array_filter( - $this->assoc_args, - static function ( $key ) { - return in_array( $key, static::PARAMETERS, true ); - }, - ARRAY_FILTER_USE_KEY - ); + $formatter = new \WP_CLI\Formatter( $this->assoc_args, $fields ); + $query_args = $this->assoc_args; /** * The `claimed` parameter expects a boolean or integer: From 3c2e1cee7fcd85f10b44e9e41cd8015dd6d9493f Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 19:59:15 -0400 Subject: [PATCH 28/52] declare property --- classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index ae72d2cc2..8cd59602e 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -7,6 +7,14 @@ */ class ActionScheduler_WPCLI_System_Command { + /** + * Data store for querying actions + * + * @var ActionScheduler_Store + * @access protected + */ + protected $store; + /** * Construct. */ From 8a4cd8bf29097f345aa0682c10e50639d54cdd87 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:00:40 -0400 Subject: [PATCH 29/52] improve command description --- classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index 8cd59602e..8182c90c9 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -83,12 +83,12 @@ public function status( array $args, array $assoc_args ) { } /** - * Get latest or all system versions. + * Display the active version, or all registered versions. * * ## OPTIONS * * [--all] - * : Get all system versions. + * : List all registered versions. * * @param array $args Positional args. * @param array $assoc_args Keyed args. From e2cde16718979e61a61ed43eb65d43e04e70d5df Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:06:26 -0400 Subject: [PATCH 30/52] add active column to versions table --- .../WP_CLI/ActionScheduler_WPCLI_System_Command.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index 8182c90c9..892d34981 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -97,6 +97,7 @@ public function status( array $args, array $assoc_args ) { public function version( array $args, array $assoc_args ) { $all = (bool) get_flag_value( $assoc_args, 'all' ); $instance = \ActionScheduler_Versions::instance(); + $latest = $this->get_latest_version( $instance ); if ( $all ) { $versions = $instance->get_versions(); @@ -104,21 +105,28 @@ public function version( array $args, array $assoc_args ) { $rows = array(); foreach ( $versions as $version => $callback ) { + $active = 'no'; + + if ( $version === $latest ) { + $active = 'yes'; + } + $rows[ $version ] = array( 'version' => $version, 'callback' => $callback, + 'active' => $active, ); } uksort( $rows, 'version_compare' ); - $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback' ) ); + $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) ); $formatter->display_items( $rows ); return; } - echo $this->get_latest_version( $instance ); + echo $latest; } /** From ee3d64e0c09da5b318983292950709d90413b9e8 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:08:39 -0400 Subject: [PATCH 31/52] call function directly --- classes/WP_CLI/Action/Cancel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel.php index 8497e6e17..bf69916b6 100644 --- a/classes/WP_CLI/Action/Cancel.php +++ b/classes/WP_CLI/Action/Cancel.php @@ -48,7 +48,7 @@ protected function cancel_single( $hook, $callback_args, $group ) { } try { - $result = call_user_func( 'as_unschedule_action', $hook, $callback_args, $group ); + $result = as_unschedule_action( $hook, $callback_args, $group ); } catch ( \Exception $e ) { $this->print_error( $e, false ); } From 45b4038a0a322f0fc7cd191ff52ef77a181cdedf Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:10:21 -0400 Subject: [PATCH 32/52] improve parameter description --- classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php index 720c045f5..5ffc09f94 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php @@ -183,7 +183,7 @@ public function generate( array $args, array $assoc_args ) { * : Instead of returning the whole action, returns the value of a single field. * * [--fields=] - * : Limit the output to specific fields. Defaults to all fields. + * : Limit the output to specific fields (comma-separated). Defaults to all fields. * * [--format=] * : Render output in a particular format. From 9fab4dc33f075efafcf7070c9a8ced0dfb247e1b Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:16:10 -0400 Subject: [PATCH 33/52] add check for fields parameter set to log_entries --- classes/WP_CLI/Action/Get.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/WP_CLI/Action/Get.php b/classes/WP_CLI/Action/Get.php index 3485dc93a..53fbc95da 100644 --- a/classes/WP_CLI/Action/Get.php +++ b/classes/WP_CLI/Action/Get.php @@ -22,6 +22,7 @@ public function execute() { } $only_logs = ! empty( $this->assoc_args['field'] ) && 'log_entries' === $this->assoc_args['field']; + $only_logs = $only_logs || ( ! empty( $this->assoc_args['fields'] && 'log_entries' === $this->assoc_args['fields'] ) ); $log_entries = array(); foreach ( $logger->get_logs( $action_id ) as $log_entry ) { From f3fad9ab474deda4b3bd65b04daf214ff1bc9bf6 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:21:01 -0400 Subject: [PATCH 34/52] fix variable name --- classes/WP_CLI/Action/Next.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/WP_CLI/Action/Next.php b/classes/WP_CLI/Action/Next.php index 6bb800df1..a5316b381 100644 --- a/classes/WP_CLI/Action/Next.php +++ b/classes/WP_CLI/Action/Next.php @@ -34,8 +34,8 @@ public function execute() { 'group' => $group, ); - if ( is_array( $args ) ) { - $params['args'] = $args; + if ( is_array( $callback_args ) ) { + $params['args'] = $callback_args; } $params['status'] = ActionScheduler_Store::STATUS_RUNNING; From e7659a1c25b5b0aa6f79cf3ac86b234442ded01e Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:22:13 -0400 Subject: [PATCH 35/52] change method to retrieve class name --- classes/abstracts/ActionScheduler_WPCLI_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/abstracts/ActionScheduler_WPCLI_Command.php b/classes/abstracts/ActionScheduler_WPCLI_Command.php index c60a9a343..908f93ed7 100644 --- a/classes/abstracts/ActionScheduler_WPCLI_Command.php +++ b/classes/abstracts/ActionScheduler_WPCLI_Command.php @@ -22,7 +22,7 @@ abstract class ActionScheduler_WPCLI_Command extends \WP_CLI_Command { public function __construct( array $args, array $assoc_args ) { if ( ! defined( 'WP_CLI' ) || ! constant( 'WP_CLI' ) ) { /* translators: %s php class name */ - throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); + throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), get_class( $this ) ) ); } $this->args = $args; From eaa3bfc3193c2ef5524d45c719230deb655fe6e3 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:30:01 -0400 Subject: [PATCH 36/52] remove unused function --- .../ActionScheduler_WPCLI_Command.php | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/classes/abstracts/ActionScheduler_WPCLI_Command.php b/classes/abstracts/ActionScheduler_WPCLI_Command.php index 908f93ed7..d5200df2f 100644 --- a/classes/abstracts/ActionScheduler_WPCLI_Command.php +++ b/classes/abstracts/ActionScheduler_WPCLI_Command.php @@ -56,30 +56,6 @@ protected function get_schedule_display_string( ActionScheduler_Schedule $schedu return $schedule_display_string; } - /** - * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. - * - * @see \ActionScheduler_ListTable::get_recurrence() - * @param ActionScheduler_Action $action Action. - * - * @return string - */ - protected function get_recurrence( $action ) { - $schedule = $action->get_schedule(); - if ( $schedule->is_recurring() ) { - $recurrence = $schedule->get_recurrence(); - - if ( is_numeric( $recurrence ) ) { - /* translators: %s: time interval */ - return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); - } else { - return $recurrence; - } - } - - return __( 'Non-repeating', 'action-scheduler' ); - } - /** * Transforms arguments with '__' from CSV into expected arrays. * From 04c4527e66aa69ebf937a5eae9551d241cf85a8e Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:33:00 -0400 Subject: [PATCH 37/52] update link --- classes/abstracts/ActionScheduler_WPCLI_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/abstracts/ActionScheduler_WPCLI_Command.php b/classes/abstracts/ActionScheduler_WPCLI_Command.php index d5200df2f..e8125f023 100644 --- a/classes/abstracts/ActionScheduler_WPCLI_Command.php +++ b/classes/abstracts/ActionScheduler_WPCLI_Command.php @@ -60,7 +60,7 @@ protected function get_schedule_display_string( ActionScheduler_Schedule $schedu * Transforms arguments with '__' from CSV into expected arrays. * * @see \WP_CLI\CommandWithDBObject::process_csv_arguments_to_arrays() - * @link https://github.com/wp-cli/entity-command/blob/6e0e77a297eefa3329b94bec16c15cf7528d343f/src/WP_CLI/CommandWithDBObject.php + * @link https://github.com/wp-cli/entity-command/blob/c270cc9a2367cb8f5845f26a6b5e203397c91392/src/WP_CLI/CommandWithDBObject.php#L99 * @return void */ protected function process_csv_arguments_to_arrays() { From 3f136e2ba0fd846849ddeb87d0589d3cfdb6f3a0 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 20:59:41 -0400 Subject: [PATCH 38/52] eliminate smelly code --- classes/WP_CLI/Action/Cancel.php | 14 +++++++------- classes/WP_CLI/Action/Create.php | 10 ++++------ classes/WP_CLI/Action/Delete.php | 14 +++++++++++--- classes/WP_CLI/Action/Generate.php | 2 +- classes/WP_CLI/Action/List.php | 5 ++++- classes/WP_CLI/Action/Next.php | 4 ++++ classes/WP_CLI/Action/Run.php | 12 ++++++++++-- .../ActionScheduler_WPCLI_Action_Command.php | 3 +-- .../ActionScheduler_WPCLI_System_Command.php | 3 ++- 9 files changed, 44 insertions(+), 23 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel.php index bf69916b6..af37fb02e 100644 --- a/classes/WP_CLI/Action/Cancel.php +++ b/classes/WP_CLI/Action/Cancel.php @@ -37,9 +37,9 @@ public function execute() { /** * Cancel single action. * - * @param string $hook - * @param array $callback_args - * @param string $group + * @param string $hook The hook that the job will trigger. + * @param array $callback_args Args that would have been passed to the job. + * @param string $group The group the job is assigned to. * @return void */ protected function cancel_single( $hook, $callback_args, $group ) { @@ -64,9 +64,9 @@ protected function cancel_single( $hook, $callback_args, $group ) { /** * Cancel all actions. * - * @param string $hook - * @param array $callback_args - * @param string $group + * @param string $hook The hook that the job will trigger. + * @param array $callback_args Args that would have been passed to the job. + * @param string $group The group the job is assigned to. * @return void */ protected function cancel_all( $hook, $callback_args, $group ) { @@ -101,7 +101,7 @@ protected function print_success() { * * @param \Exception $e The error object. * @param bool $multiple Boolean if multiple actions. - * @throws \WP_CLI\ExitException + * @throws \WP_CLI\ExitException When an error occurs. * @return void */ protected function print_error( \Exception $e, $multiple ) { diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create.php index 71df0165e..78ace2b9c 100644 --- a/classes/WP_CLI/Action/Create.php +++ b/classes/WP_CLI/Action/Create.php @@ -45,7 +45,7 @@ public function execute() { $schedule_start = as_get_datetime_object( $schedule_start ); $function_args['start'] = $schedule_start->format( 'U' ); } - } catch( \Exception $e ) { + } catch ( \Exception $e ) { \WP_CLI::error( $e->getMessage() ); } @@ -64,7 +64,7 @@ static function( $key ) { }, ARRAY_FILTER_USE_KEY ); - } else if ( ! empty( $cron ) ) { // Creating cron action. + } elseif ( ! empty( $cron ) ) { // Creating cron action. $action_type = 'cron'; $function = 'as_schedule_cron_action'; @@ -75,7 +75,7 @@ static function( $key ) { }, ARRAY_FILTER_USE_KEY ); - } else if ( in_array( $function_args['start'], static::ASYNC_OPTS ) ) { // Enqueue async action. + } elseif ( in_array( $function_args['start'], static::ASYNC_OPTS, true ) ) { // Enqueue async action. $action_type = 'async'; $function = 'as_enqueue_async_action'; @@ -135,9 +135,7 @@ protected function print_success( $action_id, $action_type ) { * Convert an exception into a WP CLI error. * * @param \Exception $e The error object. - * - * @throws \WP_CLI\ExitException - * + * @throws \WP_CLI\ExitException When an error occurs. * @return void */ protected function print_error( \Exception $e ) { diff --git a/classes/WP_CLI/Action/Delete.php b/classes/WP_CLI/Action/Delete.php index 252fd95e1..3bfdd4dda 100644 --- a/classes/WP_CLI/Action/Delete.php +++ b/classes/WP_CLI/Action/Delete.php @@ -5,10 +5,18 @@ */ class ActionScheduler_WPCLI_Action_Delete_Command extends ActionScheduler_WPCLI_Command { - /** @var int[] */ + /** + * Array of action IDs to delete. + * + * @var int[] + */ protected $action_ids = array(); - /** @var array */ + /** + * Number of deleted, failed, and total actions deleted. + * + * @var array + */ protected $action_counts = array( 'deleted' => 0, 'failed' => 0, @@ -61,7 +69,7 @@ public function execute() { $progress_bar->finish(); /* translators: %1$d: number of actions deleted */ - $format = _n( 'Deleted %1$d action', 'Deleted %1$d actions', $this->action_counts['deleted'], 'action-scheduler' ) .', '; + $format = _n( 'Deleted %1$d action', 'Deleted %1$d actions', $this->action_counts['deleted'], 'action-scheduler' ) . ', '; /* translators: %2$d: number of actions deletions failed */ $format .= _n( '%2$d failure.', '%2$d failures.', $this->action_counts['failed'], 'action-scheduler' ); diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php index 1858ed9dc..b00ba1504 100644 --- a/classes/WP_CLI/Action/Generate.php +++ b/classes/WP_CLI/Action/Generate.php @@ -103,7 +103,7 @@ protected function print_success( $actions_added, $action_type ) { * Convert an exception into a WP CLI error. * * @param \Exception $e The error object. - * @throws \WP_CLI\ExitException + * @throws \WP_CLI\ExitException When an error occurs. * @return void */ protected function print_error( \Exception $e ) { diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List.php index 8a23dccec..40e44f7cd 100644 --- a/classes/WP_CLI/Action/List.php +++ b/classes/WP_CLI/Action/List.php @@ -1,5 +1,7 @@ format, array( 'ids', 'count' ) ) ) { + if ( in_array( $formatter->format, array( 'ids', 'count' ), true ) ) { $return_format = '\'ids\''; } + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export $params = var_export( $query_args, true ); if ( empty( $query_args ) ) { diff --git a/classes/WP_CLI/Action/Next.php b/classes/WP_CLI/Action/Next.php index a5316b381..9fa750c23 100644 --- a/classes/WP_CLI/Action/Next.php +++ b/classes/WP_CLI/Action/Next.php @@ -1,5 +1,7 @@ query_action( ' . var_export( $params, true ) . ' )' ); $action_id = ActionScheduler::store()->query_action( $params ); @@ -48,6 +51,7 @@ public function execute() { } $params['status'] = ActionScheduler_Store::STATUS_PENDING; + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export WP_CLI::debug( 'ActionScheduler()::store()->query_action( ' . var_export( $params, true ) . ' )' ); $action_id = ActionScheduler::store()->query_action( $params ); diff --git a/classes/WP_CLI/Action/Run.php b/classes/WP_CLI/Action/Run.php index a9a7fa19c..7619ac687 100644 --- a/classes/WP_CLI/Action/Run.php +++ b/classes/WP_CLI/Action/Run.php @@ -5,10 +5,18 @@ */ class ActionScheduler_WPCLI_Action_Run_Command extends ActionScheduler_WPCLI_Command { - /** @var int[] */ + /** + * Array of action IDs to execute. + * + * @var int[] + */ protected $action_ids = array(); - /** @var array */ + /** + * Number of executed, failed, ignored, invalid, and total actions. + * + * @var array + */ protected $action_counts = array( 'executed' => 0, 'failed' => 0, diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php index 5ffc09f94..26305eac0 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php @@ -275,8 +275,7 @@ public function subcommand_list( array $args, array $assoc_args ) { * default: 0 * --- * - * @param array $args Positional arguments. - * @param array $assoc_args Keyed arguments. + * @param array $args Positional arguments. * @return void */ public function logs( array $args ) { diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index 892d34981..834c06b9e 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -1,5 +1,7 @@ Date: Wed, 31 Jul 2024 21:08:16 -0400 Subject: [PATCH 39/52] remove trailing comma --- classes/ActionScheduler_AdminView.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/ActionScheduler_AdminView.php b/classes/ActionScheduler_AdminView.php index 9dbdf62e4..1bc271d90 100644 --- a/classes/ActionScheduler_AdminView.php +++ b/classes/ActionScheduler_AdminView.php @@ -234,7 +234,7 @@ public function add_help_tabs() { sprintf( /* translators: %1$s is WP CLI command (not translatable) */ __( 'WP CLI commands are available: execute %1$s for a list of available commands.', 'action-scheduler' ), - 'wp help action-scheduler', + 'wp help action-scheduler' ) . '

', ) From d88bc18a40618f255bd3f7af2a13adc4cb5b592c Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 22:43:23 -0400 Subject: [PATCH 40/52] add list of commands to documentation --- docs/wp-cli.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/wp-cli.md b/docs/wp-cli.md index 2f8c1b580..63287d7ae 100644 --- a/docs/wp-cli.md +++ b/docs/wp-cli.md @@ -41,6 +41,19 @@ These are the commands available to use with Action Scheduler: * `--group` - Process only actions in a specific group, like `'woocommerce-memberships'`. By default, actions in any group (or no group) will be processed. * `--exclude-groups` - Ignore actions from the specified group or groups (to specify multiple groups, supply a comma-separated list of slugs). This option is ignored if `--group` is also specified. * `--force` - By default, Action Scheduler limits the number of concurrent batches that can be run at once to ensure the server does not get overwhelmed. Using the `--force` flag overrides this behavior to force the WP CLI queue to run. + +* `action-scheduler action cancel` +* `action-scheduler action create` +* `action-scheduler action delete` +* `action-scheduler action generate` +* `action-scheduler action get` +* `action-scheduler action list` +* `action-scheduler action next` +* `action-scheduler action run` +* `action-scheduler datastore` +* `action-scheduler runner` +* `action-scheduler status` +* `action-scheduler version` The best way to get a full list of commands and their available options is to use WP CLI itself. This can be done by running `wp action-scheduler` to list all Action Scheduler commands, or by including the `--help` flag with any of the individual commands. This will provide all relevant parameters and flags for the command. From e3e0332b39171b59b08449696a682e97266e9164 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 22:46:08 -0400 Subject: [PATCH 41/52] eliminate code smells ...and be consistent with \Exception --- .../abstracts/ActionScheduler_WPCLI_Command.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/classes/abstracts/ActionScheduler_WPCLI_Command.php b/classes/abstracts/ActionScheduler_WPCLI_Command.php index e8125f023..847c109ee 100644 --- a/classes/abstracts/ActionScheduler_WPCLI_Command.php +++ b/classes/abstracts/ActionScheduler_WPCLI_Command.php @@ -7,10 +7,18 @@ abstract class ActionScheduler_WPCLI_Command extends \WP_CLI_Command { const DATE_FORMAT = 'Y-m-d H:i:s O'; - /** @var string[] */ + /** + * Keyed arguments. + * + * @var string[] + */ protected $args; - /** @var array */ + /** + * Positional arguments. + * + * @var array + */ protected $assoc_args; /** @@ -18,11 +26,12 @@ abstract class ActionScheduler_WPCLI_Command extends \WP_CLI_Command { * * @param string[] $args Positional arguments. * @param array $assoc_args Keyed arguments. + * @throws \Exception When loading a CLI command file outside of WP CLI context. */ public function __construct( array $args, array $assoc_args ) { if ( ! defined( 'WP_CLI' ) || ! constant( 'WP_CLI' ) ) { /* translators: %s php class name */ - throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), get_class( $this ) ) ); + throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), get_class( $this ) ) ); } $this->args = $args; From 0cda6c1d8879d6b976ac37366b5e946109d81f93 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 31 Jul 2024 22:50:21 -0400 Subject: [PATCH 42/52] change function names --- classes/WP_CLI/Action/Delete.php | 4 ++-- classes/WP_CLI/Action/Run.php | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/classes/WP_CLI/Action/Delete.php b/classes/WP_CLI/Action/Delete.php index 3bfdd4dda..0a9f0e920 100644 --- a/classes/WP_CLI/Action/Delete.php +++ b/classes/WP_CLI/Action/Delete.php @@ -35,7 +35,7 @@ public function __construct( array $args, array $assoc_args ) { $this->action_ids = array_map( 'absint', $args ); $this->action_counts['total'] = count( $this->action_ids ); - add_action( 'action_scheduler_deleted_action', array( $this, 'action__deleted' ) ); + add_action( 'action_scheduler_deleted_action', array( $this, 'on_action_deleted' ) ); } /** @@ -88,7 +88,7 @@ public function execute() { * @param int $action_id Action ID. * @return void */ - public function action__deleted( $action_id ) { + public function on_action_deleted( $action_id ) { if ( 'action_scheduler_deleted_action' !== current_action() ) { return; } diff --git a/classes/WP_CLI/Action/Run.php b/classes/WP_CLI/Action/Run.php index 7619ac687..f94df672e 100644 --- a/classes/WP_CLI/Action/Run.php +++ b/classes/WP_CLI/Action/Run.php @@ -37,10 +37,10 @@ public function __construct( array $args, array $assoc_args ) { $this->action_ids = array_map( 'absint', $args ); $this->action_counts['total'] = count( $this->action_ids ); - add_action( 'action_scheduler_execution_ignored', array( $this, 'action__ignored' ) ); - add_action( 'action_scheduler_after_execute', array( $this, 'action__executed' ) ); - add_action( 'action_scheduler_failed_execution', array( $this, 'action__failed' ), 10, 2 ); - add_action( 'action_scheduler_failed_validation', array( $this, 'action__invalid' ), 10, 2 ); + add_action( 'action_scheduler_execution_ignored', array( $this, 'on_action_ignored' ) ); + add_action( 'action_scheduler_after_execute', array( $this, 'on_action_executed' ) ); + add_action( 'action_scheduler_failed_execution', array( $this, 'on_action_failed' ), 10, 2 ); + add_action( 'action_scheduler_failed_validation', array( $this, 'on_action_invalid' ), 10, 2 ); } /** @@ -109,7 +109,7 @@ public function execute() { * @param int $action_id Action ID. * @return void */ - public function action__ignored( $action_id ) { + public function on_action_ignored( $action_id ) { if ( 'action_scheduler_execution_ignored' !== current_action() ) { return; } @@ -130,7 +130,7 @@ public function action__ignored( $action_id ) { * @param int $action_id Action ID. * @return void */ - public function action__executed( $action_id ) { + public function on_action_executed( $action_id ) { if ( 'action_scheduler_after_execute' !== current_action() ) { return; } @@ -152,7 +152,7 @@ public function action__executed( $action_id ) { * @param \Exception $e Exception. * @return void */ - public function action__failed( $action_id, \Exception $e ) { + public function on_action_failed( $action_id, \Exception $e ) { if ( 'action_scheduler_failed_execution' !== current_action() ) { return; } @@ -174,7 +174,7 @@ public function action__failed( $action_id, \Exception $e ) { * @param \Exception $e Exception. * @return void */ - public function action__invalid( $action_id, \Exception $e ) { + public function on_action_invalid( $action_id, \Exception $e ) { if ( 'action_scheduler_failed_validation' !== current_action() ) { return; } From 53a608f7ec92690c9bc4b821e5ff79bc1b7aa175 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 1 Aug 2024 10:29:09 -0400 Subject: [PATCH 43/52] escape translation output --- classes/ActionScheduler_AdminView.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/ActionScheduler_AdminView.php b/classes/ActionScheduler_AdminView.php index 1bc271d90..9508186de 100644 --- a/classes/ActionScheduler_AdminView.php +++ b/classes/ActionScheduler_AdminView.php @@ -229,11 +229,11 @@ public function add_help_tabs() { '

' . __( 'Action Scheduler is a scalable, traceable job queue for background processing large sets of actions. Action Scheduler works by triggering an action hook to run at some time in the future. Scheduled actions can also be scheduled to run on a recurring schedule.', 'action-scheduler' ) . '

' . - '

' . __( 'WP CLI', 'action-scheduler' ) . '

' . + '

' . esc_html__( 'WP CLI', 'action-scheduler' ) . '

' . '

' . sprintf( /* translators: %1$s is WP CLI command (not translatable) */ - __( 'WP CLI commands are available: execute %1$s for a list of available commands.', 'action-scheduler' ), + esc_html__( 'WP CLI commands are available: execute %1$s for a list of available commands.', 'action-scheduler' ), 'wp help action-scheduler' ) . '

', From 5971a6b3b82f0331db3ac5c807589991c88f21cf Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 2 Aug 2024 22:59:52 -0400 Subject: [PATCH 44/52] add todo --- classes/WP_CLI/Action/Generate.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php index b00ba1504..88f18329e 100644 --- a/classes/WP_CLI/Action/Generate.php +++ b/classes/WP_CLI/Action/Generate.php @@ -10,6 +10,8 @@ class ActionScheduler_WPCLI_Action_Generate_Command extends ActionScheduler_WPCL /** * Execute command. * + * @todo support negative interval + * * @return void */ public function execute() { From 891116a135755104a0585d10c8852c77697a1868 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 5 Aug 2024 19:56:19 -0400 Subject: [PATCH 45/52] save --- classes/WP_CLI/Action/Generate.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate.php index 88f18329e..2bec3c3de 100644 --- a/classes/WP_CLI/Action/Generate.php +++ b/classes/WP_CLI/Action/Generate.php @@ -10,8 +10,6 @@ class ActionScheduler_WPCLI_Action_Generate_Command extends ActionScheduler_WPCL /** * Execute command. * - * @todo support negative interval - * * @return void */ public function execute() { @@ -19,7 +17,7 @@ public function execute() { $schedule_start = $this->args[1]; $callback_args = get_flag_value( $this->assoc_args, 'args', array() ); $group = get_flag_value( $this->assoc_args, 'group', '' ); - $interval = absint( get_flag_value( $this->assoc_args, 'interval', 0 ) ); + $interval = (int) get_flag_value( $this->assoc_args, 'interval', 0 ); // avoid absint() to support negative intervals $count = absint( get_flag_value( $this->assoc_args, 'count', 1 ) ); if ( ! empty( $callback_args ) ) { From fc44be77696c55e376a2c5e2ce6d106f88cace12 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 11:08:04 -0400 Subject: [PATCH 46/52] namespace --- classes/WP_CLI/Action/Cancel.php | 4 +++- classes/WP_CLI/Action/Create.php | 4 ++-- classes/WP_CLI/Action/Delete.php | 4 +++- classes/WP_CLI/Action/Generate.php | 4 +++- classes/WP_CLI/Action/Get.php | 4 +++- classes/WP_CLI/Action/List.php | 4 +++- classes/WP_CLI/Action/Next.php | 4 +++- classes/WP_CLI/Action/Run.php | 4 +++- .../ActionScheduler_WPCLI_Action_Command.php | 20 ++++++++++--------- classes/abstracts/ActionScheduler.php | 2 +- 10 files changed, 35 insertions(+), 19 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel.php index af37fb02e..26dd8194b 100644 --- a/classes/WP_CLI/Action/Cancel.php +++ b/classes/WP_CLI/Action/Cancel.php @@ -1,11 +1,13 @@ execute(); } @@ -83,7 +85,7 @@ public function cancel( array $args, array $assoc_args ) { */ public function create( array $args, array $assoc_args ) { require_once 'Action/Create.php'; - $command = new ActionScheduler_WPCLI_Action_Create_Command( $args, $assoc_args ); + $command = new Action\Create_Command( $args, $assoc_args ); $command->execute(); } @@ -115,7 +117,7 @@ public function create( array $args, array $assoc_args ) { */ public function delete( array $args, array $assoc_args ) { require_once 'Action/Delete.php'; - $command = new ActionScheduler_WPCLI_Action_Delete_Command( $args, $assoc_args ); + $command = new Action\Delete_Command( $args, $assoc_args ); $command->execute(); } @@ -164,7 +166,7 @@ public function delete( array $args, array $assoc_args ) { */ public function generate( array $args, array $assoc_args ) { require_once 'Action/Generate.php'; - $command = new ActionScheduler_WPCLI_Action_Generate_Command( $args, $assoc_args ); + $command = new Action\Generate_Command( $args, $assoc_args ); $command->execute(); } @@ -202,7 +204,7 @@ public function generate( array $args, array $assoc_args ) { */ public function get( array $args, array $assoc_args ) { require_once 'Action/Get.php'; - $command = new ActionScheduler_WPCLI_Action_Get_Command( $args, $assoc_args ); + $command = new Action\Get_Command( $args, $assoc_args ); $command->execute(); } @@ -260,7 +262,7 @@ public function get( array $args, array $assoc_args ) { */ public function subcommand_list( array $args, array $assoc_args ) { require_once 'Action/List.php'; - $command = new ActionScheduler_WPCLI_Action_List_Command( $args, $assoc_args ); + $command = new Action\List_Command( $args, $assoc_args ); $command->execute(); } @@ -312,7 +314,7 @@ public function logs( array $args ) { */ public function next( array $args, array $assoc_args ) { require_once 'Action/Next.php'; - $command = new ActionScheduler_WPCLI_Action_Next_Command( $args, $assoc_args ); + $command = new Action\Next_Command( $args, $assoc_args ); $command->execute(); } @@ -344,7 +346,7 @@ public function next( array $args, array $assoc_args ) { */ public function run( array $args, array $assoc_args ) { require_once 'Action/Run.php'; - $command = new ActionScheduler_WPCLI_Action_Run_Command( $args, $assoc_args ); + $command = new Action\Run_Command( $args, $assoc_args ); $command->execute(); } diff --git a/classes/abstracts/ActionScheduler.php b/classes/abstracts/ActionScheduler.php index bb5487762..0533258b7 100644 --- a/classes/abstracts/ActionScheduler.php +++ b/classes/abstracts/ActionScheduler.php @@ -197,7 +197,7 @@ function () { if ( defined( 'WP_CLI' ) && WP_CLI ) { WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' ); WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Clean_Command' ); - WP_CLI::add_command( 'action-scheduler action', 'ActionScheduler_WPCLI_Action_Command' ); + WP_CLI::add_command( 'action-scheduler action', '\Action_Scheduler\WP_CLI\Action_Command' ); WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_System_Command' ); if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) { $command = new Migration_Command(); From 60c66ff6119d0ed59289fe0f3e5902b54979040f Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 11:10:09 -0400 Subject: [PATCH 47/52] rename files --- .../Action/{Cancel.php => Cancel_Command.php} | 0 .../Action/{Create.php => Create_Command.php} | 0 .../Action/{Delete.php => Delete_Command.php} | 0 .../{Generate.php => Generate_Command.php} | 0 .../WP_CLI/Action/{Get.php => Get_Command.php} | 0 .../WP_CLI/Action/{List.php => List_Command.php} | 0 .../WP_CLI/Action/{Next.php => Next_Command.php} | 0 .../WP_CLI/Action/{Run.php => Run_Command.php} | 0 ...CLI_Action_Command.php => Action_Command.php} | 16 ++++++++-------- 9 files changed, 8 insertions(+), 8 deletions(-) rename classes/WP_CLI/Action/{Cancel.php => Cancel_Command.php} (100%) rename classes/WP_CLI/Action/{Create.php => Create_Command.php} (100%) rename classes/WP_CLI/Action/{Delete.php => Delete_Command.php} (100%) rename classes/WP_CLI/Action/{Generate.php => Generate_Command.php} (100%) rename classes/WP_CLI/Action/{Get.php => Get_Command.php} (100%) rename classes/WP_CLI/Action/{List.php => List_Command.php} (100%) rename classes/WP_CLI/Action/{Next.php => Next_Command.php} (100%) rename classes/WP_CLI/Action/{Run.php => Run_Command.php} (100%) rename classes/WP_CLI/{ActionScheduler_WPCLI_Action_Command.php => Action_Command.php} (95%) diff --git a/classes/WP_CLI/Action/Cancel.php b/classes/WP_CLI/Action/Cancel_Command.php similarity index 100% rename from classes/WP_CLI/Action/Cancel.php rename to classes/WP_CLI/Action/Cancel_Command.php diff --git a/classes/WP_CLI/Action/Create.php b/classes/WP_CLI/Action/Create_Command.php similarity index 100% rename from classes/WP_CLI/Action/Create.php rename to classes/WP_CLI/Action/Create_Command.php diff --git a/classes/WP_CLI/Action/Delete.php b/classes/WP_CLI/Action/Delete_Command.php similarity index 100% rename from classes/WP_CLI/Action/Delete.php rename to classes/WP_CLI/Action/Delete_Command.php diff --git a/classes/WP_CLI/Action/Generate.php b/classes/WP_CLI/Action/Generate_Command.php similarity index 100% rename from classes/WP_CLI/Action/Generate.php rename to classes/WP_CLI/Action/Generate_Command.php diff --git a/classes/WP_CLI/Action/Get.php b/classes/WP_CLI/Action/Get_Command.php similarity index 100% rename from classes/WP_CLI/Action/Get.php rename to classes/WP_CLI/Action/Get_Command.php diff --git a/classes/WP_CLI/Action/List.php b/classes/WP_CLI/Action/List_Command.php similarity index 100% rename from classes/WP_CLI/Action/List.php rename to classes/WP_CLI/Action/List_Command.php diff --git a/classes/WP_CLI/Action/Next.php b/classes/WP_CLI/Action/Next_Command.php similarity index 100% rename from classes/WP_CLI/Action/Next.php rename to classes/WP_CLI/Action/Next_Command.php diff --git a/classes/WP_CLI/Action/Run.php b/classes/WP_CLI/Action/Run_Command.php similarity index 100% rename from classes/WP_CLI/Action/Run.php rename to classes/WP_CLI/Action/Run_Command.php diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php b/classes/WP_CLI/Action_Command.php similarity index 95% rename from classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php rename to classes/WP_CLI/Action_Command.php index cc7a3089f..b32eea31d 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_Action_Command.php +++ b/classes/WP_CLI/Action_Command.php @@ -32,7 +32,7 @@ class Action_Command extends \WP_CLI_Command { * @return void */ public function cancel( array $args, array $assoc_args ) { - require_once 'Action/Cancel.php'; + require_once 'Action/Cancel_Command.php'; $command = new Action\Cancel_Command( $args, $assoc_args ); $command->execute(); } @@ -84,7 +84,7 @@ public function cancel( array $args, array $assoc_args ) { * @return void */ public function create( array $args, array $assoc_args ) { - require_once 'Action/Create.php'; + require_once 'Action/Create_Command.php'; $command = new Action\Create_Command( $args, $assoc_args ); $command->execute(); } @@ -116,7 +116,7 @@ public function create( array $args, array $assoc_args ) { * @return void */ public function delete( array $args, array $assoc_args ) { - require_once 'Action/Delete.php'; + require_once 'Action/Delete_Command.php'; $command = new Action\Delete_Command( $args, $assoc_args ); $command->execute(); } @@ -165,7 +165,7 @@ public function delete( array $args, array $assoc_args ) { * @return void */ public function generate( array $args, array $assoc_args ) { - require_once 'Action/Generate.php'; + require_once 'Action/Generate_Command.php'; $command = new Action\Generate_Command( $args, $assoc_args ); $command->execute(); } @@ -203,7 +203,7 @@ public function generate( array $args, array $assoc_args ) { * @return void */ public function get( array $args, array $assoc_args ) { - require_once 'Action/Get.php'; + require_once 'Action/Get_Command.php'; $command = new Action\Get_Command( $args, $assoc_args ); $command->execute(); } @@ -261,7 +261,7 @@ public function get( array $args, array $assoc_args ) { * @subcommand list */ public function subcommand_list( array $args, array $assoc_args ) { - require_once 'Action/List.php'; + require_once 'Action/List_Command.php'; $command = new Action\List_Command( $args, $assoc_args ); $command->execute(); } @@ -313,7 +313,7 @@ public function logs( array $args ) { * @return void */ public function next( array $args, array $assoc_args ) { - require_once 'Action/Next.php'; + require_once 'Action/Next_Command.php'; $command = new Action\Next_Command( $args, $assoc_args ); $command->execute(); } @@ -345,7 +345,7 @@ public function next( array $args, array $assoc_args ) { * @return void */ public function run( array $args, array $assoc_args ) { - require_once 'Action/Run.php'; + require_once 'Action/Run_Command.php'; $command = new Action\Run_Command( $args, $assoc_args ); $command->execute(); } From 28ce0cb3c81d669ea73022d5684c26686890d4c4 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 11:14:50 -0400 Subject: [PATCH 48/52] save --- classes/WP_CLI/Action/Cancel_Command.php | 2 +- classes/WP_CLI/Action/Create_Command.php | 2 +- classes/WP_CLI/Action/Delete_Command.php | 2 +- classes/WP_CLI/Action/Generate_Command.php | 2 +- classes/WP_CLI/Action/Get_Command.php | 2 +- classes/WP_CLI/Action/List_Command.php | 4 ++-- classes/WP_CLI/Action/Next_Command.php | 21 ++++++++++++--------- classes/WP_CLI/Action/Run_Command.php | 2 +- 8 files changed, 20 insertions(+), 17 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel_Command.php b/classes/WP_CLI/Action/Cancel_Command.php index 26dd8194b..0f1a161e2 100644 --- a/classes/WP_CLI/Action/Cancel_Command.php +++ b/classes/WP_CLI/Action/Cancel_Command.php @@ -7,7 +7,7 @@ /** * WP-CLI command: action-scheduler action cancel */ -class Cancel_Command extends ActionScheduler_WPCLI_Command { +class Cancel_Command extends \ActionScheduler_WPCLI_Command { /** * Execute command. diff --git a/classes/WP_CLI/Action/Create_Command.php b/classes/WP_CLI/Action/Create_Command.php index 000352bbc..fedd417e2 100644 --- a/classes/WP_CLI/Action/Create_Command.php +++ b/classes/WP_CLI/Action/Create_Command.php @@ -5,7 +5,7 @@ /** * WP-CLI command: action-scheduler action create */ -class Create_Command extends ActionScheduler_WPCLI_Command { +class Create_Command extends \ActionScheduler_WPCLI_Command { const ASYNC_OPTS = array( 'async', 0 ); diff --git a/classes/WP_CLI/Action/Delete_Command.php b/classes/WP_CLI/Action/Delete_Command.php index 85d30e8a1..a549e0b4e 100644 --- a/classes/WP_CLI/Action/Delete_Command.php +++ b/classes/WP_CLI/Action/Delete_Command.php @@ -5,7 +5,7 @@ /** * WP-CLI command: action-scheduler action delete */ -class Delete_Command extends ActionScheduler_WPCLI_Command { +class Delete_Command extends \ActionScheduler_WPCLI_Command { /** * Array of action IDs to delete. diff --git a/classes/WP_CLI/Action/Generate_Command.php b/classes/WP_CLI/Action/Generate_Command.php index ea82fc111..6e6e8c77b 100644 --- a/classes/WP_CLI/Action/Generate_Command.php +++ b/classes/WP_CLI/Action/Generate_Command.php @@ -7,7 +7,7 @@ /** * WP-CLI command: action-scheduler action generate */ -class Generate_Command extends ActionScheduler_WPCLI_Command { +class Generate_Command extends \ActionScheduler_WPCLI_Command { /** * Execute command. diff --git a/classes/WP_CLI/Action/Get_Command.php b/classes/WP_CLI/Action/Get_Command.php index 4ec985863..95df59550 100644 --- a/classes/WP_CLI/Action/Get_Command.php +++ b/classes/WP_CLI/Action/Get_Command.php @@ -5,7 +5,7 @@ /** * WP-CLI command: action-scheduler action get */ -class Get_Command extends ActionScheduler_WPCLI_Command { +class Get_Command extends \ActionScheduler_WPCLI_Command { /** * Execute command. diff --git a/classes/WP_CLI/Action/List_Command.php b/classes/WP_CLI/Action/List_Command.php index 1b68a66fb..4a3e0835f 100644 --- a/classes/WP_CLI/Action/List_Command.php +++ b/classes/WP_CLI/Action/List_Command.php @@ -7,7 +7,7 @@ /** * WP-CLI command: action-scheduler action list */ -class List_Command extends ActionScheduler_WPCLI_Command { +class List_Command extends \ActionScheduler_WPCLI_Command { const PARAMETERS = array( 'hook', @@ -73,7 +73,7 @@ public function execute() { $params = 'array()'; } - WP_CLI::debug( + \WP_CLI::debug( sprintf( 'as_get_scheduled_actions( %s, %s )', $params, diff --git a/classes/WP_CLI/Action/Next_Command.php b/classes/WP_CLI/Action/Next_Command.php index 5530ce4c2..b71744597 100644 --- a/classes/WP_CLI/Action/Next_Command.php +++ b/classes/WP_CLI/Action/Next_Command.php @@ -9,7 +9,7 @@ /** * WP-CLI command: action-scheduler action next */ -class Next_Command extends ActionScheduler_WPCLI_Command { +class Next_Command extends \ActionScheduler_WPCLI_Command { /** * Execute command. @@ -27,7 +27,7 @@ public function execute() { } if ( $raw ) { - WP_CLI::line( as_next_scheduled_action( $hook, $callback_args, $group ) ); + \WP_CLI::line( as_next_scheduled_action( $hook, $callback_args, $group ) ); return; } @@ -42,27 +42,30 @@ public function execute() { $params['args'] = $callback_args; } - $params['status'] = ActionScheduler_Store::STATUS_RUNNING; + $params['status'] = \ActionScheduler_Store::STATUS_RUNNING; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export - WP_CLI::debug( 'ActionScheduler()::store()->query_action( ' . var_export( $params, true ) . ' )' ); + \WP_CLI::debug( 'ActionScheduler()::store()->query_action( ' . var_export( $params, true ) . ' )' ); + + $store = \ActionScheduler::store(); + $action_id = $store->query_action( $params ); - $action_id = ActionScheduler::store()->query_action( $params ); if ( $action_id ) { echo $action_id; return; } - $params['status'] = ActionScheduler_Store::STATUS_PENDING; + $params['status'] = \ActionScheduler_Store::STATUS_PENDING; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export - WP_CLI::debug( 'ActionScheduler()::store()->query_action( ' . var_export( $params, true ) . ' )' ); + \WP_CLI::debug( 'ActionScheduler()::store()->query_action( ' . var_export( $params, true ) . ' )' ); + + $action_id = $store->query_action( $params ); - $action_id = ActionScheduler::store()->query_action( $params ); if ( $action_id ) { echo $action_id; return; } - WP_CLI::warning( 'No matching next action.' ); + \WP_CLI::warning( 'No matching next action.' ); } } diff --git a/classes/WP_CLI/Action/Run_Command.php b/classes/WP_CLI/Action/Run_Command.php index 6b50fd549..efff37dd9 100644 --- a/classes/WP_CLI/Action/Run_Command.php +++ b/classes/WP_CLI/Action/Run_Command.php @@ -5,7 +5,7 @@ /** * WP-CLI command: action-scheduler action run */ -class Run_Command extends ActionScheduler_WPCLI_Command { +class Run_Command extends \ActionScheduler_WPCLI_Command { /** * Array of action IDs to execute. From fcca8abf6cb3394fd6967bf3a098c944d52edb50 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 12:45:04 -0400 Subject: [PATCH 49/52] save --- classes/WP_CLI/Action/Cancel_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel_Command.php b/classes/WP_CLI/Action/Cancel_Command.php index 0f1a161e2..a9802e780 100644 --- a/classes/WP_CLI/Action/Cancel_Command.php +++ b/classes/WP_CLI/Action/Cancel_Command.php @@ -110,8 +110,8 @@ protected function print_error( \Exception $e, $multiple ) { \WP_CLI::error( sprintf( /* translators: %1$s: singular or plural %2$s: refers to the exception error message. */ - __( 'There was an error cancelling the scheduled %1$s: %2$s', 'action-scheduler' ), - $multiple ? 'actions' : 'action', + __( 'There was an error cancelling the %1$s: %2$s', 'action-scheduler' ), + $multiple ? __( 'scheduled actions', 'action-scheduler' ) : __( 'scheduled action', 'action-scheduler' ), $e->getMessage() ) ); From f60e586703ceb7765797819a11951592404eece6 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 13:24:33 -0400 Subject: [PATCH 50/52] save --- classes/WP_CLI/Action/Cancel_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/Action/Cancel_Command.php b/classes/WP_CLI/Action/Cancel_Command.php index a9802e780..e9eb9f260 100644 --- a/classes/WP_CLI/Action/Cancel_Command.php +++ b/classes/WP_CLI/Action/Cancel_Command.php @@ -77,7 +77,7 @@ protected function cancel_all( $hook, $callback_args, $group ) { } try { - $result = call_user_func( 'as_unschedule_all_actions', $hook, $callback_args, $group ); + $result = as_unschedule_all_actions( $hook, $callback_args, $group ); } catch ( \Exception $e ) { $this->print_error( $e, $multiple ); } From 001ac6a82b7f00e572b2530f79e1a80100e30f9d Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 14:52:35 -0400 Subject: [PATCH 51/52] save --- ...r_WPCLI_System_Command.php => System_Command.php} | 4 +++- classes/abstracts/ActionScheduler.php | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) rename classes/WP_CLI/{ActionScheduler_WPCLI_System_Command.php => System_Command.php} (98%) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/System_Command.php similarity index 98% rename from classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php rename to classes/WP_CLI/System_Command.php index 834c06b9e..c503f660f 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/System_Command.php @@ -1,5 +1,7 @@ allow_migration() ) { $command = new Migration_Command(); $command->register(); @@ -300,11 +300,11 @@ protected static function is_class_migration( $class ) { */ protected static function is_class_cli( $class ) { static $cli_segments = array( - 'QueueRunner' => true, - 'Command' => true, - 'ProgressBar' => true, - 'ActionScheduler_WPCLI_Action_Command' => true, - 'ActionScheduler_WPCLI_System_Command' => true, + 'QueueRunner' => true, + 'Command' => true, + 'ProgressBar' => true, + '\Action_Scheduler\WP_CLI\Action_Command' => true, + '\Action_Scheduler\WP_CLI\System_Command' => true, ); $segments = explode( '_', $class ); From e5b492e44f1cbde87e811482080b99678582ae96 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 14:57:12 -0400 Subject: [PATCH 52/52] save --- classes/WP_CLI/System_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_CLI/System_Command.php b/classes/WP_CLI/System_Command.php index c503f660f..5a585b5cc 100644 --- a/classes/WP_CLI/System_Command.php +++ b/classes/WP_CLI/System_Command.php @@ -62,7 +62,7 @@ public function status( array $args, array $assoc_args ) { * * @link https://github.com/woocommerce/action-scheduler-disable-default-runner */ - $runner_enabled = has_action( 'action_scheduler_run_queue', array( ActionScheduler::runner(), 'run' ) ); + $runner_enabled = has_action( 'action_scheduler_run_queue', array( \ActionScheduler::runner(), 'run' ) ); \WP_CLI::line( sprintf( 'Data store: %s', $this->get_current_datastore() ) ); \WP_CLI::line( sprintf( 'Runner: %s%s', $this->get_current_runner(), ( $runner_enabled ? '' : ' (disabled)' ) ) );