[ Index ]

PHP Cross Reference of WordPress

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 array $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 ( false === $attributes['useFeaturedImage'] ) {
  18          return $content;
  19      }
  20  
  21      $current_featured_image = get_the_post_thumbnail_url();
  22  
  23      if ( false === $current_featured_image ) {
  24          return $content;
  25      }
  26  
  27      $is_img_element      = ! ( $attributes['hasParallax'] || $attributes['isRepeated'] );
  28      $is_image_background = 'image' === $attributes['backgroundType'];
  29  
  30      if ( $is_image_background && ! $is_img_element ) {
  31          $content = preg_replace(
  32              '/class=\".*?\"/',
  33              '$0} style="background-image:url(' . esc_url( $current_featured_image ) . ')"',
  34              $content,
  35              1
  36          );
  37      }
  38  
  39      if ( $is_image_background && $is_img_element ) {
  40          $object_position = '';
  41          if ( isset( $attributes['focalPoint'] ) ) {
  42              $object_position = round( $attributes['focalPoint']['x'] * 100 ) . '%' . ' ' .
  43              round( $attributes['focalPoint']['y'] * 100 ) . '%';
  44          }
  45  
  46          $image_template = '<img
  47              class="wp-block-cover__image-background"
  48              alt="%s"
  49              src="%s"
  50              style="object-position: %s"
  51              data-object-fit="cover"
  52              data-object-position="%s"
  53          />';
  54  
  55          $image = sprintf(
  56              $image_template,
  57              esc_attr( get_the_post_thumbnail_caption() ),
  58              esc_url( $current_featured_image ),
  59              esc_attr( $object_position ),
  60              esc_attr( $object_position )
  61          );
  62  
  63          $content = str_replace(
  64              '</span><div',
  65              '</span>' . $image . '<div',
  66              $content
  67          );
  68  
  69      }
  70  
  71      return $content;
  72  }
  73  
  74  /**
  75   * Registers the `core/cover` block renderer on server.
  76   */
  77  function register_block_core_cover() {
  78      register_block_type_from_metadata(
  79          __DIR__ . '/cover',
  80          array(
  81              'render_callback' => 'render_block_core_cover',
  82          )
  83      );
  84  }
  85  add_action( 'init', 'register_block_core_cover' );


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1