[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> block.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/block` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/block` block on server.
  10   *
  11   * @since 5.0.0
  12   *
  13   * @global WP_Embed $wp_embed
  14   *
  15   * @param array $attributes The block attributes.
  16   *
  17   * @return string Rendered HTML of the referenced block.
  18   */
  19  function render_block_core_block( $attributes ) {
  20      static $seen_refs = array();
  21  
  22      if ( empty( $attributes['ref'] ) ) {
  23          return '';
  24      }
  25  
  26      $reusable_block = get_post( $attributes['ref'] );
  27      if ( ! $reusable_block || 'wp_block' !== $reusable_block->post_type ) {
  28          return '';
  29      }
  30  
  31      if ( isset( $seen_refs[ $attributes['ref'] ] ) ) {
  32          // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
  33          // is set in `wp_debug_mode()`.
  34          $is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;
  35  
  36          return $is_debug ?
  37              // translators: Visible only in the front end, this warning takes the place of a faulty block.
  38              __( '[block rendering halted]' ) :
  39              '';
  40      }
  41  
  42      if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) {
  43          return '';
  44      }
  45  
  46      $seen_refs[ $attributes['ref'] ] = true;
  47  
  48      // Handle embeds for reusable blocks.
  49      global $wp_embed;
  50      $content = $wp_embed->run_shortcode( $reusable_block->post_content );
  51      $content = $wp_embed->autoembed( $content );
  52  
  53      // Back compat.
  54      // For blocks that have not been migrated in the editor, add some back compat
  55      // so that front-end rendering continues to work.
  56  
  57      // This matches the `v2` deprecation. Removes the inner `values` property
  58      // from every item.
  59      if ( isset( $attributes['content'] ) ) {
  60          foreach ( $attributes['content'] as &$content_data ) {
  61              if ( isset( $content_data['values'] ) ) {
  62                  $is_assoc_array = is_array( $content_data['values'] ) && ! wp_is_numeric_array( $content_data['values'] );
  63  
  64                  if ( $is_assoc_array ) {
  65                      $content_data = $content_data['values'];
  66                  }
  67              }
  68          }
  69      }
  70  
  71      // This matches the `v1` deprecation. Rename `overrides` to `content`.
  72      if ( isset( $attributes['overrides'] ) && ! isset( $attributes['content'] ) ) {
  73          $attributes['content'] = $attributes['overrides'];
  74      }
  75  
  76      /**
  77       * We set the `pattern/overrides` context through the `render_block_context`
  78       * filter so that it is available when a pattern's inner blocks are
  79       * rendering via do_blocks given it only receives the inner content.
  80       */
  81      $has_pattern_overrides = isset( $attributes['content'] ) && null !== get_block_bindings_source( 'core/pattern-overrides' );
  82      if ( $has_pattern_overrides ) {
  83          $filter_block_context = static function ( $context ) use ( $attributes ) {
  84              $context['pattern/overrides'] = $attributes['content'];
  85              return $context;
  86          };
  87          add_filter( 'render_block_context', $filter_block_context, 1 );
  88      }
  89  
  90      $content = do_blocks( $content );
  91      unset( $seen_refs[ $attributes['ref'] ] );
  92  
  93      if ( $has_pattern_overrides ) {
  94          remove_filter( 'render_block_context', $filter_block_context, 1 );
  95      }
  96  
  97      return $content;
  98  }
  99  
 100  /**
 101   * Registers the `core/block` block.
 102   *
 103   * @since 5.3.0
 104   */
 105  function register_block_core_block() {
 106      register_block_type_from_metadata(
 107          __DIR__ . '/block',
 108          array(
 109              'render_callback' => 'render_block_core_block',
 110          )
 111      );
 112  }
 113  add_action( 'init', 'register_block_core_block' );


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