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'

POST_TYPE

POST_TYPE = 'scheduled-action'

GROUP_TAXONOMY

GROUP_TAXONOMY = 'action-group'

SCHEDULE_META_KEY

SCHEDULE_META_KEY = '_action_manager_schedule'

DEPENDENCIES_MET

DEPENDENCIES_MET = 'as-post-store-dependencies-met'

Properties

$max_args_length

$max_args_length : int

Maximum length of args.

Type

normal

$local_timezone

$local_timezone : \DateTimeZone

Local Timezone.

Type

\DateTimeZone

$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 action.

Parameters

\ActionScheduler_Action $action

Scheduled Action.

\DateTime|null $scheduled_date

Scheduled Date.

Throws

\RuntimeException

Throws an exception if the action could not be saved.

Returns

int

fetch_action()

fetch_action(int  $action_id) : object

Fetch actions.

Parameters

int $action_id

Action ID.

Returns

object

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

extra_action_counts()

extra_action_counts() : array

Get additional action counts.

  • add past-due actions

Returns

array

cancel_action()

cancel_action(int  $action_id) : mixed

Cancel action.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

If $action_id is not identified.

Returns

mixed

delete_action()

delete_action(int  $action_id) : void

Delete action.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

If action is not identified.

get_date()

get_date(int  $action_id) : \ActionScheduler_DateTime

Get date for claim id.

Parameters

int $action_id

Action ID.

Returns

\ActionScheduler_DateTime —

The date the action is schedule 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 claim.

Parameters

int $max_actions

Maximum number of actions.

\DateTime|null $before_date

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

array $hooks

Claim only actions with a hook or hooks.

string $group

Claim only actions in the given group.

Throws

\RuntimeException

When there is an error staking a claim.

\InvalidArgumentException

When the given group is not valid.

Returns

\ActionScheduler_ActionClaim

get_claim_count()

get_claim_count() : int

Get claim count.

Returns

int

release_claim()

release_claim(\ActionScheduler_ActionClaim  $claim) : void

Release claim.

Parameters

\ActionScheduler_ActionClaim $claim

Claim object to release.

Throws

\RuntimeException

When the claim is not unlocked.

unclaim_action()

unclaim_action(string  $action_id) : mixed

Unclaim action.

Parameters

string $action_id

Action ID.

Throws

\RuntimeException

When unable to unlock claim on action ID.

Returns

mixed

mark_failure()

mark_failure(int  $action_id) : void

Mark failure on action.

Parameters

int $action_id

Action ID.

Throws

\RuntimeException

When unable to mark failure on action ID.

log_execution()

log_execution(string  $action_id) : mixed

Log Execution.

Parameters

string $action_id

Action ID.

Throws

\Exception

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

Returns

mixed

mark_complete()

mark_complete(string  $action_id) : mixed

Record that an action was completed.

Parameters

string $action_id

ID of the completed action.

Throws

\InvalidArgumentException

When the action ID is invalid.

\RuntimeException

When there was an error executing the action.

Returns

mixed

get_status()

get_status(int  $action_id) : mixed

Return an action's status, as stored in the post status column

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

When the action ID is invalid.

Returns

mixed

get_claim_id()

get_claim_id(int  $action_id) : mixed

Return an action's claim ID, as stored in the post password column

Parameters

int $action_id

Action ID.

Returns

mixed

find_actions_by_claim_id()

find_actions_by_claim_id(string  $claim_id) : array

Find actions by claim ID.

Parameters

string $claim_id

Claim ID.

Returns

array

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

(@codeCoverageIgnore)

Returns

mixed

mark_migrated()

mark_migrated(int  $action_id) : mixed

Mark action as migrated when there is an error deleting the action.

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

filter_insert_post_data()

filter_insert_post_data(array  $postdata) : array

Filter insert post data.

Parameters

array $postdata

Post data to filter.

Returns

array

set_unique_post_slug()

set_unique_post_slug(string  $override_slug, string  $slug, int  $post_ID, string  $post_status, string  $post_type) : string

Create a (probably unique) post name for scheduled actions in a more performant manner than wp_unique_post_slug().

When an action's post status is transitioned to something other than 'draft', 'pending' or 'auto-draft, like 'publish' or 'failed' or 'trash', WordPress will find a unique slug (stored in post_name column) using the wp_unique_post_slug() function. This is done to ensure URL uniqueness. The approach taken by wp_unique_post_slug() is to iterate over existing post_name values that match, and append a number 1 greater than the largest. This makes sense when manually creating a post from the Edit Post screen. It becomes a bottleneck when automatically processing thousands of actions, with a database containing thousands of related post_name values.

WordPress 5.1 introduces the 'pre_wp_unique_post_slug' filter for plugins to address this issue.

We can short-circuit WordPress's wp_unique_post_slug() approach using the 'pre_wp_unique_post_slug' filter. This method is available to be used as a callback on that filter. It provides a more scalable approach to generating a post_name/slug that is probably unique. Because Action Scheduler never actually uses the post_name field, or an action's slug, being probably unique is good enough.

For more backstory on this issue, see:

Parameters

string $override_slug

Short-circuit return value.

string $slug

The desired slug (post_name).

int $post_ID

Post ID.

string $post_status

The post status.

string $post_type

Post type.

Returns

string

get_date_gmt()

get_date_gmt(int  $action_id) : \ActionScheduler_DateTime

Get Date GMT.

Parameters

int $action_id

Action ID.

Throws

\InvalidArgumentException

If $action_id is not identified.

Returns

\ActionScheduler_DateTime —

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

migration_dependencies_met()

migration_dependencies_met(mixed  $setting) : bool

Determine whether the post store can be migrated.

Parameters

mixed $setting

Returns

bool

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, as we prepare to move to custom tables, and can use an indexed VARCHAR column instead, we want to warn developers of this impending requirement.

Parameters

\ActionScheduler_Action $action

Action object.

Returns

mixed

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

create_post_array()

create_post_array(\ActionScheduler_Action  $action, \DateTime|null  $scheduled_date = null) : array

Create post array.

Parameters

\ActionScheduler_Action $action

Scheduled Action.

\DateTime|null $scheduled_date

Scheduled Date.

Returns

array —

Returns an array of post data.

save_post_array()

save_post_array(array  $post_array) : int

Save post array.

Parameters

array $post_array

Post array.

Throws

\RuntimeException

Throws an exception if the action could not be saved.

Returns

int —

Returns the post ID.

save_post_schedule()

save_post_schedule(int  $post_id, string  $schedule) : void

Save post schedule.

Parameters

int $post_id

Post ID of the scheduled action.

string $schedule

Schedule to save.

save_action_group()

save_action_group(int  $post_id, string  $group) : void

Save action group.

Parameters

int $post_id

Post ID.

string $group

Group to save.

get_post()

get_post(string  $action_id) : \WP_Post|null

Get post.

Parameters

string $action_id
  • Action ID.

Returns

\WP_Post|null

get_null_action()

get_null_action() : \ActionScheduler_NullAction

Get NULL action.

Returns

\ActionScheduler_NullAction

make_action_from_post()

make_action_from_post(\WP_Post  $post) : \WP_Post

Make action from post.

Parameters

\WP_Post $post

Post object.

Returns

\WP_Post

get_action_status_by_post_status()

get_action_status_by_post_status(string  $post_status) : string

Get action status by post status.

Parameters

string $post_status

Post status.

Throws

\InvalidArgumentException

Throw InvalidArgumentException if $post_status not in known status fields returned by $this->get_status_labels().

Returns

string

get_post_status_by_action_status()

get_post_status_by_action_status(string  $action_status) : string

Get post status by action status.

Parameters

string $action_status

Action status.

Throws

\InvalidArgumentException

Throws InvalidArgumentException if $post_status not in known status fields returned by $this->get_status_labels().

Returns

string

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
  • Throw InvalidArgumentException if $select_or_count not count or select.

Returns

string —

SQL statement. The returned SQL is already properly escaped.

generate_claim_id()

generate_claim_id() : string

Generate claim id.

Returns

string

claim_actions()

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

Claim actions.

Parameters

string $claim_id

Claim ID.

int $limit

Limit.

\DateTime|null $before_date

Should use UTC timezone.

array $hooks

Claim only actions with a hook or hooks.

string $group

Claim only actions in the given group.

Throws

\RuntimeException

When there is a database error.

Returns

int —

The number of actions that were claimed.

get_actions_by_group()

get_actions_by_group(string  $group, int  $limit, \DateTime  $date) : array

Get IDs of actions within a certain group and up to a certain date/time.

Parameters

string $group

The group to use in finding actions.

int $limit

The number of actions to retrieve.

\DateTime $date

DateTime object representing cutoff time for actions. Actions retrieved will be up to and including this DateTime.

Throws

\InvalidArgumentException

When the group does not exist.

Returns

array —

IDs of actions in the appropriate group and before the appropriate time.

bulk_cancel_actions()

bulk_cancel_actions(int[]  $action_ids) : void

Cancel a set of action IDs.

Parameters

int[] $action_ids

List of action IDs.

get_post_column()

get_post_column(string  $action_id, string  $column_name) : string|null

Get post column

Parameters

string $action_id

Action ID.

string $column_name

Column Name.

Returns

string|null