PluginProbe
Gutenberg / 10.3.0
Gutenberg v10.3.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 10.3.0, at lib/class-wp-widget-block.php

212 lines 6.1 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' => '%s', // Set dynamically in widget().
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 echo sprintf( $args['before_widget'], $this->get_dynamic_classname( $instance ) );
66 // Handle embeds for block widgets.
67 //
68 // When this feature is added to core it may need to be implemented
69 // differently. WP_Widget_Text is a good reference, that applies a
70 // filter for its content, which WP_Embed uses in its constructor.
71 // See https://core.trac.wordpress.org/ticket/51566.
72 global $wp_embed;
73 $content = $wp_embed->run_shortcode( $instance['content'] );
74 $content = $wp_embed->autoembed( $content );
75
76 $content = do_blocks( $content );
77 $content = do_shortcode( $content );
78
79 echo $content;
80
81 echo $args['after_widget'];
82 }
83
84 /**
85 * Calculates the classname to use in the block widget's container HTML.
86 *
87 * Usually this is set to $this->widget_options['classname'] by
88 * dynamic_sidebar(). In this case, however, we want to set the classname
89 * dynamically depending on the block contained by this block widget.
90 *
91 * If a block widget contains a block that has an equivalent legacy widget,
92 * we display that legacy widget's class name. This helps with theme
93 * backwards compatibility.
94 *
95 * @since 9.3.0
96 *
97 * @param array $instance Settings for the current block widget instance.
98 *
99 * @return string The classname to use in the block widget's container HTML.
100 */
101 private function get_dynamic_classname( $instance ) {
102 $blocks = parse_blocks( $instance['content'] );
103
104 $block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;
105
106 switch ( $block_name ) {
107 case 'core/paragraph':
108 $classname = 'widget_block widget_text';
109 break;
110 case 'core/calendar':
111 $classname = 'widget_block widget_calendar';
112 break;
113 case 'core/search':
114 $classname = 'widget_block widget_search';
115 break;
116 case 'core/html':
117 $classname = 'widget_block widget_custom_html';
118 break;
119 case 'core/archives':
120 $classname = 'widget_block widget_archive';
121 break;
122 case 'core/latest-posts':
123 $classname = 'widget_block widget_recent_entries';
124 break;
125 case 'core/latest-comments':
126 $classname = 'widget_block widget_recent_comments';
127 break;
128 case 'core/tag-cloud':
129 $classname = 'widget_block widget_tag_cloud';
130 break;
131 case 'core/categories':
132 $classname = 'widget_block widget_categories';
133 break;
134 case 'core/audio':
135 $classname = 'widget_block widget_media_audio';
136 break;
137 case 'core/video':
138 $classname = 'widget_block widget_media_video';
139 break;
140 case 'core/image':
141 $classname = 'widget_block widget_media_image';
142 break;
143 case 'core/gallery':
144 $classname = 'widget_block widget_media_gallery';
145 break;
146 case 'core/rss':
147 $classname = 'widget_block widget_rss';
148 break;
149 default:
150 $classname = 'widget_block';
151 }
152
153 /**
154 * The classname used in the block widget's container HTML.
155 *
156 * This can be set according to the name of the block contained by the
157 * block widget.
158 *
159 * @param string $classname The classname to be used in the block widget's container HTML, e.g. 'widget_block widget_text'.
160 * @param string $block_name The name of the block contained by the block widget, e.g. 'core/paragraph'.
161 */
162 return apply_filters( 'widget_block_dynamic_classname', $classname, $block_name );
163 }
164
165 /**
166 * Handles updating settings for the current Block widget instance.
167 *
168 * @param array $new_instance New settings for this instance as input by the user via
169 * WP_Widget::form().
170 * @param array $old_instance Old settings for this instance.
171 *
172 * @return array Settings to save or bool false to cancel saving.
173 * @since 4.8.1
174 */
175 public function update( $new_instance, $old_instance ) {
176 $instance = array_merge( $this->default_instance, $old_instance );
177 $instance['content'] = $new_instance['content'];
178
179 return $instance;
180 }
181
182 /**
183 * Outputs the Block widget settings form.
184 *
185 * @param array $instance Current instance.
186 *
187 * @see WP_Widget_Custom_HTML::render_control_template_scripts()
188 */
189 public function form( $instance ) {
190 $instance = wp_parse_args( (array) $instance, $this->default_instance );
191 ?>
192 <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>
193 <?php
194 }
195
196 /**
197 * Make sure no block widget is considered to be wide.
198 *
199 * @param boolean $is_wide Is regarded wide.
200 * @param string $widget_id Widget ID.
201 *
202 * @return bool Updated is_wide value.
203 */
204 public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
205 if ( strpos( $widget_id, 'block-' ) === 0 ) {
206 return false;
207 }
208
209 return $is_wide;
210 }
211 }
212