| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/legacy-widget` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns the result of rendering a widget having its instance id. |
| 10 |
* |
| 11 |
* @param string $id Widget id. |
| 12 |
* |
| 13 |
* @return string Returns the rendered widget as a string. |
| 14 |
*/ |
| 15 |
function gutenberg_render_widget_by_id( $id ) { |
| 16 |
// Code extracted from src/wp-includes/widgets.php dynamic_sidebar function. |
| 17 |
// Todo: When merging to core extract this part of dynamic_sidebar into its own function. |
| 18 |
global $wp_registered_widgets; |
| 19 |
|
| 20 |
if ( ! isset( $wp_registered_widgets[ $id ] ) ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
$params = array_merge( |
| 24 |
array( |
| 25 |
array_merge( |
| 26 |
array( |
| 27 |
'before_widget' => '<div class="widget %s">', |
| 28 |
'after_widget' => '</div>', |
| 29 |
'before_title' => '<h2 class="widgettitle">', |
| 30 |
'after_title' => '</h2>', |
| 31 |
), |
| 32 |
array( |
| 33 |
'widget_id' => $id, |
| 34 |
'widget_name' => $wp_registered_widgets[ $id ]['name'], |
| 35 |
) |
| 36 |
), |
| 37 |
), |
| 38 |
(array) $wp_registered_widgets[ $id ]['params'] |
| 39 |
); |
| 40 |
|
| 41 |
// Substitute HTML id and class attributes into before_widget. |
| 42 |
$classname_ = ''; |
| 43 |
foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) { |
| 44 |
if ( is_string( $cn ) ) { |
| 45 |
$classname_ .= '_' . $cn; |
| 46 |
} elseif ( is_object( $cn ) ) { |
| 47 |
$classname_ .= '_' . get_class( $cn ); |
| 48 |
} |
| 49 |
} |
| 50 |
$classname_ = ltrim( $classname_, '_' ); |
| 51 |
$params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $id, $classname_ ); |
| 52 |
|
| 53 |
$params = apply_filters( 'dynamic_sidebar_params', $params ); |
| 54 |
$callback = $wp_registered_widgets[ $id ]['callback']; |
| 55 |
do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] ); |
| 56 |
|
| 57 |
if ( is_callable( $callback ) ) { |
| 58 |
ob_start(); |
| 59 |
call_user_func_array( $callback, $params ); |
| 60 |
return ob_get_clean(); |
| 61 |
} |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Renders the `core/legacy-widget` block on server. |
| 67 |
* |
| 68 |
* @see WP_Widget |
| 69 |
* |
| 70 |
* @param array $attributes The block attributes. |
| 71 |
* |
| 72 |
* @return string Returns the post content with the legacy widget added. |
| 73 |
*/ |
| 74 |
function gutenberg_render_block_legacy_widget( $attributes ) { |
| 75 |
$id = null; |
| 76 |
$widget_class = null; |
| 77 |
if ( isset( $attributes['id'] ) ) { |
| 78 |
$id = $attributes['id']; |
| 79 |
} |
| 80 |
if ( isset( $attributes['widgetClass'] ) ) { |
| 81 |
$widget_class = $attributes['widgetClass']; |
| 82 |
} |
| 83 |
|
| 84 |
if ( $id ) { |
| 85 |
return gutenberg_render_widget_by_id( $id ); |
| 86 |
} |
| 87 |
if ( ! $widget_class ) { |
| 88 |
return ''; |
| 89 |
} |
| 90 |
|
| 91 |
ob_start(); |
| 92 |
$instance = null; |
| 93 |
if ( isset( $attributes['instance'] ) ) { |
| 94 |
$instance = $attributes['instance']; |
| 95 |
} |
| 96 |
the_widget( $widget_class, $instance ); |
| 97 |
return ob_get_clean(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Register legacy widget block. |
| 102 |
*/ |
| 103 |
function gutenberg_register_block_core_legacy_widget() { |
| 104 |
register_block_type( |
| 105 |
'core/legacy-widget', |
| 106 |
array( |
| 107 |
'attributes' => array( |
| 108 |
'widgetClass' => array( |
| 109 |
'type' => 'string', |
| 110 |
), |
| 111 |
'id' => array( |
| 112 |
'type' => 'string', |
| 113 |
), |
| 114 |
'idBase' => array( |
| 115 |
'type' => 'string', |
| 116 |
), |
| 117 |
'number' => array( |
| 118 |
'type' => 'number', |
| 119 |
), |
| 120 |
'instance' => array( |
| 121 |
'type' => 'object', |
| 122 |
), |
| 123 |
), |
| 124 |
'render_callback' => 'gutenberg_render_block_legacy_widget', |
| 125 |
) |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
add_action( 'init', 'gutenberg_register_block_core_legacy_widget', 20 ); |
| 130 |
|