On this page
WP_REST_Posts_Controller::handle_status_param( string $post_status, WP_Post_Type $post_type ): string|WP_Error
Determines validity and normalizes the given status parameter.
Parameters
$post_statusstring Required-
Post status.
$post_typeWP_Post_Type Required-
Post type.
Return
string|WP_Error Post status or WP_Error if lacking the proper permission.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php. View all references
protected function handle_status_param( $post_status, $post_type ) {
switch ( $post_status ) {
case 'draft':
case 'pending':
break;
case 'private':
if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
return new WP_Error(
'rest_cannot_publish',
__( 'Sorry, you are not allowed to create private posts in this post type.' ),
array( 'status' => rest_authorization_required_code() )
);
}
break;
case 'publish':
case 'future':
if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
return new WP_Error(
'rest_cannot_publish',
__( 'Sorry, you are not allowed to publish posts in this post type.' ),
array( 'status' => rest_authorization_required_code() )
);
}
break;
default:
if ( ! get_post_status_object( $post_status ) ) {
$post_status = 'draft';
}
break;
}
return $post_status;
}
Related
Uses
| Uses | Description |
|---|---|
| get_post_status_object() wp-includes/post.php | Retrieves a post status object by name. |
| rest_authorization_required_code() wp-includes/rest-api.php | Returns a contextual HTTP error code for authorization failure. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Posts_Controller::prepare_item_for_database() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post for create or update. |
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/handle_status_param