PluginProbe
Gutenberg / 11.5.1
Gutenberg v11.5.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
gutenberg / lib / rest-api.php

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

227 lines 7.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 /**
15 * Registers the REST API routes for URL Details.
16 *
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.
78 *
79 * @param array $args Current registered post type args.
80 * @param string $post_type Name of post type.
81 *
82 * @return array
83 */
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 }
90
91 return $args;
92 }
93 add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );
94
95 /**
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
102 */
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;
111 }
112 add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );
113
114 /**
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.
132 */
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;
184 }
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' );
227