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

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