author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
9 | 1 |
<?php |
2 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3 |
* Synced patterns REST API: WP_REST_Blocks_Controller class |
9 | 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, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* edit, and delete synced patterns (formerly called reusable blocks). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* Patterns are stored as posts with the wp_block post type. |
9 | 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 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
23 |
* Checks if a pattern can be read. |
9 | 24 |
* |
25 |
* @since 5.0.0 |
|
26 |
* |
|
16 | 27 |
* @param WP_Post $post Post object that backs the block. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
28 |
* @return bool Whether the pattern can be read. |
9 | 29 |
*/ |
30 |
public function check_read_permission( $post ) { |
|
16 | 31 |
// By default the read_post capability is mapped to edit_posts. |
32 |
if ( ! current_user_can( 'read_post', $post->ID ) ) { |
|
9 | 33 |
return false; |
34 |
} |
|
35 |
||
36 |
return parent::check_read_permission( $post ); |
|
37 |
} |
|
38 |
||
39 |
/** |
|
40 |
* Filters a response based on the context defined in the schema. |
|
41 |
* |
|
42 |
* @since 5.0.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
43 |
* @since 6.3.0 Adds the `wp_pattern_sync_status` postmeta property to the top level of response. |
9 | 44 |
* |
19 | 45 |
* @param array $data Response data to filter. |
9 | 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 |
/* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
* Remove `title.rendered` and `content.rendered` from the response. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
* It doesn't make sense for a pattern to have rendered content on its own, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
* since rendering a block requires it to be inside a post or a page. |
9 | 56 |
*/ |
57 |
unset( $data['title']['rendered'] ); |
|
58 |
unset( $data['content']['rendered'] ); |
|
59 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
60 |
// Add the core wp_pattern_sync_status meta as top level property to the response. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
61 |
$data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
62 |
unset( $data['meta']['wp_pattern_sync_status'] ); |
9 | 63 |
return $data; |
64 |
} |
|
65 |
||
66 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
* Retrieves the pattern's schema, conforming to JSON Schema. |
9 | 68 |
* |
69 |
* @since 5.0.0 |
|
70 |
* |
|
71 |
* @return array Item schema data. |
|
72 |
*/ |
|
73 |
public function get_item_schema() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
if ( $this->schema ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
return $this->add_additional_fields_schema( $this->schema ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
|
9 | 78 |
$schema = parent::get_item_schema(); |
79 |
||
80 |
/* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
81 |
* Allow all contexts to access `title.raw` and `content.raw`. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
82 |
* Clients always need the raw markup of a pattern to do anything useful, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
* e.g. parse it or display it in an editor. |
9 | 84 |
*/ |
85 |
$schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' ); |
|
86 |
$schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' ); |
|
87 |
||
88 |
/* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
89 |
* Remove `title.rendered` and `content.rendered` from the schema. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
90 |
* It doesn't make sense for a pattern to have rendered content on its own, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
* since rendering a block requires it to be inside a post or a page. |
9 | 92 |
*/ |
93 |
unset( $schema['properties']['title']['properties']['rendered'] ); |
|
94 |
unset( $schema['properties']['content']['properties']['rendered'] ); |
|
95 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
$this->schema = $schema; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
97 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
return $this->add_additional_fields_schema( $this->schema ); |
9 | 99 |
} |
100 |
} |