PluginProbe
Gutenberg / 9.5.0
Gutenberg v9.5.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / rest-api.php

rest-api.php in Gutenberg 9.5.0, at lib/rest-api.php

357 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHP and WordPress configuration compatibility functions for the Gutenberg
4 * editor plugin changes related to REST API.
5 *
6 * @package gutenberg
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die( 'Silence is golden.' );
11 }
12
13 /**
14 * Handle a failing oEmbed proxy request to try embedding as a shortcode.
15 *
16 * @see https://core.trac.wordpress.org/ticket/45447
17 *
18 * @since 2.3.0
19 *
20 * @param WP_HTTP_Response|WP_Error $response The REST Request response.
21 * @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server).
22 * @param WP_REST_Request $request Request used to generate the response.
23 * @return WP_HTTP_Response|object|WP_Error The REST Request response.
24 */
25 function gutenberg_filter_oembed_result( $response, $handler, $request ) {
26 if ( ! is_wp_error( $response ) || 'oembed_invalid_url' !== $response->get_error_code() ||
27 '/oembed/1.0/proxy' !== $request->get_route() ) {
28 return $response;
29 }
30
31 // Try using a classic embed instead.
32 global $wp_embed;
33 $html = $wp_embed->shortcode( array(), $_GET['url'] );
34 if ( ! $html ) {
35 return $response;
36 }
37
38 global $wp_scripts;
39
40 // Check if any scripts were enqueued by the shortcode, and include them in
41 // the response.
42 $enqueued_scripts = array();
43 foreach ( $wp_scripts->queue as $script ) {
44 $enqueued_scripts[] = $wp_scripts->registered[ $script ]->src;
45 }
46
47 return array(
48 'provider_name' => __( 'Embed Handler', 'gutenberg' ),
49 'html' => $html,
50 'scripts' => $enqueued_scripts,
51 );
52 }
53 add_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result', 10, 3 );
54
55 /**
56 * Add fields required for site editing to the /themes endpoint.
57 *
58 * @todo Remove once Gutenberg's minimum required WordPress version is v5.5.
59 * @see https://core.trac.wordpress.org/ticket/49906
60 * @see https://core.trac.wordpress.org/changeset/47921
61 *
62 * @param WP_REST_Response $response The response object.
63 * @param WP_Theme $theme Theme object used to create response.
64 */
65 function gutenberg_filter_rest_prepare_theme( $response, $theme ) {
66 $data = $response->get_data();
67 $fields = array_keys( $data );
68
69 /**
70 * The following is basically copied from Core's WP_REST_Themes_Controller::prepare_item_for_response()
71 * (as of WP v5.5), with `rest_is_field_included()` replaced by `! in_array()`.
72 * This makes sure that we add all the fields that are missing from Core.
73 *
74 * @see https://github.com/WordPress/WordPress/blob/019bc2d244c4d536338d2c634419583e928143df/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php#L118-L167
75 */
76 if ( ! in_array( 'stylesheet', $fields, true ) ) {
77 $data['stylesheet'] = $theme->get_stylesheet();
78 }
79
80 if ( ! in_array( 'template', $fields, true ) ) {
81 /**
82 * Use the get_template() method, not the 'Template' header, for finding the template.
83 * The 'Template' header is only good for what was written in the style.css, while
84 * get_template() takes into account where WordPress actually located the theme and
85 * whether it is actually valid.
86 */
87 $data['template'] = $theme->get_template();
88 }
89
90 $plain_field_mappings = array(
91 'requires_php' => 'RequiresPHP',
92 'requires_wp' => 'RequiresWP',
93 'textdomain' => 'TextDomain',
94 'version' => 'Version',
95 );
96
97 foreach ( $plain_field_mappings as $field => $header ) {
98 if ( ! in_array( $field, $fields, true ) ) {
99 $data[ $field ] = $theme->get( $header );
100 }
101 }
102
103 if ( ! in_array( 'screenshot', $fields, true ) ) {
104 // Using $theme->get_screenshot() with no args to get absolute URL.
105 $data['screenshot'] = $theme->get_screenshot() ? $theme->get_screenshot() : '';
106 }
107
108 $rich_field_mappings = array(
109 'author' => 'Author',
110 'author_uri' => 'AuthorURI',
111 'description' => 'Description',
112 'name' => 'Name',
113 'tags' => 'Tags',
114 'theme_uri' => 'ThemeURI',
115 );
116
117 foreach ( $rich_field_mappings as $field => $header ) {
118 if ( ! in_array( $field, $fields, true ) ) {
119 $data[ $field ]['raw'] = $theme->display( $header, false, true );
120 $data[ $field ]['rendered'] = $theme->display( $header );
121 }
122 }
123
124 $response->set_data( $data );
125 return $response;
126 }
127 add_filter( 'rest_prepare_theme', 'gutenberg_filter_rest_prepare_theme', 10, 2 );
128
129 /**
130 * Registers the block directory.
131 *
132 * @since 6.5.0
133 */
134 function gutenberg_register_rest_block_directory() {
135 $block_directory_controller = new WP_REST_Block_Directory_Controller();
136 $block_directory_controller->register_routes();
137 }
138 add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' );
139
140 /**
141 * Registers the Block types REST API routes.
142 */
143 function gutenberg_register_block_type() {
144 $block_types = new WP_REST_Block_Types_Controller();
145 $block_types->register_routes();
146 }
147 add_action( 'rest_api_init', 'gutenberg_register_block_type' );
148 /**
149 * Registers the menu locations area REST API routes.
150 */
151 function gutenberg_register_rest_menu_location() {
152 $nav_menu_location = new WP_REST_Menu_Locations_Controller();
153 $nav_menu_location->register_routes();
154 }
155 add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' );
156
157 /**
158 * Registers the menu locations area REST API routes.
159 */
160 function gutenberg_register_rest_customizer_nonces() {
161 $nav_menu_location = new WP_Rest_Customizer_Nonces();
162 $nav_menu_location->register_routes();
163 }
164 add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' );
165
166
167 /**
168 * Registers the Plugins REST API routes.
169 */
170 function gutenberg_register_plugins_endpoint() {
171 $plugins = new WP_REST_Plugins_Controller();
172 $plugins->register_routes();
173 }
174 add_action( 'rest_api_init', 'gutenberg_register_plugins_endpoint' );
175
176 /**
177 * Registers the Sidebars & Widgets REST API routes.
178 */
179 function gutenberg_register_sidebars_and_widgets_endpoint() {
180 $sidebars = new WP_REST_Sidebars_Controller();
181 $sidebars->register_routes();
182
183 $widget_types = new WP_REST_Widget_Types_Controller();
184 $widget_types->register_routes();
185 $widgets = new WP_REST_Widgets_Controller();
186 $widgets->register_routes();
187 }
188 add_action( 'rest_api_init', 'gutenberg_register_sidebars_and_widgets_endpoint' );
189
190 /**
191 * Registers the Batch REST API routes.
192 */
193 function gutenberg_register_batch_endpoint() {
194 $batch = new WP_REST_Batch_Controller();
195 $batch->register_routes();
196 }
197 add_action( 'rest_api_init', 'gutenberg_register_batch_endpoint' );
198
199 /**
200 * Hook in to the nav menu item post type and enable a post type rest endpoint.
201 *
202 * @param array $args Current registered post type args.
203 * @param string $post_type Name of post type.
204 *
205 * @return array
206 */
207 function wp_api_nav_menus_post_type_args( $args, $post_type ) {
208 if ( 'nav_menu_item' === $post_type ) {
209 $args['show_in_rest'] = true;
210 $args['rest_base'] = 'menu-items';
211 $args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller';
212 }
213
214 return $args;
215 }
216 add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );
217
218 /**
219 * Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint.
220 *
221 * @param array $args Current registered taxonomy args.
222 * @param string $taxonomy Name of taxonomy.
223 *
224 * @return array
225 */
226 function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) {
227 if ( 'nav_menu' === $taxonomy ) {
228 $args['show_in_rest'] = true;
229 $args['rest_base'] = 'menus';
230 $args['rest_controller_class'] = 'WP_REST_Menus_Controller';
231 }
232
233 return $args;
234 }
235 add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );
236
237 /**
238 * Shim for get_sample_permalink() to add support for auto-draft status.
239 *
240 * This function filters the return from get_sample_permalink() and essentially
241 * re-runs the same logic minus the filters, but pretends a status of auto-save
242 * is actually publish in order to return the future permalink format.
243 *
244 * This is a temporary fix until we can patch get_sample_permalink()
245 *
246 * @see https://core.trac.wordpress.org/ticket/46266
247 *
248 * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
249 * @param int $id ID of the post.
250 * @param string $title Title of the post.
251 * @param string $name Slug of the post.
252 * @param object $post WP_Post object.
253 *
254 * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
255 */
256 function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) {
257 if ( 'auto-draft' !== $post->post_status ) {
258 return $permalink;
259 }
260 $ptype = get_post_type_object( $post->post_type );
261
262 $original_status = $post->post_status;
263 $original_date = $post->post_date;
264 $original_name = $post->post_name;
265
266 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
267 $post->post_status = 'publish';
268 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
269
270 // If the user wants to set a new name -- override the current one.
271 // Note: if empty name is supplied -- use the title instead, see #6072.
272 if ( ! is_null( $name ) ) {
273 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
274 }
275
276 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
277
278 $post->filter = 'sample';
279
280 $permalink = get_permalink( $post, true );
281
282 // Replace custom post_type Token with generic pagename token for ease of use.
283 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
284
285 // Handle page hierarchy.
286 if ( $ptype->hierarchical ) {
287 $uri = get_page_uri( $post );
288 if ( $uri ) {
289 $uri = untrailingslashit( $uri );
290 $uri = strrev( stristr( strrev( $uri ), '/' ) );
291 $uri = untrailingslashit( $uri );
292 }
293
294 if ( ! empty( $uri ) ) {
295 $uri .= '/';
296 }
297 $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
298 }
299
300 $permalink = array( $permalink, $post->post_name );
301 $post->post_status = $original_status;
302 $post->post_date = $original_date;
303 $post->post_name = $original_name;
304 unset( $post->filter );
305
306 return $permalink;
307 }
308 add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );
309
310 /**
311 * Registers the image editor.
312 *
313 * @since 7.x.0
314 */
315 function gutenberg_register_image_editor() {
316 global $wp_version;
317
318 // Strip '-src' from the version string. Messes up version_compare().
319 $version = str_replace( '-src', '', $wp_version );
320
321 // Only register routes for versions older than WP 5.5.
322 if ( version_compare( $version, '5.5-beta', '<' ) ) {
323 $image_editor = new WP_REST_Image_Editor_Controller();
324 $image_editor->register_routes();
325 }
326 }
327 add_filter( 'rest_api_init', 'gutenberg_register_image_editor' );
328
329 /**
330 * Registers the post format search handler.
331 *
332 * @param string $search_handlers Title list of current handlers.
333 *
334 * @return array Title updated list of handlers.
335 */
336 function gutenberg_post_format_search_handler( $search_handlers ) {
337 if ( current_theme_supports( 'post-formats' ) ) {
338 $search_handlers[] = new WP_REST_Post_Format_Search_Handler();
339 }
340
341 return $search_handlers;
342 }
343 add_filter( 'wp_rest_search_handlers', 'gutenberg_post_format_search_handler', 10, 5 );
344
345 /**
346 * Registers the terms search handler.
347 *
348 * @param string $search_handlers Title list of current handlers.
349 *
350 * @return array Title updated list of handlers.
351 */
352 function gutenberg_term_search_handler( $search_handlers ) {
353 $search_handlers[] = new WP_REST_Term_Search_Handler();
354 return $search_handlers;
355 }
356 add_filter( 'wp_rest_search_handlers', 'gutenberg_term_search_handler', 10, 5 );
357