PluginProbe
Gutenberg / 9.2.2
Gutenberg v9.2.2
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.2.2, at lib/rest-api.php

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