PluginProbe
Gutenberg / 10.7.4
Gutenberg v10.7.4
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 10.7.4, at lib/rest-api.php

186 lines 5.9 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