PHP Class WP_REST_Posts_Controller, wordpress

See also: WP_REST_Controller
Since: 4.7.0
Inheritance: extends WP_REST_Controller
Mostrar archivo Open project: johnpbloch/wordpress Class Usage Examples

Protected Properties

Property Type Description
$meta WP_REST_Post_Meta_Fields Instance of a post meta fields object.
$post_type string Post type.

Public Methods

Method Description
__construct ( string $post_type ) Constructor.
can_access_password_content ( WP_Post $post, WP_REST_Request $request ) : boolean Checks if the user can access password-protected content.
check_read_permission ( object $post ) : boolean Checks if a post can be read.
create_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error Creates a single post.
create_item_permissions_check ( WP_REST_Request $request ) : true | WP_Error Checks if a given request has access to create a post.
delete_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error Deletes a single post.
delete_item_permissions_check ( WP_REST_Request $request ) : true | WP_Error Checks if a given request has access to delete a post.
get_collection_params ( ) : array Retrieves the query params for the posts collection.
get_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error Retrieves a single post.
get_item_permissions_check ( WP_REST_Request $request ) : boolean | WP_Error Checks if a given request has access to read a post.
get_item_schema ( ) : array Retrieves the post's schema, conforming to JSON Schema.
get_items ( WP_REST_Request $request ) : WP_REST_Response | WP_Error Retrieves a collection of posts.
get_items_permissions_check ( WP_REST_Request $request ) : true | WP_Error Checks if a given request has access to read posts.
handle_template ( string $template, integer $post_id ) Sets the template for a post.
prepare_item_for_response ( WP_Post $post, WP_REST_Request $request ) : WP_REST_Response Prepares a single post output for response.
protected_title_format ( ) : string Overwrites the default protected title format.
register_routes ( ) Registers the routes for the objects of the controller.
sanitize_post_statuses ( string | array $statuses, WP_REST_Request $request, string $parameter ) : array | WP_Error Sanitizes and validates the list of post statuses, including whether the user can query private statuses.
update_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error Updates a single post.
update_item_permissions_check ( WP_REST_Request $request ) : true | WP_Error Checks if a given request has access to update a post.

Protected Methods

Method Description
check_assign_terms_permission ( WP_REST_Request $request ) : boolean Checks whether current user can assign all terms sent with the current request.
check_create_permission ( object $post ) : boolean Checks if a post can be created.
check_delete_permission ( object $post ) : boolean Checks if a post can be deleted.
check_is_post_type_allowed ( object | string $post_type ) : boolean Checks if a given post type can be viewed or managed.
check_update_permission ( object $post ) : boolean Checks if a post can be edited.
handle_featured_media ( integer $featured_media, integer $post_id ) : boolean | WP_Error Determines the featured media based on a request param.
handle_status_param ( string $post_status, object $post_type ) : string | WP_Error Determines validity and normalizes the given status parameter.
handle_terms ( integer $post_id, WP_REST_Request $request ) : null | WP_Error Updates the post's terms from a REST request.
prepare_date_response ( string $date_gmt, string | null $date = null ) : string | null Checks the post_date_gmt or modified_gmt and prepare any post or modified date for single post output.
prepare_item_for_database ( WP_REST_Request $request ) : stdClass | WP_Error Prepares a single post for create or update.
prepare_items_query ( array $prepared_args = [], WP_REST_Request $request = null ) : array Determines the allowed query_vars for a get_items() response and prepares them for WP_Query.
prepare_links ( WP_Post $post ) : array Prepares links for the request.

Method Details

__construct() public method

Constructor.
Since: 4.7.0
public __construct ( string $post_type )
$post_type string Post type.

can_access_password_content() public method

This method determines whether we need to override the regular password check in core with a filter.
Since: 4.7.0
public can_access_password_content ( WP_Post $post, WP_REST_Request $request ) : boolean
$post WP_Post Post to check against.
$request WP_REST_Request Request data to check.
return boolean True if the user can access password-protected content, otherwise false.

check_assign_terms_permission() protected method

Checks whether current user can assign all terms sent with the current request.
Since: 4.7.0
protected check_assign_terms_permission ( WP_REST_Request $request ) : boolean
$request WP_REST_Request The request object with post and terms data.
return boolean Whether the current user can assign the provided terms.

check_create_permission() protected method

Checks if a post can be created.
Since: 4.7.0
protected check_create_permission ( object $post ) : boolean
$post object Post object.
return boolean Whether the post can be created.

check_delete_permission() protected method

Checks if a post can be deleted.
Since: 4.7.0
protected check_delete_permission ( object $post ) : boolean
$post object Post object.
return boolean Whether the post can be deleted.

check_is_post_type_allowed() protected method

Checks if a given post type can be viewed or managed.
Since: 4.7.0
protected check_is_post_type_allowed ( object | string $post_type ) : boolean
$post_type object | string Post type name or object.
return boolean Whether the post type is allowed in REST.

check_read_permission() public method

Correctly handles posts with the inherit status.
Since: 4.7.0
public check_read_permission ( object $post ) : boolean
$post object Post object.
return boolean Whether the post can be read.

check_update_permission() protected method

Checks if a post can be edited.
Since: 4.7.0
protected check_update_permission ( object $post ) : boolean
$post object Post object.
return boolean Whether the post can be edited.

create_item() public method

Creates a single post.
Since: 4.7.0
public create_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
return WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.

create_item_permissions_check() public method

Checks if a given request has access to create a post.
Since: 4.7.0
public create_item_permissions_check ( WP_REST_Request $request ) : true | WP_Error
$request WP_REST_Request Full details about the request.
return true | WP_Error True if the request has access to create items, WP_Error object otherwise.

delete_item() public method

Deletes a single post.
Since: 4.7.0
public delete_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
return WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.

delete_item_permissions_check() public method

Checks if a given request has access to delete a post.
Since: 4.7.0
public delete_item_permissions_check ( WP_REST_Request $request ) : true | WP_Error
$request WP_REST_Request Full details about the request.
return true | WP_Error True if the request has access to delete the item, WP_Error object otherwise.

get_collection_params() public method

Retrieves the query params for the posts collection.
Since: 4.7.0
public get_collection_params ( ) : array
return array Collection parameters.

get_item() public method

Retrieves a single post.
Since: 4.7.0
public get_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
return WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.

get_item_permissions_check() public method

Checks if a given request has access to read a post.
Since: 4.7.0
public get_item_permissions_check ( WP_REST_Request $request ) : boolean | WP_Error
$request WP_REST_Request Full details about the request.
return boolean | WP_Error True if the request has read access for the item, WP_Error object otherwise.

get_item_schema() public method

Retrieves the post's schema, conforming to JSON Schema.
Since: 4.7.0
public get_item_schema ( ) : array
return array Item schema data.

get_items() public method

Retrieves a collection of posts.
Since: 4.7.0
public get_items ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
return WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.

get_items_permissions_check() public method

Checks if a given request has access to read posts.
Since: 4.7.0
public get_items_permissions_check ( WP_REST_Request $request ) : true | WP_Error
$request WP_REST_Request Full details about the request.
return true | WP_Error True if the request has read access, WP_Error object otherwise.

handle_status_param() protected method

Determines validity and normalizes the given status parameter.
Since: 4.7.0
protected handle_status_param ( string $post_status, object $post_type ) : string | WP_Error
$post_status string Post status.
$post_type object Post type.
return string | WP_Error Post status or WP_Error if lacking the proper permission.

handle_template() public method

Sets the template for a post.
Since: 4.7.0
public handle_template ( string $template, integer $post_id )
$template string Page template filename.
$post_id integer Post ID.

handle_terms() protected method

Updates the post's terms from a REST request.
Since: 4.7.0
protected handle_terms ( integer $post_id, WP_REST_Request $request ) : null | WP_Error
$post_id integer The post ID to update the terms form.
$request WP_REST_Request The request object with post and terms data.
return null | WP_Error WP_Error on an error assigning any of the terms, otherwise null.

prepare_date_response() protected method

Checks the post_date_gmt or modified_gmt and prepare any post or modified date for single post output.
Since: 4.7.0
protected prepare_date_response ( string $date_gmt, string | null $date = null ) : string | null
$date_gmt string GMT publication time.
$date string | null Optional. Local publication time. Default null.
return string | null ISO8601/RFC3339 formatted datetime.

prepare_item_for_database() protected method

Prepares a single post for create or update.
Since: 4.7.0
protected prepare_item_for_database ( WP_REST_Request $request ) : stdClass | WP_Error
$request WP_REST_Request Request object.
return stdClass | WP_Error Post object or WP_Error.

prepare_item_for_response() public method

Prepares a single post output for response.
Since: 4.7.0
public prepare_item_for_response ( WP_Post $post, WP_REST_Request $request ) : WP_REST_Response
$post WP_Post Post object.
$request WP_REST_Request Request object.
return WP_REST_Response Response object.

prepare_items_query() protected method

Determines the allowed query_vars for a get_items() response and prepares them for WP_Query.
Since: 4.7.0
protected prepare_items_query ( array $prepared_args = [], WP_REST_Request $request = null ) : array
$prepared_args array Optional. Prepared WP_Query arguments. Default empty array.
$request WP_REST_Request Optional. Full details about the request.
return array Items query arguments.

protected_title_format() public method

By default, WordPress will show password protected posts with a title of "Protected: %s", as the REST API communicates the protected status of a post in a machine readable format, we remove the "Protected: " prefix.
public protected_title_format ( ) : string
return string Protected title format.

register_routes() public method

Registers the routes for the objects of the controller.
See also: register_rest_route()
Since: 4.7.0
public register_routes ( )

sanitize_post_statuses() public method

Sanitizes and validates the list of post statuses, including whether the user can query private statuses.
Since: 4.7.0
public sanitize_post_statuses ( string | array $statuses, WP_REST_Request $request, string $parameter ) : array | WP_Error
$statuses string | array One or more post statuses.
$request WP_REST_Request Full details about the request.
$parameter string Additional parameter to pass to validation.
return array | WP_Error A list of valid statuses, otherwise WP_Error object.

update_item() public method

Updates a single post.
Since: 4.7.0
public update_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
return WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.

update_item_permissions_check() public method

Checks if a given request has access to update a post.
Since: 4.7.0
public update_item_permissions_check ( WP_REST_Request $request ) : true | WP_Error
$request WP_REST_Request Full details about the request.
return true | WP_Error True if the request has access to update the item, WP_Error object otherwise.

Property Details

$meta protected_oe property

Instance of a post meta fields object.
Since: 4.7.0
protected WP_REST_Post_Meta_Fields $meta
return WP_REST_Post_Meta_Fields

$post_type protected_oe property

Post type.
Since: 4.7.0
protected string $post_type
return string