PluginProbe
Gutenberg / 10.1.0
Gutenberg v10.1.0
24.0.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 All 403 releases
← All changes | lib/rest-api.php +187 -20 23.7.210.1.0 View file →
@@ -9,39 +9,206 @@
9 9 if ( ! defined( 'ABSPATH' ) ) {
10 10 die( 'Silence is golden.' );
11 11 }
12 12
13 +
13 14 /**
14 - * Overrides the REST controller for the `wp_global_styles` post type.
15 + * Registers the REST API routes for URL Details.
15 16 *
16 - * @param array $args Array of arguments for registering a post type.
17 - * See the register_post_type() function for accepted arguments.
17 + * @since 5.0.0
18 + */
19 +function gutenberg_register_url_details_routes() {
20 + $url_details_controller = new WP_REST_URL_Details_Controller();
21 + $url_details_controller->register_routes();
22 +}
23 +add_action( 'rest_api_init', 'gutenberg_register_url_details_routes' );
24 +
25 +/**
26 + * Registers the block pattern directory.
27 + */
28 +function gutenberg_register_rest_pattern_directory() {
29 + $block_directory_controller = new WP_REST_Pattern_Directory_Controller();
30 + $block_directory_controller->register_routes();
31 +}
32 +add_filter( 'rest_api_init', 'gutenberg_register_rest_pattern_directory' );
33 +
34 +/**
35 + * Registers the menu locations area REST API routes.
36 + */
37 +function gutenberg_register_rest_menu_location() {
38 + $nav_menu_location = new WP_REST_Menu_Locations_Controller();
39 + $nav_menu_location->register_routes();
40 +}
41 +add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' );
42 +
43 +/**
44 + * Registers the menu locations area REST API routes.
45 + */
46 +function gutenberg_register_rest_customizer_nonces() {
47 + $nav_menu_location = new WP_Rest_Customizer_Nonces();
48 + $nav_menu_location->register_routes();
49 +}
50 +add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' );
51 +
52 +/**
53 + * Registers the Sidebars & Widgets REST API routes.
54 + */
55 +function gutenberg_register_sidebars_and_widgets_endpoint() {
56 + $sidebars = new WP_REST_Sidebars_Controller();
57 + $sidebars->register_routes();
58 +
59 + $widgets = new WP_REST_Widgets_Controller();
60 + $widgets->register_routes();
61 +
62 + $widget_types = new WP_REST_Widget_Types_Controller();
63 + $widget_types->register_routes();
64 +}
65 +add_action( 'rest_api_init', 'gutenberg_register_sidebars_and_widgets_endpoint' );
66 +
67 +/**
68 + * Registers the Batch REST API routes.
69 + */
70 +function gutenberg_register_batch_endpoint() {
71 + $batch = new WP_REST_Batch_Controller();
72 + $batch->register_routes();
73 +}
74 +add_action( 'rest_api_init', 'gutenberg_register_batch_endpoint' );
75 +
76 +/**
77 + * Hook in to the nav menu item post type and enable a post type rest endpoint.
18 78 *
19 - * @return array Array of arguments for registering a post type.
79 + * @param array $args Current registered post type args.
80 + * @param string $post_type Name of post type.
81 + *
82 + * @return array
20 83 */
21 -function gutenberg_override_global_styles_endpoint( array $args ): array {
22 - $args['rest_controller_class'] = 'WP_REST_Global_Styles_Controller_Gutenberg';
23 - $args['late_route_registration'] = true;
24 - $args['show_in_rest'] = true;
25 - $args['rest_base'] = 'global-styles';
84 +function wp_api_nav_menus_post_type_args( $args, $post_type ) {
85 + if ( 'nav_menu_item' === $post_type ) {
86 + $args['show_in_rest'] = true;
87 + $args['rest_base'] = 'menu-items';
88 + $args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller';
89 + }
26 90
27 91 return $args;
28 92 }
29 -add_filter( 'register_wp_global_styles_post_type_args', 'gutenberg_override_global_styles_endpoint' );
93 +add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );
30 94
31 95 /**
32 - * Registers the Edit Site Export REST API routes.
96 + * Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint.
97 + *
98 + * @param array $args Current registered taxonomy args.
99 + * @param string $taxonomy Name of taxonomy.
100 + *
101 + * @return array
33 102 */
34 -function gutenberg_register_edit_site_export_controller_endpoints() {
35 - $edit_site_export_controller = new WP_REST_Edit_Site_Export_Controller_Gutenberg();
36 - $edit_site_export_controller->register_routes();
103 +function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) {
104 + if ( 'nav_menu' === $taxonomy ) {
105 + $args['show_in_rest'] = true;
106 + $args['rest_base'] = 'menus';
107 + $args['rest_controller_class'] = 'WP_REST_Menus_Controller';
108 + }
109 +
110 + return $args;
37 111 }
38 -add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' );
112 +add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );
39 113
40 114 /**
41 - * Registers the Icons Registry REST API routes.
115 + * Shim for get_sample_permalink() to add support for auto-draft status.
116 + *
117 + * This function filters the return from get_sample_permalink() and essentially
118 + * re-runs the same logic minus the filters, but pretends a status of auto-save
119 + * is actually publish in order to return the future permalink format.
120 + *
121 + * This is a temporary fix until we can patch get_sample_permalink()
122 + *
123 + * @see https://core.trac.wordpress.org/ticket/46266
124 + *
125 + * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
126 + * @param int $id ID of the post.
127 + * @param string $title Title of the post.
128 + * @param string $name Slug of the post.
129 + * @param object $post WP_Post object.
130 + *
131 + * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
42 132 */
43 -function gutenberg_register_icon_controller_endpoints() {
44 - $icons_controller = new WP_REST_Icons_Controller_Gutenberg();
45 - $icons_controller->register_routes();
133 +function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) {
134 + if ( 'auto-draft' !== $post->post_status ) {
135 + return $permalink;
136 + }
137 + $ptype = get_post_type_object( $post->post_type );
138 +
139 + $original_status = $post->post_status;
140 + $original_date = $post->post_date;
141 + $original_name = $post->post_name;
142 +
143 + // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
144 + $post->post_status = 'publish';
145 + $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
146 +
147 + // If the user wants to set a new name -- override the current one.
148 + // Note: if empty name is supplied -- use the title instead, see #6072.
149 + if ( ! is_null( $name ) ) {
150 + $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
151 + }
152 +
153 + $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
154 +
155 + $post->filter = 'sample';
156 +
157 + $permalink = get_permalink( $post, true );
158 +
159 + // Replace custom post_type Token with generic pagename token for ease of use.
160 + $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
161 +
162 + // Handle page hierarchy.
163 + if ( $ptype->hierarchical ) {
164 + $uri = get_page_uri( $post );
165 + if ( $uri ) {
166 + $uri = untrailingslashit( $uri );
167 + $uri = strrev( stristr( strrev( $uri ), '/' ) );
168 + $uri = untrailingslashit( $uri );
169 + }
170 +
171 + if ( ! empty( $uri ) ) {
172 + $uri .= '/';
173 + }
174 + $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
175 + }
176 +
177 + $permalink = array( $permalink, $post->post_name );
178 + $post->post_status = $original_status;
179 + $post->post_date = $original_date;
180 + $post->post_name = $original_name;
181 + unset( $post->filter );
182 +
183 + return $permalink;
46 184 }
47 -add_action( 'rest_api_init', 'gutenberg_register_icon_controller_endpoints' );
185 +add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );
186 +
187 +/**
188 + * Registers the post format search handler.
189 + *
190 + * @param string $search_handlers Title list of current handlers.
191 + *
192 + * @return array Title updated list of handlers.
193 + */
194 +function gutenberg_post_format_search_handler( $search_handlers ) {
195 + if ( current_theme_supports( 'post-formats' ) ) {
196 + $search_handlers[] = new WP_REST_Post_Format_Search_Handler();
197 + }
198 +
199 + return $search_handlers;
200 +}
201 +add_filter( 'wp_rest_search_handlers', 'gutenberg_post_format_search_handler', 10, 5 );
202 +
203 +/**
204 + * Registers the terms search handler.
205 + *
206 + * @param string $search_handlers Title list of current handlers.
207 + *
208 + * @return array Title updated list of handlers.
209 + */
210 +function gutenberg_term_search_handler( $search_handlers ) {
211 + $search_handlers[] = new WP_REST_Term_Search_Handler();
212 + return $search_handlers;
213 +}
214 +add_filter( 'wp_rest_search_handlers', 'gutenberg_term_search_handler', 10, 5 );