WordPress/wp-includes/blocks/legacy-widget.php
noisysocks 8775b69cd0 Adds the widgets block editor to widgets.php and customize.php
Moves the widgets block editor from Gutenberg into WordPress Core.

- Adds @wordpress/edit-widgets, @wordpress/customize-widgets and
  @wordpress/widgets.
- Modifies wp-admin/widgets.php to branch between the old editor and new editor
  depending on wp_use_widgets_block_editor().
- Modifies WP_Customize_Widgets to branch between the old editor control and new
  editor control depending on wp_use_widgets_block_editor().

Follows [50996] which was missing these files.
See #51506.
Props isabel_brison, TimothyBlynJacobs, andraganescu, kevin940726, talldanwp.

Built from https://develop.svn.wordpress.org/trunk@50997


git-svn-id: http://core.svn.wordpress.org/trunk@50606 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-05-25 08:52:58 +00:00

112 lines
2.7 KiB
PHP

<?php
/**
* Server-side rendering of the `core/legacy-widget` block.
*
* @package WordPress
*/
/**
* Renders the 'core/legacy-widget' block.
*
* @param array $attributes The block attributes.
*
* @return string Rendered block.
*/
function render_block_core_legacy_widget( $attributes ) {
if ( isset( $attributes['id'] ) ) {
$sidebar_id = gutenberg_find_widgets_sidebar( $attributes['id'] );
return gutenberg_render_widget( $attributes['id'], $sidebar_id );
}
if ( ! isset( $attributes['idBase'] ) ) {
return '';
}
$widget_object = gutenberg_get_widget_object( $attributes['idBase'] );
if ( ! $widget_object ) {
return '';
}
if ( isset( $attributes['instance']['encoded'], $attributes['instance']['hash'] ) ) {
$serialized_instance = base64_decode( $attributes['instance']['encoded'] );
if ( wp_hash( $serialized_instance ) !== $attributes['instance']['hash'] ) {
return '';
}
$instance = unserialize( $serialized_instance );
} else {
$instance = array();
}
ob_start();
the_widget( get_class( $widget_object ), $instance );
return ob_get_clean();
}
/**
* Registers the 'core/legacy-widget' block.
*/
function register_block_core_legacy_widget() {
register_block_type_from_metadata(
__DIR__ . '/legacy-widget',
array(
'render_callback' => 'render_block_core_legacy_widget',
)
);
}
add_action( 'init', 'register_block_core_legacy_widget', 20 );
/**
* Intercepts any request with legacy-widget-preview in the query param and, if
* set, renders a page containing a preview of the requested Legacy Widget
* block.
*/
function handle_legacy_widget_preview_iframe() {
if ( empty( $_GET['legacy-widget-preview'] ) ) {
return;
}
if ( ! current_user_can( 'edit_theme_options' ) ) {
return;
}
define( 'IFRAME_REQUEST', true );
?>
<!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 {
background: #FFF !important;
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( $_GET['legacy-widget-preview'] );
?>
</div><!-- #content -->
</div><!-- #page -->
<?php wp_footer(); ?>
</body>
</html>
<?php
exit;
}
add_action( 'init', 'handle_legacy_widget_preview_iframe', 21 );