PluginProbe
Gutenberg / 10.2.1
Gutenberg v10.2.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 / lib / class-wp-widget-block.php

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

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