PluginProbe
Gutenberg / 7.9.0
Gutenberg v7.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 7.9.0, at lib/rest-api.php

215 lines 6.7 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 * Handle a failing oEmbed proxy request to try embedding as a shortcode.
15 *
16 * @see https://core.trac.wordpress.org/ticket/45447
17 *
18 * @since 2.3.0
19 *
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.
24 */
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;
29 }
30
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;
36 }
37
38 global $wp_scripts;
39
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;
45 }
46
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 );
54
55
56
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();
68 }
69 add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' );
70
71 /**
72 * Registers the widget area REST API routes.
73 *
74 * @since 5.7.0
75 */
76 function gutenberg_register_rest_widget_areas() {
77 $widget_areas_controller = new WP_REST_Widget_Areas_Controller();
78 $widget_areas_controller->register_routes();
79 }
80 add_action( 'rest_api_init', 'gutenberg_register_rest_widget_areas' );
81
82 /**
83 * Registers the block directory.
84 *
85 * @since 6.5.0
86 */
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();
94 }
95 add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' );
96
97 /**
98 * Registers the menu locations area REST API routes.
99 */
100 function gutenberg_register_rest_menu_location() {
101 $nav_menu_location = new WP_REST_Menu_Locations_Controller();
102 $nav_menu_location->register_routes();
103 }
104 add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' );
105 /**
106 * Hook in to the nav menu item post type and enable a post type rest endpoint.
107 *
108 * @param array $args Current registered post type args.
109 * @param string $post_type Name of post type.
110 *
111 * @return array
112 */
113 function wp_api_nav_menus_post_type_args( $args, $post_type ) {
114 if ( 'nav_menu_item' === $post_type ) {
115 $args['show_in_rest'] = true;
116 $args['rest_base'] = 'menu-items';
117 $args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller';
118 }
119
120 return $args;
121 }
122 add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );
123
124 /**
125 * Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint.
126 *
127 * @param array $args Current registered taxonomy args.
128 * @param string $taxonomy Name of taxonomy.
129 *
130 * @return array
131 */
132 function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) {
133 if ( 'nav_menu' === $taxonomy ) {
134 $args['show_in_rest'] = true;
135 $args['rest_base'] = 'menus';
136 $args['rest_controller_class'] = 'WP_REST_Menus_Controller';
137 }
138
139 return $args;
140 }
141 add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );
142
143 /**
144 * Shim for get_sample_permalink() to add support for auto-draft status.
145 *
146 * This function filters the return from get_sample_permalink() and essentially
147 * re-runs the same logic minus the filters, but pretends a status of auto-save
148 * is actually publish in order to return the future permalink format.
149 *
150 * This is a temporary fix until we can patch get_sample_permalink()
151 *
152 * @see https://core.trac.wordpress.org/ticket/46266
153 *
154 * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
155 * @param int $id ID of the post.
156 * @param string $title Title of the post.
157 * @param string $name Slug of the post.
158 * @param object $post WP_Post object.
159 *
160 * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
161 */
162 function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) {
163 if ( 'auto-draft' !== $post->post_status ) {
164 return $permalink;
165 }
166 $ptype = get_post_type_object( $post->post_type );
167
168 $original_status = $post->post_status;
169 $original_date = $post->post_date;
170 $original_name = $post->post_name;
171
172 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
173 $post->post_status = 'publish';
174 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
175
176 // If the user wants to set a new name -- override the current one.
177 // Note: if empty name is supplied -- use the title instead, see #6072.
178 if ( ! is_null( $name ) ) {
179 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
180 }
181
182 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
183
184 $post->filter = 'sample';
185
186 $permalink = get_permalink( $post, true );
187
188 // Replace custom post_type Token with generic pagename token for ease of use.
189 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
190
191 // Handle page hierarchy.
192 if ( $ptype->hierarchical ) {
193 $uri = get_page_uri( $post );
194 if ( $uri ) {
195 $uri = untrailingslashit( $uri );
196 $uri = strrev( stristr( strrev( $uri ), '/' ) );
197 $uri = untrailingslashit( $uri );
198 }
199
200 if ( ! empty( $uri ) ) {
201 $uri .= '/';
202 }
203 $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
204 }
205
206 $permalink = array( $permalink, $post->post_name );
207 $post->post_status = $original_status;
208 $post->post_date = $original_date;
209 $post->post_name = $original_name;
210 unset( $post->filter );
211
212 return $permalink;
213 }
214 add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );
215