[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/cover` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/cover` block on server.
  10   *
  11   * @param array  $attributes The block attributes.
  12   * @param string $content    The block rendered content.
  13   *
  14   * @return string Returns the cover block markup, if useFeaturedImage is true.
  15   */
  16  function render_block_core_cover( $attributes, $content ) {
  17      if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) {
  18          return $content;
  19      }
  20  
  21      if ( ! ( $attributes['hasParallax'] || $attributes['isRepeated'] ) ) {
  22          $attr = array(
  23              'class'           => 'wp-block-cover__image-background',
  24              'data-object-fit' => 'cover',
  25          );
  26  
  27          if ( isset( $attributes['focalPoint'] ) ) {
  28              $object_position              = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
  29              $attr['data-object-position'] = $object_position;
  30              $attr['style']                = 'object-position: ' . $object_position;
  31          }
  32  
  33          $image = get_the_post_thumbnail( null, 'post-thumbnail', $attr );
  34  
  35          /*
  36           * Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`,
  37           * and removes eventual whitespace characters between the two (typically introduced at template level)
  38           */
  39          $inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U';
  40          if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) {
  41              $offset  = $matches[0][1];
  42              $content = substr( $content, 0, $offset ) . $image . substr( $content, $offset );
  43          }
  44      } else {
  45          if ( in_the_loop() ) {
  46              update_post_thumbnail_cache();
  47          }
  48          $current_featured_image = get_the_post_thumbnail_url();
  49          if ( ! $current_featured_image ) {
  50              return $content;
  51          }
  52  
  53          $processor = new WP_HTML_Tag_Processor( $content );
  54          $processor->next_tag();
  55  
  56          $styles         = $processor->get_attribute( 'style' );
  57          $merged_styles  = ! empty( $styles ) ? $styles . ';' : '';
  58          $merged_styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');';
  59  
  60          $processor->set_attribute( 'style', $merged_styles );
  61          $content = $processor->get_updated_html();
  62      }
  63  
  64      return $content;
  65  }
  66  
  67  /**
  68   * Registers the `core/cover` block renderer on server.
  69   */
  70  function register_block_core_cover() {
  71      register_block_type_from_metadata(
  72          __DIR__ . '/cover',
  73          array(
  74              'render_callback' => 'render_block_core_cover',
  75          )
  76      );
  77  }
  78  add_action( 'init', 'register_block_core_cover' );


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref