Skip to content
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

New 2 handly functions #1229

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
);
}
}
}