Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legacy widget rendering endpoint #1508

Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ public function register_routes() {
),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-zA-Z0-9_-]+)/render',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'callback' => array( $this, 'render' ),
'args' => array(
'id_base' => array(
'description' => __( 'The widget type id.' ),
'type' => 'string',
'required' => true,
),
'instance' => array(
'description' => __( 'Current instance settings of the widget.' ),
'type' => 'object',
),
),
),
)
);
}

/**
Expand Down Expand Up @@ -397,6 +420,71 @@ public function get_item_schema() {
return $this->add_additional_fields_schema( $this->schema );
}

public function render( $request ) {
adamziel marked this conversation as resolved.
Show resolved Hide resolved
return array(
'preview' => $this->render_legacy_widget_preview_iframe(
adamziel marked this conversation as resolved.
Show resolved Hide resolved
$request['id_base'],
isset( $request['instance'] ) ? $request['instance'] : null
),
);
}

/**
* Renders a page containing a preview of the requested Legacy Widget block.
*
adamziel marked this conversation as resolved.
Show resolved Hide resolved
* @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 ( ! current_user_can( 'edit_theme_options' ) ) {
adamziel marked this conversation as resolved.
Show resolved Hide resolved
return '';
}

define( 'IFRAME_REQUEST', true );
adamziel marked this conversation as resolved.
Show resolved Hide resolved

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
$content = ob_get_contents();
ob_end_clean();
return $content;
adamziel marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* An RPC-style endpoint which can be used by clients to turn user input in
* a widget admin form into an encoded instance object.
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public function test_expected_routes_in_schema() {
'/wp/v2/widget-types',
'/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)',
'/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)/encode',
'/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)/render',
'/wp/v2/widgets',
'/wp/v2/widgets/(?P<id>[\w\-]+)',
'/wp-site-health/v1',
Expand Down
25 changes: 25 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -7067,6 +7067,31 @@ mockedApiResponse.Schema = {
}
]
},
"/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)/render": {
"namespace": "wp/v2",
"methods": [
"POST"
],
"endpoints": [
{
"methods": [
"POST"
],
"args": {
"id_base": {
"description": "The widget type id.",
"type": "string",
"required": true
},
"instance": {
"description": "Current instance settings of the widget.",
"type": "object",
"required": false
}
}
}
]
},
"/wp/v2/widgets": {
"namespace": "wp/v2",
"methods": [
Expand Down