| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/legacy-widget` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Register legacy widget block. |
| 10 |
*/ |
| 11 |
function gutenberg_register_block_core_legacy_widget() { |
| 12 |
register_block_type_from_metadata( |
| 13 |
__DIR__ . '/legacy-widget', |
| 14 |
array( |
| 15 |
'render_callback' => 'gutenberg_render_block_core_legacy_widget', |
| 16 |
) |
| 17 |
); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Renders the `core/legacy-widget` block on server. |
| 22 |
* |
| 23 |
* @param array $attributes The block attributes. |
| 24 |
* |
| 25 |
* @return string Returns the post content with the legacy widget added. |
| 26 |
* @see WP_Widget |
| 27 |
*/ |
| 28 |
function gutenberg_render_block_core_legacy_widget( $attributes ) { |
| 29 |
global $wp_widget_factory, $wp_registered_sidebars; |
| 30 |
|
| 31 |
if ( isset( $attributes['widgetId'] ) ) { |
| 32 |
return __( 'Rendering legacy widget block using widgetId is unsupported.', 'gutenberg' ); |
| 33 |
} |
| 34 |
$widget_id = - 1; |
| 35 |
|
| 36 |
if ( ! isset( $attributes['sidebarId'] ) || ! isset( $wp_registered_sidebars[ $attributes['sidebarId'] ] ) ) { |
| 37 |
return ''; |
| 38 |
} |
| 39 |
$sidebar_id = $attributes['sidebarId']; |
| 40 |
|
| 41 |
if ( ! isset( $attributes['widgetClass'] ) || ! isset( $wp_widget_factory->widgets[ $attributes['widgetClass'] ] ) ) { |
| 42 |
return ''; |
| 43 |
} |
| 44 |
$widget_class = $attributes['widgetClass']; |
| 45 |
$widget_obj = $wp_widget_factory->widgets[ $widget_class ]; |
| 46 |
|
| 47 |
$instance = isset( $attributes['instance'] ) ? $attributes['instance'] : null; |
| 48 |
|
| 49 |
$widget_params = array_merge( |
| 50 |
array( |
| 51 |
'classname' => array(), |
| 52 |
), |
| 53 |
$widget_obj->widget_options |
| 54 |
); |
| 55 |
|
| 56 |
/** This filter is documented in wp-includes/widgets/widgets.php */ |
| 57 |
do_action( 'dynamic_sidebar_before', $sidebar_id, true ); |
| 58 |
$sidebar = $wp_registered_sidebars[ $sidebar_id ]; |
| 59 |
|
| 60 |
$params = array_merge( |
| 61 |
array( |
| 62 |
array_merge( |
| 63 |
$sidebar, |
| 64 |
array( |
| 65 |
'widget_id' => $widget_id, |
| 66 |
'widget_name' => $widget_obj->name, |
| 67 |
) |
| 68 |
), |
| 69 |
), |
| 70 |
array( |
| 71 |
$instance, |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
// Substitute HTML `id` and `class` attributes into `before_widget`. |
| 76 |
$classname_ = ''; |
| 77 |
foreach ( (array) $widget_params['classname'] as $cn ) { |
| 78 |
if ( is_string( $cn ) ) { |
| 79 |
$classname_ .= '_' . $cn; |
| 80 |
} elseif ( is_object( $cn ) ) { |
| 81 |
$classname_ .= '_' . get_class( $cn ); |
| 82 |
} |
| 83 |
} |
| 84 |
$classname_ = ltrim( $classname_, '_' ); |
| 85 |
$params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ ); |
| 86 |
|
| 87 |
/** This filter is documented in wp-includes/widgets/widgets.php */ |
| 88 |
$params = apply_filters( 'dynamic_sidebar_params', $params ); |
| 89 |
|
| 90 |
/** This filter is documented in wp-includes/widgets/widgets.php */ |
| 91 |
do_action( 'dynamic_sidebar', $widget_params ); |
| 92 |
|
| 93 |
ob_start(); |
| 94 |
$widget_obj->_set( - 1 ); |
| 95 |
call_user_func_array( array( $widget_obj, 'widget' ), $params ); |
| 96 |
|
| 97 |
return ob_get_clean(); |
| 98 |
} |
| 99 |
|
| 100 |
add_action( 'init', 'gutenberg_register_block_core_legacy_widget', 20 ); |
| 101 |
|