From 6089d69b79b02d22138c1af4a772b7134e9aa7d3 Mon Sep 17 00:00:00 2001 From: Arifursdev <52378239+Arifursdev@users.noreply.github.com> Date: Sun, 12 Jan 2025 19:59:49 +0600 Subject: [PATCH 1/2] new handy functions 'as_run_queued_actions' and 'as_run_next_queued_action' --- functions.php | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/functions.php b/functions.php index 59e5542c3..dc94ae272 100644 --- a/functions.php +++ b/functions.php @@ -16,7 +16,7 @@ * * @return int The action ID. Zero if there was an error scheduling the action. */ -function as_enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { +function as_enqueue_async_acspetion( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { return 0; } @@ -493,3 +493,91 @@ function as_get_datetime_object( $date_string = null, $timezone = 'UTC' ) { } return $date; } + +/** + * Run all queued actions for a given hook, arguments, and group. + * + * @param string $hook The hook that the actions are associated with. + * @param array $args Optional. An array of arguments to filter the actions. Default is an empty array. + * @param string $group Optional. The group the actions are assigned to. Default is an empty string. + */ +function as_run_queued_actions($hook, $args = array(), $group = '') { + if (!ActionScheduler::is_initialized(__FUNCTION__)) { + return; + } + + $params = array( + 'hook' => $hook, + 'status' => ActionScheduler_Store::STATUS_PENDING, + 'orderby' => 'date', + 'order' => 'ASC', + 'group' => $group, + ); + + if (is_array($args)) { + $params['args'] = $args; + } + + $actions = ActionScheduler::store()->query_actions($params); + + $queue_runner = ActionScheduler::runner(); + + foreach ($actions as $action_id) { + try { + $queue_runner->process_action($action_id); + } catch (Exception $exception) { + ActionScheduler::logger()->log( + $action_id, + sprintf( + 'Caught exception while running action "%1$s": %2$s', + $hook, + $exception->getMessage() + ) + ); + } + } +} + +/** + * Run the next queued action for a given hook, arguments, and group. + * + * @param string $hook The hook that the action is associated with. + * @param array $args Optional. An array of arguments to filter the action. Default is an empty array. + * @param string $group Optional. The group the action is assigned to. Default is an empty string. + */ +function as_run_next_queued_action($hook, $args = array(), $group = '') { + if (!ActionScheduler::is_initialized(__FUNCTION__)) { + return; + } + + $params = array( + 'hook' => $hook, + 'status' => ActionScheduler_Store::STATUS_PENDING, + 'orderby' => 'date', + 'order' => 'ASC', + 'group' => $group, + ); + + if (is_array($args)) { + $params['args'] = $args; + } + + $action_id = ActionScheduler::store()->query_action($params); + + if ($action_id) { + $queue_runner = ActionScheduler::runner(); + + try { + $queue_runner->process_action($action_id); + } catch (Exception $exception) { + ActionScheduler::logger()->log( + $action_id, + sprintf( + 'Caught exception while running action "%1$s": %2$s', + $hook, + $exception->getMessage() + ) + ); + } + } +} \ No newline at end of file From 0468f9fbd095af8759e2c8533147d5327157aa44 Mon Sep 17 00:00:00 2001 From: Arifursdev <52378239+Arifursdev@users.noreply.github.com> Date: Sun, 12 Jan 2025 20:04:50 +0600 Subject: [PATCH 2/2] fix --- functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.php b/functions.php index dc94ae272..ca195e038 100644 --- a/functions.php +++ b/functions.php @@ -16,7 +16,7 @@ * * @return int The action ID. Zero if there was an error scheduling the action. */ -function as_enqueue_async_acspetion( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { +function as_enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { return 0; }