PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / legacy-widget.php

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

130 lines 3.1 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 * Returns the result of rendering a widget having its instance id.
10 *
11 * @param string $id Widget id.
12 *
13 * @return string Returns the rendered widget as a string.
14 */
15 function gutenberg_render_widget_by_id( $id ) {
16 // Code extracted from src/wp-includes/widgets.php dynamic_sidebar function.
17 // Todo: When merging to core extract this part of dynamic_sidebar into its own function.
18 global $wp_registered_widgets;
19
20 if ( ! isset( $wp_registered_widgets[ $id ] ) ) {
21 return false;
22 }
23 $params = array_merge(
24 array(
25 array_merge(
26 array(
27 'before_widget' => '<div class="widget %s">',
28 'after_widget' => '</div>',
29 'before_title' => '<h2 class="widgettitle">',
30 'after_title' => '</h2>',
31 ),
32 array(
33 'widget_id' => $id,
34 'widget_name' => $wp_registered_widgets[ $id ]['name'],
35 )
36 ),
37 ),
38 (array) $wp_registered_widgets[ $id ]['params']
39 );
40
41 // Substitute HTML id and class attributes into before_widget.
42 $classname_ = '';
43 foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) {
44 if ( is_string( $cn ) ) {
45 $classname_ .= '_' . $cn;
46 } elseif ( is_object( $cn ) ) {
47 $classname_ .= '_' . get_class( $cn );
48 }
49 }
50 $classname_ = ltrim( $classname_, '_' );
51 $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $id, $classname_ );
52
53 $params = apply_filters( 'dynamic_sidebar_params', $params );
54 $callback = $wp_registered_widgets[ $id ]['callback'];
55 do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] );
56
57 if ( is_callable( $callback ) ) {
58 ob_start();
59 call_user_func_array( $callback, $params );
60 return ob_get_clean();
61 }
62 return false;
63 }
64
65 /**
66 * Renders the `core/legacy-widget` block on server.
67 *
68 * @see WP_Widget
69 *
70 * @param array $attributes The block attributes.
71 *
72 * @return string Returns the post content with the legacy widget added.
73 */
74 function gutenberg_render_block_legacy_widget( $attributes ) {
75 $id = null;
76 $widget_class = null;
77 if ( isset( $attributes['id'] ) ) {
78 $id = $attributes['id'];
79 }
80 if ( isset( $attributes['widgetClass'] ) ) {
81 $widget_class = $attributes['widgetClass'];
82 }
83
84 if ( $id ) {
85 return gutenberg_render_widget_by_id( $id );
86 }
87 if ( ! $widget_class ) {
88 return '';
89 }
90
91 ob_start();
92 $instance = null;
93 if ( isset( $attributes['instance'] ) ) {
94 $instance = $attributes['instance'];
95 }
96 the_widget( $widget_class, $instance );
97 return ob_get_clean();
98 }
99
100 /**
101 * Register legacy widget block.
102 */
103 function gutenberg_register_block_core_legacy_widget() {
104 register_block_type(
105 'core/legacy-widget',
106 array(
107 'attributes' => array(
108 'widgetClass' => array(
109 'type' => 'string',
110 ),
111 'id' => array(
112 'type' => 'string',
113 ),
114 'idBase' => array(
115 'type' => 'string',
116 ),
117 'number' => array(
118 'type' => 'number',
119 ),
120 'instance' => array(
121 'type' => 'object',
122 ),
123 ),
124 'render_callback' => 'gutenberg_render_block_legacy_widget',
125 )
126 );
127 }
128
129 add_action( 'init', 'gutenberg_register_block_core_legacy_widget', 20 );
130