PluginProbe
Gutenberg / 4.6.0
Gutenberg v4.6.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / class-wp-rest-blocks-controller.php

class-wp-rest-blocks-controller.php in Gutenberg 4.6.0, at lib/class-wp-rest-blocks-controller.php

90 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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