ExtensibleAbilities
in package
The entry point for extensions in WooCommerce's abilities.
An extension registers a resource inside the woocommerce_extensible_abilities_resources_init
action. It registers a field on a resource, such as product, inside the
woocommerce_extensible_abilities_init action, which fires after. An ability that formats an
object of that resource returns the field values under
extensions.<namespace>.<field>, and lists the fields in its output schema.
An ActionableAbility writes the same extensions input to the fields that
have an update_callback, runs the validators of the resource, and saves
the object one time.
An extension also registers a query filter, which the list ability of the
resource accepts under filters.extensions.<namespace>.<filter>.
The API is experimental while the extensible_abilities feature exists.
Tags
Table of Contents
- FEATURE_ID = 'extensible_abilities'
- QUERY_ARG_KEYS = array('meta_query', 'tax_query', 'include_ids', 'exclude_ids')
- Query arguments that a filter callback can return.
- $fields : array<string|int, mixed>
- Fields keyed by resource, then namespace, then field name.
- $filters : array<string|int, mixed>
- Query filters keyed by resource, then namespace, then filter name.
- $reported : array<string|int, mixed>
- Fields already reported for a value that does not match the schema, keyed by resource, namespace and field.
- $resources : array<string|int, mixed>
- Registered resources, keyed by resource name.
- $validators : array<string|int, mixed>
- Validators keyed by resource.
- add_fields_schema() : array<string, mixed>
- Add the `extensions` property to an object schema when the feature is on.
- add_fields_to_object() : array<string, mixed>
- Add the field values of an object to its formatted output, under `extensions.<namespace>.<field>`. A value is cast to its schema type, so `'12'` becomes `12` for an integer field. A field that throws or returns a value that its schema does not allow is left out. A namespace with no values is left out, and `extensions` is left out when no namespace has values or the feature is off.
- add_query_filters_schema() : array<string, mixed>
- Add the `filters.extensions` property to the input schema of a list ability, when the feature is on and the resource has filters. It does not allow other filters, because a filter that the query ignores returns wrong results.
- get_query_args() : array<string, array<string|int, mixed>>|WP_Error
- Get the query arguments of the filters in the input of a list ability.
- is_enabled() : bool
- Whether the store enabled the experimental feature.
- register_field() : void
- Register a field that the abilities of a resource return under `extensions.<namespace>.<field>`.
- register_query_filter() : void
- Register a filter that the list ability of a resource accepts under `filters.extensions.<namespace>.<filter>`.
- register_resource() : void
- Register a resource that abilities format, so extensions can register fields on it.
- register_validator() : void
- Register a validator that can refuse a change to an object of a resource.
- get_filter_ids() : array<string|int, int>|WP_Error
- The IDs that a filter returned, as integers.
- matches_schema() : bool
- Whether a value matches its field schema. A value other than null that does not match is logged and reported one time for each field.
Constants
FEATURE_ID
public
mixed
FEATURE_ID
= 'extensible_abilities'
QUERY_ARG_KEYS
Query arguments that a filter callback can return.
private
mixed
QUERY_ARG_KEYS
= array('meta_query', 'tax_query', 'include_ids', 'exclude_ids')
Properties
$fields
Fields keyed by resource, then namespace, then field name.
private
static array<string|int, mixed>
$fields
= array()
$filters
Query filters keyed by resource, then namespace, then filter name.
private
static array<string|int, mixed>
$filters
= array()
$reported
Fields already reported for a value that does not match the schema, keyed by resource, namespace and field.
private
static array<string|int, mixed>
$reported
= array()
$resources
Registered resources, keyed by resource name.
private
static array<string|int, mixed>
$resources
= array()
$validators
Validators keyed by resource.
private
static array<string|int, mixed>
$validators
= array()
Methods
add_fields_schema()
Add the `extensions` property to an object schema when the feature is on.
public
static add_fields_schema(array<string, mixed> $schema, string $resource_name) : array<string, mixed>
It lists each namespace with its fields. Call it after the
woocommerce_extensible_abilities_init action, when the ability builds its
schema. The schema stays unchanged when no field is registered for the resource.
Parameters
- $schema : array<string, mixed>
-
Object schema.
- $resource_name : string
-
Resource.
Tags
Return values
array<string, mixed> —add_fields_to_object()
Add the field values of an object to its formatted output, under `extensions.<namespace>.<field>`. A value is cast to its schema type, so `'12'` becomes `12` for an integer field. A field that throws or returns a value that its schema does not allow is left out. A namespace with no values is left out, and `extensions` is left out when no namespace has values or the feature is off.
public
static add_fields_to_object(array<string, mixed> $output, string $resource_name, object $subject) : array<string, mixed>
Parameters
- $output : array<string, mixed>
-
Formatted object.
- $resource_name : string
-
Resource.
- $subject : object
-
Object to read.
Tags
Return values
array<string, mixed> —add_query_filters_schema()
Add the `filters.extensions` property to the input schema of a list ability, when the feature is on and the resource has filters. It does not allow other filters, because a filter that the query ignores returns wrong results.
public
static add_query_filters_schema(array<string, mixed> $schema, string $resource_name) : array<string, mixed>
Parameters
- $schema : array<string, mixed>
-
Input schema.
- $resource_name : string
-
Resource.
Tags
Return values
array<string, mixed> —get_query_args()
Get the query arguments of the filters in the input of a list ability.
public
static get_query_args(string $resource_name, array<string, mixed> $input) : array<string, array<string|int, mixed>>|WP_Error
Each key holds one clause for each filter, so array_merge_recursive()
adds them to WP_Query or wc_get_orders() arguments with AND. post__in
holds the IDs that every include_ids filter returned, without the IDs
that any exclude_ids filter returned, or 0 when none remain, because
an empty post__in does not filter. Without include_ids, the excluded
IDs go to post__not_in, which WP_Query ignores next to post__in.
Parameters
- $resource_name : string
-
Resource.
- $input : array<string, mixed>
-
Ability input.
Tags
Return values
array<string, array<string|int, mixed>>|WP_Error — The error of a value that fails its schema, or of a filter that refused its value or failed.is_enabled()
Whether the store enabled the experimental feature.
public
static is_enabled() : bool
Tags
Return values
bool —register_field()
Register a field that the abilities of a resource return under `extensions.<namespace>.<field>`.
public
static register_field(array<string|int, mixed> $args) : void
Call it inside the woocommerce_extensible_abilities_init action. A call after that action is refused.
The resource must be registered with register_resource() in the earlier woocommerce_extensible_abilities_resources_init action.
Parameters
- $args : array<string|int, mixed>
-
{ Field arguments.
@type string $resource Resource that an ability formats, for example `product`, `order` or `order_item`. @type string $namespace Slug of the extension that owns the field, such as `subscriptions`. @type string $field Field name inside the namespace, such as `trial_length`. @type array $schema JSON schema of the value. It needs a `type`. Its `title` is the label clients show. It cannot set `required`, because a value that fails its schema is left out. For a field that is always present, allow null and return null. @type callable $get_callback Receives the object and returns the value. A value that the schema does not allow is left out, so return null when the object has no value. A schema that allows null, such as `array( 'integer', 'null' )`, keeps it. It reads the object it is given, not the database, because an ability can format an object before it is saved. @type callable $update_callback Optional. Receives the object and the value, and changes the object in memory. It returns a WP_Error to reject the value, and then nothing is saved. A field without it cannot be written. Rules: 1. It only sets values in memory. It does not save, send email or make HTTP requests. 2. It receives the value already cast to the field schema. 3. A null value deletes the value of the field.}
Tags
Return values
void —register_query_filter()
Register a filter that the list ability of a resource accepts under `filters.extensions.<namespace>.<filter>`.
public
static register_query_filter(array<string|int, mixed> $args) : void
Call it inside the woocommerce_extensible_abilities_init action. A call after that action is refused.
The resource must be registered with register_resource().
Parameters
- $args : array<string|int, mixed>
-
{ Filter arguments.
@type string $resource Resource whose list ability applies the filters. Core's `products-query` and `orders-query` apply the filters of `product` and `order`. The list ability of another resource must call `get_query_args()` and `add_query_filters_schema()` itself. @type string $namespace Slug of the extension that owns the filter, such as `subscriptions`. @type string $filter Filter name inside the namespace, such as `subscribable`. @type array $schema JSON schema of the value. Its `description` tells agents how to use the filter. @type callable $callback Receives the value, sanitized with the schema, and returns query arguments: a `meta_query`, for products also a `tax_query`, `include_ids` or `exclude_ids`. Core adds each one to the query with AND. Return a WP_Error to refuse the value. A `tax_query` is accepted only for `product`. Any other key, or a `tax_query` for another resource, fails the call. `include_ids` and `exclude_ids` are lists of object IDs. Use them for data that is not meta, such as order items or a table of the extension: query that data and return the IDs that match. Core keeps the IDs that every `include_ids` filter returns, then drops the IDs that any `exclude_ids` filter returns. An empty `include_ids` matches nothing.}
Tags
Return values
void —register_resource()
Register a resource that abilities format, so extensions can register fields on it.
public
static register_resource(string $resource_name) : void
Call it inside the woocommerce_extensible_abilities_resources_init action. A call after that action is refused.
Core registers product, order and order_item there. An
extension registers a resource that its own abilities format, and then calls add_fields_to_object()
and add_fields_schema() from those abilities, exactly as Core's abilities do.
Registering the same resource again does nothing.
Parameters
- $resource_name : string
-
Slug of the resource, such as
ticket.
Tags
Return values
void —register_validator()
Register a validator that can refuse a change to an object of a resource.
public
static register_validator(array<string|int, mixed> $args) : void
Call it inside the woocommerce_extensible_abilities_init action. Validators
run only when the extensible_abilities feature is on.
Use a field update_callback to refuse a bad value of one field. It runs
only when the input sends that field. Use a validator to refuse a change as
a whole. It runs on every change of the resource, also when the input sends
no extension fields. For example, a validator refuses a product change that
would break a subscription plan.
Core's product abilities run validators. Core's order abilities keep their
trunk write for now, so validators for order run only in the
ActionableAbility classes of extensions.
Parameters
- $args : array<string|int, mixed>
-
{ Validator arguments.
@type string $resource Resource that an ActionableAbility changes, such as `product`. @type string $namespace Slug of the extension that owns the validator, such as `subscriptions`. @type callable $callback Receives the changed object and the ability, after Core's changes and the field callbacks, before the one save. It returns a WP_Error to refuse the change, and then nothing is saved. The error gets the namespace in its data, and the 400 status when it has no status. Its message can name the ability or screen to use instead. Rules: 1. It only reads. It does not change the object, save, send email or make HTTP requests. 2. It checks the ability name when the extension's own abilities change the same resource.}
Tags
Return values
void —get_filter_ids()
The IDs that a filter returned, as integers.
private
static get_filter_ids(array<string|int, mixed> $ids, string $filter) : array<string|int, int>|WP_Error
Parameters
- $ids : array<string|int, mixed>
-
IDs from the filter callback.
- $filter : string
-
Filter name, such as
subscriptions.related_to.
Return values
array<string|int, int>|WP_Error — An error when an ID is not a positive integer.matches_schema()
Whether a value matches its field schema. A value other than null that does not match is logged and reported one time for each field.
private
static matches_schema(array<string, mixed> $args, mixed $value) : bool
Parameters
- $args : array<string, mixed>
-
Field arguments.
- $value : mixed
-
Value that the get_callback returned.
