WooCommerce Code Reference

ActionableAbility extends WP_Ability
in package

An ability that changes one object and saves it one time. Register it with a subclass as the `ability_class`. The execute callback loads the object, checks the `extensions` input against the field schemas, changes the object in memory, writes the fields with their `update_callback`, runs the validators that ExtensibleAbilities::register_validator() adds, and then saves it. A rejection from any step saves nothing.

The input schema gets the extensions property of the resource, so the ability accepts the fields with no extra code.

With the feature off, the input schema gets no extensions property and the input is ignored. An ability whose own input schema sets additionalProperties to false rejects it instead.

Only save() saves. The other steps must not save, send email or make HTTP requests.

The API is experimental while the extensible_abilities feature exists.

Tags
since
11.3.0

Table of Contents

change()  : null|WP_Error
Change the object in memory.
get_resource()  : string
Resource that the ability changes, for example `product` or `order`. The fields of this resource are written.
load()  : WC_Data|WP_Error
Load the object to change.
prepare_response()  : mixed
The ability output for the saved object. It must match the output schema of the ability.
save()  : null|WP_Error
Save the changed object. The default calls WC_Data::save(). When the save throws or leaves the object without an ID, it logs the error and returns a WP_Error with a fixed message, so the exception text stays out of the response.
prepare_properties()  : array<string|int, mixed>
Use the change as the execute callback, and add the `extensions` input when the feature is on.
add_extensions_input_schema()  : array<string, mixed>
Add the `extensions` property one time, at the top level of the input schema. It lists the fields that have an update_callback, and allows null for each one. Each `oneOf`, `anyOf` and `allOf` branch only accepts the key, so a branch with `additionalProperties` set to false does not reject it, and the schema does not grow with the number of branches.
apply_fields()  : WP_Error|null
Write the checked values to the object in memory with the update_callback of each field.
prepare_fields()  : array<string|int, mixed>|WP_Error
Check `extensions.<namespace>.<field>` input values against the schema of each field, and cast them. No callback runs here. A null value is kept as is, to delete the value.
run_callback()  : WP_Error|null
Run an extension callback. A WP_Error that it returns, or an exception that it throws, becomes an error with the given data. An exception is logged and becomes a 500 with a fixed message, so its text never reaches the client.
save_failed()  : WP_Error
The error for a save that failed.
validate()  : WP_Error|null
Run the validators of the resource on the changed object.
with_data()  : WP_Error
Add data to an error, and the 400 status when it has none.

Methods

change()

Change the object in memory.

public abstract change(WC_Data $subject, array<string|int, mixed> $input) : null|WP_Error
Parameters
$subject : WC_Data

Object to change.

$input : array<string|int, mixed>

Ability input.

Tags
since
11.3.0
Return values
null|WP_Error — A WP_Error rejects the change, and nothing is saved.

get_resource()

Resource that the ability changes, for example `product` or `order`. The fields of this resource are written.

public abstract get_resource() : string
Tags
since
11.3.0
Return values
string —

load()

Load the object to change.

public abstract load(array<string|int, mixed> $input) : WC_Data|WP_Error
Parameters
$input : array<string|int, mixed>

Ability input.

Tags
since
11.3.0
Return values
WC_Data|WP_Error —

prepare_response()

The ability output for the saved object. It must match the output schema of the ability.

public abstract prepare_response(WC_Data $subject) : mixed
Parameters
$subject : WC_Data

Saved object.

Tags
since
11.3.0
Return values
mixed —

save()

Save the changed object. The default calls WC_Data::save(). When the save throws or leaves the object without an ID, it logs the error and returns a WP_Error with a fixed message, so the exception text stays out of the response.

public save(WC_Data $subject) : null|WP_Error
Parameters
$subject : WC_Data

Changed object.

Tags
since
11.3.0
Return values
null|WP_Error — Null when the object was saved, a WP_Error when it was not.

prepare_properties()

Use the change as the execute callback, and add the `extensions` input when the feature is on.

protected prepare_properties(array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

Ability arguments.

Return values
array<string|int, mixed> —

add_extensions_input_schema()

Add the `extensions` property one time, at the top level of the input schema. It lists the fields that have an update_callback, and allows null for each one. Each `oneOf`, `anyOf` and `allOf` branch only accepts the key, so a branch with `additionalProperties` set to false does not reject it, and the schema does not grow with the number of branches.

private add_extensions_input_schema(array<string, mixed> $schema) : array<string, mixed>
Parameters
$schema : array<string, mixed>

Input schema.

Return values
array<string, mixed> —

apply_fields()

Write the checked values to the object in memory with the update_callback of each field.

private apply_fields(WC_Data $subject, array<string|int, mixed> $updates) : WP_Error|null
Parameters
$subject : WC_Data

Object to change.

$updates : array<string|int, mixed>

Pairs of update_callback and value from prepare_fields().

Return values
WP_Error|null — A WP_Error when a callback rejects a value or throws.

prepare_fields()

Check `extensions.<namespace>.<field>` input values against the schema of each field, and cast them. No callback runs here. A null value is kept as is, to delete the value.

private prepare_fields(array<string, mixed> $values) : array<string|int, mixed>|WP_Error
Parameters
$values : array<string, mixed>

Values keyed by namespace, then field.

Return values
array<string|int, mixed>|WP_Error — A list of update_callback and value pairs, or a WP_Error when a value is rejected.

run_callback()

Run an extension callback. A WP_Error that it returns, or an exception that it throws, becomes an error with the given data. An exception is logged and becomes a 500 with a fixed message, so its text never reaches the client.

private static run_callback(callable $callback, array<string|int, mixed> $args, array<string, mixed> $data) : WP_Error|null
Parameters
$callback : callable

Field update_callback or validator.

$args : array<string|int, mixed>

Callback arguments.

$data : array<string, mixed>

Data that names the field or the namespace.

Return values
WP_Error|null —

validate()

Run the validators of the resource on the changed object.

private validate(WC_Data $subject) : WP_Error|null
Parameters
$subject : WC_Data

Changed object.

Return values
WP_Error|null — A WP_Error when a validator refuses the change.

with_data()

Add data to an error, and the 400 status when it has none.

private static with_data(WP_Error $error, array<string, mixed> $data) : WP_Error
Parameters
$error : WP_Error

Error.

$data : array<string, mixed>

Data to add.

Return values
WP_Error —