diff --git a/lib/compat/wordpress-5.8.1/index.php b/lib/compat/wordpress-5.8.1/index.php new file mode 100644 index 00000000000000..d825f2d93c9167 --- /dev/null +++ b/lib/compat/wordpress-5.8.1/index.php @@ -0,0 +1,9 @@ +rest_base . '/(?P[a-zA-Z0-9_-]+)/render'; + + // Don't override if already registered. + $registered_routes = rest_get_server()->get_routes( 'wp/v2' ); + if ( array_key_exists( $route, $registered_routes ) ) { + return; + } + + register_rest_route( + $this->namespace, + $route, + array( + array( + 'methods' => WP_REST_Server::CREATABLE, + 'permission_callback' => array( $this, 'get_item_permissions_check' ), + 'callback' => array( $this, 'render' ), + 'args' => array( + 'id' => array( + 'description' => __( 'The widget type id.', 'default' ), + 'type' => 'string', + 'required' => true, + ), + 'instance' => array( + 'description' => __( 'Current instance settings of the widget.', 'default' ), + 'type' => 'object', + ), + ), + ), + ) + ); + } + + /** + * Renders a single Legacy Widget and wraps it in a JSON-encodable array. + * + * @param WP_REST_Request $request Full details about the request. + * + * @return array An array with rendered Legacy Widget HTML. + */ + public function render( $request ) { + return array( + 'preview' => $this->render_legacy_widget_preview_iframe( + $request['id'], + isset( $request['instance'] ) ? $request['instance'] : null + ), + ); + } + + /** + * Renders a page containing a preview of the requested Legacy Widget block. + * + * @param string $id_base The id base of the requested widget. + * @param array $instance The widget instance attributes. + * + * @return string Rendered Legacy Widget block preview. + */ + private function render_legacy_widget_preview_iframe( $id_base, $instance ) { + if ( ! defined( 'IFRAME_REQUEST' ) ) { + define( 'IFRAME_REQUEST', true ); + } + + ob_start(); + ?> + + > + + + + + + + + > +
+
+ get_registered( 'core/legacy-widget' ); + echo $block->render( + array( + 'idBase' => $id_base, + 'instance' => $instance, + ) + ); + ?> +
+
+ + + + /render endpoint in WP versions where it's missing + * + * @package gutenberg + */ + +// Load the polyfill class. +require_once __DIR__ . '/class-gb-rest-widget-render-endpoint-polyfill.php'; + +/** + * Registers routes from the GB_REST_Widget_Render_Endpoint_Polyfill class. + */ +function setup_widget_render_api_endpoint_polyfill() { + $polyfill = new GB_REST_Widget_Render_Endpoint_Polyfill(); + $polyfill->register_routes(); +} + +// Priority should be larger than 99 which is the one used for registering the core routes. +add_action( 'rest_api_init', 'setup_widget_render_api_endpoint_polyfill', 100 ); + + diff --git a/lib/load.php b/lib/load.php index 43456b27da16b5..57692dc37319ca 100644 --- a/lib/load.php +++ b/lib/load.php @@ -82,6 +82,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat.php'; require __DIR__ . '/compat/wordpress-5.8/index.php'; +require __DIR__ . '/compat/wordpress-5.8.1/index.php'; require __DIR__ . '/utils.php'; require __DIR__ . '/editor-settings.php';