PluginProbe
Gutenberg / 10.0.0
Gutenberg v10.0.0
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 / edit-widgets / blocks / legacy-widget.php

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

101 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 * Register legacy widget block.
10 */
11 function gutenberg_register_block_core_legacy_widget() {
12 register_block_type_from_metadata(
13 __DIR__ . '/legacy-widget',
14 array(
15 'render_callback' => 'gutenberg_render_block_core_legacy_widget',
16 )
17 );
18 }
19
20 /**
21 * Renders the `core/legacy-widget` block on server.
22 *
23 * @param array $attributes The block attributes.
24 *
25 * @return string Returns the post content with the legacy widget added.
26 * @see WP_Widget
27 */
28 function gutenberg_render_block_core_legacy_widget( $attributes ) {
29 global $wp_widget_factory, $wp_registered_sidebars;
30
31 if ( isset( $attributes['widgetId'] ) ) {
32 return __( 'Rendering legacy widget block using widgetId is unsupported.', 'gutenberg' );
33 }
34 $widget_id = - 1;
35
36 if ( ! isset( $attributes['sidebarId'] ) || ! isset( $wp_registered_sidebars[ $attributes['sidebarId'] ] ) ) {
37 return '';
38 }
39 $sidebar_id = $attributes['sidebarId'];
40
41 if ( ! isset( $attributes['widgetClass'] ) || ! isset( $wp_widget_factory->widgets[ $attributes['widgetClass'] ] ) ) {
42 return '';
43 }
44 $widget_class = $attributes['widgetClass'];
45 $widget_obj = $wp_widget_factory->widgets[ $widget_class ];
46
47 $instance = isset( $attributes['instance'] ) ? $attributes['instance'] : null;
48
49 $widget_params = array_merge(
50 array(
51 'classname' => array(),
52 ),
53 $widget_obj->widget_options
54 );
55
56 /** This filter is documented in wp-includes/widgets/widgets.php */
57 do_action( 'dynamic_sidebar_before', $sidebar_id, true );
58 $sidebar = $wp_registered_sidebars[ $sidebar_id ];
59
60 $params = array_merge(
61 array(
62 array_merge(
63 $sidebar,
64 array(
65 'widget_id' => $widget_id,
66 'widget_name' => $widget_obj->name,
67 )
68 ),
69 ),
70 array(
71 $instance,
72 )
73 );
74
75 // Substitute HTML `id` and `class` attributes into `before_widget`.
76 $classname_ = '';
77 foreach ( (array) $widget_params['classname'] as $cn ) {
78 if ( is_string( $cn ) ) {
79 $classname_ .= '_' . $cn;
80 } elseif ( is_object( $cn ) ) {
81 $classname_ .= '_' . get_class( $cn );
82 }
83 }
84 $classname_ = ltrim( $classname_, '_' );
85 $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ );
86
87 /** This filter is documented in wp-includes/widgets/widgets.php */
88 $params = apply_filters( 'dynamic_sidebar_params', $params );
89
90 /** This filter is documented in wp-includes/widgets/widgets.php */
91 do_action( 'dynamic_sidebar', $widget_params );
92
93 ob_start();
94 $widget_obj->_set( - 1 );
95 call_user_func_array( array( $widget_obj, 'widget' ), $params );
96
97 return ob_get_clean();
98 }
99
100 add_action( 'init', 'gutenberg_register_block_core_legacy_widget', 20 );
101