| 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 |
* Add fields required for site editing to the /themes endpoint. |
| 57 |
* |
| 58 |
* @todo Remove once Gutenberg's minimum required WordPress version is v5.5. |
| 59 |
* @see https://core.trac.wordpress.org/ticket/49906 |
| 60 |
* @see https://core.trac.wordpress.org/changeset/47921 |
| 61 |
* |
| 62 |
* @param WP_REST_Response $response The response object. |
| 63 |
* @param WP_Theme $theme Theme object used to create response. |
| 64 |
* @param WP_REST_Request $request Request object. |
| 65 |
*/ |
| 66 |
function gutenberg_filter_rest_prepare_theme( $response, $theme, $request ) { |
| 67 |
$data = $response->get_data(); |
| 68 |
$fields = array_keys( $data ); |
| 69 |
|
| 70 |
/** |
| 71 |
* The following is basically copied from Core's WP_REST_Themes_Controller::prepare_item_for_response() |
| 72 |
* (as of WP v5.5), with `rest_is_field_included()` replaced by `! in_array()`. |
| 73 |
* This makes sure that we add all the fields that are missing from Core. |
| 74 |
* |
| 75 |
* @see https://github.com/WordPress/WordPress/blob/019bc2d244c4d536338d2c634419583e928143df/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php#L118-L167 |
| 76 |
*/ |
| 77 |
if ( ! in_array( 'stylesheet', $fields, true ) ) { |
| 78 |
$data['stylesheet'] = $theme->get_stylesheet(); |
| 79 |
} |
| 80 |
|
| 81 |
if ( ! in_array( 'template', $fields, true ) ) { |
| 82 |
/** |
| 83 |
* Use the get_template() method, not the 'Template' header, for finding the template. |
| 84 |
* The 'Template' header is only good for what was written in the style.css, while |
| 85 |
* get_template() takes into account where WordPress actually located the theme and |
| 86 |
* whether it is actually valid. |
| 87 |
*/ |
| 88 |
$data['template'] = $theme->get_template(); |
| 89 |
} |
| 90 |
|
| 91 |
$plain_field_mappings = array( |
| 92 |
'requires_php' => 'RequiresPHP', |
| 93 |
'requires_wp' => 'RequiresWP', |
| 94 |
'textdomain' => 'TextDomain', |
| 95 |
'version' => 'Version', |
| 96 |
); |
| 97 |
|
| 98 |
foreach ( $plain_field_mappings as $field => $header ) { |
| 99 |
if ( ! in_array( $field, $fields, true ) ) { |
| 100 |
$data[ $field ] = $theme->get( $header ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! in_array( 'screenshot', $fields, true ) ) { |
| 105 |
// Using $theme->get_screenshot() with no args to get absolute URL. |
| 106 |
$data['screenshot'] = $theme->get_screenshot() ? $theme->get_screenshot() : ''; |
| 107 |
} |
| 108 |
|
| 109 |
$rich_field_mappings = array( |
| 110 |
'author' => 'Author', |
| 111 |
'author_uri' => 'AuthorURI', |
| 112 |
'description' => 'Description', |
| 113 |
'name' => 'Name', |
| 114 |
'tags' => 'Tags', |
| 115 |
'theme_uri' => 'ThemeURI', |
| 116 |
); |
| 117 |
|
| 118 |
foreach ( $rich_field_mappings as $field => $header ) { |
| 119 |
if ( ! in_array( $field, $fields, true ) ) { |
| 120 |
$data[ $field ]['raw'] = $theme->display( $header, false, true ); |
| 121 |
$data[ $field ]['rendered'] = $theme->display( $header ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$response->set_data( $data ); |
| 126 |
return $response; |
| 127 |
} |
| 128 |
add_filter( 'rest_prepare_theme', 'gutenberg_filter_rest_prepare_theme', 10, 3 ); |
| 129 |
|
| 130 |
/** |
| 131 |
* Start: Include for phase 2 |
| 132 |
*/ |
| 133 |
/** |
| 134 |
* Registers the REST API routes needed by the legacy widget block. |
| 135 |
* |
| 136 |
* @since 5.0.0 |
| 137 |
*/ |
| 138 |
function gutenberg_register_rest_widget_updater_routes() { |
| 139 |
$widget_forms = new WP_REST_Widget_Utils_Controller(); |
| 140 |
$widget_forms->register_routes(); |
| 141 |
} |
| 142 |
add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' ); |
| 143 |
|
| 144 |
/** |
| 145 |
* Registers the block directory. |
| 146 |
* |
| 147 |
* @since 6.5.0 |
| 148 |
*/ |
| 149 |
function gutenberg_register_rest_block_directory() { |
| 150 |
$block_directory_controller = new WP_REST_Block_Directory_Controller(); |
| 151 |
$block_directory_controller->register_routes(); |
| 152 |
} |
| 153 |
add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' ); |
| 154 |
|
| 155 |
/** |
| 156 |
* Registers the Block types REST API routes. |
| 157 |
*/ |
| 158 |
function gutenberg_register_block_type() { |
| 159 |
$block_types = new WP_REST_Block_Types_Controller(); |
| 160 |
$block_types->register_routes(); |
| 161 |
} |
| 162 |
add_action( 'rest_api_init', 'gutenberg_register_block_type' ); |
| 163 |
/** |
| 164 |
* Registers the menu locations area REST API routes. |
| 165 |
*/ |
| 166 |
function gutenberg_register_rest_menu_location() { |
| 167 |
$nav_menu_location = new WP_REST_Menu_Locations_Controller(); |
| 168 |
$nav_menu_location->register_routes(); |
| 169 |
} |
| 170 |
add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' ); |
| 171 |
|
| 172 |
/** |
| 173 |
* Registers the menu locations area REST API routes. |
| 174 |
*/ |
| 175 |
function gutenberg_register_rest_customizer_nonces() { |
| 176 |
$nav_menu_location = new WP_Rest_Customizer_Nonces(); |
| 177 |
$nav_menu_location->register_routes(); |
| 178 |
} |
| 179 |
add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' ); |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Registers the Plugins REST API routes. |
| 184 |
*/ |
| 185 |
function gutenberg_register_plugins_endpoint() { |
| 186 |
$plugins = new WP_REST_Plugins_Controller(); |
| 187 |
$plugins->register_routes(); |
| 188 |
} |
| 189 |
add_action( 'rest_api_init', 'gutenberg_register_plugins_endpoint' ); |
| 190 |
|
| 191 |
/** |
| 192 |
* Registers the Sidebars REST API routes. |
| 193 |
*/ |
| 194 |
function gutenberg_register_sidebars_endpoint() { |
| 195 |
$sidebars = new WP_REST_Sidebars_Controller(); |
| 196 |
$sidebars->register_routes(); |
| 197 |
} |
| 198 |
add_action( 'rest_api_init', 'gutenberg_register_sidebars_endpoint' ); |
| 199 |
|
| 200 |
/** |
| 201 |
* Hook in to the nav menu item post type and enable a post type rest endpoint. |
| 202 |
* |
| 203 |
* @param array $args Current registered post type args. |
| 204 |
* @param string $post_type Name of post type. |
| 205 |
* |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
function wp_api_nav_menus_post_type_args( $args, $post_type ) { |
| 209 |
if ( 'nav_menu_item' === $post_type ) { |
| 210 |
$args['show_in_rest'] = true; |
| 211 |
$args['rest_base'] = 'menu-items'; |
| 212 |
$args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller'; |
| 213 |
} |
| 214 |
|
| 215 |
return $args; |
| 216 |
} |
| 217 |
add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 ); |
| 218 |
|
| 219 |
/** |
| 220 |
* Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint. |
| 221 |
* |
| 222 |
* @param array $args Current registered taxonomy args. |
| 223 |
* @param string $taxonomy Name of taxonomy. |
| 224 |
* |
| 225 |
* @return array |
| 226 |
*/ |
| 227 |
function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) { |
| 228 |
if ( 'nav_menu' === $taxonomy ) { |
| 229 |
$args['show_in_rest'] = true; |
| 230 |
$args['rest_base'] = 'menus'; |
| 231 |
$args['rest_controller_class'] = 'WP_REST_Menus_Controller'; |
| 232 |
} |
| 233 |
|
| 234 |
return $args; |
| 235 |
} |
| 236 |
add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 ); |
| 237 |
|
| 238 |
/** |
| 239 |
* Shim for get_sample_permalink() to add support for auto-draft status. |
| 240 |
* |
| 241 |
* This function filters the return from get_sample_permalink() and essentially |
| 242 |
* re-runs the same logic minus the filters, but pretends a status of auto-save |
| 243 |
* is actually publish in order to return the future permalink format. |
| 244 |
* |
| 245 |
* This is a temporary fix until we can patch get_sample_permalink() |
| 246 |
* |
| 247 |
* @see https://core.trac.wordpress.org/ticket/46266 |
| 248 |
* |
| 249 |
* @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. |
| 250 |
* @param int $id ID of the post. |
| 251 |
* @param string $title Title of the post. |
| 252 |
* @param string $name Slug of the post. |
| 253 |
* @param object $post WP_Post object. |
| 254 |
* |
| 255 |
* @return array Array containing the sample permalink with placeholder for the post name, and the post name. |
| 256 |
*/ |
| 257 |
function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) { |
| 258 |
if ( 'auto-draft' !== $post->post_status ) { |
| 259 |
return $permalink; |
| 260 |
} |
| 261 |
$ptype = get_post_type_object( $post->post_type ); |
| 262 |
|
| 263 |
$original_status = $post->post_status; |
| 264 |
$original_date = $post->post_date; |
| 265 |
$original_name = $post->post_name; |
| 266 |
|
| 267 |
// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. |
| 268 |
$post->post_status = 'publish'; |
| 269 |
$post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); |
| 270 |
|
| 271 |
// If the user wants to set a new name -- override the current one. |
| 272 |
// Note: if empty name is supplied -- use the title instead, see #6072. |
| 273 |
if ( ! is_null( $name ) ) { |
| 274 |
$post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); |
| 275 |
} |
| 276 |
|
| 277 |
$post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); |
| 278 |
|
| 279 |
$post->filter = 'sample'; |
| 280 |
|
| 281 |
$permalink = get_permalink( $post, true ); |
| 282 |
|
| 283 |
// Replace custom post_type Token with generic pagename token for ease of use. |
| 284 |
$permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); |
| 285 |
|
| 286 |
// Handle page hierarchy. |
| 287 |
if ( $ptype->hierarchical ) { |
| 288 |
$uri = get_page_uri( $post ); |
| 289 |
if ( $uri ) { |
| 290 |
$uri = untrailingslashit( $uri ); |
| 291 |
$uri = strrev( stristr( strrev( $uri ), '/' ) ); |
| 292 |
$uri = untrailingslashit( $uri ); |
| 293 |
} |
| 294 |
|
| 295 |
if ( ! empty( $uri ) ) { |
| 296 |
$uri .= '/'; |
| 297 |
} |
| 298 |
$permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink ); |
| 299 |
} |
| 300 |
|
| 301 |
$permalink = array( $permalink, $post->post_name ); |
| 302 |
$post->post_status = $original_status; |
| 303 |
$post->post_date = $original_date; |
| 304 |
$post->post_name = $original_name; |
| 305 |
unset( $post->filter ); |
| 306 |
|
| 307 |
return $permalink; |
| 308 |
} |
| 309 |
add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); |
| 310 |
|
| 311 |
/** |
| 312 |
* Registers the image editor. |
| 313 |
* |
| 314 |
* @since 7.x.0 |
| 315 |
*/ |
| 316 |
function gutenberg_register_image_editor() { |
| 317 |
global $wp_version; |
| 318 |
|
| 319 |
// Strip '-src' from the version string. Messes up version_compare(). |
| 320 |
$version = str_replace( '-src', '', $wp_version ); |
| 321 |
|
| 322 |
// Only register routes for versions older than WP 5.5. |
| 323 |
if ( version_compare( $version, '5.5-beta', '<' ) ) { |
| 324 |
$image_editor = new WP_REST_Image_Editor_Controller(); |
| 325 |
$image_editor->register_routes(); |
| 326 |
} |
| 327 |
} |
| 328 |
add_filter( 'rest_api_init', 'gutenberg_register_image_editor' ); |
| 329 |
|
| 330 |
/** |
| 331 |
* Registers the post format search handler. |
| 332 |
* |
| 333 |
* @param string $search_handlers Title list of current handlers. |
| 334 |
* |
| 335 |
* @return array Title updated list of handlers. |
| 336 |
*/ |
| 337 |
function gutenberg_post_format_search_handler( $search_handlers ) { |
| 338 |
if ( current_theme_supports( 'post-formats' ) ) { |
| 339 |
$search_handlers[] = new WP_REST_Post_Format_Search_Handler(); |
| 340 |
} |
| 341 |
|
| 342 |
return $search_handlers; |
| 343 |
} |
| 344 |
add_filter( 'wp_rest_search_handlers', 'gutenberg_post_format_search_handler', 10, 5 ); |
| 345 |
|
| 346 |
/** |
| 347 |
* Registers the terms search handler. |
| 348 |
* |
| 349 |
* @param string $search_handlers Title list of current handlers. |
| 350 |
* |
| 351 |
* @return array Title updated list of handlers. |
| 352 |
*/ |
| 353 |
function gutenberg_term_search_handler( $search_handlers ) { |
| 354 |
$search_handlers[] = new WP_REST_Term_Search_Handler(); |
| 355 |
return $search_handlers; |
| 356 |
} |
| 357 |
add_filter( 'wp_rest_search_handlers', 'gutenberg_term_search_handler', 10, 5 ); |
| 358 |
|