author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
/** |
|
3 |
* REST API: WP_REST_Templates_Controller class |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage REST_API |
|
7 |
* @since 5.8.0 |
|
8 |
*/ |
|
9 |
||
10 |
/** |
|
11 |
* Base Templates REST API Controller. |
|
12 |
* |
|
13 |
* @since 5.8.0 |
|
14 |
* |
|
15 |
* @see WP_REST_Controller |
|
16 |
*/ |
|
17 |
class WP_REST_Templates_Controller extends WP_REST_Controller { |
|
18 |
||
19 |
/** |
|
20 |
* Post type. |
|
21 |
* |
|
22 |
* @since 5.8.0 |
|
23 |
* @var string |
|
24 |
*/ |
|
25 |
protected $post_type; |
|
26 |
||
27 |
/** |
|
28 |
* Constructor. |
|
29 |
* |
|
30 |
* @since 5.8.0 |
|
31 |
* |
|
32 |
* @param string $post_type Post type. |
|
33 |
*/ |
|
34 |
public function __construct( $post_type ) { |
|
35 |
$this->post_type = $post_type; |
|
36 |
$obj = get_post_type_object( $post_type ); |
|
37 |
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name; |
|
19 | 38 |
$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2'; |
18 | 39 |
} |
40 |
||
41 |
/** |
|
42 |
* Registers the controllers routes. |
|
43 |
* |
|
44 |
* @since 5.8.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
45 |
* @since 6.1.0 Endpoint for fallback template content. |
18 | 46 |
*/ |
47 |
public function register_routes() { |
|
48 |
// Lists all templates. |
|
49 |
register_rest_route( |
|
50 |
$this->namespace, |
|
51 |
'/' . $this->rest_base, |
|
52 |
array( |
|
53 |
array( |
|
54 |
'methods' => WP_REST_Server::READABLE, |
|
55 |
'callback' => array( $this, 'get_items' ), |
|
56 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
57 |
'args' => $this->get_collection_params(), |
|
58 |
), |
|
59 |
array( |
|
60 |
'methods' => WP_REST_Server::CREATABLE, |
|
61 |
'callback' => array( $this, 'create_item' ), |
|
62 |
'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
63 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
|
64 |
), |
|
65 |
'schema' => array( $this, 'get_public_item_schema' ), |
|
66 |
) |
|
67 |
); |
|
68 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
// Get fallback template content. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
70 |
register_rest_route( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
$this->namespace, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
'/' . $this->rest_base . '/lookup', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
'methods' => WP_REST_Server::READABLE, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
'callback' => array( $this, 'get_template_fallback' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
'args' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
'slug' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
80 |
'description' => __( 'The slug of the template to get the fallback for' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
81 |
'type' => 'string', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
82 |
'required' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
84 |
'is_custom' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
'description' => __( 'Indicates if a template is custom or part of the template hierarchy' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
86 |
'type' => 'boolean', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
87 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
88 |
'template_prefix' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
89 |
'description' => __( 'The template prefix for the created template. This is used to extract the main template type, e.g. in `taxonomy-books` extracts the `taxonomy`' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
90 |
'type' => 'string', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
92 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
93 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
|
18 | 97 |
// Lists/updates a single template based on the given id. |
98 |
register_rest_route( |
|
99 |
$this->namespace, |
|
19 | 100 |
// The route. |
101 |
sprintf( |
|
102 |
'/%s/(?P<id>%s%s)', |
|
103 |
$this->rest_base, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
* Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
* Excludes invalid directory name characters: `/:<>*?"|`. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
*/ |
19 | 108 |
'([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', |
109 |
// Matches the template name. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
'[\/\w%-]+' |
19 | 111 |
), |
18 | 112 |
array( |
19 | 113 |
'args' => array( |
114 |
'id' => array( |
|
115 |
'description' => __( 'The id of a template' ), |
|
116 |
'type' => 'string', |
|
117 |
'sanitize_callback' => array( $this, '_sanitize_template_id' ), |
|
118 |
), |
|
119 |
), |
|
18 | 120 |
array( |
121 |
'methods' => WP_REST_Server::READABLE, |
|
122 |
'callback' => array( $this, 'get_item' ), |
|
123 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
124 |
'args' => array( |
|
19 | 125 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
18 | 126 |
), |
127 |
), |
|
128 |
array( |
|
129 |
'methods' => WP_REST_Server::EDITABLE, |
|
130 |
'callback' => array( $this, 'update_item' ), |
|
131 |
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
132 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
133 |
), |
|
134 |
array( |
|
135 |
'methods' => WP_REST_Server::DELETABLE, |
|
136 |
'callback' => array( $this, 'delete_item' ), |
|
137 |
'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
138 |
'args' => array( |
|
139 |
'force' => array( |
|
140 |
'type' => 'boolean', |
|
141 |
'default' => false, |
|
142 |
'description' => __( 'Whether to bypass Trash and force deletion.' ), |
|
143 |
), |
|
144 |
), |
|
145 |
), |
|
146 |
'schema' => array( $this, 'get_public_item_schema' ), |
|
147 |
) |
|
148 |
); |
|
149 |
} |
|
150 |
||
151 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
* Returns the fallback template for the given slug. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
153 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
* @since 6.3.0 Ignore empty templates. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
* @param WP_REST_Request $request The request instance. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
* @return WP_REST_Response|WP_Error |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
public function get_template_fallback( $request ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
do { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
array_shift( $hierarchy ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
} while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
// To maintain original behavior, return an empty object rather than a 404 error when no template is found. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
$response = $fallback_template ? $this->prepare_item_for_response( $fallback_template, $request ) : new stdClass(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
170 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
171 |
return rest_ensure_response( $response ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
172 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
173 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
174 |
/** |
18 | 175 |
* Checks if the user has permissions to make the request. |
176 |
* |
|
177 |
* @since 5.8.0 |
|
178 |
* |
|
179 |
* @param WP_REST_Request $request Full details about the request. |
|
180 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
181 |
*/ |
|
182 |
protected function permissions_check( $request ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
183 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
184 |
* Verify if the current user has edit_theme_options capability. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
185 |
* This capability is required to edit/view/delete templates. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
186 |
*/ |
18 | 187 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
188 |
return new WP_Error( |
|
189 |
'rest_cannot_manage_templates', |
|
190 |
__( 'Sorry, you are not allowed to access the templates on this site.' ), |
|
191 |
array( |
|
192 |
'status' => rest_authorization_required_code(), |
|
193 |
) |
|
194 |
); |
|
195 |
} |
|
196 |
||
197 |
return true; |
|
198 |
} |
|
199 |
||
200 |
/** |
|
19 | 201 |
* Requesting this endpoint for a template like 'twentytwentytwo//home' |
202 |
* requires using a path like /wp/v2/templates/twentytwentytwo//home. There |
|
203 |
* are special cases when WordPress routing corrects the name to contain |
|
204 |
* only a single slash like 'twentytwentytwo/home'. |
|
205 |
* |
|
206 |
* This method doubles the last slash if it's not already doubled. It relies |
|
207 |
* on the template ID format {theme_name}//{template_slug} and the fact that |
|
208 |
* slugs cannot contain slashes. |
|
209 |
* |
|
210 |
* @since 5.9.0 |
|
211 |
* @see https://core.trac.wordpress.org/ticket/54507 |
|
212 |
* |
|
213 |
* @param string $id Template ID. |
|
214 |
* @return string Sanitized template ID. |
|
215 |
*/ |
|
216 |
public function _sanitize_template_id( $id ) { |
|
217 |
$id = urldecode( $id ); |
|
218 |
||
219 |
$last_slash_pos = strrpos( $id, '/' ); |
|
220 |
if ( false === $last_slash_pos ) { |
|
221 |
return $id; |
|
222 |
} |
|
223 |
||
224 |
$is_double_slashed = substr( $id, $last_slash_pos - 1, 1 ) === '/'; |
|
225 |
if ( $is_double_slashed ) { |
|
226 |
return $id; |
|
227 |
} |
|
228 |
return ( |
|
229 |
substr( $id, 0, $last_slash_pos ) |
|
230 |
. '/' |
|
231 |
. substr( $id, $last_slash_pos ) |
|
232 |
); |
|
233 |
} |
|
234 |
||
235 |
/** |
|
18 | 236 |
* Checks if a given request has access to read templates. |
237 |
* |
|
238 |
* @since 5.8.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
239 |
* @since 6.6.0 Allow users with edit_posts capability to read templates. |
18 | 240 |
* |
241 |
* @param WP_REST_Request $request Full details about the request. |
|
242 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
243 |
*/ |
|
244 |
public function get_items_permissions_check( $request ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
245 |
if ( current_user_can( 'edit_posts' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
246 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
247 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
248 |
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
249 |
if ( current_user_can( $post_type->cap->edit_posts ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
250 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
251 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
252 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
253 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
254 |
return new WP_Error( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
255 |
'rest_cannot_manage_templates', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
256 |
__( 'Sorry, you are not allowed to access the templates on this site.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
257 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
258 |
'status' => rest_authorization_required_code(), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
259 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
260 |
); |
18 | 261 |
} |
262 |
||
263 |
/** |
|
264 |
* Returns a list of templates. |
|
265 |
* |
|
266 |
* @since 5.8.0 |
|
267 |
* |
|
268 |
* @param WP_REST_Request $request The request instance. |
|
269 |
* @return WP_REST_Response |
|
270 |
*/ |
|
271 |
public function get_items( $request ) { |
|
272 |
$query = array(); |
|
273 |
if ( isset( $request['wp_id'] ) ) { |
|
274 |
$query['wp_id'] = $request['wp_id']; |
|
275 |
} |
|
276 |
if ( isset( $request['area'] ) ) { |
|
277 |
$query['area'] = $request['area']; |
|
278 |
} |
|
19 | 279 |
if ( isset( $request['post_type'] ) ) { |
280 |
$query['post_type'] = $request['post_type']; |
|
281 |
} |
|
18 | 282 |
|
283 |
$templates = array(); |
|
284 |
foreach ( get_block_templates( $query, $this->post_type ) as $template ) { |
|
285 |
$data = $this->prepare_item_for_response( $template, $request ); |
|
286 |
$templates[] = $this->prepare_response_for_collection( $data ); |
|
287 |
} |
|
288 |
||
289 |
return rest_ensure_response( $templates ); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Checks if a given request has access to read a single template. |
|
294 |
* |
|
295 |
* @since 5.8.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
296 |
* @since 6.6.0 Allow users with edit_posts capability to read individual templates. |
18 | 297 |
* |
298 |
* @param WP_REST_Request $request Full details about the request. |
|
299 |
* @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. |
|
300 |
*/ |
|
301 |
public function get_item_permissions_check( $request ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
302 |
if ( current_user_can( 'edit_posts' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
303 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
304 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
305 |
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
306 |
if ( current_user_can( $post_type->cap->edit_posts ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
307 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
308 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
309 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
310 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
311 |
return new WP_Error( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
312 |
'rest_cannot_manage_templates', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
313 |
__( 'Sorry, you are not allowed to access the templates on this site.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
314 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
315 |
'status' => rest_authorization_required_code(), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
316 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
317 |
); |
18 | 318 |
} |
319 |
||
320 |
/** |
|
321 |
* Returns the given template |
|
322 |
* |
|
323 |
* @since 5.8.0 |
|
324 |
* |
|
325 |
* @param WP_REST_Request $request The request instance. |
|
326 |
* @return WP_REST_Response|WP_Error |
|
327 |
*/ |
|
328 |
public function get_item( $request ) { |
|
19 | 329 |
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { |
330 |
$template = get_block_file_template( $request['id'], $this->post_type ); |
|
331 |
} else { |
|
332 |
$template = get_block_template( $request['id'], $this->post_type ); |
|
333 |
} |
|
18 | 334 |
|
335 |
if ( ! $template ) { |
|
336 |
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); |
|
337 |
} |
|
338 |
||
339 |
return $this->prepare_item_for_response( $template, $request ); |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* Checks if a given request has access to write a single template. |
|
344 |
* |
|
345 |
* @since 5.8.0 |
|
346 |
* |
|
347 |
* @param WP_REST_Request $request Full details about the request. |
|
348 |
* @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise. |
|
349 |
*/ |
|
350 |
public function update_item_permissions_check( $request ) { |
|
351 |
return $this->permissions_check( $request ); |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Updates a single template. |
|
356 |
* |
|
357 |
* @since 5.8.0 |
|
358 |
* |
|
359 |
* @param WP_REST_Request $request Full details about the request. |
|
360 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
361 |
*/ |
|
362 |
public function update_item( $request ) { |
|
363 |
$template = get_block_template( $request['id'], $this->post_type ); |
|
364 |
if ( ! $template ) { |
|
365 |
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); |
|
366 |
} |
|
367 |
||
19 | 368 |
$post_before = get_post( $template->wp_id ); |
369 |
||
370 |
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { |
|
371 |
wp_delete_post( $template->wp_id, true ); |
|
372 |
$request->set_param( 'context', 'edit' ); |
|
373 |
||
374 |
$template = get_block_template( $request['id'], $this->post_type ); |
|
375 |
$response = $this->prepare_item_for_response( $template, $request ); |
|
376 |
||
377 |
return rest_ensure_response( $response ); |
|
378 |
} |
|
379 |
||
18 | 380 |
$changes = $this->prepare_item_for_database( $request ); |
381 |
||
19 | 382 |
if ( is_wp_error( $changes ) ) { |
383 |
return $changes; |
|
384 |
} |
|
385 |
||
18 | 386 |
if ( 'custom' === $template->source ) { |
19 | 387 |
$update = true; |
388 |
$result = wp_update_post( wp_slash( (array) $changes ), false ); |
|
18 | 389 |
} else { |
19 | 390 |
$update = false; |
391 |
$post_before = null; |
|
392 |
$result = wp_insert_post( wp_slash( (array) $changes ), false ); |
|
18 | 393 |
} |
19 | 394 |
|
18 | 395 |
if ( is_wp_error( $result ) ) { |
19 | 396 |
if ( 'db_update_error' === $result->get_error_code() ) { |
397 |
$result->add_data( array( 'status' => 500 ) ); |
|
398 |
} else { |
|
399 |
$result->add_data( array( 'status' => 400 ) ); |
|
400 |
} |
|
18 | 401 |
return $result; |
402 |
} |
|
403 |
||
404 |
$template = get_block_template( $request['id'], $this->post_type ); |
|
405 |
$fields_update = $this->update_additional_fields_for_object( $template, $request ); |
|
406 |
if ( is_wp_error( $fields_update ) ) { |
|
407 |
return $fields_update; |
|
408 |
} |
|
409 |
||
19 | 410 |
$request->set_param( 'context', 'edit' ); |
411 |
||
412 |
$post = get_post( $template->wp_id ); |
|
413 |
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ |
|
414 |
do_action( "rest_after_insert_{$this->post_type}", $post, $request, false ); |
|
415 |
||
416 |
wp_after_insert_post( $post, $update, $post_before ); |
|
417 |
||
418 |
$response = $this->prepare_item_for_response( $template, $request ); |
|
419 |
||
420 |
return rest_ensure_response( $response ); |
|
18 | 421 |
} |
422 |
||
423 |
/** |
|
424 |
* Checks if a given request has access to create a template. |
|
425 |
* |
|
426 |
* @since 5.8.0 |
|
427 |
* |
|
428 |
* @param WP_REST_Request $request Full details about the request. |
|
429 |
* @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
430 |
*/ |
|
431 |
public function create_item_permissions_check( $request ) { |
|
432 |
return $this->permissions_check( $request ); |
|
433 |
} |
|
434 |
||
435 |
/** |
|
436 |
* Creates a single template. |
|
437 |
* |
|
438 |
* @since 5.8.0 |
|
439 |
* |
|
440 |
* @param WP_REST_Request $request Full details about the request. |
|
441 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
442 |
*/ |
|
443 |
public function create_item( $request ) { |
|
19 | 444 |
$prepared_post = $this->prepare_item_for_database( $request ); |
445 |
||
446 |
if ( is_wp_error( $prepared_post ) ) { |
|
447 |
return $prepared_post; |
|
18 | 448 |
} |
19 | 449 |
|
450 |
$prepared_post->post_name = $request['slug']; |
|
451 |
$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true ); |
|
452 |
if ( is_wp_error( $post_id ) ) { |
|
453 |
if ( 'db_insert_error' === $post_id->get_error_code() ) { |
|
454 |
$post_id->add_data( array( 'status' => 500 ) ); |
|
455 |
} else { |
|
456 |
$post_id->add_data( array( 'status' => 400 ) ); |
|
457 |
} |
|
458 |
||
459 |
return $post_id; |
|
460 |
} |
|
461 |
$posts = get_block_templates( array( 'wp_id' => $post_id ), $this->post_type ); |
|
18 | 462 |
if ( ! count( $posts ) ) { |
19 | 463 |
return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ), array( 'status' => 400 ) ); |
18 | 464 |
} |
465 |
$id = $posts[0]->id; |
|
19 | 466 |
$post = get_post( $post_id ); |
18 | 467 |
$template = get_block_template( $id, $this->post_type ); |
468 |
$fields_update = $this->update_additional_fields_for_object( $template, $request ); |
|
469 |
if ( is_wp_error( $fields_update ) ) { |
|
470 |
return $fields_update; |
|
471 |
} |
|
472 |
||
19 | 473 |
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ |
474 |
do_action( "rest_after_insert_{$this->post_type}", $post, $request, true ); |
|
475 |
||
476 |
wp_after_insert_post( $post, false, null ); |
|
477 |
||
478 |
$response = $this->prepare_item_for_response( $template, $request ); |
|
479 |
$response = rest_ensure_response( $response ); |
|
480 |
||
481 |
$response->set_status( 201 ); |
|
482 |
$response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $template->id ) ) ); |
|
483 |
||
484 |
return $response; |
|
18 | 485 |
} |
486 |
||
487 |
/** |
|
488 |
* Checks if a given request has access to delete a single template. |
|
489 |
* |
|
490 |
* @since 5.8.0 |
|
491 |
* |
|
492 |
* @param WP_REST_Request $request Full details about the request. |
|
493 |
* @return true|WP_Error True if the request has delete access for the item, WP_Error object otherwise. |
|
494 |
*/ |
|
495 |
public function delete_item_permissions_check( $request ) { |
|
496 |
return $this->permissions_check( $request ); |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* Deletes a single template. |
|
501 |
* |
|
502 |
* @since 5.8.0 |
|
503 |
* |
|
504 |
* @param WP_REST_Request $request Full details about the request. |
|
505 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
506 |
*/ |
|
507 |
public function delete_item( $request ) { |
|
508 |
$template = get_block_template( $request['id'], $this->post_type ); |
|
509 |
if ( ! $template ) { |
|
510 |
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); |
|
511 |
} |
|
512 |
if ( 'custom' !== $template->source ) { |
|
513 |
return new WP_Error( 'rest_invalid_template', __( 'Templates based on theme files can\'t be removed.' ), array( 'status' => 400 ) ); |
|
514 |
} |
|
515 |
||
516 |
$id = $template->wp_id; |
|
517 |
$force = (bool) $request['force']; |
|
518 |
||
19 | 519 |
$request->set_param( 'context', 'edit' ); |
520 |
||
18 | 521 |
// If we're forcing, then delete permanently. |
522 |
if ( $force ) { |
|
523 |
$previous = $this->prepare_item_for_response( $template, $request ); |
|
19 | 524 |
$result = wp_delete_post( $id, true ); |
18 | 525 |
$response = new WP_REST_Response(); |
526 |
$response->set_data( |
|
527 |
array( |
|
528 |
'deleted' => true, |
|
529 |
'previous' => $previous->get_data(), |
|
530 |
) |
|
531 |
); |
|
19 | 532 |
} else { |
533 |
// Otherwise, only trash if we haven't already. |
|
534 |
if ( 'trash' === $template->status ) { |
|
535 |
return new WP_Error( |
|
536 |
'rest_template_already_trashed', |
|
537 |
__( 'The template has already been deleted.' ), |
|
538 |
array( 'status' => 410 ) |
|
539 |
); |
|
540 |
} |
|
18 | 541 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
542 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
543 |
* (Note that internally this falls through to `wp_delete_post()` |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
544 |
* if the Trash is disabled.) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
545 |
*/ |
19 | 546 |
$result = wp_trash_post( $id ); |
547 |
$template->status = 'trash'; |
|
548 |
$response = $this->prepare_item_for_response( $template, $request ); |
|
18 | 549 |
} |
550 |
||
19 | 551 |
if ( ! $result ) { |
18 | 552 |
return new WP_Error( |
19 | 553 |
'rest_cannot_delete', |
554 |
__( 'The template cannot be deleted.' ), |
|
555 |
array( 'status' => 500 ) |
|
18 | 556 |
); |
557 |
} |
|
558 |
||
19 | 559 |
return $response; |
18 | 560 |
} |
561 |
||
562 |
/** |
|
563 |
* Prepares a single template for create or update. |
|
564 |
* |
|
565 |
* @since 5.8.0 |
|
566 |
* |
|
567 |
* @param WP_REST_Request $request Request object. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
568 |
* @return stdClass|WP_Error Changes to pass to wp_update_post. |
18 | 569 |
*/ |
570 |
protected function prepare_item_for_database( $request ) { |
|
571 |
$template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null; |
|
572 |
$changes = new stdClass(); |
|
573 |
if ( null === $template ) { |
|
574 |
$changes->post_type = $this->post_type; |
|
575 |
$changes->post_status = 'publish'; |
|
576 |
$changes->tax_input = array( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
577 |
'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : get_stylesheet(), |
18 | 578 |
); |
579 |
} elseif ( 'custom' !== $template->source ) { |
|
580 |
$changes->post_name = $template->slug; |
|
581 |
$changes->post_type = $this->post_type; |
|
582 |
$changes->post_status = 'publish'; |
|
583 |
$changes->tax_input = array( |
|
584 |
'wp_theme' => $template->theme, |
|
585 |
); |
|
19 | 586 |
$changes->meta_input = array( |
587 |
'origin' => $template->source, |
|
588 |
); |
|
18 | 589 |
} else { |
590 |
$changes->post_name = $template->slug; |
|
591 |
$changes->ID = $template->wp_id; |
|
592 |
$changes->post_status = 'publish'; |
|
593 |
} |
|
594 |
if ( isset( $request['content'] ) ) { |
|
19 | 595 |
if ( is_string( $request['content'] ) ) { |
596 |
$changes->post_content = $request['content']; |
|
597 |
} elseif ( isset( $request['content']['raw'] ) ) { |
|
598 |
$changes->post_content = $request['content']['raw']; |
|
599 |
} |
|
18 | 600 |
} elseif ( null !== $template && 'custom' !== $template->source ) { |
601 |
$changes->post_content = $template->content; |
|
602 |
} |
|
603 |
if ( isset( $request['title'] ) ) { |
|
19 | 604 |
if ( is_string( $request['title'] ) ) { |
605 |
$changes->post_title = $request['title']; |
|
606 |
} elseif ( ! empty( $request['title']['raw'] ) ) { |
|
607 |
$changes->post_title = $request['title']['raw']; |
|
608 |
} |
|
18 | 609 |
} elseif ( null !== $template && 'custom' !== $template->source ) { |
610 |
$changes->post_title = $template->title; |
|
611 |
} |
|
612 |
if ( isset( $request['description'] ) ) { |
|
613 |
$changes->post_excerpt = $request['description']; |
|
614 |
} elseif ( null !== $template && 'custom' !== $template->source ) { |
|
615 |
$changes->post_excerpt = $template->description; |
|
616 |
} |
|
617 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
618 |
if ( 'wp_template' === $this->post_type && isset( $request['is_wp_suggestion'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
619 |
$changes->meta_input = wp_parse_args( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
620 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
621 |
'is_wp_suggestion' => $request['is_wp_suggestion'], |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
622 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
623 |
$changes->meta_input = array() |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
624 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
625 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
626 |
|
19 | 627 |
if ( 'wp_template_part' === $this->post_type ) { |
628 |
if ( isset( $request['area'] ) ) { |
|
629 |
$changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] ); |
|
630 |
} elseif ( null !== $template && 'custom' !== $template->source && $template->area ) { |
|
631 |
$changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
632 |
} elseif ( empty( $template->area ) ) { |
19 | 633 |
$changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; |
634 |
} |
|
635 |
} |
|
636 |
||
637 |
if ( ! empty( $request['author'] ) ) { |
|
638 |
$post_author = (int) $request['author']; |
|
639 |
||
640 |
if ( get_current_user_id() !== $post_author ) { |
|
641 |
$user_obj = get_userdata( $post_author ); |
|
642 |
||
643 |
if ( ! $user_obj ) { |
|
644 |
return new WP_Error( |
|
645 |
'rest_invalid_author', |
|
646 |
__( 'Invalid author ID.' ), |
|
647 |
array( 'status' => 400 ) |
|
648 |
); |
|
649 |
} |
|
650 |
} |
|
651 |
||
652 |
$changes->post_author = $post_author; |
|
653 |
} |
|
654 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
655 |
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
656 |
return apply_filters( "rest_pre_insert_{$this->post_type}", $changes, $request ); |
18 | 657 |
} |
658 |
||
659 |
/** |
|
660 |
* Prepare a single template output for response |
|
661 |
* |
|
662 |
* @since 5.8.0 |
|
19 | 663 |
* @since 5.9.0 Renamed `$template` to `$item` to match parent class for PHP 8 named parameter support. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
664 |
* @since 6.3.0 Added `modified` property to the response. |
18 | 665 |
* |
19 | 666 |
* @param WP_Block_Template $item Template instance. |
18 | 667 |
* @param WP_REST_Request $request Request object. |
19 | 668 |
* @return WP_REST_Response Response object. |
18 | 669 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
670 |
public function prepare_item_for_response( $item, $request ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
671 |
// Resolve pattern blocks so they don't need to be resolved client-side |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
672 |
// in the editor, improving performance. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
673 |
$blocks = parse_blocks( $item->content ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
674 |
$blocks = resolve_pattern_blocks( $blocks ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
675 |
$item->content = serialize_blocks( $blocks ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
676 |
|
19 | 677 |
// Restores the more descriptive, specific name for use within this method. |
678 |
$template = $item; |
|
679 |
||
680 |
$fields = $this->get_fields_for_response( $request ); |
|
681 |
||
682 |
// Base fields for every template. |
|
683 |
$data = array(); |
|
684 |
||
685 |
if ( rest_is_field_included( 'id', $fields ) ) { |
|
686 |
$data['id'] = $template->id; |
|
687 |
} |
|
688 |
||
689 |
if ( rest_is_field_included( 'theme', $fields ) ) { |
|
690 |
$data['theme'] = $template->theme; |
|
691 |
} |
|
18 | 692 |
|
19 | 693 |
if ( rest_is_field_included( 'content', $fields ) ) { |
694 |
$data['content'] = array(); |
|
695 |
} |
|
696 |
if ( rest_is_field_included( 'content.raw', $fields ) ) { |
|
697 |
$data['content']['raw'] = $template->content; |
|
698 |
} |
|
699 |
||
700 |
if ( rest_is_field_included( 'content.block_version', $fields ) ) { |
|
701 |
$data['content']['block_version'] = block_version( $template->content ); |
|
702 |
} |
|
703 |
||
704 |
if ( rest_is_field_included( 'slug', $fields ) ) { |
|
705 |
$data['slug'] = $template->slug; |
|
706 |
} |
|
707 |
||
708 |
if ( rest_is_field_included( 'source', $fields ) ) { |
|
709 |
$data['source'] = $template->source; |
|
710 |
} |
|
711 |
||
712 |
if ( rest_is_field_included( 'origin', $fields ) ) { |
|
713 |
$data['origin'] = $template->origin; |
|
714 |
} |
|
715 |
||
716 |
if ( rest_is_field_included( 'type', $fields ) ) { |
|
717 |
$data['type'] = $template->type; |
|
18 | 718 |
} |
719 |
||
19 | 720 |
if ( rest_is_field_included( 'description', $fields ) ) { |
721 |
$data['description'] = $template->description; |
|
722 |
} |
|
723 |
||
724 |
if ( rest_is_field_included( 'title', $fields ) ) { |
|
725 |
$data['title'] = array(); |
|
726 |
} |
|
727 |
||
728 |
if ( rest_is_field_included( 'title.raw', $fields ) ) { |
|
729 |
$data['title']['raw'] = $template->title; |
|
730 |
} |
|
731 |
||
732 |
if ( rest_is_field_included( 'title.rendered', $fields ) ) { |
|
733 |
if ( $template->wp_id ) { |
|
734 |
/** This filter is documented in wp-includes/post-template.php */ |
|
735 |
$data['title']['rendered'] = apply_filters( 'the_title', $template->title, $template->wp_id ); |
|
736 |
} else { |
|
737 |
$data['title']['rendered'] = $template->title; |
|
738 |
} |
|
739 |
} |
|
740 |
||
741 |
if ( rest_is_field_included( 'status', $fields ) ) { |
|
742 |
$data['status'] = $template->status; |
|
743 |
} |
|
18 | 744 |
|
19 | 745 |
if ( rest_is_field_included( 'wp_id', $fields ) ) { |
746 |
$data['wp_id'] = (int) $template->wp_id; |
|
747 |
} |
|
748 |
||
749 |
if ( rest_is_field_included( 'has_theme_file', $fields ) ) { |
|
750 |
$data['has_theme_file'] = (bool) $template->has_theme_file; |
|
751 |
} |
|
752 |
||
753 |
if ( rest_is_field_included( 'is_custom', $fields ) && 'wp_template' === $template->type ) { |
|
754 |
$data['is_custom'] = $template->is_custom; |
|
755 |
} |
|
756 |
||
757 |
if ( rest_is_field_included( 'author', $fields ) ) { |
|
758 |
$data['author'] = (int) $template->author; |
|
759 |
} |
|
760 |
||
761 |
if ( rest_is_field_included( 'area', $fields ) && 'wp_template_part' === $template->type ) { |
|
762 |
$data['area'] = $template->area; |
|
763 |
} |
|
764 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
765 |
if ( rest_is_field_included( 'modified', $fields ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
766 |
$data['modified'] = mysql_to_rfc3339( $template->modified ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
767 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
768 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
769 |
if ( rest_is_field_included( 'author_text', $fields ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
770 |
$data['author_text'] = self::get_wp_templates_author_text_field( $template ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
771 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
772 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
773 |
if ( rest_is_field_included( 'original_source', $fields ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
774 |
$data['original_source'] = self::get_wp_templates_original_source_field( $template ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
775 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
776 |
|
19 | 777 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
778 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
|
779 |
$data = $this->filter_response_by_context( $data, $context ); |
|
780 |
||
781 |
// Wrap the data in a response object. |
|
782 |
$response = rest_ensure_response( $data ); |
|
783 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
784 |
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
785 |
$links = $this->prepare_links( $template->id ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
786 |
$response->add_links( $links ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
787 |
if ( ! empty( $links['self']['href'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
788 |
$actions = $this->get_available_actions(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
789 |
$self = $links['self']['href']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
790 |
foreach ( $actions as $rel ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
791 |
$response->add_link( $rel, $self ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
792 |
} |
18 | 793 |
} |
794 |
} |
|
795 |
||
796 |
return $response; |
|
797 |
} |
|
798 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
799 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
800 |
* Returns the source from where the template originally comes from. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
801 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
802 |
* @since 6.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
803 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
804 |
* @param WP_Block_Template $template_object Template instance. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
805 |
* @return string Original source of the template one of theme, plugin, site, or user. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
806 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
807 |
private static function get_wp_templates_original_source_field( $template_object ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
808 |
if ( 'wp_template' === $template_object->type || 'wp_template_part' === $template_object->type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
809 |
// Added by theme. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
810 |
// Template originally provided by a theme, but customized by a user. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
811 |
// Templates originally didn't have the 'origin' field so identify |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
812 |
// older customized templates by checking for no origin and a 'theme' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
813 |
// or 'custom' source. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
814 |
if ( $template_object->has_theme_file && |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
815 |
( 'theme' === $template_object->origin || ( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
816 |
empty( $template_object->origin ) && in_array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
817 |
$template_object->source, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
818 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
819 |
'theme', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
820 |
'custom', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
821 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
822 |
true |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
823 |
) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
824 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
825 |
) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
826 |
return 'theme'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
827 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
828 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
829 |
// Added by plugin. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
830 |
if ( $template_object->has_theme_file && 'plugin' === $template_object->origin ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
831 |
return 'plugin'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
832 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
833 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
834 |
// Added by site. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
835 |
// Template was created from scratch, but has no author. Author support |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
836 |
// was only added to templates in WordPress 5.9. Fallback to showing the |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
837 |
// site logo and title. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
838 |
if ( empty( $template_object->has_theme_file ) && 'custom' === $template_object->source && empty( $template_object->author ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
839 |
return 'site'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
840 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
841 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
842 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
843 |
// Added by user. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
844 |
return 'user'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
845 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
846 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
847 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
848 |
* Returns a human readable text for the author of the template. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
849 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
850 |
* @since 6.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
851 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
852 |
* @param WP_Block_Template $template_object Template instance. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
853 |
* @return string Human readable text for the author. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
854 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
855 |
private static function get_wp_templates_author_text_field( $template_object ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
856 |
$original_source = self::get_wp_templates_original_source_field( $template_object ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
857 |
switch ( $original_source ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
858 |
case 'theme': |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
859 |
$theme_name = wp_get_theme( $template_object->theme )->get( 'Name' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
860 |
return empty( $theme_name ) ? $template_object->theme : $theme_name; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
861 |
case 'plugin': |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
862 |
$plugins = get_plugins(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
863 |
$plugin = $plugins[ plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ) ]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
864 |
return empty( $plugin['Name'] ) ? $template_object->theme : $plugin['Name']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
865 |
case 'site': |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
866 |
return get_bloginfo( 'name' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
867 |
case 'user': |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
868 |
$author = get_user_by( 'id', $template_object->author ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
869 |
if ( ! $author ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
870 |
return __( 'Unknown author' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
871 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
872 |
return $author->get( 'display_name' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
873 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
874 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
875 |
|
18 | 876 |
|
877 |
/** |
|
878 |
* Prepares links for the request. |
|
879 |
* |
|
880 |
* @since 5.8.0 |
|
881 |
* |
|
882 |
* @param integer $id ID. |
|
883 |
* @return array Links for the given post. |
|
884 |
*/ |
|
885 |
protected function prepare_links( $id ) { |
|
886 |
$links = array( |
|
887 |
'self' => array( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
888 |
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), |
18 | 889 |
), |
890 |
'collection' => array( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
891 |
'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ), |
18 | 892 |
), |
893 |
'about' => array( |
|
894 |
'href' => rest_url( 'wp/v2/types/' . $this->post_type ), |
|
895 |
), |
|
896 |
); |
|
897 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
898 |
if ( post_type_supports( $this->post_type, 'revisions' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
899 |
$template = get_block_template( $id, $this->post_type ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
900 |
if ( $template instanceof WP_Block_Template && ! empty( $template->wp_id ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
901 |
$revisions = wp_get_latest_revision_id_and_total_count( $template->wp_id ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
902 |
$revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
903 |
$revisions_base = sprintf( '/%s/%s/%s/revisions', $this->namespace, $this->rest_base, $id ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
904 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
905 |
$links['version-history'] = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
906 |
'href' => rest_url( $revisions_base ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
907 |
'count' => $revisions_count, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
908 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
909 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
910 |
if ( $revisions_count > 0 ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
911 |
$links['predecessor-version'] = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
912 |
'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
913 |
'id' => $revisions['latest_id'], |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
914 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
915 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
916 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
917 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
918 |
|
18 | 919 |
return $links; |
920 |
} |
|
921 |
||
922 |
/** |
|
923 |
* Get the link relations available for the post and current user. |
|
924 |
* |
|
925 |
* @since 5.8.0 |
|
926 |
* |
|
19 | 927 |
* @return string[] List of link relations. |
18 | 928 |
*/ |
929 |
protected function get_available_actions() { |
|
930 |
$rels = array(); |
|
931 |
||
932 |
$post_type = get_post_type_object( $this->post_type ); |
|
933 |
||
934 |
if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
935 |
$rels[] = 'https://api.w.org/action-publish'; |
|
936 |
} |
|
937 |
||
938 |
if ( current_user_can( 'unfiltered_html' ) ) { |
|
939 |
$rels[] = 'https://api.w.org/action-unfiltered-html'; |
|
940 |
} |
|
941 |
||
942 |
return $rels; |
|
943 |
} |
|
944 |
||
945 |
/** |
|
946 |
* Retrieves the query params for the posts collection. |
|
947 |
* |
|
948 |
* @since 5.8.0 |
|
19 | 949 |
* @since 5.9.0 Added `'area'` and `'post_type'`. |
18 | 950 |
* |
951 |
* @return array Collection parameters. |
|
952 |
*/ |
|
953 |
public function get_collection_params() { |
|
954 |
return array( |
|
19 | 955 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
956 |
'wp_id' => array( |
|
18 | 957 |
'description' => __( 'Limit to the specified post id.' ), |
958 |
'type' => 'integer', |
|
959 |
), |
|
19 | 960 |
'area' => array( |
961 |
'description' => __( 'Limit to the specified template part area.' ), |
|
962 |
'type' => 'string', |
|
963 |
), |
|
964 |
'post_type' => array( |
|
965 |
'description' => __( 'Post type to get the templates for.' ), |
|
966 |
'type' => 'string', |
|
967 |
), |
|
18 | 968 |
); |
969 |
} |
|
970 |
||
971 |
/** |
|
972 |
* Retrieves the block type' schema, conforming to JSON Schema. |
|
973 |
* |
|
974 |
* @since 5.8.0 |
|
19 | 975 |
* @since 5.9.0 Added `'area'`. |
18 | 976 |
* |
977 |
* @return array Item schema data. |
|
978 |
*/ |
|
979 |
public function get_item_schema() { |
|
980 |
if ( $this->schema ) { |
|
981 |
return $this->add_additional_fields_schema( $this->schema ); |
|
982 |
} |
|
983 |
||
984 |
$schema = array( |
|
985 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
986 |
'title' => $this->post_type, |
|
987 |
'type' => 'object', |
|
988 |
'properties' => array( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
989 |
'id' => array( |
18 | 990 |
'description' => __( 'ID of template.' ), |
991 |
'type' => 'string', |
|
992 |
'context' => array( 'embed', 'view', 'edit' ), |
|
993 |
'readonly' => true, |
|
994 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
995 |
'slug' => array( |
18 | 996 |
'description' => __( 'Unique slug identifying the template.' ), |
997 |
'type' => 'string', |
|
998 |
'context' => array( 'embed', 'view', 'edit' ), |
|
999 |
'required' => true, |
|
1000 |
'minLength' => 1, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1001 |
'pattern' => '[a-zA-Z0-9_\%-]+', |
18 | 1002 |
), |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1003 |
'theme' => array( |
18 | 1004 |
'description' => __( 'Theme identifier for the template.' ), |
1005 |
'type' => 'string', |
|
1006 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1007 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1008 |
'type' => array( |
19 | 1009 |
'description' => __( 'Type of template.' ), |
1010 |
'type' => 'string', |
|
1011 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1012 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1013 |
'source' => array( |
18 | 1014 |
'description' => __( 'Source of template' ), |
1015 |
'type' => 'string', |
|
1016 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1017 |
'readonly' => true, |
|
1018 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1019 |
'origin' => array( |
19 | 1020 |
'description' => __( 'Source of a customized template' ), |
1021 |
'type' => 'string', |
|
1022 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1023 |
'readonly' => true, |
|
1024 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1025 |
'content' => array( |
18 | 1026 |
'description' => __( 'Content of template.' ), |
1027 |
'type' => array( 'object', 'string' ), |
|
1028 |
'default' => '', |
|
1029 |
'context' => array( 'embed', 'view', 'edit' ), |
|
19 | 1030 |
'properties' => array( |
1031 |
'raw' => array( |
|
1032 |
'description' => __( 'Content for the template, as it exists in the database.' ), |
|
1033 |
'type' => 'string', |
|
1034 |
'context' => array( 'view', 'edit' ), |
|
1035 |
), |
|
1036 |
'block_version' => array( |
|
1037 |
'description' => __( 'Version of the content block format used by the template.' ), |
|
1038 |
'type' => 'integer', |
|
1039 |
'context' => array( 'edit' ), |
|
1040 |
'readonly' => true, |
|
1041 |
), |
|
1042 |
), |
|
18 | 1043 |
), |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1044 |
'title' => array( |
18 | 1045 |
'description' => __( 'Title of template.' ), |
1046 |
'type' => array( 'object', 'string' ), |
|
1047 |
'default' => '', |
|
1048 |
'context' => array( 'embed', 'view', 'edit' ), |
|
19 | 1049 |
'properties' => array( |
1050 |
'raw' => array( |
|
1051 |
'description' => __( 'Title for the template, as it exists in the database.' ), |
|
1052 |
'type' => 'string', |
|
1053 |
'context' => array( 'view', 'edit', 'embed' ), |
|
1054 |
), |
|
1055 |
'rendered' => array( |
|
1056 |
'description' => __( 'HTML title for the template, transformed for display.' ), |
|
1057 |
'type' => 'string', |
|
1058 |
'context' => array( 'view', 'edit', 'embed' ), |
|
1059 |
'readonly' => true, |
|
1060 |
), |
|
1061 |
), |
|
18 | 1062 |
), |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1063 |
'description' => array( |
18 | 1064 |
'description' => __( 'Description of template.' ), |
1065 |
'type' => 'string', |
|
1066 |
'default' => '', |
|
1067 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1068 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1069 |
'status' => array( |
18 | 1070 |
'description' => __( 'Status of template.' ), |
1071 |
'type' => 'string', |
|
19 | 1072 |
'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
18 | 1073 |
'default' => 'publish', |
1074 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1075 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1076 |
'wp_id' => array( |
18 | 1077 |
'description' => __( 'Post ID.' ), |
1078 |
'type' => 'integer', |
|
1079 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1080 |
'readonly' => true, |
|
1081 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1082 |
'has_theme_file' => array( |
18 | 1083 |
'description' => __( 'Theme file exists.' ), |
1084 |
'type' => 'bool', |
|
1085 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1086 |
'readonly' => true, |
|
1087 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1088 |
'author' => array( |
19 | 1089 |
'description' => __( 'The ID for the author of the template.' ), |
1090 |
'type' => 'integer', |
|
1091 |
'context' => array( 'view', 'edit', 'embed' ), |
|
1092 |
), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1093 |
'modified' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1094 |
'description' => __( "The date the template was last modified, in the site's timezone." ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1095 |
'type' => 'string', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1096 |
'format' => 'date-time', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1097 |
'context' => array( 'view', 'edit' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1098 |
'readonly' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1099 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1100 |
'author_text' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1101 |
'type' => 'string', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1102 |
'description' => __( 'Human readable text for the author.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1103 |
'readonly' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1104 |
'context' => array( 'view', 'edit', 'embed' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1105 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1106 |
'original_source' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1107 |
'description' => __( 'Where the template originally comes from e.g. \'theme\'' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1108 |
'type' => 'string', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1109 |
'readonly' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1110 |
'context' => array( 'view', 'edit', 'embed' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1111 |
'enum' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1112 |
'theme', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1113 |
'plugin', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1114 |
'site', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1115 |
'user', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1116 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1117 |
), |
18 | 1118 |
), |
1119 |
); |
|
1120 |
||
19 | 1121 |
if ( 'wp_template' === $this->post_type ) { |
1122 |
$schema['properties']['is_custom'] = array( |
|
1123 |
'description' => __( 'Whether a template is a custom template.' ), |
|
1124 |
'type' => 'bool', |
|
1125 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1126 |
'readonly' => true, |
|
1127 |
); |
|
1128 |
} |
|
1129 |
||
1130 |
if ( 'wp_template_part' === $this->post_type ) { |
|
1131 |
$schema['properties']['area'] = array( |
|
1132 |
'description' => __( 'Where the template part is intended for use (header, footer, etc.)' ), |
|
1133 |
'type' => 'string', |
|
1134 |
'context' => array( 'embed', 'view', 'edit' ), |
|
1135 |
); |
|
1136 |
} |
|
1137 |
||
18 | 1138 |
$this->schema = $schema; |
1139 |
||
1140 |
return $this->add_additional_fields_schema( $this->schema ); |
|
1141 |
} |
|
1142 |
} |