[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/pattern` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   *  Registers the `core/pattern` block on the server.
  10   *
  11   * @since 5.9.0
  12   */
  13  function register_block_core_pattern() {
  14      register_block_type_from_metadata(
  15          __DIR__ . '/pattern',
  16          array(
  17              'render_callback' => 'render_block_core_pattern',
  18          )
  19      );
  20  }
  21  
  22  /**
  23   * Renders the `core/pattern` block on the server.
  24   *
  25   * @since 6.3.0 Backwards compatibility: blocks with no `syncStatus` attribute do not receive block wrapper.
  26   *
  27   * @global WP_Embed $wp_embed Used to process embedded content within patterns
  28   *
  29   * @param array $attributes Block attributes.
  30   *
  31   * @return string Returns the output of the pattern.
  32   */
  33  function render_block_core_pattern( $attributes ) {
  34      static $seen_refs = array();
  35  
  36      if ( empty( $attributes['slug'] ) ) {
  37          return '';
  38      }
  39  
  40      $slug     = $attributes['slug'];
  41      $registry = WP_Block_Patterns_Registry::get_instance();
  42  
  43      if ( ! $registry->is_registered( $slug ) ) {
  44          return '';
  45      }
  46  
  47      if ( isset( $seen_refs[ $attributes['slug'] ] ) ) {
  48          // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
  49          // is set in `wp_debug_mode()`.
  50          $is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;
  51  
  52          return $is_debug ?
  53              // translators: Visible only in the front end, this warning takes the place of a faulty block. %s represents a pattern's slug.
  54              sprintf( __( '[block rendering halted for pattern "%s"]' ), $slug ) :
  55              '';
  56      }
  57  
  58      $pattern = $registry->get_registered( $slug );
  59      $content = $pattern['content'];
  60  
  61      // Backward compatibility for handling Block Hooks and injecting the theme attribute in the Gutenberg plugin.
  62      // This can be removed when the minimum supported WordPress is >= 6.4.
  63      if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && ! function_exists( 'traverse_and_serialize_blocks' ) ) {
  64          $blocks  = parse_blocks( $content );
  65          $content = gutenberg_serialize_blocks( $blocks );
  66      }
  67  
  68      $seen_refs[ $attributes['slug'] ] = true;
  69  
  70      $content = do_blocks( $content );
  71  
  72      global $wp_embed;
  73      $content = $wp_embed->autoembed( $content );
  74  
  75      unset( $seen_refs[ $attributes['slug'] ] );
  76      return $content;
  77  }
  78  
  79  add_action( 'init', 'register_block_core_pattern' );


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