Skip to content

Commit

Permalink
Legacy widget rendering endpoint (#34230)
Browse files Browse the repository at this point in the history
* Add a polyfill for the widget render endpoint

* Remove unnecessary whitespace

* Fill some blanks in docstrings

* Adjust the registered callback name

* Lint

* Lint

* Polyfill the entire /encode endpoint

* Adjust directory and class names

* Revert to /render polyfill for WordPress 5.8.1

* Rename id_base parameter to id

* Lint
  • Loading branch information
adamziel authored Aug 30, 2021
1 parent 893b2b8 commit 4d6e174
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/compat/wordpress-5.8.1/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Temporary compatibility shims for features present in Gutenberg.
*
* @package gutenberg
*/

// Load the polyfills.
require_once __DIR__ . '/widget-render-api-endpoint/index.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* REST API: GB_REST_Widget_Render_Endpoint_Polyfill class
*
* @package gutenberg
*/

/**
* Polyfill API class to render widgets via the REST API.
*
* @see \WP_REST_Controller
*/
class GB_REST_Widget_Render_Endpoint_Polyfill extends \WP_REST_Widget_Types_Controller {

/**
* Registers the widget type routes.
*
* @see register_rest_route()
*/
public function register_routes() {
$route = '/' . $this->rest_base . '/(?P<id>[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();
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="profile" href="https://gmpg.org/xfn/11" />
<?php wp_head(); ?>
<style>
/* Reset theme styles */
html, body, #page, #content {
padding: 0 !important;
margin: 0 !important;
}
</style>
</head>
<body <?php body_class(); ?>>
<div id="page" class="site">
<div id="content" class="site-content">
<?php
$registry = WP_Block_Type_Registry::get_instance();
$block = $registry->get_registered( 'core/legacy-widget' );
echo $block->render(
array(
'idBase' => $id_base,
'instance' => $instance,
)
);
?>
</div><!-- #content -->
</div><!-- #page -->
<?php wp_footer(); ?>
</body>
</html>
<?php
return ob_get_clean();
}

}
22 changes: 22 additions & 0 deletions lib/compat/wordpress-5.8.1/widget-render-api-endpoint/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Shims the /wp/v2/widget-types/<id>/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 );


1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 4d6e174

Please sign in to comment.