[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> post-featured-image.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/post-featured-image` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/post-featured-image` block on the server.
  10   *
  11   * @param array    $attributes Block attributes.
  12   * @param string   $content    Block default content.
  13   * @param WP_Block $block      Block instance.
  14   * @return string Returns the featured image for the current post.
  15   */
  16  function render_block_core_post_featured_image( $attributes, $content, $block ) {
  17      if ( ! isset( $block->context['postId'] ) ) {
  18          return '';
  19      }
  20      $post_ID = $block->context['postId'];
  21  
  22      $featured_image = get_the_post_thumbnail( $post_ID );
  23      if ( ! $featured_image ) {
  24          return '';
  25      }
  26      $wrapper_attributes = get_block_wrapper_attributes();
  27      if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  28          $featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image );
  29      }
  30  
  31      $has_width  = ! empty( $attributes['width'] );
  32      $has_height = ! empty( $attributes['height'] );
  33      if ( ! $has_height && ! $has_width ) {
  34          return "<figure $wrapper_attributes>$featured_image</figure>";
  35      }
  36  
  37      if ( $has_width ) {
  38          $wrapper_attributes = get_block_wrapper_attributes( array( 'style' => "width:{$attributes['width']};" ) );
  39      }
  40  
  41      if ( $has_height ) {
  42          $image_styles = "height:{$attributes['height']};";
  43          if ( ! empty( $attributes['scale'] ) ) {
  44              $image_styles .= "object-fit:{$attributes['scale']};";
  45          }
  46          $featured_image = str_replace( 'src=', "style='$image_styles' src=", $featured_image );
  47      }
  48  
  49      return "<figure $wrapper_attributes>$featured_image</figure>";
  50  }
  51  
  52  /**
  53   * Registers the `core/post-featured-image` block on the server.
  54   */
  55  function register_block_core_post_featured_image() {
  56      register_block_type_from_metadata(
  57          __DIR__ . '/post-featured-image',
  58          array(
  59              'render_callback' => 'render_block_core_post_featured_image',
  60          )
  61      );
  62  }
  63  add_action( 'init', 'register_block_core_post_featured_image' );


Generated: Fri Dec 3 01:00:03 2021 Cross-referenced by PHPXref 0.7.1