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