author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
19 | 1 |
<?php |
2 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3 |
* REST API: WP_REST_Block_Pattern_Categories_Controller class |
19 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage REST_API |
|
7 |
* @since 6.0.0 |
|
8 |
*/ |
|
9 |
||
10 |
/** |
|
11 |
* Core class used to access block pattern categories via the REST API. |
|
12 |
* |
|
13 |
* @since 6.0.0 |
|
14 |
* |
|
15 |
* @see WP_REST_Controller |
|
16 |
*/ |
|
17 |
class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller { |
|
18 |
||
19 |
/** |
|
20 |
* Constructs the controller. |
|
21 |
* |
|
22 |
* @since 6.0.0 |
|
23 |
*/ |
|
24 |
public function __construct() { |
|
25 |
$this->namespace = 'wp/v2'; |
|
26 |
$this->rest_base = 'block-patterns/categories'; |
|
27 |
} |
|
28 |
||
29 |
/** |
|
30 |
* Registers the routes for the objects of the controller. |
|
31 |
* |
|
32 |
* @since 6.0.0 |
|
33 |
*/ |
|
34 |
public function register_routes() { |
|
35 |
register_rest_route( |
|
36 |
$this->namespace, |
|
37 |
'/' . $this->rest_base, |
|
38 |
array( |
|
39 |
array( |
|
40 |
'methods' => WP_REST_Server::READABLE, |
|
41 |
'callback' => array( $this, 'get_items' ), |
|
42 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
43 |
), |
|
44 |
'schema' => array( $this, 'get_public_item_schema' ), |
|
45 |
) |
|
46 |
); |
|
47 |
} |
|
48 |
||
49 |
/** |
|
50 |
* Checks whether a given request has permission to read block patterns. |
|
51 |
* |
|
52 |
* @since 6.0.0 |
|
53 |
* |
|
54 |
* @param WP_REST_Request $request Full details about the request. |
|
55 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
56 |
*/ |
|
57 |
public function get_items_permissions_check( $request ) { |
|
58 |
if ( current_user_can( 'edit_posts' ) ) { |
|
59 |
return true; |
|
60 |
} |
|
61 |
||
62 |
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { |
|
63 |
if ( current_user_can( $post_type->cap->edit_posts ) ) { |
|
64 |
return true; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
return new WP_Error( |
|
69 |
'rest_cannot_view', |
|
70 |
__( 'Sorry, you are not allowed to view the registered block pattern categories.' ), |
|
71 |
array( 'status' => rest_authorization_required_code() ) |
|
72 |
); |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Retrieves all block pattern categories. |
|
77 |
* |
|
78 |
* @since 6.0.0 |
|
79 |
* |
|
80 |
* @param WP_REST_Request $request Full details about the request. |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
81 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
19 | 82 |
*/ |
83 |
public function get_items( $request ) { |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
84 |
if ( $request->is_method( 'HEAD' ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
85 |
// Return early as this handler doesn't add any response headers. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
86 |
return new WP_REST_Response( array() ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
87 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
88 |
|
19 | 89 |
$response = array(); |
90 |
$categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); |
|
91 |
foreach ( $categories as $category ) { |
|
92 |
$prepared_category = $this->prepare_item_for_response( $category, $request ); |
|
93 |
$response[] = $this->prepare_response_for_collection( $prepared_category ); |
|
94 |
} |
|
95 |
||
96 |
return rest_ensure_response( $response ); |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Prepare a raw block pattern category before it gets output in a REST API response. |
|
101 |
* |
|
102 |
* @since 6.0.0 |
|
103 |
* |
|
104 |
* @param array $item Raw category as registered, before any changes. |
|
105 |
* @param WP_REST_Request $request Request object. |
|
106 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
107 |
*/ |
|
108 |
public function prepare_item_for_response( $item, $request ) { |
|
109 |
$fields = $this->get_fields_for_response( $request ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
$keys = array( 'name', 'label', 'description' ); |
19 | 111 |
$data = array(); |
112 |
foreach ( $keys as $key ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
if ( isset( $item[ $key ] ) && rest_is_field_included( $key, $fields ) ) { |
19 | 114 |
$data[ $key ] = $item[ $key ]; |
115 |
} |
|
116 |
} |
|
117 |
||
118 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
119 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
|
120 |
$data = $this->filter_response_by_context( $data, $context ); |
|
121 |
||
122 |
return rest_ensure_response( $data ); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Retrieves the block pattern category schema, conforming to JSON Schema. |
|
127 |
* |
|
128 |
* @since 6.0.0 |
|
129 |
* |
|
130 |
* @return array Item schema data. |
|
131 |
*/ |
|
132 |
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
|
133 |
if ( $this->schema ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
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
|
135 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
|
19 | 137 |
$schema = array( |
138 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
139 |
'title' => 'block-pattern-category', |
|
140 |
'type' => 'object', |
|
141 |
'properties' => array( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
'name' => array( |
19 | 143 |
'description' => __( 'The category name.' ), |
144 |
'type' => 'string', |
|
145 |
'readonly' => true, |
|
146 |
'context' => array( 'view', 'edit', 'embed' ), |
|
147 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
'label' => array( |
19 | 149 |
'description' => __( 'The category label, in human readable format.' ), |
150 |
'type' => 'string', |
|
151 |
'readonly' => true, |
|
152 |
'context' => array( 'view', 'edit', 'embed' ), |
|
153 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
'description' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
'description' => __( 'The category description, in human readable format.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
'type' => 'string', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
'readonly' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
'context' => array( 'view', 'edit', 'embed' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
), |
19 | 160 |
), |
161 |
); |
|
162 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
$this->schema = $schema; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
return $this->add_additional_fields_schema( $this->schema ); |
19 | 166 |
} |
167 |
} |