PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 12.6.0, at lib/rest-api.php

223 lines 7.3 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 menu locations REST API routes.
27 */
28 function gutenberg_register_rest_menu_location() {
29 $nav_menu_location = new WP_REST_Menu_Locations_Controller();
30 $nav_menu_location->register_routes();
31 }
32 add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' );
33
34 /**
35 * Registers the navigation areas REST API routes.
36 */
37 function gutenberg_register_rest_navigation_areas() {
38 $navigation_areas = new WP_REST_Block_Navigation_Areas_Controller();
39 $navigation_areas->register_routes();
40 }
41 add_action( 'rest_api_init', 'gutenberg_register_rest_navigation_areas' );
42
43 /**
44 * Registers the customizer nonces REST API routes.
45 */
46 function gutenberg_register_rest_customizer_nonces() {
47 $customizer_nonces = new WP_Rest_Customizer_Nonces();
48 $customizer_nonces->register_routes();
49 }
50 add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' );
51
52 /**
53 * Registers the Block editor settings REST API routes.
54 */
55 function gutenberg_register_block_editor_settings() {
56 $editor_settings = new WP_REST_Block_Editor_Settings_Controller();
57 $editor_settings->register_routes();
58 }
59 add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' );
60
61 /**
62 * Hook in to the nav menu item post type and enable a post type rest endpoint.
63 *
64 * @param array $args Current registered post type args.
65 * @param string $post_type Name of post type.
66 *
67 * @return array
68 */
69 function wp_api_nav_menus_post_type_args( $args, $post_type ) {
70 if ( 'nav_menu_item' === $post_type ) {
71 $args['show_in_rest'] = true;
72 $args['rest_base'] = 'menu-items';
73 $args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller';
74 }
75
76 return $args;
77 }
78 add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );
79
80 /**
81 * Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint.
82 *
83 * @param array $args Current registered taxonomy args.
84 * @param string $taxonomy Name of taxonomy.
85 *
86 * @return array
87 */
88 function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) {
89 if ( 'nav_menu' === $taxonomy ) {
90 $args['show_in_rest'] = true;
91 $args['rest_base'] = 'menus';
92 $args['rest_controller_class'] = 'WP_REST_Menus_Controller';
93 }
94
95 return $args;
96 }
97 add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );
98
99 /**
100 * Shim for get_sample_permalink() to add support for auto-draft status.
101 *
102 * This function filters the return from get_sample_permalink() and essentially
103 * re-runs the same logic minus the filters, but pretends a status of auto-save
104 * is actually publish in order to return the future permalink format.
105 *
106 * This is a temporary fix until we can patch get_sample_permalink()
107 *
108 * @see https://core.trac.wordpress.org/ticket/46266
109 *
110 * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
111 * @param int $id ID of the post.
112 * @param string $title Title of the post.
113 * @param string $name Slug of the post.
114 * @param object $post WP_Post object.
115 *
116 * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
117 */
118 function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) {
119 if ( 'auto-draft' !== $post->post_status ) {
120 return $permalink;
121 }
122 $ptype = get_post_type_object( $post->post_type );
123
124 $original_status = $post->post_status;
125 $original_date = $post->post_date;
126 $original_name = $post->post_name;
127
128 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
129 $post->post_status = 'publish';
130 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
131
132 // If the user wants to set a new name -- override the current one.
133 // Note: if empty name is supplied -- use the title instead, see #6072.
134 if ( ! is_null( $name ) ) {
135 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
136 }
137
138 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
139
140 $post->filter = 'sample';
141
142 $permalink = get_permalink( $post, true );
143
144 // Replace custom post_type Token with generic pagename token for ease of use.
145 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
146
147 // Handle page hierarchy.
148 if ( $ptype->hierarchical ) {
149 $uri = get_page_uri( $post );
150 if ( $uri ) {
151 $uri = untrailingslashit( $uri );
152 $uri = strrev( stristr( strrev( $uri ), '/' ) );
153 $uri = untrailingslashit( $uri );
154 }
155
156 if ( ! empty( $uri ) ) {
157 $uri .= '/';
158 }
159 $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
160 }
161
162 $permalink = array( $permalink, $post->post_name );
163 $post->post_status = $original_status;
164 $post->post_date = $original_date;
165 $post->post_name = $original_name;
166 unset( $post->filter );
167
168 return $permalink;
169 }
170 add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );
171
172 /**
173 * Filters WP_User_Query arguments when querying users via the REST API.
174 *
175 * Allow using the has_published_post argument.
176 *
177 * @param array $prepared_args Array of arguments for WP_User_Query.
178 * @param WP_REST_Request $request The REST API request.
179 *
180 * @return array Returns modified $prepared_args.
181 */
182 function gutenberg_rest_user_query_has_published_posts( $prepared_args, $request ) {
183 if ( ! empty( $request['has_published_posts'] ) ) {
184 $prepared_args['has_published_posts'] = ( true === $request['has_published_posts'] )
185 ? get_post_types( array( 'show_in_rest' => true ), 'names' )
186 : (array) $request['has_published_posts'];
187 }
188 return $prepared_args;
189 }
190 add_filter( 'rest_user_query', 'gutenberg_rest_user_query_has_published_posts', 10, 2 );
191
192
193 /**
194 * Filters REST API collection parameters for the users controller.
195 *
196 * @param array $query_params JSON Schema-formatted collection parameters.
197 *
198 * @return array Returns the $query_params with "has_published_posts".
199 */
200 function gutenberg_rest_user_collection_params_has_published_posts( $query_params ) {
201 $query_params['has_published_posts'] = array(
202 'description' => __( 'Limit result set to users who have published posts.', 'gutenberg' ),
203 'type' => array( 'boolean', 'array' ),
204 'items' => array(
205 'type' => 'string',
206 'enum' => get_post_types( array( 'show_in_rest' => true ), 'names' ),
207 ),
208 );
209 return $query_params;
210 }
211 add_filter( 'rest_user_collection_params', 'gutenberg_rest_user_collection_params_has_published_posts' );
212
213 /**
214 * Registers the Edit Site's Export REST API routes.
215 *
216 * @return void
217 */
218 function gutenberg_register_edit_site_export_endpoint() {
219 $editor_settings = new WP_REST_Edit_Site_Export_Controller();
220 $editor_settings->register_routes();
221 }
222 add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_endpoint' );
223