PluginProbe
Gutenberg / 17.5.2
Gutenberg v17.5.2
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 / widgets / blocks / legacy-widget.php

legacy-widget.php in Gutenberg 17.5.2, at build/widgets/blocks/legacy-widget.php

150 lines 3.9 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 global $wp_widget_factory;
17
18 if ( isset( $attributes['id'] ) ) {
19 $sidebar_id = wp_find_widgets_sidebar( $attributes['id'] );
20 return wp_render_widget( $attributes['id'], $sidebar_id );
21 }
22
23 if ( ! isset( $attributes['idBase'] ) ) {
24 return '';
25 }
26
27 $id_base = $attributes['idBase'];
28 $widget_key = $wp_widget_factory->get_widget_key( $id_base );
29 $widget_object = $wp_widget_factory->get_widget_object( $id_base );
30
31 if ( ! $widget_key || ! $widget_object ) {
32 return '';
33 }
34
35 if ( isset( $attributes['instance']['encoded'], $attributes['instance']['hash'] ) ) {
36 $serialized_instance = base64_decode( $attributes['instance']['encoded'] );
37 if ( ! hash_equals( wp_hash( $serialized_instance ), (string) $attributes['instance']['hash'] ) ) {
38 return '';
39 }
40 $instance = unserialize( $serialized_instance );
41 } else {
42 $instance = array();
43 }
44
45 $args = array(
46 'widget_id' => $widget_object->id,
47 'widget_name' => $widget_object->name,
48 );
49
50 ob_start();
51 the_widget( $widget_key, $instance, $args );
52 return ob_get_clean();
53 }
54
55 /**
56 * Registers the 'core/legacy-widget' block.
57 */
58 function gutenberg_register_block_core_legacy_widget() {
59 register_block_type_from_metadata(
60 __DIR__ . '/legacy-widget',
61 array(
62 'render_callback' => 'gutenberg_render_block_core_legacy_widget',
63 )
64 );
65 }
66
67 add_action( 'init', 'gutenberg_register_block_core_legacy_widget', 20 );
68
69 /**
70 * Intercepts any request with legacy-widget-preview in the query param and, if
71 * set, renders a page containing a preview of the requested Legacy Widget
72 * block.
73 */
74 function gutenberg_handle_legacy_widget_preview_iframe() {
75 if ( empty( $_GET['legacy-widget-preview'] ) ) {
76 return;
77 }
78
79 if ( ! current_user_can( 'edit_theme_options' ) ) {
80 return;
81 }
82
83 define( 'IFRAME_REQUEST', true );
84
85 ?>
86 <!doctype html>
87 <html <?php language_attributes(); ?>>
88 <head>
89 <meta charset="<?php bloginfo( 'charset' ); ?>" />
90 <meta name="viewport" content="width=device-width, initial-scale=1" />
91 <link rel="profile" href="https://gmpg.org/xfn/11" />
92 <?php wp_head(); ?>
93 <style>
94 /* Reset theme styles */
95 html, body, #page, #content {
96 padding: 0 !important;
97 margin: 0 !important;
98 }
99
100 /* Hide root level text nodes */
101 body {
102 font-size: 0 !important;
103 }
104
105 /* Hide non-widget elements */
106 body *:not(#page):not(#content):not(.widget):not(.widget *) {
107 display: none !important;
108 font-size: 0 !important;
109 height: 0 !important;
110 left: -9999px !important;
111 max-height: 0 !important;
112 max-width: 0 !important;
113 opacity: 0 !important;
114 pointer-events: none !important;
115 position: absolute !important;
116 top: -9999px !important;
117 transform: translate(-9999px, -9999px) !important;
118 visibility: hidden !important;
119 z-index: -999 !important;
120 }
121
122 /* Restore widget font-size */
123 .widget {
124 font-size: var(--global--font-size-base);
125 }
126 </style>
127 </head>
128 <body <?php body_class(); ?>>
129 <div id="page" class="site">
130 <div id="content" class="site-content">
131 <?php
132 $registry = WP_Block_Type_Registry::get_instance();
133 $block = $registry->get_registered( 'core/legacy-widget' );
134 echo $block->render( $_GET['legacy-widget-preview'] );
135 ?>
136 </div><!-- #content -->
137 </div><!-- #page -->
138 <?php wp_footer(); ?>
139 </body>
140 </html>
141 <?php
142
143 exit;
144 }
145
146 // Use admin_init instead of init to ensure get_current_screen function is already available.
147 // This isn't strictly required, but enables better compatibility with existing plugins.
148 // See: https://github.com/WordPress/gutenberg/issues/32624.
149 add_action( 'admin_init', 'gutenberg_handle_legacy_widget_preview_iframe', 20 );
150