[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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   * @since 5.8.0
  12   *
  13   * @param array    $attributes Block attributes.
  14   * @param string   $content    Block default content.
  15   * @param WP_Block $block      Block instance.
  16   * @return string Returns the featured image for the current post.
  17   */
  18  function render_block_core_post_featured_image( $attributes, $content, $block ) {
  19      if ( ! isset( $block->context['postId'] ) ) {
  20          return '';
  21      }
  22      $post_ID = $block->context['postId'];
  23  
  24      $is_link        = isset( $attributes['isLink'] ) && $attributes['isLink'];
  25      $size_slug      = $attributes['sizeSlug'] ?? 'post-thumbnail';
  26      $attr           = get_block_core_post_featured_image_border_attributes( $attributes );
  27      $overlay_markup = get_block_core_post_featured_image_overlay_element_markup( $attributes );
  28  
  29      if ( $is_link ) {
  30          $title = get_the_title( $post_ID );
  31          if ( $title ) {
  32              $attr['alt'] = trim( strip_tags( $title ) );
  33          } else {
  34              $attr['alt'] = sprintf(
  35                  // translators: %d is the post ID.
  36                  __( 'Untitled post %d' ),
  37                  $post_ID
  38              );
  39          }
  40      }
  41  
  42      $extra_styles = '';
  43      $has_width    = array_key_exists( 'width', $attributes ) && null !== $attributes['width'] && '' !== $attributes['width'];
  44      $has_height   = array_key_exists( 'height', $attributes ) && null !== $attributes['height'] && '' !== $attributes['height'];
  45  
  46      if ( ! empty( $attributes['aspectRatio'] ) ) {
  47          $extra_styles .= esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';';
  48          $extra_styles .= 'width:100%;';
  49      }
  50  
  51      if ( $has_height ) {
  52          $extra_styles .= esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';';
  53      } elseif ( $has_width ) {
  54          $extra_styles .= 'height:auto;';
  55      }
  56  
  57      if ( $has_width ) {
  58          $extra_styles .= esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';';
  59      }
  60  
  61      if ( ! empty( $attributes['scale'] ) ) {
  62          $extra_styles .= esc_attr( safecss_filter_attr( 'object-fit:' . $attributes['scale'] ) ) . ';';
  63      }
  64      if ( ! empty( $attributes['style']['shadow'] ) ) {
  65          $shadow_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) );
  66  
  67          if ( ! empty( $shadow_styles['css'] ) ) {
  68              $extra_styles .= $shadow_styles['css'];
  69          }
  70      }
  71  
  72      if ( ! empty( $extra_styles ) ) {
  73          $attr['style'] = empty( $attr['style'] ) ? $extra_styles : $attr['style'] . $extra_styles;
  74      }
  75  
  76      $featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr );
  77  
  78      // Get the first image from the post.
  79      if ( $attributes['useFirstImageFromPost'] && ! $featured_image ) {
  80          $content_post = get_post( $post_ID );
  81          $content      = $content_post->post_content;
  82          $processor    = new WP_HTML_Tag_Processor( $content );
  83  
  84          /*
  85           * Transfer the image tag from the post into a new text snippet.
  86           * Because the HTML API doesn't currently expose a way to extract
  87           * HTML substrings this is necessary as a workaround. Of note, this
  88           * is different than directly extracting the IMG tag:
  89           * - If there are duplicate attributes in the source there will only be one in the output.
  90           * - If there are single-quoted or unquoted attributes they will be double-quoted in the output.
  91           * - If there are named character references in the attribute values they may be replaced with their direct code points. E.g. `&hellip;` becomes `…`.
  92           * In the future there will likely be a mechanism to copy snippets of HTML from
  93           * one document into another, via the HTML Processor's `get_outer_html()` or
  94           * equivalent. When that happens it would be appropriate to replace this custom
  95           * code with that canonical code.
  96           */
  97          if ( $processor->next_tag( 'img' ) ) {
  98              $tag_html = new WP_HTML_Tag_Processor( '<img>' );
  99              $tag_html->next_tag();
 100              foreach ( $processor->get_attribute_names_with_prefix( '' ) as $name ) {
 101                  $tag_html->set_attribute( $name, $processor->get_attribute( $name ) );
 102              }
 103              if ( ! empty( $attr['style'] ) ) {
 104                  $existing_style = $tag_html->get_attribute( 'style' );
 105                  $style          = is_string( $existing_style ) && '' !== $existing_style
 106                      ? rtrim( $existing_style, ';' ) . ';' . $attr['style']
 107                      : $attr['style'];
 108                  $tag_html->set_attribute( 'style', $style );
 109              }
 110              $featured_image = $tag_html->get_updated_html();
 111          }
 112      }
 113  
 114      if ( ! $featured_image ) {
 115          return '';
 116      }
 117  
 118      if ( $is_link ) {
 119          $link_target    = $attributes['linkTarget'];
 120          $rel            = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
 121          $featured_image = sprintf(
 122              '<a href="%1$s" target="%2$s" %3$s>%4$s%5$s</a>',
 123              esc_url( get_the_permalink( $post_ID ) ),
 124              esc_attr( $link_target ),
 125              $rel,
 126              $featured_image,
 127              $overlay_markup
 128          );
 129      } else {
 130          $featured_image = $featured_image . $overlay_markup;
 131      }
 132  
 133      $wrapper_attributes = get_block_wrapper_attributes();
 134      return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
 135  }
 136  
 137  /**
 138   * Generate markup for the HTML element that will be used for the overlay.
 139   *
 140   * @since 6.1.0
 141   *
 142   * @param array $attributes Block attributes.
 143   *
 144   * @return string HTML markup in string format.
 145   */
 146  function get_block_core_post_featured_image_overlay_element_markup( $attributes ) {
 147      $has_dim_background  = isset( $attributes['dimRatio'] ) && $attributes['dimRatio'];
 148      $has_gradient        = isset( $attributes['gradient'] ) && $attributes['gradient'];
 149      $has_custom_gradient = isset( $attributes['customGradient'] ) && $attributes['customGradient'];
 150      $has_solid_overlay   = isset( $attributes['overlayColor'] ) && $attributes['overlayColor'];
 151      $has_custom_overlay  = isset( $attributes['customOverlayColor'] ) && $attributes['customOverlayColor'];
 152      $class_names         = array( 'wp-block-post-featured-image__overlay' );
 153      $styles              = array();
 154  
 155      if ( ! $has_dim_background ) {
 156          return '';
 157      }
 158  
 159      // Apply border classes and styles.
 160      $border_attributes = get_block_core_post_featured_image_border_attributes( $attributes );
 161  
 162      if ( ! empty( $border_attributes['class'] ) ) {
 163          $class_names[] = $border_attributes['class'];
 164      }
 165  
 166      if ( ! empty( $border_attributes['style'] ) ) {
 167          $styles[] = $border_attributes['style'];
 168      }
 169  
 170      // Apply overlay and gradient classes.
 171      $class_names[] = 'has-background-dim';
 172      $class_names[] = "has-background-dim-{$attributes['dimRatio']}";
 173  
 174      if ( $has_solid_overlay ) {
 175          $class_names[] = "has-{$attributes['overlayColor']}-background-color";
 176      }
 177  
 178      if ( $has_gradient || $has_custom_gradient ) {
 179          $class_names[] = 'has-background-gradient';
 180      }
 181  
 182      if ( $has_gradient ) {
 183          $class_names[] = "has-{$attributes['gradient']}-gradient-background";
 184      }
 185  
 186      // Apply background styles.
 187      if ( $has_custom_gradient ) {
 188          $styles[] = sprintf( 'background-image: %s;', $attributes['customGradient'] );
 189      }
 190  
 191      if ( $has_custom_overlay ) {
 192          $styles[] = sprintf( 'background-color: %s;', $attributes['customOverlayColor'] );
 193      }
 194  
 195      return sprintf(
 196          '<span class="%s" style="%s" aria-hidden="true"></span>',
 197          esc_attr( implode( ' ', $class_names ) ),
 198          esc_attr( safecss_filter_attr( implode( ' ', $styles ) ) )
 199      );
 200  }
 201  
 202  /**
 203   * Generates class names and styles to apply the border support styles for
 204   * the Post Featured Image block.
 205   *
 206   * @since 6.1.0
 207   *
 208   * @param array $attributes The block attributes.
 209   * @return array The border-related classnames and styles for the block.
 210   */
 211  function get_block_core_post_featured_image_border_attributes( $attributes ) {
 212      $border_styles = array();
 213      $sides         = array( 'top', 'right', 'bottom', 'left' );
 214  
 215      // Border radius.
 216      if ( isset( $attributes['style']['border']['radius'] ) ) {
 217          $border_styles['radius'] = $attributes['style']['border']['radius'];
 218      }
 219  
 220      // Border style.
 221      if ( isset( $attributes['style']['border']['style'] ) ) {
 222          $border_styles['style'] = $attributes['style']['border']['style'];
 223      }
 224  
 225      // Border width.
 226      if ( isset( $attributes['style']['border']['width'] ) ) {
 227          $border_styles['width'] = $attributes['style']['border']['width'];
 228      }
 229  
 230      // Border color.
 231      $preset_color           = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
 232      $custom_color           = $attributes['style']['border']['color'] ?? null;
 233      $border_styles['color'] = $preset_color ? $preset_color : $custom_color;
 234  
 235      // Individual border styles e.g. top, left etc.
 236      foreach ( $sides as $side ) {
 237          $border                 = $attributes['style']['border'][ $side ] ?? null;
 238          $border_styles[ $side ] = array(
 239              'color' => $border['color'] ?? null,
 240              'style' => $border['style'] ?? null,
 241              'width' => $border['width'] ?? null,
 242          );
 243      }
 244  
 245      $styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
 246      $attributes = array();
 247      if ( ! empty( $styles['classnames'] ) ) {
 248          $attributes['class'] = $styles['classnames'];
 249      }
 250      if ( ! empty( $styles['css'] ) ) {
 251          $attributes['style'] = $styles['css'];
 252      }
 253      return $attributes;
 254  }
 255  
 256  /**
 257   * Registers the `core/post-featured-image` block on the server.
 258   *
 259   * @since 5.8.0
 260   */
 261  function register_block_core_post_featured_image() {
 262      register_block_type_from_metadata(
 263          __DIR__ . '/post-featured-image',
 264          array(
 265              'render_callback' => 'render_block_core_post_featured_image',
 266          )
 267      );
 268  }
 269  add_action( 'init', 'register_block_core_post_featured_image' );


Generated : Tue Jul 21 08:20:16 2026 Cross-referenced by PHPXref