[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> widget-group.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/widget-group` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the 'core/widget-group' block.
  10   *
  11   * @since 5.9.0
  12   *
  13   * @global array      $wp_registered_sidebars
  14   * @global int|string $_sidebar_being_rendered
  15   *
  16   * @param array    $attributes The block attributes.
  17   * @param string   $content The block content.
  18   * @param WP_Block $block The block.
  19   *
  20   * @return string Rendered block.
  21   */
  22  function render_block_core_widget_group( $attributes, $content, $block ) {
  23      global $wp_registered_sidebars, $_sidebar_being_rendered;
  24  
  25      if ( isset( $wp_registered_sidebars[ $_sidebar_being_rendered ] ) ) {
  26          $before_title = $wp_registered_sidebars[ $_sidebar_being_rendered ]['before_title'];
  27          $after_title  = $wp_registered_sidebars[ $_sidebar_being_rendered ]['after_title'];
  28      } else {
  29          $before_title = '<h2 class="widget-title">';
  30          $after_title  = '</h2>';
  31      }
  32  
  33      $html = '';
  34  
  35      if ( ! empty( $attributes['title'] ) ) {
  36          $html .= $before_title . esc_html( $attributes['title'] ) . $after_title;
  37      }
  38  
  39      $html .= '<div class="wp-widget-group__inner-blocks">';
  40      foreach ( $block->inner_blocks as $inner_block ) {
  41          $html .= $inner_block->render();
  42      }
  43      $html .= '</div>';
  44  
  45      return $html;
  46  }
  47  
  48  /**
  49   * Registers the 'core/widget-group' block.
  50   *
  51   * @since 5.9.0
  52   */
  53  function register_block_core_widget_group() {
  54      register_block_type_from_metadata(
  55          __DIR__ . '/widget-group',
  56          array(
  57              'render_callback' => 'render_block_core_widget_group',
  58          )
  59      );
  60  }
  61  
  62  add_action( 'init', 'register_block_core_widget_group' );
  63  
  64  /**
  65   * Make a note of the sidebar being rendered before WordPress starts rendering
  66   * it. This lets us get to the current sidebar in
  67   * render_block_core_widget_group().
  68   *
  69   * @since 5.9.0
  70   *
  71   * @global int|string $_sidebar_being_rendered
  72   *
  73   * @param int|string $index       Index, name, or ID of the dynamic sidebar.
  74   */
  75  function note_sidebar_being_rendered( $index ) {
  76      global $_sidebar_being_rendered;
  77      $_sidebar_being_rendered = $index;
  78  }
  79  add_action( 'dynamic_sidebar_before', 'note_sidebar_being_rendered' );
  80  
  81  /**
  82   * Clear whatever we set in note_sidebar_being_rendered() after WordPress
  83   * finishes rendering a sidebar.
  84   *
  85   * @since 5.9.0
  86   *
  87   * @global int|string $_sidebar_being_rendered
  88   */
  89  function discard_sidebar_being_rendered() {
  90      global $_sidebar_being_rendered;
  91      unset( $_sidebar_being_rendered );
  92  }
  93  add_action( 'dynamic_sidebar_after', 'discard_sidebar_being_rendered' );


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref