PluginProbe
Gutenberg / 9.9.0
Gutenberg v9.9.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 9.9.0, at lib/rest-api.php

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