PHP Class WP_REST_Controller

Show file Open project: woocommerce/woocommerce Class Usage Examples

Protected Properties

Property Type Description
$namespace string The namespace of this controller's route.
$rest_base string The base of this controller's route.

Public Methods

Method Description
create_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response Create one item from the collection.
create_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean Check if a given request has access to create items.
delete_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response Delete one item from the collection.
delete_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean Check if a given request has access to delete a specific item.
filter_response_by_context ( array $data, string $context ) : array Filter a response based on the context defined in the schema.
get_collection_params ( ) : array Get the query params for collections.
get_context_param ( array $args = [] ) : array Get the magical context param.
get_endpoint_args_for_item_schema ( string $method = WP_REST_Server::CREATABLE ) : array Get an array of endpoint arguments from the item schema for the controller.
get_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response Get one item from the collection.
get_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean Check if a given request has access to get a specific item.
get_item_schema ( ) : array Get the item's schema, conforming to JSON Schema.
get_items ( WP_REST_Request $request ) : WP_Error | WP_REST_Response Get a collection of items.
get_items_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean Check if a given request has access to get items.
get_public_item_schema ( ) : array Get the item's schema for display / public consumption purposes.
prepare_item_for_response ( mixed $item, WP_REST_Request $request ) : WP_REST_Response Prepare the item for the REST response.
prepare_response_for_collection ( WP_REST_Response $response ) : array Prepare a response for inserting into a collection.
register_routes ( ) Register the routes for the objects of the controller.
update_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response Update one item from the collection.
update_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean Check if a given request has access to update a specific item.

Protected Methods

Method Description
add_additional_fields_schema ( array $schema ) : array Add the schema from additional fields to an schema array.
add_additional_fields_to_object ( array $object, WP_REST_Request $request ) : array Add the values from additional fields to a data object.
get_additional_fields ( string $object_type = null ) : array Get all the registered additional fields for a given object-type.
get_object_type ( ) : string Get the object type this controller is responsible for managing.
prepare_item_for_database ( WP_REST_Request $request ) : WP_Error | object Prepare the item for create or update operation.
update_additional_fields_for_object ( array $object, WP_REST_Request $request ) Update the values of additional fields added to a data object.

Method Details

add_additional_fields_schema() protected method

The type of object is inferred from the passed schema.
protected add_additional_fields_schema ( array $schema ) : array
$schema array Schema array.
return array $schema Schema array.

add_additional_fields_to_object() protected method

Add the values from additional fields to a data object.
protected add_additional_fields_to_object ( array $object, WP_REST_Request $request ) : array
$object array
$request WP_REST_Request
return array modified object with additional fields.

create_item() public method

Create one item from the collection.
public create_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response
$request WP_REST_Request Full data about the request.
return WP_Error | WP_REST_Response

create_item_permissions_check() public method

Check if a given request has access to create items.
public create_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean
$request WP_REST_Request Full data about the request.
return WP_Error | boolean

delete_item() public method

Delete one item from the collection.
public delete_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response
$request WP_REST_Request Full data about the request.
return WP_Error | WP_REST_Response

delete_item_permissions_check() public method

Check if a given request has access to delete a specific item.
public delete_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean
$request WP_REST_Request Full data about the request.
return WP_Error | boolean

filter_response_by_context() public method

Filter a response based on the context defined in the schema.
public filter_response_by_context ( array $data, string $context ) : array
$data array
$context string
return array

get_additional_fields() protected method

Get all the registered additional fields for a given object-type.
protected get_additional_fields ( string $object_type = null ) : array
$object_type string
return array

get_collection_params() public method

Get the query params for collections.
public get_collection_params ( ) : array
return array

get_context_param() public method

Ensures consistent description between endpoints, and populates enum from schema.
public get_context_param ( array $args = [] ) : array
$args array
return array

get_endpoint_args_for_item_schema() public method

Get an array of endpoint arguments from the item schema for the controller.
public get_endpoint_args_for_item_schema ( string $method = WP_REST_Server::CREATABLE ) : array
$method string HTTP method of the request. The arguments for `CREATABLE` requests are checked for required values and may fall-back to a given default, this is not done on `EDITABLE` requests. Default is WP_REST_Server::CREATABLE.
return array $endpoint_args

get_item() public method

Get one item from the collection.
public get_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response
$request WP_REST_Request Full data about the request.
return WP_Error | WP_REST_Response

get_item_permissions_check() public method

Check if a given request has access to get a specific item.
public get_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean
$request WP_REST_Request Full data about the request.
return WP_Error | boolean

get_item_schema() public method

Get the item's schema, conforming to JSON Schema.
public get_item_schema ( ) : array
return array

get_items() public method

Get a collection of items.
public get_items ( WP_REST_Request $request ) : WP_Error | WP_REST_Response
$request WP_REST_Request Full data about the request.
return WP_Error | WP_REST_Response

get_items_permissions_check() public method

Check if a given request has access to get items.
public get_items_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean
$request WP_REST_Request Full data about the request.
return WP_Error | boolean

get_object_type() protected method

Get the object type this controller is responsible for managing.
protected get_object_type ( ) : string
return string

get_public_item_schema() public method

Get the item's schema for display / public consumption purposes.
public get_public_item_schema ( ) : array
return array

prepare_item_for_database() protected method

Prepare the item for create or update operation.
protected prepare_item_for_database ( WP_REST_Request $request ) : WP_Error | object
$request WP_REST_Request Request object.
return WP_Error | object $prepared_item

prepare_item_for_response() public method

Prepare the item for the REST response.
public prepare_item_for_response ( mixed $item, WP_REST_Request $request ) : WP_REST_Response
$item mixed WordPress representation of the item.
$request WP_REST_Request Request object.
return WP_REST_Response $response

prepare_response_for_collection() public method

Prepare a response for inserting into a collection.
public prepare_response_for_collection ( WP_REST_Response $response ) : array
$response WP_REST_Response Response object.
return array Response data, ready for insertion into collection data.

register_routes() public method

Register the routes for the objects of the controller.
public register_routes ( )

update_additional_fields_for_object() protected method

Update the values of additional fields added to a data object.
protected update_additional_fields_for_object ( array $object, WP_REST_Request $request )
$object array
$request WP_REST_Request

update_item() public method

Update one item from the collection.
public update_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response
$request WP_REST_Request Full data about the request.
return WP_Error | WP_REST_Response

update_item_permissions_check() public method

Check if a given request has access to update a specific item.
public update_item_permissions_check ( WP_REST_Request $request ) : WP_Error | boolean
$request WP_REST_Request Full data about the request.
return WP_Error | boolean

Property Details

$namespace protected property

The namespace of this controller's route.
protected string $namespace
return string

$rest_base protected property

The base of this controller's route.
protected string $rest_base
return string