PluginProbe
Gutenberg / 11.2.1
Gutenberg v11.2.1
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
← All changes | lib/rest-api.php +199 -20 23.9.011.2.1 View file →
@@ -9,39 +9,218 @@
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 Block editor settings REST API routes.
69 + */
70 +function gutenberg_register_block_editor_settings() {
71 + $editor_settings = new WP_REST_Block_Editor_Settings_Controller();
72 + $editor_settings->register_routes();
73 +}
74 +add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' );
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 + * Filters WP_User_Query arguments when querying users via the REST API.
189 + *
190 + * Allow using the has_published_post argument.
191 + *
192 + * @param array $prepared_args Array of arguments for WP_User_Query.
193 + * @param WP_REST_Request $request The REST API request.
194 + *
195 + * @return array Returns modified $prepared_args.
196 + */
197 +function gutenberg_rest_user_query_has_published_posts( $prepared_args, $request ) {
198 + if ( ! empty( $request['has_published_posts'] ) ) {
199 + $prepared_args['has_published_posts'] = ( true === $request['has_published_posts'] )
200 + ? get_post_types( array( 'show_in_rest' => true ), 'names' )
201 + : (array) $request['has_published_posts'];
202 + }
203 + return $prepared_args;
204 +}
205 +add_filter( 'rest_user_query', 'gutenberg_rest_user_query_has_published_posts', 10, 2 );
206 +
207 +
208 +/**
209 + * Filters REST API collection parameters for the users controller.
210 + *
211 + * @param array $query_params JSON Schema-formatted collection parameters.
212 + *
213 + * @return array Returns the $query_params with "has_published_posts".
214 + */
215 +function gutenberg_rest_user_collection_params_has_published_posts( $query_params ) {
216 + $query_params['has_published_posts'] = array(
217 + 'description' => __( 'Limit result set to users who have published posts.', 'gutenberg' ),
218 + 'type' => array( 'boolean', 'array' ),
219 + 'items' => array(
220 + 'type' => 'string',
221 + 'enum' => get_post_types( array( 'show_in_rest' => true ), 'names' ),
222 + ),
223 + );
224 + return $query_params;
225 +}
226 +add_filter( 'rest_user_collection_params', 'gutenberg_rest_user_collection_params_has_published_posts' );