| 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 |
$widgets_controller = new WP_REST_Widget_Updater_Controller(); |
| 67 |
$widgets_controller->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 |
* End: Include for phase 2 |
| 83 |
*/ |
| 84 |
|