PluginProbe
Gutenberg / 10.5.3
Gutenberg v10.5.3
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / legacy-widget.php

legacy-widget.php in Gutenberg 10.5.3, at build/block-library/blocks/legacy-widget.php

112 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/legacy-widget` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the 'core/legacy-widget' block.
10 *
11 * @param array $attributes The block attributes.
12 *
13 * @return string Rendered block.
14 */
15 function gutenberg_render_block_core_legacy_widget( $attributes ) {
16 if ( isset( $attributes['id'] ) ) {
17 $sidebar_id = gutenberg_find_widgets_sidebar( $attributes['id'] );
18 return gutenberg_render_widget( $attributes['id'], $sidebar_id );
19 }
20
21 if ( ! isset( $attributes['idBase'] ) ) {
22 return '';
23 }
24
25 $widget_object = gutenberg_get_widget_object( $attributes['idBase'] );
26
27 if ( ! $widget_object ) {
28 return '';
29 }
30
31 if ( isset( $attributes['instance']['encoded'], $attributes['instance']['hash'] ) ) {
32 $serialized_instance = base64_decode( $attributes['instance']['encoded'] );
33 if ( wp_hash( $serialized_instance ) !== $attributes['instance']['hash'] ) {
34 return '';
35 }
36 $instance = unserialize( $serialized_instance );
37 } else {
38 $instance = array();
39 }
40
41 ob_start();
42 the_widget( get_class( $widget_object ), $instance );
43 return ob_get_clean();
44 }
45
46 /**
47 * Registers the 'core/legacy-widget' block.
48 */
49 function gutenberg_register_block_core_legacy_widget() {
50 register_block_type_from_metadata(
51 __DIR__ . '/legacy-widget',
52 array(
53 'render_callback' => 'gutenberg_render_block_core_legacy_widget',
54 )
55 );
56 }
57
58 add_action( 'init', 'gutenberg_register_block_core_legacy_widget', 20 );
59
60 /**
61 * Intercepts any request with legacy-widget-preview in the query param and, if
62 * set, renders a page containing a preview of the requested Legacy Widget
63 * block.
64 */
65 function gutenberg_handle_legacy_widget_preview_iframe() {
66 if ( empty( $_GET['legacy-widget-preview'] ) ) {
67 return;
68 }
69
70 if ( ! current_user_can( 'edit_theme_options' ) ) {
71 return;
72 }
73
74 define( 'IFRAME_REQUEST', true );
75
76 ?>
77 <!doctype html>
78 <html <?php language_attributes(); ?>>
79 <head>
80 <meta charset="<?php bloginfo( 'charset' ); ?>" />
81 <meta name="viewport" content="width=device-width, initial-scale=1" />
82 <link rel="profile" href="https://gmpg.org/xfn/11" />
83 <?php wp_head(); ?>
84 <style>
85 /* Reset theme styles */
86 html, body, #page, #content {
87 background: #FFF !important;
88 padding: 0 !important;
89 margin: 0 !important;
90 }
91 </style>
92 </head>
93 <body <?php body_class(); ?>>
94 <div id="page" class="site">
95 <div id="content" class="site-content">
96 <?php
97 $registry = WP_Block_Type_Registry::get_instance();
98 $block = $registry->get_registered( 'core/legacy-widget' );
99 echo $block->render( $_GET['legacy-widget-preview'] );
100 ?>
101 </div><!-- #content -->
102 </div><!-- #page -->
103 <?php wp_footer(); ?>
104 </body>
105 </html>
106 <?php
107
108 exit;
109 }
110
111 add_action( 'init', 'gutenberg_handle_legacy_widget_preview_iframe', 21 );
112