1 <?php |
1 <?php |
2 /** |
2 /** |
3 * Reusable blocks REST API: WP_REST_Blocks_Controller class |
3 * Synced patterns REST API: WP_REST_Blocks_Controller class |
4 * |
4 * |
5 * @package WordPress |
5 * @package WordPress |
6 * @subpackage REST_API |
6 * @subpackage REST_API |
7 * @since 5.0.0 |
7 * @since 5.0.0 |
8 */ |
8 */ |
9 |
9 |
10 /** |
10 /** |
11 * Controller which provides a REST endpoint for the editor to read, create, |
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 |
12 * edit, and delete synced patterns (formerly called reusable blocks). |
13 * post type. |
13 * Patterns are stored as posts with the wp_block post type. |
14 * |
14 * |
15 * @since 5.0.0 |
15 * @since 5.0.0 |
16 * |
16 * |
17 * @see WP_REST_Posts_Controller |
17 * @see WP_REST_Posts_Controller |
18 * @see WP_REST_Controller |
18 * @see WP_REST_Controller |
19 */ |
19 */ |
20 class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller { |
20 class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller { |
21 |
21 |
22 /** |
22 /** |
23 * Checks if a block can be read. |
23 * Checks if a pattern can be read. |
24 * |
24 * |
25 * @since 5.0.0 |
25 * @since 5.0.0 |
26 * |
26 * |
27 * @param WP_Post $post Post object that backs the block. |
27 * @param WP_Post $post Post object that backs the block. |
28 * @return bool Whether the block can be read. |
28 * @return bool Whether the pattern can be read. |
29 */ |
29 */ |
30 public function check_read_permission( $post ) { |
30 public function check_read_permission( $post ) { |
31 // By default the read_post capability is mapped to edit_posts. |
31 // By default the read_post capability is mapped to edit_posts. |
32 if ( ! current_user_can( 'read_post', $post->ID ) ) { |
32 if ( ! current_user_can( 'read_post', $post->ID ) ) { |
33 return false; |
33 return false; |
38 |
38 |
39 /** |
39 /** |
40 * Filters a response based on the context defined in the schema. |
40 * Filters a response based on the context defined in the schema. |
41 * |
41 * |
42 * @since 5.0.0 |
42 * @since 5.0.0 |
|
43 * @since 6.3.0 Adds the `wp_pattern_sync_status` postmeta property to the top level of response. |
43 * |
44 * |
44 * @param array $data Response data to filter. |
45 * @param array $data Response data to filter. |
45 * @param string $context Context defined in the schema. |
46 * @param string $context Context defined in the schema. |
46 * @return array Filtered response. |
47 * @return array Filtered response. |
47 */ |
48 */ |
48 public function filter_response_by_context( $data, $context ) { |
49 public function filter_response_by_context( $data, $context ) { |
49 $data = parent::filter_response_by_context( $data, $context ); |
50 $data = parent::filter_response_by_context( $data, $context ); |
50 |
51 |
51 /* |
52 /* |
52 * Remove `title.rendered` and `content.rendered` from the response. It |
53 * Remove `title.rendered` and `content.rendered` from the response. |
53 * doesn't make sense for a reusable block to have rendered content on its |
54 * It doesn't make sense for a pattern to have rendered content on its own, |
54 * own, since rendering a block requires it to be inside a post or a page. |
55 * since rendering a block requires it to be inside a post or a page. |
55 */ |
56 */ |
56 unset( $data['title']['rendered'] ); |
57 unset( $data['title']['rendered'] ); |
57 unset( $data['content']['rendered'] ); |
58 unset( $data['content']['rendered'] ); |
58 |
59 |
|
60 // Add the core wp_pattern_sync_status meta as top level property to the response. |
|
61 $data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : ''; |
|
62 unset( $data['meta']['wp_pattern_sync_status'] ); |
59 return $data; |
63 return $data; |
60 } |
64 } |
61 |
65 |
62 /** |
66 /** |
63 * Retrieves the block's schema, conforming to JSON Schema. |
67 * Retrieves the pattern's schema, conforming to JSON Schema. |
64 * |
68 * |
65 * @since 5.0.0 |
69 * @since 5.0.0 |
66 * |
70 * |
67 * @return array Item schema data. |
71 * @return array Item schema data. |
68 */ |
72 */ |
69 public function get_item_schema() { |
73 public function get_item_schema() { |
70 // Do not cache this schema because all properties are derived from parent controller. |
74 if ( $this->schema ) { |
|
75 return $this->add_additional_fields_schema( $this->schema ); |
|
76 } |
|
77 |
71 $schema = parent::get_item_schema(); |
78 $schema = parent::get_item_schema(); |
72 |
79 |
73 /* |
80 /* |
74 * Allow all contexts to access `title.raw` and `content.raw`. Clients always |
81 * Allow all contexts to access `title.raw` and `content.raw`. |
75 * need the raw markup of a reusable block to do anything useful, e.g. parse |
82 * Clients always need the raw markup of a pattern to do anything useful, |
76 * it or display it in an editor. |
83 * e.g. parse it or display it in an editor. |
77 */ |
84 */ |
78 $schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' ); |
85 $schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' ); |
79 $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' ); |
86 $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' ); |
80 |
87 |
81 /* |
88 /* |
82 * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t |
89 * Remove `title.rendered` and `content.rendered` from the schema. |
83 * make sense for a reusable block to have rendered content on its own, since |
90 * It doesn't make sense for a pattern to have rendered content on its own, |
84 * rendering a block requires it to be inside a post or a page. |
91 * since rendering a block requires it to be inside a post or a page. |
85 */ |
92 */ |
86 unset( $schema['properties']['title']['properties']['rendered'] ); |
93 unset( $schema['properties']['title']['properties']['rendered'] ); |
87 unset( $schema['properties']['content']['properties']['rendered'] ); |
94 unset( $schema['properties']['content']['properties']['rendered'] ); |
88 |
95 |
89 return $schema; |
96 $this->schema = $schema; |
|
97 |
|
98 return $this->add_additional_fields_schema( $this->schema ); |
90 } |
99 } |
91 |
|
92 } |
100 } |