-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
class-experimental-wp-widget-blocks-manager.php
420 lines (382 loc) · 11.6 KB
/
class-experimental-wp-widget-blocks-manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* Start: Include for phase 2
*
* @package gutenberg
* @since 5.7.0
*/
/**
* Class that provides a set of static abstractions to deal with widgets.
* Intended to be used by WP_REST_Widget_Areas_Controller.
*
* @since 5.7.0
*/
class Experimental_WP_Widget_Blocks_Manager {
/**
* Array of sidebar_widgets as it was before the filter swap_out_sidebars_blocks_for_block_widgets was ever executed.
*
* @var array
*/
private static $unfiltered_sidebar_widgets = null;
/**
* Returns the $wp_registered_widgets global.
*
* @since 5.7.0
*
* @return array $wp_registered_widgets global.
*/
private static function get_wp_registered_widgets() {
global $wp_registered_widgets;
return $wp_registered_widgets;
}
/**
* Returns the $wp_registered_sidebars global.
*
* @since 5.7.0
*
* @return array $wp_registered_sidebars global.
*/
private static function get_wp_registered_sidebars() {
global $wp_registered_sidebars;
return $wp_registered_sidebars;
}
/**
* Given a sidebar_id returns the sidebar object in $wp_registered_sidebars for tha sidebar_id.
*
* @since 5.7.0
*
* @param string $sidebar_id Identifier of the sidebar.
* @return array Sidebar structure.
*/
public static function get_wp_registered_sidebars_sidebar( $sidebar_id ) {
$wp_registered_sidebars = self::get_wp_registered_sidebars();
return $wp_registered_sidebars[ $sidebar_id ];
}
/**
* Returns the result of wp_get_sidebars_widgets without swap_out_sidebars_blocks_for_block_widgets filter being applied.
*
* @since 5.7.0
*/
private static function get_raw_sidebar_widgets() {
return self::$unfiltered_sidebar_widgets;
}
/**
* Returns a post id being referenced in a sidebar area.
*
* @since 5.7.0
*
* @param string $sidebar_id Identifier of the sidebar.
* @return integer Post id.
*/
public static function get_post_id_referenced_in_sidebar( $sidebar_id ) {
$sidebars = self::get_raw_sidebar_widgets();
$sidebar = $sidebars[ $sidebar_id ];
return is_numeric( $sidebar ) ? $sidebar : 0;
}
/**
* Updates a sidebar structure to reference a $post_id, and makes the widgets being referenced inactive.
*
* @since 5.7.0
*
* @param string $sidebar_id Identifier of the sidebar.
* @param integer $post_id Post id.
*/
public static function reference_post_id_in_sidebar( $sidebar_id, $post_id ) {
$sidebars = self::get_raw_sidebar_widgets();
$sidebar = $sidebars[ $sidebar_id ];
wp_set_sidebars_widgets(
array_merge(
$sidebars,
array(
$sidebar_id => $post_id,
'wp_inactive_widgets' => array_merge(
$sidebars['wp_inactive_widgets'],
$sidebar
),
)
)
);
}
/**
* Returns a sidebar as an array of legacy widget blocks.
*
* @since 5.7.0
*
* @param string $sidebar_id Identifier of the sidebar.
* @return array $post_id Post id.
*/
public static function get_sidebar_as_blocks( $sidebar_id ) {
$blocks = array();
$sidebars_items = self::get_raw_sidebar_widgets();
$wp_registered_sidebars = self::get_wp_registered_sidebars();
foreach ( $sidebars_items[ $sidebar_id ] as $item ) {
$widget_class = self::get_widget_class( $item );
list( $object, $number ) = self::get_widget_info( $item );
$new_block = array(
'blockName' => 'core/legacy-widget',
'attrs' => array(
'id' => $item,
'instance' => self::get_sidebar_widget_instance( $wp_registered_sidebars[ $sidebar_id ], $item ),
),
'innerHTML' => '',
);
if ( null !== $widget_class ) {
$new_block['attrs']['widgetClass'] = $widget_class;
}
if ( isset( $object->id_base ) ) {
$new_block['attrs']['idBase'] = $object->id_base;
}
if ( is_int( $number ) ) {
$new_block['attrs']['number'] = $number;
}
$blocks[] = $new_block;
}
return $blocks;
}
/**
* Verifies if a sidebar id is valid or not.
*
* @since 5.7.0
*
* @param string $sidebar_id Identifier of the sidebar.
* @return boolean True if the $sidebar_id value is valid and false otherwise.
*/
public static function is_valid_sidebar_id( $sidebar_id ) {
$wp_registered_sidebars = self::get_wp_registered_sidebars();
return isset( $wp_registered_sidebars[ $sidebar_id ] );
}
/**
* Given a widget id returns the name of the class the represents the widget.
*
* @since 5.7.0
*
* @param string $widget_id Identifier of the widget.
* @return string|null Name of the class that represents the widget or null if the widget is not represented by a class.
*/
private static function get_widget_class( $widget_id ) {
$wp_registered_widgets = self::get_wp_registered_widgets();
if (
isset( $wp_registered_widgets[ $widget_id ]['callback'][0] ) &&
$wp_registered_widgets[ $widget_id ]['callback'][0] instanceof WP_Widget
) {
return get_class( $wp_registered_widgets[ $widget_id ]['callback'][0] );
}
return null;
}
/**
* Retrieves a widget instance.
*
* @since 5.7.0
*
* @param array $sidebar sidebar data available at $wp_registered_sidebars.
* @param string $id Identifier of the widget instance.
* @return array Array containing the widget instance.
*/
private static function get_sidebar_widget_instance( $sidebar, $id ) {
list( $object, $number, $name ) = self::get_widget_info( $id );
if ( ! $object ) {
return array();
}
$object->_set( $number );
$instances = $object->get_settings();
$instance = $instances[ $number ];
$args = array_merge(
$sidebar,
array(
'widget_id' => $id,
'widget_name' => $name,
)
);
/**
* Filters the settings for a particular widget instance.
*
* Returning false will effectively short-circuit display of the widget.
*
* @since 2.8.0
*
* @param array $instance The current widget instance's settings.
* @param WP_Widget $this The current widget instance.
* @param array $args An array of default widget arguments.
*/
$instance = apply_filters( 'widget_display_callback', $instance, $object, $args );
if ( false === $instance ) {
return array();
}
return $instance;
}
/**
* Given a widget id returns an array containing information about the widget.
*
* @since 5.7.0
*
* @param string $widget_id Identifier of the widget.
* @return array Array containing the the widget object, the number, and the name.
*/
private static function get_widget_info( $widget_id ) {
$wp_registered_widgets = self::get_wp_registered_widgets();
if (
! isset( $wp_registered_widgets[ $widget_id ]['callback'][0] ) ||
! isset( $wp_registered_widgets[ $widget_id ]['params'][0]['number'] ) ||
! isset( $wp_registered_widgets[ $widget_id ]['name'] ) ||
! ( $wp_registered_widgets[ $widget_id ]['callback'][0] instanceof WP_Widget )
) {
return array( null, null, null );
}
$object = $wp_registered_widgets[ $widget_id ]['callback'][0];
$number = $wp_registered_widgets[ $widget_id ]['params'][0]['number'];
$name = $wp_registered_widgets[ $widget_id ]['name'];
return array( $object, $number, $name );
}
/**
* Serializes an array of blocks.
*
* @since 5.7.0
*
* @param array $blocks Post Array of block objects.
* @return string String representing the blocks.
*/
public static function serialize_blocks( $blocks ) {
return implode( array_map( 'self::serialize_block', $blocks ) );
}
/**
* Serializes a block.
*
* @since 5.7.0
*
* @param array $block Block object.
* @return string String representing the block.
*/
public static function serialize_block( $block ) {
if ( ! isset( $block['blockName'] ) ) {
return false;
}
$name = $block['blockName'];
if ( 0 === strpos( $name, 'core/' ) ) {
$name = substr( $name, strlen( 'core/' ) );
}
if ( empty( $block['attrs'] ) ) {
$opening_tag_suffix = '';
} else {
$opening_tag_suffix = ' ' . json_encode( $block['attrs'] );
}
if ( empty( $block['innerHTML'] ) ) {
return sprintf(
'<!-- wp:%s%s /-->',
$name,
$opening_tag_suffix
);
} else {
return sprintf(
'<!-- wp:%1$s%2$s -->%3$s<!-- /wp:%1$s -->',
$name,
$opening_tag_suffix,
$block['innerHTML']
);
}
}
/**
* Outputs a block widget on the website frontend.
*
* @param array $options Widget options.
* @param array $arguments Arguments array.
*/
public static function output_blocks_widget( $options, $arguments ) {
echo $options['before_widget'];
foreach ( $arguments['blocks'] as $block ) {
echo render_block( $block );
}
echo $options['after_widget'];
}
/**
* Noop block widget control output function for the necessary call to `wp_register_widget_control`.
*/
public static function output_blocks_widget_control() {}
/**
* Registers a widget that should represent a set of blocks and returns its ID.
*
* @param array $blocks Array of blocks.
*/
public static function convert_blocks_to_widget( $blocks ) {
$widget_id = 'blocks-widget-' . md5( self::serialize_blocks( $blocks ) );
global $wp_registered_widgets;
if ( isset( $wp_registered_widgets[ $widget_id ] ) ) {
return $widget_id;
}
wp_register_sidebar_widget(
$widget_id,
__( 'Blocks Area', 'gutenberg' ),
'Experimental_WP_Widget_Blocks_Manager::output_blocks_widget',
array(
'classname' => 'widget-area',
'description' => __( 'Displays a set of blocks', 'gutenberg' ),
),
array(
'blocks' => $blocks,
)
);
wp_register_widget_control(
$widget_id,
__( 'Blocks Area', 'gutenberg' ),
'Experimental_WP_Widget_Blocks_Manager::output_blocks_widget_control',
array( 'id_base' => 'blocks-widget' )
);
return $widget_id;
}
/**
* Filters the $sidebars_widgets to exchange wp_area post id with a widget that renders that block area.
*
* @param array $sidebars_widgets_input An associative array of sidebars and their widgets.
*/
public static function swap_out_sidebars_blocks_for_block_widgets( $sidebars_widgets_input ) {
global $sidebars_widgets;
global $wp_customize;
if ( null === self::$unfiltered_sidebar_widgets ) {
self::$unfiltered_sidebar_widgets = $sidebars_widgets;
}
$changeset_data = null;
if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) {
$changeset_data = $wp_customize->changeset_data();
if ( isset( $changeset_data['gutenberg_widget_blocks']['value'] ) ) {
$changeset_data = json_decode( $changeset_data['gutenberg_widget_blocks']['value'] );
}
}
$filtered_sidebar_widgets = array();
foreach ( $sidebars_widgets_input as $sidebar_id => $item ) {
$changeset_value = $changeset_data && isset( $changeset_data->$sidebar_id )
? $changeset_data->$sidebar_id
: null;
if ( ! is_numeric( $item ) && ! $changeset_value ) {
$filtered_sidebar_widgets[ $sidebar_id ] = $item;
continue;
}
$filtered_widgets = array();
$last_set_of_blocks = array();
$blocks = parse_blocks(
$changeset_value ? $changeset_value : get_post( $item )->post_content
);
foreach ( $blocks as $block ) {
if ( ! isset( $block['blockName'] ) ) {
continue;
}
if (
'core/legacy-widget' === $block['blockName'] &&
isset( $block['attrs']['identifier'] )
) {
if ( ! empty( $last_set_of_blocks ) ) {
$filtered_widgets[] = self::convert_blocks_to_widget( $last_set_of_blocks );
$last_set_of_blocks = array();
}
$filtered_widgets[] = $block['attrs']['identifier'];
} else {
$last_set_of_blocks[] = $block;
}
}
if ( ! empty( $last_set_of_blocks ) ) {
$filtered_widgets[] = self::convert_blocks_to_widget( $last_set_of_blocks );
}
$filtered_sidebar_widgets[ $sidebar_id ] = $filtered_widgets;
}
$sidebars_widgets = $filtered_sidebar_widgets;
return $filtered_sidebar_widgets;
}
}