-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Legacy widget rendering endpoint (#34230)
* 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
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
121 changes: 121 additions & 0 deletions
121
...dpress-5.8.1/widget-render-api-endpoint/class-gb-rest-widget-render-endpoint-polyfill.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
lib/compat/wordpress-5.8.1/widget-render-api-endpoint/index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters