PluginProbe
Gutenberg / 12.5.1
Gutenberg v12.5.1
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 12.5.1, at build/widgets/blocks/legacy-widget.php

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