PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 / lib / class-wp-widget-block.php

class-wp-widget-block.php in Gutenberg 12.1.0, at lib/class-wp-widget-block.php

227 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widget API: WP_Widget_Block class
4 *
5 * @package Gutenberg
6 */
7
8 /**
9 * Core class used to implement a Block widget.
10 *
11 * @see WP_Widget
12 */
13 class WP_Widget_Block extends WP_Widget {
14
15 /**
16 * Default instance.
17 *
18 * @since 4.8.1
19 * @var array
20 */
21 protected $default_instance = array(
22 'content' => '',
23 );
24
25 /**
26 * Whether or not to show the widget's instance settings array in the REST
27 * API.
28 *
29 * @since 5.8.0
30 * @var array
31 */
32 public $show_instance_in_rest = true;
33
34 /**
35 * Sets up a new Block widget instance.
36 *
37 * @since 4.8.1
38 */
39 public function __construct() {
40 $widget_ops = array(
41 'classname' => 'widget_block',
42 'description' => __( 'Gutenberg block.', 'gutenberg' ),
43 'customize_selective_refresh' => true,
44 );
45 $control_ops = array(
46 'width' => 400,
47 'height' => 350,
48 );
49 parent::__construct( 'block', __( 'Block', 'gutenberg' ), $widget_ops, $control_ops );
50 add_action( 'is_wide_widget_in_customizer', array( $this, 'set_is_wide_widget_in_customizer' ), 10, 2 );
51 }
52
53 /**
54 * Outputs the content for the current Block widget instance.
55 *
56 * @param array $args Display arguments including 'before_title', 'after_title',
57 * 'before_widget', and 'after_widget'.
58 * @param array $instance Settings for the current Block widget instance.
59 *
60 * @since 4.8.1
61 *
62 * @global WP_Post $post Global post object.
63 */
64 public function widget( $args, $instance ) {
65 $instance = wp_parse_args( $instance, $this->default_instance );
66
67 echo str_replace(
68 'widget_block',
69 $this->get_dynamic_classname( $instance['content'] ),
70 $args['before_widget']
71 );
72
73 // Handle embeds for block widgets.
74 //
75 // When this feature is added to core it may need to be implemented
76 // differently. WP_Widget_Text is a good reference, that applies a
77 // filter for its content, which WP_Embed uses in its constructor.
78 // See https://core.trac.wordpress.org/ticket/51566.
79 global $wp_embed;
80 $content = $wp_embed->run_shortcode( $instance['content'] );
81 $content = $wp_embed->autoembed( $content );
82
83 $content = do_blocks( $content );
84 $content = do_shortcode( $content );
85
86 echo $content;
87
88 echo $args['after_widget'];
89 }
90
91 /**
92 * Calculates the classname to use in the block widget's container HTML.
93 *
94 * Usually this is set to $this->widget_options['classname'] by
95 * dynamic_sidebar(). In this case, however, we want to set the classname
96 * dynamically depending on the block contained by this block widget.
97 *
98 * If a block widget contains a block that has an equivalent legacy widget,
99 * we display that legacy widget's class name. This helps with theme
100 * backwards compatibility.
101 *
102 * @since 9.3.0
103 *
104 * @param array $content The HTML content of the current block widget.
105 *
106 * @return string The classname to use in the block widget's container HTML.
107 */
108 private function get_dynamic_classname( $content ) {
109 $blocks = parse_blocks( $content );
110
111 $block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;
112
113 switch ( $block_name ) {
114 case 'core/paragraph':
115 $classname = 'widget_block widget_text';
116 break;
117 case 'core/calendar':
118 $classname = 'widget_block widget_calendar';
119 break;
120 case 'core/search':
121 $classname = 'widget_block widget_search';
122 break;
123 case 'core/html':
124 $classname = 'widget_block widget_custom_html';
125 break;
126 case 'core/archives':
127 $classname = 'widget_block widget_archive';
128 break;
129 case 'core/latest-posts':
130 $classname = 'widget_block widget_recent_entries';
131 break;
132 case 'core/latest-comments':
133 $classname = 'widget_block widget_recent_comments';
134 break;
135 case 'core/tag-cloud':
136 $classname = 'widget_block widget_tag_cloud';
137 break;
138 case 'core/categories':
139 $classname = 'widget_block widget_categories';
140 break;
141 case 'core/audio':
142 $classname = 'widget_block widget_media_audio';
143 break;
144 case 'core/video':
145 $classname = 'widget_block widget_media_video';
146 break;
147 case 'core/image':
148 $classname = 'widget_block widget_media_image';
149 break;
150 case 'core/gallery':
151 $classname = 'widget_block widget_media_gallery';
152 break;
153 case 'core/rss':
154 $classname = 'widget_block widget_rss';
155 break;
156 default:
157 $classname = 'widget_block';
158 }
159
160 /**
161 * The classname used in the block widget's container HTML.
162 *
163 * This can be set according to the name of the block contained by the
164 * block widget.
165 *
166 * @param string $classname The classname to be used in the block widget's container HTML, e.g. 'widget_block widget_text'.
167 * @param string $block_name The name of the block contained by the block widget, e.g. 'core/paragraph'.
168 */
169 return apply_filters( 'widget_block_dynamic_classname', $classname, $block_name );
170 }
171
172 /**
173 * Handles updating settings for the current Block widget instance.
174 *
175 * @param array $new_instance New settings for this instance as input by the user via
176 * WP_Widget::form().
177 * @param array $old_instance Old settings for this instance.
178 *
179 * @return array Settings to save or bool false to cancel saving.
180 * @since 4.8.1
181 */
182 public function update( $new_instance, $old_instance ) {
183 $instance = array_merge( $this->default_instance, $old_instance );
184 $instance['content'] = $new_instance['content'];
185
186 return $instance;
187 }
188
189 /**
190 * Outputs the Block widget settings form.
191 *
192 * @param array $instance Current instance.
193 *
194 * @see WP_Widget_Custom_HTML::render_control_template_scripts()
195 */
196 public function form( $instance ) {
197 $instance = wp_parse_args( (array) $instance, $this->default_instance );
198 ?>
199 <p>
200 <label for="<?php echo $this->get_field_id( 'content' ); ?>">
201 <?php
202 /* translators: HTML code of the block, not an option that blocks HTML. */
203 _e( 'Block HTML:', 'gutenberg' );
204 ?>
205 </label>
206 <textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" rows="6" cols="50" class="widefat code"><?php echo esc_textarea( $instance['content'] ); ?></textarea>
207 </p>
208 <?php
209 }
210
211 /**
212 * Make sure no block widget is considered to be wide.
213 *
214 * @param boolean $is_wide Is regarded wide.
215 * @param string $widget_id Widget ID.
216 *
217 * @return bool Updated is_wide value.
218 */
219 public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
220 if ( strpos( $widget_id, 'block-' ) === 0 ) {
221 return false;
222 }
223
224 return $is_wide;
225 }
226 }
227