PluginProbe
Gutenberg / 9.7.2
Gutenberg v9.7.2
24.0.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 All 403 releases
← All changes | lib/rest-api.php +154 -57 7.4.09.7.2 View file →
@@ -10,86 +10,183 @@
10 10 die( 'Silence is golden.' );
11 11 }
12 12
13 13 /**
14 - * Handle a failing oEmbed proxy request to try embedding as a shortcode.
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.
15 56 *
16 - * @see https://core.trac.wordpress.org/ticket/45447
57 + * @param array $args Current registered post type args.
58 + * @param string $post_type Name of post type.
17 59 *
18 - * @since 2.3.0
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.
19 75 *
20 - * @param WP_HTTP_Response|WP_Error $response The REST Request response.
21 - * @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server).
22 - * @param WP_REST_Request $request Request used to generate the response.
23 - * @return WP_HTTP_Response|object|WP_Error The REST Request response.
76 + * @param array $args Current registered taxonomy args.
77 + * @param string $taxonomy Name of taxonomy.
78 + *
79 + * @return array
24 80 */
25 -function gutenberg_filter_oembed_result( $response, $handler, $request ) {
26 - if ( ! is_wp_error( $response ) || 'oembed_invalid_url' !== $response->get_error_code() ||
27 - '/oembed/1.0/proxy' !== $request->get_route() ) {
28 - return $response;
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';
29 86 }
30 87
31 - // Try using a classic embed instead.
32 - global $wp_embed;
33 - $html = $wp_embed->shortcode( array(), $_GET['url'] );
34 - if ( ! $html ) {
35 - return $response;
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;
36 114 }
115 + $ptype = get_post_type_object( $post->post_type );
37 116
38 - global $wp_scripts;
117 + $original_status = $post->post_status;
118 + $original_date = $post->post_date;
119 + $original_name = $post->post_name;
39 120
40 - // Check if any scripts were enqueued by the shortcode, and include them in
41 - // the response.
42 - $enqueued_scripts = array();
43 - foreach ( $wp_scripts->queue as $script ) {
44 - $enqueued_scripts[] = $wp_scripts->registered[ $script ]->src;
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 );
45 129 }
46 130
47 - return array(
48 - 'provider_name' => __( 'Embed Handler', 'gutenberg' ),
49 - 'html' => $html,
50 - 'scripts' => $enqueued_scripts,
51 - );
52 -}
53 -add_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result', 10, 3 );
131 + $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
54 132
133 + $post->filter = 'sample';
55 134
135 + $permalink = get_permalink( $post, true );
56 136
57 -/**
58 - * Start: Include for phase 2
59 - */
60 -/**
61 - * Registers the REST API routes needed by the legacy widget block.
62 - *
63 - * @since 5.0.0
64 - */
65 -function gutenberg_register_rest_widget_updater_routes() {
66 - $widget_forms = new WP_REST_Widget_Forms();
67 - $widget_forms->register_routes();
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;
68 162 }
69 -add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' );
163 +add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );
70 164
71 165 /**
72 - * Registers the widget area REST API routes.
166 + * Registers the post format search handler.
73 167 *
74 - * @since 5.7.0
168 + * @param string $search_handlers Title list of current handlers.
169 + *
170 + * @return array Title updated list of handlers.
75 171 */
76 -function gutenberg_register_rest_widget_areas() {
77 - $widget_areas_controller = new WP_REST_Widget_Areas_Controller();
78 - $widget_areas_controller->register_routes();
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;
79 178 }
80 -add_action( 'rest_api_init', 'gutenberg_register_rest_widget_areas' );
179 +add_filter( 'wp_rest_search_handlers', 'gutenberg_post_format_search_handler', 10, 5 );
81 180
82 181 /**
83 - * Registers the block directory.
182 + * Registers the terms search handler.
84 183 *
85 - * @since 6.5.0
184 + * @param string $search_handlers Title list of current handlers.
185 + *
186 + * @return array Title updated list of handlers.
86 187 */
87 -function gutenberg_register_rest_block_directory() {
88 - if ( ! gutenberg_is_experiment_enabled( 'gutenberg-block-directory' ) ) {
89 - return;
90 - }
91 -
92 - $block_directory_controller = new WP_REST_Block_Directory_Controller();
93 - $block_directory_controller->register_routes();
188 +function gutenberg_term_search_handler( $search_handlers ) {
189 + $search_handlers[] = new WP_REST_Term_Search_Handler();
190 + return $search_handlers;
94 191 }
95 -add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' );
192 +add_filter( 'wp_rest_search_handlers', 'gutenberg_term_search_handler', 10, 5 );