PluginProbe
Gutenberg / 8.3.0
Gutenberg v8.3.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 8.3.0, at lib/rest-api.php

282 lines 8.7 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 https://core.trac.wordpress.org/ticket/49906 is fixed.
59 * @see https://github.com/WordPress/wordpress-develop/pull/222
60 *
61 * @param WP_REST_Response $response The response object.
62 * @param WP_Theme $theme Theme object used to create response.
63 * @param WP_REST_Request $request Request object.
64 */
65 function gutenberg_filter_rest_prepare_theme( $response, $theme, $request ) {
66 $data = $response->get_data();
67 $field_mappings = array(
68 'author' => 'Author',
69 'author_name' => 'Author Name',
70 'author_uri' => 'Author URI',
71 'description' => 'Description',
72 'name' => 'Name',
73 'stylesheet' => 'Stylesheet',
74 'template' => 'Template',
75 'theme_uri' => 'Theme URI',
76 'version' => 'Version',
77 );
78
79 foreach ( $field_mappings as $field => $theme_field ) {
80 $data[ $field ] = $theme[ $theme_field ];
81 }
82
83 // Using $theme->get_screenshot() with no args to get absolute URL.
84 $data['screenshot'] = $theme->get_screenshot();
85
86 $response->set_data( $data );
87 return $response;
88 }
89 add_filter( 'rest_prepare_theme', 'gutenberg_filter_rest_prepare_theme', 10, 3 );
90
91 /**
92 * Start: Include for phase 2
93 */
94 /**
95 * Registers the REST API routes needed by the legacy widget block.
96 *
97 * @since 5.0.0
98 */
99 function gutenberg_register_rest_widget_updater_routes() {
100 $widget_forms = new WP_REST_Widget_Forms();
101 $widget_forms->register_routes();
102 }
103 add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' );
104
105 /**
106 * Registers the widget area REST API routes.
107 *
108 * @since 5.7.0
109 */
110 function gutenberg_register_rest_widget_areas() {
111 $widget_areas_controller = new WP_REST_Widget_Areas_Controller();
112 $widget_areas_controller->register_routes();
113 }
114 add_action( 'rest_api_init', 'gutenberg_register_rest_widget_areas' );
115
116 /**
117 * Registers the block directory.
118 *
119 * @since 6.5.0
120 */
121 function gutenberg_register_rest_block_directory() {
122 if ( ! gutenberg_is_experiment_enabled( 'gutenberg-block-directory' ) ) {
123 return;
124 }
125
126 $block_directory_controller = new WP_REST_Block_Directory_Controller();
127 $block_directory_controller->register_routes();
128 }
129 add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' );
130
131 /**
132 * Registers the Block types REST API routes.
133 */
134 function gutenberg_register_block_type() {
135 $block_types = new WP_REST_Block_Types_Controller();
136 $block_types->register_routes();
137 }
138 add_action( 'rest_api_init', 'gutenberg_register_block_type' );
139 /**
140 * Registers the menu locations area REST API routes.
141 */
142 function gutenberg_register_rest_menu_location() {
143 $nav_menu_location = new WP_REST_Menu_Locations_Controller();
144 $nav_menu_location->register_routes();
145 }
146 add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' );
147
148 /**
149 * Registers the menu locations area REST API routes.
150 */
151 function gutenberg_register_rest_customizer_nonces() {
152 $nav_menu_location = new WP_Rest_Customizer_Nonces();
153 $nav_menu_location->register_routes();
154 }
155 add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' );
156
157 /**
158 * Hook in to the nav menu item post type and enable a post type rest endpoint.
159 *
160 * @param array $args Current registered post type args.
161 * @param string $post_type Name of post type.
162 *
163 * @return array
164 */
165 function wp_api_nav_menus_post_type_args( $args, $post_type ) {
166 if ( 'nav_menu_item' === $post_type ) {
167 $args['show_in_rest'] = true;
168 $args['rest_base'] = 'menu-items';
169 $args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller';
170 }
171
172 return $args;
173 }
174 add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );
175
176 /**
177 * Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint.
178 *
179 * @param array $args Current registered taxonomy args.
180 * @param string $taxonomy Name of taxonomy.
181 *
182 * @return array
183 */
184 function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) {
185 if ( 'nav_menu' === $taxonomy ) {
186 $args['show_in_rest'] = true;
187 $args['rest_base'] = 'menus';
188 $args['rest_controller_class'] = 'WP_REST_Menus_Controller';
189 }
190
191 return $args;
192 }
193 add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );
194
195 /**
196 * Shim for get_sample_permalink() to add support for auto-draft status.
197 *
198 * This function filters the return from get_sample_permalink() and essentially
199 * re-runs the same logic minus the filters, but pretends a status of auto-save
200 * is actually publish in order to return the future permalink format.
201 *
202 * This is a temporary fix until we can patch get_sample_permalink()
203 *
204 * @see https://core.trac.wordpress.org/ticket/46266
205 *
206 * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
207 * @param int $id ID of the post.
208 * @param string $title Title of the post.
209 * @param string $name Slug of the post.
210 * @param object $post WP_Post object.
211 *
212 * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
213 */
214 function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) {
215 if ( 'auto-draft' !== $post->post_status ) {
216 return $permalink;
217 }
218 $ptype = get_post_type_object( $post->post_type );
219
220 $original_status = $post->post_status;
221 $original_date = $post->post_date;
222 $original_name = $post->post_name;
223
224 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
225 $post->post_status = 'publish';
226 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
227
228 // If the user wants to set a new name -- override the current one.
229 // Note: if empty name is supplied -- use the title instead, see #6072.
230 if ( ! is_null( $name ) ) {
231 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
232 }
233
234 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
235
236 $post->filter = 'sample';
237
238 $permalink = get_permalink( $post, true );
239
240 // Replace custom post_type Token with generic pagename token for ease of use.
241 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
242
243 // Handle page hierarchy.
244 if ( $ptype->hierarchical ) {
245 $uri = get_page_uri( $post );
246 if ( $uri ) {
247 $uri = untrailingslashit( $uri );
248 $uri = strrev( stristr( strrev( $uri ), '/' ) );
249 $uri = untrailingslashit( $uri );
250 }
251
252 if ( ! empty( $uri ) ) {
253 $uri .= '/';
254 }
255 $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
256 }
257
258 $permalink = array( $permalink, $post->post_name );
259 $post->post_status = $original_status;
260 $post->post_date = $original_date;
261 $post->post_name = $original_name;
262 unset( $post->filter );
263
264 return $permalink;
265 }
266 add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );
267
268 /**
269 * Registers the image editor.
270 *
271 * @since 7.x.0
272 */
273 function gutenberg_register_image_editor() {
274 if ( ! gutenberg_is_experiment_enabled( 'gutenberg-rich-image-editing' ) ) {
275 return;
276 }
277
278 $image_editor = new WP_REST_Image_Editor_Controller();
279 $image_editor->register_routes();
280 }
281 add_filter( 'rest_api_init', 'gutenberg_register_image_editor' );
282