You are here

legacy-widget.php in Gutenberg 8

Same filename and directory in other branches
  1. 8.2 vendor/gutenberg/block-library/blocks/legacy-widget.php

File

vendor/gutenberg/block-library/blocks/legacy-widget.php
View source
<?php

/**
 * Server-side rendering of the `core/legacy-widget` block.
 *
 * @package WordPress
 */

/**
 * Renders the `core/legacy-widget` block on server.
 *
 * @see WP_Widget
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the post content with the legacy widget added.
 */
function gutenberg_render_block_legacy_widget($attributes) {
  if (!isset($attributes['identifier'])) {
    return '';
  }
  $identifier = $attributes['identifier'];
  if (isset($attributes['isCallbackWidget']) && $attributes['isCallbackWidget']) {
    global $wp_registered_widgets;
    if (!isset($wp_registered_widgets[$identifier])) {
      return '';
    }
    $widget = $wp_registered_widgets[$identifier];
    $params = array_merge(array(
      'widget_id' => $identifier,
      'widget_name' => $widget['name'],
    ), (array) $wp_registered_widgets[$identifier]['params']);
    $params = apply_filters('dynamic_sidebar_params', $params);
    $callback = $widget['callback'];
    if (is_callable($callback)) {
      ob_start();
      call_user_func_array($callback, $params);
      return ob_get_clean();
    }
    return '';
  }
  ob_start();
  the_widget($identifier, $attributes['instance']);
  return ob_get_clean();
}

/**
 * Register legacy widget block.
 */
function gutenberg_register_block_core_legacy_widget() {
  register_block_type('core/legacy-widget', array(
    'attributes' => array(
      'identifier' => array(
        'type' => 'string',
      ),
      'instance' => array(
        'type' => 'object',
      ),
      'isCallbackWidget' => array(
        'type' => 'boolean',
      ),
    ),
    'render_callback' => 'gutenberg_render_block_legacy_widget',
  ));
}
add_action('init', 'gutenberg_register_block_core_legacy_widget', 20);

Functions

Namesort descending Description
gutenberg_register_block_core_legacy_widget Register legacy widget block.
gutenberg_render_block_legacy_widget Renders the `core/legacy-widget` block on server.