UNPKG

1.74 kBapplication/x-httpd-phpView Raw
1<?php
2/**
3 * Server-side rendering of the `core/legacy-widget` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/legacy-widget` block on server.
10 *
11 * @see WP_Widget
12 *
13 * @param array $attributes The block attributes.
14 *
15 * @return string Returns the post content with the legacy widget added.
16 */
17function render_block_legacy_widget( $attributes ) {
18 if ( ! isset( $attributes['identifier'] ) ) {
19 return '';
20 }
21 $identifier = $attributes['identifier'];
22 if (
23 isset( $attributes['isCallbackWidget'] ) &&
24 $attributes['isCallbackWidget']
25 ) {
26 global $wp_registered_widgets;
27 if ( ! isset( $wp_registered_widgets[ $identifier ] ) ) {
28 return '';
29 }
30 $widget = $wp_registered_widgets[ $identifier ];
31 $params = array_merge(
32 array(
33 'widget_id' => $identifier,
34 'widget_name' => $widget['name'],
35 ),
36 (array) $wp_registered_widgets[ $identifier ]['params']
37 );
38 $params = apply_filters( 'dynamic_sidebar_params', $params );
39
40 $callback = $widget['callback'];
41
42 if ( is_callable( $callback ) ) {
43 ob_start();
44 call_user_func_array( $callback, $params );
45 return ob_get_clean();
46 }
47 return '';
48 }
49 ob_start();
50 the_widget( $identifier, $attributes['instance'] );
51 return ob_get_clean();
52
53}
54
55/**
56 * Register legacy widget block.
57 */
58function register_block_core_legacy_widget() {
59 register_block_type(
60 'core/legacy-widget',
61 array(
62 'attributes' => array(
63 'identifier' => array(
64 'type' => 'string',
65 ),
66 'instance' => array(
67 'type' => 'object',
68 ),
69 'isCallbackWidget' => array(
70 'type' => 'boolean',
71 ),
72 ),
73 'render_callback' => 'render_block_legacy_widget',
74 )
75 );
76}
77
78add_action( 'init', 'register_block_core_legacy_widget' );