wp/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    33 	 *
    33 	 *
    34 	 * @see register_rest_route()
    34 	 * @see register_rest_route()
    35 	 */
    35 	 */
    36 	public function register_routes() {
    36 	public function register_routes() {
    37 
    37 
    38 		register_rest_route( $this->namespace, '/' . $this->rest_base, array(
    38 		register_rest_route(
       
    39 			$this->namespace,
       
    40 			'/' . $this->rest_base,
    39 			array(
    41 			array(
    40 				'methods'             => WP_REST_Server::READABLE,
    42 				array(
    41 				'callback'            => array( $this, 'get_items' ),
    43 					'methods'             => WP_REST_Server::READABLE,
    42 				'permission_callback' => array( $this, 'get_items_permissions_check' ),
    44 					'callback'            => array( $this, 'get_items' ),
    43 				'args'                => $this->get_collection_params(),
    45 					'permission_callback' => array( $this, 'get_items_permissions_check' ),
    44 			),
    46 					'args'                => $this->get_collection_params(),
    45 			'schema' => array( $this, 'get_public_item_schema' ),
    47 				),
    46 		) );
    48 				'schema' => array( $this, 'get_public_item_schema' ),
    47 
    49 			)
    48 		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<status>[\w-]+)', array(
    50 		);
    49 			'args' => array(
    51 
    50 				'status' => array(
    52 		register_rest_route(
    51 					'description' => __( 'An alphanumeric identifier for the status.' ),
    53 			$this->namespace,
    52 					'type'        => 'string',
    54 			'/' . $this->rest_base . '/(?P<status>[\w-]+)',
    53 				),
       
    54 			),
       
    55 			array(
    55 			array(
    56 				'methods'             => WP_REST_Server::READABLE,
    56 				'args'   => array(
    57 				'callback'            => array( $this, 'get_item' ),
    57 					'status' => array(
    58 				'permission_callback' => array( $this, 'get_item_permissions_check' ),
    58 						'description' => __( 'An alphanumeric identifier for the status.' ),
    59 				'args'                => array(
    59 						'type'        => 'string',
    60 					'context' => $this->get_context_param( array( 'default' => 'view' ) ),
    60 					),
    61 				),
    61 				),
    62 			),
    62 				array(
    63 			'schema' => array( $this, 'get_public_item_schema' ),
    63 					'methods'             => WP_REST_Server::READABLE,
    64 		) );
    64 					'callback'            => array( $this, 'get_item' ),
       
    65 					'permission_callback' => array( $this, 'get_item_permissions_check' ),
       
    66 					'args'                => array(
       
    67 						'context' => $this->get_context_param( array( 'default' => 'view' ) ),
       
    68 					),
       
    69 				),
       
    70 				'schema' => array( $this, 'get_public_item_schema' ),
       
    71 			)
       
    72 		);
    65 	}
    73 	}
    66 
    74 
    67 	/**
    75 	/**
    68 	 * Checks whether a given request has permission to read post statuses.
    76 	 * Checks whether a given request has permission to read post statuses.
    69 	 *
    77 	 *
    79 			foreach ( $types as $type ) {
    87 			foreach ( $types as $type ) {
    80 				if ( current_user_can( $type->cap->edit_posts ) ) {
    88 				if ( current_user_can( $type->cap->edit_posts ) ) {
    81 					return true;
    89 					return true;
    82 				}
    90 				}
    83 			}
    91 			}
    84 			return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to edit posts in this post type.' ), array( 'status' => rest_authorization_required_code() ) );
    92 			return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage post statuses.' ), array( 'status' => rest_authorization_required_code() ) );
    85 		}
    93 		}
    86 
    94 
    87 		return true;
    95 		return true;
    88 	}
    96 	}
    89 
    97 
    94 	 *
   102 	 *
    95 	 * @param WP_REST_Request $request Full details about the request.
   103 	 * @param WP_REST_Request $request Full details about the request.
    96 	 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
   104 	 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
    97 	 */
   105 	 */
    98 	public function get_items( $request ) {
   106 	public function get_items( $request ) {
    99 		$data = array();
   107 		$data              = array();
   100 		$statuses = get_post_stati( array( 'internal' => false ), 'object' );
   108 		$statuses          = get_post_stati( array( 'internal' => false ), 'object' );
   101 		$statuses['trash'] = get_post_status_object( 'trash' );
   109 		$statuses['trash'] = get_post_status_object( 'trash' );
   102 
   110 
   103 		foreach ( $statuses as $slug => $obj ) {
   111 		foreach ( $statuses as $slug => $obj ) {
   104 			$ret = $this->check_read_permission( $obj );
   112 			$ret = $this->check_read_permission( $obj );
   105 
   113 
   106 			if ( ! $ret ) {
   114 			if ( ! $ret ) {
   107 				continue;
   115 				continue;
   108 			}
   116 			}
   109 
   117 
   110 			$status = $this->prepare_item_for_response( $obj, $request );
   118 			$status             = $this->prepare_item_for_response( $obj, $request );
   111 			$data[ $obj->name ] = $this->prepare_response_for_collection( $status );
   119 			$data[ $obj->name ] = $this->prepare_response_for_collection( $status );
   112 		}
   120 		}
   113 
   121 
   114 		return rest_ensure_response( $data );
   122 		return rest_ensure_response( $data );
   115 	}
   123 	}
   225 		if ( in_array( 'slug', $fields, true ) ) {
   233 		if ( in_array( 'slug', $fields, true ) ) {
   226 			$data['slug'] = $status->name;
   234 			$data['slug'] = $status->name;
   227 		}
   235 		}
   228 
   236 
   229 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   237 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   230 		$data = $this->add_additional_fields_to_object( $data, $request );
   238 		$data    = $this->add_additional_fields_to_object( $data, $request );
   231 		$data = $this->filter_response_by_context( $data, $context );
   239 		$data    = $this->filter_response_by_context( $data, $context );
   232 
   240 
   233 		$response = rest_ensure_response( $data );
   241 		$response = rest_ensure_response( $data );
   234 
   242 
   235 		if ( 'publish' === $status->name ) {
   243 		if ( 'publish' === $status->name ) {
   236 			$response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
   244 			$response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
   259 	 *
   267 	 *
   260 	 * @return array Item schema data.
   268 	 * @return array Item schema data.
   261 	 */
   269 	 */
   262 	public function get_item_schema() {
   270 	public function get_item_schema() {
   263 		$schema = array(
   271 		$schema = array(
   264 			'$schema'              => 'http://json-schema.org/draft-04/schema#',
   272 			'$schema'    => 'http://json-schema.org/draft-04/schema#',
   265 			'title'                => 'status',
   273 			'title'      => 'status',
   266 			'type'                 => 'object',
   274 			'type'       => 'object',
   267 			'properties'           => array(
   275 			'properties' => array(
   268 				'name'             => array(
   276 				'name'         => array(
   269 					'description'  => __( 'The title for the status.' ),
   277 					'description' => __( 'The title for the status.' ),
   270 					'type'         => 'string',
   278 					'type'        => 'string',
   271 					'context'      => array( 'embed', 'view', 'edit' ),
   279 					'context'     => array( 'embed', 'view', 'edit' ),
   272 					'readonly'     => true,
   280 					'readonly'    => true,
   273 				),
   281 				),
   274 				'private'          => array(
   282 				'private'      => array(
   275 					'description'  => __( 'Whether posts with this status should be private.' ),
   283 					'description' => __( 'Whether posts with this status should be private.' ),
   276 					'type'         => 'boolean',
   284 					'type'        => 'boolean',
   277 					'context'      => array( 'edit' ),
   285 					'context'     => array( 'edit' ),
   278 					'readonly'     => true,
   286 					'readonly'    => true,
   279 				),
   287 				),
   280 				'protected'        => array(
   288 				'protected'    => array(
   281 					'description'  => __( 'Whether posts with this status should be protected.' ),
   289 					'description' => __( 'Whether posts with this status should be protected.' ),
   282 					'type'         => 'boolean',
   290 					'type'        => 'boolean',
   283 					'context'      => array( 'edit' ),
   291 					'context'     => array( 'edit' ),
   284 					'readonly'     => true,
   292 					'readonly'    => true,
   285 				),
   293 				),
   286 				'public'           => array(
   294 				'public'       => array(
   287 					'description'  => __( 'Whether posts of this status should be shown in the front end of the site.' ),
   295 					'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ),
   288 					'type'         => 'boolean',
   296 					'type'        => 'boolean',
   289 					'context'      => array( 'view', 'edit' ),
   297 					'context'     => array( 'view', 'edit' ),
   290 					'readonly'     => true,
   298 					'readonly'    => true,
   291 				),
   299 				),
   292 				'queryable'        => array(
   300 				'queryable'    => array(
   293 					'description'  => __( 'Whether posts with this status should be publicly-queryable.' ),
   301 					'description' => __( 'Whether posts with this status should be publicly-queryable.' ),
   294 					'type'         => 'boolean',
   302 					'type'        => 'boolean',
   295 					'context'      => array( 'view', 'edit' ),
   303 					'context'     => array( 'view', 'edit' ),
   296 					'readonly'     => true,
   304 					'readonly'    => true,
   297 				),
   305 				),
   298 				'show_in_list'     => array(
   306 				'show_in_list' => array(
   299 					'description'  => __( 'Whether to include posts in the edit listing for their post type.' ),
   307 					'description' => __( 'Whether to include posts in the edit listing for their post type.' ),
   300 					'type'         => 'boolean',
   308 					'type'        => 'boolean',
   301 					'context'      => array( 'edit' ),
   309 					'context'     => array( 'edit' ),
   302 					'readonly'     => true,
   310 					'readonly'    => true,
   303 				),
   311 				),
   304 				'slug'             => array(
   312 				'slug'         => array(
   305 					'description'  => __( 'An alphanumeric identifier for the status.' ),
   313 					'description' => __( 'An alphanumeric identifier for the status.' ),
   306 					'type'         => 'string',
   314 					'type'        => 'string',
   307 					'context'      => array( 'embed', 'view', 'edit' ),
   315 					'context'     => array( 'embed', 'view', 'edit' ),
   308 					'readonly'     => true,
   316 					'readonly'    => true,
   309 				),
   317 				),
   310 			),
   318 			),
   311 		);
   319 		);
   312 
   320 
   313 		return $this->add_additional_fields_schema( $schema );
   321 		return $this->add_additional_fields_schema( $schema );