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


Generated : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref