Skip to content

Action queue hooks

CampaignSuite Core has a small queue for work that has to happen in the background and be retried when it fails, such as sending an update to a connected service. Add an action with CampaignSuite\Core\Includes\ActionHandler::add_to_queue($action_type, $context). A recurring Action Scheduler job processes the queue every 5 minutes. Only one process runs at a time, and the oldest pending actions go first.

Each action is executed through a filter named after its type. An exception or a WP_Error result counts as a failure, and the action is retried on the next run until it succeeds, hits a permanent error or reaches the maximum number of attempts. If the “Bypass Action Scheduler” debug setting is on, actions run as soon as they are added instead of being queued.

Filter · includes/class-actionhandler.php

Executes a queued action. The dynamic part {type} is the $action_type that was given to add_to_queue(). When at least one callback is registered for the type, this filter runs and the generic cs_action filter does not.

The incoming $result is a WP_Error with the code action_failed. Return anything that is not a WP_Error to mark the action as executed, or a WP_Error to mark it as failed (and retry it). Core itself uses the types mtc_update_contact_field, mtc_update_contact_field_bulk and mtc_update_contact_integration, which the Mautic client handles.

Parameter Type Description
$result mixed|WP_Error The current result. Default: an action_failed WP_Error.
$context mixed The context that was passed to add_to_queue(). It is stored serialized and restored without objects, so use arrays of scalars.
$action array The queue row, including id, action_type, the serialized context and the number of attempts.

Returns: mixed|WP_Error — a WP_Error to fail (and retry) the action, or any other value to complete it.

add_filter('cs_action_send_welcome_mail', function ($result, $context, $action) {
$sent = wp_mail($context['email'], 'Welcome', 'Thanks for joining.');
return $sent ? true : new WP_Error('mail_failed', 'The mail could not be sent.');
}, 10, 3);
// Somewhere else:
CampaignSuite\Core\Includes\ActionHandler::add_to_queue('send_welcome_mail', ['email' => '[email protected]']);

Filter · includes/class-actionhandler.php

Executes a queued action that has no cs_action_{type} callback. Use it when one handler serves several action types. Check $action_type, and return $result unchanged for types you don’t handle.

Parameter Type Description
$result mixed|WP_Error The current result. Default: an action_failed WP_Error.
$context mixed The context that was passed to add_to_queue().
$action_type string The action type.
$action array The queue row.

Returns: mixed|WP_Error — a WP_Error to fail (and retry) the action, or any other value to complete it.

add_filter('cs_action', function ($result, $context, $action_type, $action) {
if (strpos($action_type, 'my_addon_') !== 0) {
return $result;
}
// Handle all my_addon_* actions here.
return true;
}, 10, 4);

Filter · includes/class-actionhandler.php

Filters the maximum number of actions one queue run handles. Anything left over waits for the next run.

Parameter Type Description
$batch_size int The number of actions per run. Default: 50. A value below 1 is treated as 1.

Returns: int — the number of actions per run.

add_filter('cs_action_batch_size', function ($batch_size) {
return 100;
});

Filter · includes/class-actionhandler.php

Filters how many times a failing action is tried before it is marked as failed and no longer retried. It runs every time an action fails.

Parameter Type Description
$max_attempts int The maximum number of attempts. Default: 12.
$item array The queue row of the action that failed.

Returns: int — the maximum number of attempts.

add_filter('cs_action_max_attempts', function ($max_attempts, $item) {
return $item['action_type'] === 'send_welcome_mail' ? 3 : $max_attempts;
}, 10, 2);

Filter · includes/class-actionhandler.php

Filters whether a failed action is a permanent failure that should not be retried. By default an error whose data has an HTTP status in the 4xx range, except 408 and 429, is permanent, because repeating the request won’t help. A permanent failure marks the action as failed right away.

Parameter Type Description
$is_permanent bool Whether the error is permanent.
$error WP_Error The error that the action returned.
$item array The queue row of the action.

Returns: bool — true to give up on the action.

add_filter('cs_action_is_permanent_error', function ($is_permanent, $error, $item) {
if ($error->get_error_code() === 'invalid_email') {
return true;
}
return $is_permanent;
}, 10, 3);

Filter · includes/class-actionhandler.php

Filters how many days executed and failed actions are kept before the queue handler deletes them. Pending actions are never deleted.

Parameter Type Description
$days int The number of days. Default: 14.

Returns: int — the number of days.

add_filter('cs_action_cleanup_days', function ($days) {
return 30;
});