wp/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php
changeset 9 177826044cd9
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
       
     1 <?php
       
     2 /**
       
     3  * Reusable blocks REST API: WP_REST_Blocks_Controller class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage REST_API
       
     7  * @since 5.0.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Controller which provides a REST endpoint for the editor to read, create,
       
    12  * edit and delete reusable blocks. Blocks are stored as posts with the wp_block
       
    13  * post type.
       
    14  *
       
    15  * @since 5.0.0
       
    16  *
       
    17  * @see WP_REST_Posts_Controller
       
    18  * @see WP_REST_Controller
       
    19  */
       
    20 class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller {
       
    21 
       
    22 	/**
       
    23 	 * Checks if a block can be read.
       
    24 	 *
       
    25 	 * @since 5.0.0
       
    26 	 *
       
    27 	 * @param object $post Post object that backs the block.
       
    28 	 * @return bool Whether the block can be read.
       
    29 	 */
       
    30 	public function check_read_permission( $post ) {
       
    31 		// Ensure that the user is logged in and has the read_blocks capability.
       
    32 		$post_type = get_post_type_object( $post->post_type );
       
    33 		if ( ! current_user_can( $post_type->cap->read_post, $post->ID ) ) {
       
    34 			return false;
       
    35 		}
       
    36 
       
    37 		return parent::check_read_permission( $post );
       
    38 	}
       
    39 
       
    40 	/**
       
    41 	 * Filters a response based on the context defined in the schema.
       
    42 	 *
       
    43 	 * @since 5.0.0
       
    44 	 *
       
    45 	 * @param array  $data    Response data to fiter.
       
    46 	 * @param string $context Context defined in the schema.
       
    47 	 * @return array Filtered response.
       
    48 	 */
       
    49 	public function filter_response_by_context( $data, $context ) {
       
    50 		$data = parent::filter_response_by_context( $data, $context );
       
    51 
       
    52 		/*
       
    53 		 * Remove `title.rendered` and `content.rendered` from the response. It
       
    54 		 * doesn't make sense for a reusable block to have rendered content on its
       
    55 		 * own, since rendering a block requires it to be inside a post or a page.
       
    56 		 */
       
    57 		unset( $data['title']['rendered'] );
       
    58 		unset( $data['content']['rendered'] );
       
    59 
       
    60 		return $data;
       
    61 	}
       
    62 
       
    63 	/**
       
    64 	 * Retrieves the block's schema, conforming to JSON Schema.
       
    65 	 *
       
    66 	 * @since 5.0.0
       
    67 	 *
       
    68 	 * @return array Item schema data.
       
    69 	 */
       
    70 	public function get_item_schema() {
       
    71 		$schema = parent::get_item_schema();
       
    72 
       
    73 		/*
       
    74 		 * Allow all contexts to access `title.raw` and `content.raw`. Clients always
       
    75 		 * need the raw markup of a reusable block to do anything useful, e.g. parse
       
    76 		 * it or display it in an editor.
       
    77 		 */
       
    78 		$schema['properties']['title']['properties']['raw']['context']   = array( 'view', 'edit' );
       
    79 		$schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
       
    80 
       
    81 		/*
       
    82 		 * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t
       
    83 		 * make sense for a reusable block to have rendered content on its own, since
       
    84 		 * rendering a block requires it to be inside a post or a page.
       
    85 		 */
       
    86 		unset( $schema['properties']['title']['properties']['rendered'] );
       
    87 		unset( $schema['properties']['content']['properties']['rendered'] );
       
    88 
       
    89 		return $schema;
       
    90 	}
       
    91 
       
    92 }