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