Constants

STATUS_COMPLETE

STATUS_COMPLETE = 'complete'

STATUS_PENDING

STATUS_PENDING = 'pending'

STATUS_RUNNING

STATUS_RUNNING = 'in-progress'

STATUS_FAILED

STATUS_FAILED = 'failed'

STATUS_CANCELED

STATUS_CANCELED = 'canceled'

DEFAULT_CLASS

DEFAULT_CLASS = 'ActionScheduler_wpPostStore'

Properties

$max_args_length

$max_args_length : int

Maximum length of args.

Type

normal

$max_index_length

$max_index_length : int

Maximum length of index.

Type

normal

$claim_filters

$claim_filters : array

List of claim filters.

Type

normal

$store

$store : \ActionScheduler_Store

ActionScheduler_Store instance.

Type

normal

$claim_before_date

$claim_before_date : \DateTime|null

Used to share information about the before_date property of claims internally.

This is used in preference to passing the same information as a method param for backwards-compatibility reasons.

Type

normal

Methods

save_action()

save_action(\ActionScheduler_Action  $action, \DateTime|null  $scheduled_date = null) : int

Save an action. Can save duplicate action as well, prefer using `save_unique_action` instead.

Parameters

\ActionScheduler_Action $action

Action object.

\DateTime|null $scheduled_date

Optional schedule date. Default null.

Throws

\RuntimeException

Throws exception when saving the action fails.

Returns

int —

Action ID.

fetch_action()

fetch_action(int  $action_id) : \ActionScheduler_Action

Retrieve an action.

Parameters

int $action_id

Action ID.

Returns

\ActionScheduler_Action

find_action()

find_action(string  $hook, array  $params = array()) : string|null

Find an action.

Note: the query ordering changes based on the passed 'status' value.

Parameters

string $hook

Action hook.

array $params

Parameters of the action to find.

Returns

string|null —

ID of the next action matching the criteria or NULL if not found.

query_actions()

query_actions(array  $query = array(), string  $query_type = 'select') : string|array|null

Query for action count or list of action IDs.

Parameters

array $query

Query filtering options.

string $query_type

Whether to select or count the results. Defaults to select.

Returns

string|array|null —

The IDs of actions matching the query. Null on failure.

query_action()

query_action(array  $query) : int|null

Run query to get a single action ID.

Parameters

array $query

Query parameters.

Returns

int|null

action_counts()

action_counts() : array

Get a count of all actions in the store, grouped by status.

Returns

array —

Set of 'status' => int $count pairs for statuses with 1 or more actions of that status.

extra_action_counts()

extra_action_counts() : array

Get additional action counts.

  • add past-due actions

Returns

array

cancel_action()

cancel_action(int  $action_id) : void

Cancel an action.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

If the action update failed.

delete_action()

delete_action(int  $action_id) : mixed

Delete an action.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

If the action deletion failed.

Returns

mixed

get_date()

get_date(string  $action_id) : \DateTime

Get the schedule date for an action.

Parameters

string $action_id

Action ID.

Returns

\DateTime —

The local date the action is scheduled to run, or the date that it ran.

stake_claim()

stake_claim(int  $max_actions = 10, \DateTime|null  $before_date = null, array  $hooks = array(), string  $group = '') : \ActionScheduler_ActionClaim

Stake a claim on actions.

Parameters

int $max_actions

Maximum number of action to include in claim.

\DateTime|null $before_date

Jobs must be schedule before this date. Defaults to now.

array $hooks

Hooks to filter for.

string $group

Group to filter for.

Returns

\ActionScheduler_ActionClaim

get_claim_count()

get_claim_count() : int

Get the number of active claims.

Returns

int

release_claim()

release_claim(\ActionScheduler_ActionClaim  $claim) : mixed

Release actions from a claim and delete the claim.

Parameters

\ActionScheduler_ActionClaim $claim

Claim object.

Throws

\RuntimeException

When unable to release actions from claim.

Returns

mixed

unclaim_action()

unclaim_action(int  $action_id) : void

Remove the claim from an action.

Parameters

int $action_id

Action ID.

mark_failure()

mark_failure(int  $action_id) : mixed

Mark an action as failed.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

Throw an exception if action was not updated.

Returns

mixed

log_execution()

log_execution(int  $action_id) : void

Add execution message to action log.

Parameters

int $action_id

Action ID.

Throws

\Exception

If the action status cannot be updated to self::STATUS_RUNNING ('in-progress').

mark_complete()

mark_complete(int  $action_id) : void

Mark an action as complete.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

Throw an exception if action was not updated.

get_status()

get_status(int  $action_id) : string

Get an action's status.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

Throw an exception if not status was found for action_id.

\RuntimeException

Throw an exception if action status could not be retrieved.

Returns

string

get_claim_id()

get_claim_id(string  $action_id) : mixed

Return an action's claim ID, as stored in the claim_id column.

Parameters

string $action_id

Action ID.

Returns

mixed

find_actions_by_claim_id()

find_actions_by_claim_id(int  $claim_id) : int[]

Retrieve the action IDs of action in a claim.

Parameters

int $claim_id

Claim ID.

Returns

int[]

cancel_actions_by_hook()

cancel_actions_by_hook(string  $hook) : void

Cancel pending actions by hook.

Parameters

string $hook

Hook name.

cancel_actions_by_group()

cancel_actions_by_group(string  $group) : void

Cancel pending actions by group.

Parameters

string $group

Group slug.

get_status_labels()

get_status_labels() : array<string,string>

Get status labels.

Returns

array

has_pending_actions_due()

has_pending_actions_due() : string

Check if there are any pending scheduled actions due to run.

Returns

string

init()

init() : mixed

Initialize the data store

Returns

mixed

mark_migrated()

mark_migrated(int  $action_id) : mixed

Callable function to mark an action as migrated optionally overridden in derived classes.

Parameters

int $action_id

Action ID.

Returns

mixed

instance()

instance() : \ActionScheduler_Store

Get instance.

Returns

\ActionScheduler_Store

mark_failed_fetch_action()

mark_failed_fetch_action(int  $action_id) : mixed

Mark an action that failed to fetch correctly as failed.

Parameters

int $action_id

The ID of the action.

Returns

mixed

save_unique_action()

save_unique_action(\ActionScheduler_Action  $action, \DateTime|null  $scheduled_date = null) : int

Save an action, checks if this is a unique action before actually saving.

Parameters

\ActionScheduler_Action $action

Action object.

\DateTime|null $scheduled_date

Optional schedule date. Default null.

Throws

\RuntimeException

Throws exception when saving the action fails.

Returns

int —

Action ID.

set_claim_filter()

set_claim_filter(string  $filter_name, mixed  $filter_values) : void

Set a claim filter.

Parameters

string $filter_name

Claim filter name.

mixed $filter_values

Values to filter.

get_claim_filter()

get_claim_filter(string  $filter_name) : mixed

Get the claim filter value.

Parameters

string $filter_name

Claim filter name.

Returns

mixed

validate_sql_comparator()

validate_sql_comparator(string  $comparison_operator) : string

Validate SQL operator.

Parameters

string $comparison_operator

Operator.

Returns

string

get_scheduled_date_string()

get_scheduled_date_string(\ActionScheduler_Action  $action, null|\DateTime  $scheduled_date = null) : string

Get the time MySQL formatted date/time string for an action's (next) scheduled date.

Parameters

\ActionScheduler_Action $action

Action.

null|\DateTime $scheduled_date

Action's schedule date (optional).

Returns

string

get_scheduled_date_string_local()

get_scheduled_date_string_local(\ActionScheduler_Action|null  $action, null|\DateTime  $scheduled_date = null) : string

Get the time MySQL formatted date/time string for an action's (next) scheduled date.

Parameters

\ActionScheduler_Action|null $action

Action.

null|\DateTime $scheduled_date

Action's scheduled date (optional).

Returns

string

validate_args()

validate_args(mixed  $args, int  $action_id) : mixed

Validate that we could decode action arguments.

Parameters

mixed $args

The decoded arguments.

int $action_id

The action ID.

Throws

\ActionScheduler_InvalidActionException

When the decoded arguments are invalid.

Returns

mixed

validate_schedule()

validate_schedule(mixed  $schedule, int  $action_id) : mixed

Validate a ActionScheduler_Schedule object.

Parameters

mixed $schedule

The unserialized ActionScheduler_Schedule object.

int $action_id

The action ID.

Throws

\ActionScheduler_InvalidActionException

When the schedule is invalid.

Returns

mixed

validate_action()

validate_action(\ActionScheduler_Action  $action) : mixed

InnoDB indexes have a maximum size of 767 bytes by default, which is only 191 characters with utf8mb4.

Previously, AS wasn't concerned about args length, as we used the (unindex) post_content column. However, with custom tables, we use an indexed VARCHAR column instead.

Parameters

\ActionScheduler_Action $action

Action to be validated.

Throws

\InvalidArgumentException

When json encoded args is too long.

Returns

mixed

bulk_cancel_actions()

bulk_cancel_actions(array  $query_args) : void

Bulk cancel actions.

Parameters

array $query_args

Query parameters.

hook()

hook() : mixed

Add base hooks

Returns

mixed

unhook()

unhook() : mixed

Remove base hooks

Returns

mixed

get_local_timezone()

get_local_timezone() : \DateTimeZone

Get the site's local time.

Returns

\DateTimeZone

hash_args()

hash_args(string  $args) : string

Generate a hash from json_encoded $args using MD5 as this isn't for security.

Parameters

string $args

JSON encoded action args.

Returns

string

get_args_for_query()

get_args_for_query(array  $args) : string

Get action args query param value from action args.

Parameters

array $args

Action args.

Returns

string

get_group_ids()

get_group_ids(string|array  $slugs, bool  $create_if_not_exists = true) : array

Get a group's ID based on its name/slug.

Parameters

string|array $slugs

The string name of a group, or names for several groups.

bool $create_if_not_exists

Whether to create the group if it does not already exist. Default, true - create the group.

Returns

array —

The group IDs, if they exist or were successfully created. May be empty.

create_group()

create_group(string  $slug) : int

Create an action group.

Parameters

string $slug

Group slug.

Returns

int —

Group ID.

get_null_action()

get_null_action() : \ActionScheduler_NullAction

Create a null action.

Returns

\ActionScheduler_NullAction

make_action_from_db_record()

make_action_from_db_record(object  $data) : \ActionScheduler_Action|\ActionScheduler_CanceledAction|\ActionScheduler_FinishedAction

Create an action from a database record.

Parameters

object $data

Action database record.

Returns

\ActionScheduler_Action|\ActionScheduler_CanceledAction|\ActionScheduler_FinishedAction

get_query_actions_sql()

get_query_actions_sql(array  $query, string  $select_or_count = 'select') : string

Returns the SQL statement to query (or count) actions.

Parameters

array $query

Filtering options.

string $select_or_count

Whether the SQL should select and return the IDs or just the row count.

Throws

\InvalidArgumentException

If the query is invalid.

\RuntimeException

When "unknown partial args matching value".

Returns

string —

SQL statement already properly escaped.

get_date_gmt()

get_date_gmt(int  $action_id) : \DateTime

Get the GMT schedule date for an action.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

If action cannot be identified.

Returns

\DateTime —

The GMT date the action is scheduled to run, or the date that it ran.

generate_claim_id()

generate_claim_id() : int

Generate a new action claim.

Returns

int —

Claim ID.

claim_actions()

claim_actions(string  $claim_id, int  $limit, \DateTime|null  $before_date = null, array  $hooks = array(), string  $group = '') : int

Mark actions claimed.

Parameters

string $claim_id

Claim Id.

int $limit

Number of action to include in claim.

\DateTime|null $before_date

Should use UTC timezone.

array $hooks

Hooks to filter for.

string $group

Group to filter for.

Throws

\InvalidArgumentException

Throws InvalidArgumentException if group doesn't exist.

\RuntimeException

Throws RuntimeException if unable to claim action.

Returns

int —

The number of actions that were claimed.

save_action_to_db()

save_action_to_db(\ActionScheduler_Action  $action, ?\DateTime  $date = null, bool  $unique = false) : int

Save an action.

Parameters

\ActionScheduler_Action $action

Action object.

?\DateTime $date

Optional schedule date. Default null.

bool $unique

Whether the action should be unique.

Throws

\RuntimeException

Throws exception when saving the action fails.

Returns

int —

Action ID.

build_insert_sql()

build_insert_sql(array  $data, bool  $unique) : string

Helper function to build insert query.

Parameters

array $data

Row data for action.

bool $unique

Whether the action should be unique.

Returns

string —

Insert query.

build_where_clause_for_insert()

build_where_clause_for_insert(array  $data, string  $table_name, bool  $unique) : string

Helper method to build where clause for action insert statement.

Parameters

array $data

Row data for action.

string $table_name

Action table name.

bool $unique

Where action should be unique.

Returns

string —

Where clause to be used with insert.

get_placeholder_for_column()

get_placeholder_for_column(string  $column_name) : string

Helper method to get $wpdb->prepare placeholder for a given column name.

Parameters

string $column_name

Name of column in actions table.

Returns

string —

Placeholder to use for given column.