WooCommerce Code Reference

WC_CLI_REST_Command
in package

Main Command for WooCommere CLI.

Since a lot of WC operations can be handled via the REST API, we base our CLI off of Restful to generate commands for each WooCommerce REST API endpoint so most of the logic is shared.

Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT). https://github.com/wp-cli/restful

Tags
version
3.0.0

Table of Contents

$routes_with_parent_id  : array<string|int, mixed>
Endpoints that have a parent ID.
$name  : string
Name of command/endpoint object.
$resource_identifier  : int
Main resource ID.
$route  : string
Endpoint route.
$schema  : array<string|int, mixed>
Schema for command.
$supported_ids  : array<string|int, mixed>
List of supported IDs and their description (name => desc).
__construct()  : mixed
Sets up REST Command.
create_item()  : mixed
Create a new item.
delete_item()  : mixed
Delete an existing item.
get_item()  : mixed
Get a single item.
get_supported_ids()  : array<string|int, mixed>
Returns an ID of supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.
list_items()  : mixed
List all items.
set_supported_ids()  : mixed
Passes supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.
update_item()  : mixed
Update an existing item.
decode_json()  : array<string|int, mixed>
JSON can be passed in some more complicated objects, like the payment gateway settings array.
get_formatter()  : Formatter
Get Formatter object based on supplied parameters.
do_request()  : array<string|int, mixed>
Do a REST Request
get_context_fields()  : array<string|int, mixed>
Get a list of fields present in a given context
get_filled_route()  : string
Get the route for this resource
limit_item_to_fields()  : array<string|int, mixed>
Reduce an item to specific fields.

Properties

$routes_with_parent_id

Endpoints that have a parent ID.

protected array<string|int, mixed> $routes_with_parent_id = array('customer_download', 'product_review', 'order_note', 'shop_order_refund')

Ex: Product reviews, which has a product ID and a review ID.

Methods

__construct()

Sets up REST Command.

public __construct(string $name, string $route, array<string|int, mixed> $schema) : mixed
Parameters
$name : string

Name of endpoint object (comes from schema).

$route : string

Path to route of this endpoint.

$schema : array<string|int, mixed>

Schema object.

Return values
mixed

create_item()

Create a new item.

public create_item(array<string|int, mixed> $args, array<string|int, mixed> $assoc_args) : mixed
Parameters
$args : array<string|int, mixed>

WP-CLI positional arguments.

$assoc_args : array<string|int, mixed>

WP-CLI associative arguments.

Tags
subcommand

create

Return values
mixed

delete_item()

Delete an existing item.

public delete_item(array<string|int, mixed> $args, array<string|int, mixed> $assoc_args) : mixed
Parameters
$args : array<string|int, mixed>

WP-CLI positional arguments.

$assoc_args : array<string|int, mixed>

WP-CLI associative arguments.

Tags
subcommand

delete

Return values
mixed

get_item()

Get a single item.

public get_item(array<string|int, mixed> $args, array<string|int, mixed> $assoc_args) : mixed
Parameters
$args : array<string|int, mixed>

WP-CLI positional arguments.

$assoc_args : array<string|int, mixed>

WP-CLI associative arguments.

Tags
subcommand

get

Return values
mixed

get_supported_ids()

Returns an ID of supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.

public get_supported_ids() : array<string|int, mixed>
Return values
array<string|int, mixed>

list_items()

List all items.

public list_items(array<string|int, mixed> $args, array<string|int, mixed> $assoc_args) : mixed
Parameters
$args : array<string|int, mixed>

WP-CLI positional arguments.

$assoc_args : array<string|int, mixed>

WP-CLI associative arguments.

Tags
subcommand

list

Return values
mixed

set_supported_ids()

Passes supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.

public set_supported_ids([array<string|int, mixed> $supported_ids = array() ]) : mixed
Parameters
$supported_ids : array<string|int, mixed> = array()

List of supported IDs.

Return values
mixed

update_item()

Update an existing item.

public update_item(array<string|int, mixed> $args, array<string|int, mixed> $assoc_args) : mixed
Parameters
$args : array<string|int, mixed>

WP-CLI positional arguments.

$assoc_args : array<string|int, mixed>

WP-CLI associative arguments.

Tags
subcommand

update

Return values
mixed

decode_json()

JSON can be passed in some more complicated objects, like the payment gateway settings array.

protected decode_json(array<string|int, mixed> $arr) : array<string|int, mixed>

This function decodes the json (if present) and tries to get it's value.

Parameters
$arr : array<string|int, mixed>

Array that will be scanned for JSON encoded values.

Return values
array<string|int, mixed>

get_formatter()

Get Formatter object based on supplied parameters.

protected get_formatter(array<string|int, mixed> &$assoc_args) : Formatter
Parameters
$assoc_args : array<string|int, mixed>

Parameters passed to command. Determines formatting.

Return values
Formatter

do_request()

Do a REST Request

private do_request(string $method, string $route, array<string|int, mixed> $assoc_args) : array<string|int, mixed>
Parameters
$method : string

Request method. Examples: 'POST', 'PUT', 'DELETE' or 'GET'.

$route : string

Resource route.

$assoc_args : array<string|int, mixed>

Associative arguments passed to the originating WP-CLI command.

Return values
array<string|int, mixed>

get_context_fields()

Get a list of fields present in a given context

private get_context_fields(string $context) : array<string|int, mixed>
Parameters
$context : string

Scope under which the request is made. Determines fields present in response.

Return values
array<string|int, mixed>

get_filled_route()

Get the route for this resource

private get_filled_route([array<string|int, mixed> $args = array() ]) : string
Parameters
$args : array<string|int, mixed> = array()

Positional arguments passed to the originating WP-CLI command.

Return values
string

limit_item_to_fields()

Reduce an item to specific fields.

private static limit_item_to_fields(array<string|int, mixed> $item, array<string|int, mixed> $fields) : array<string|int, mixed>
Parameters
$item : array<string|int, mixed>

Item to reduce.

$fields : array<string|int, mixed>

Fields to keep.

Return values
array<string|int, mixed>