-
Notifications
You must be signed in to change notification settings - Fork 135
New 2 handly functions #1229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
New 2 handly functions #1229
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Comment on lines
+509
to
+521
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A concern here is that we may pick up claimed actions (once an action is claimed, its status initially remains |
||
|
|
||
| $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 = '') { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For both functions, what are your thoughts on a return type? That might be a boolean for this function (because it is only interested in running a single action) or possibly an int for the other function (a count of how many actions were processed)? |
||
| 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); | ||
|
Comment on lines
+553
to
+565
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same concern as before (possible race condition here). |
||
|
|
||
| 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() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth running the new functions through the linter (
composer run phpcs).