Changeset 54257 for trunk/src/wp-includes/blocks/post-featured-image.php
- Timestamp:
- 09/20/2022 03:14:54 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/post-featured-image.php
r53157 r54257 20 20 $post_ID = $block->context['postId']; 21 21 22 $is_link = isset( $attributes['isLink'] ) && $attributes['isLink']; 22 23 $size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail'; 23 24 $post_title = trim( strip_tags( get_the_title( $post_ID ) ) ); 24 $featured_image = get_the_post_thumbnail( $post_ID, $size_slug, array( 'alt' => $post_title ) ); 25 $attr = get_block_core_post_featured_image_border_attributes( $attributes ); 26 $overlay_markup = get_block_core_post_featured_image_overlay_element_markup( $attributes ); 27 28 if ( $is_link ) { 29 $attr['alt'] = $post_title; 30 } 31 32 $featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr ); 25 33 if ( ! $featured_image ) { 26 34 return ''; 27 35 } 28 36 $wrapper_attributes = get_block_wrapper_attributes(); 29 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { 30 $featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image ); 37 if ( $is_link ) { 38 $link_target = $attributes['linkTarget']; 39 $rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : ''; 40 $featured_image = sprintf( 41 '<a href="%1$s" target="%2$s" %3$s>%4$s%5$s</a>', 42 get_the_permalink( $post_ID ), 43 esc_attr( $link_target ), 44 $rel, 45 $featured_image, 46 $overlay_markup 47 ); 48 } else { 49 $featured_image = $featured_image . $overlay_markup; 31 50 } 32 51 … … 34 53 $has_height = ! empty( $attributes['height'] ); 35 54 if ( ! $has_height && ! $has_width ) { 36 return "<figure $wrapper_attributes>$featured_image</figure>";55 return "<figure {$wrapper_attributes}>{$featured_image}</figure>"; 37 56 } 38 57 … … 49 68 } 50 69 51 return "<figure $wrapper_attributes>$featured_image</figure>"; 70 return "<figure {$wrapper_attributes}>{$featured_image}</figure>"; 71 } 72 73 /** 74 * Generate markup for the HTML element that will be used for the overlay. 75 * 76 * @param array $attributes Block attributes. 77 * 78 * @return string HTML markup in string format. 79 */ 80 function get_block_core_post_featured_image_overlay_element_markup( $attributes ) { 81 $has_dim_background = isset( $attributes['dimRatio'] ) && $attributes['dimRatio']; 82 $has_gradient = isset( $attributes['gradient'] ) && $attributes['gradient']; 83 $has_custom_gradient = isset( $attributes['customGradient'] ) && $attributes['customGradient']; 84 $has_solid_overlay = isset( $attributes['overlayColor'] ) && $attributes['overlayColor']; 85 $has_custom_overlay = isset( $attributes['customOverlayColor'] ) && $attributes['customOverlayColor']; 86 $class_names = array( 87 'wp-block-post-featured-image__overlay', 88 ); 89 $styles_properties = array(); 90 91 if ( ! $has_dim_background ) { 92 return ''; 93 } 94 95 // Generate required classes for the element. 96 if ( $has_dim_background ) { 97 $class_names[] = 'has-background-dim'; 98 $class_names[] = "has-background-dim-{$attributes['dimRatio']}"; 99 } 100 101 if ( $has_solid_overlay ) { 102 $class_names[] = "has-{$attributes['overlayColor']}-background-color"; 103 } 104 105 if ( $has_gradient || $has_custom_gradient ) { 106 $class_names[] = 'has-background-gradient'; 107 } 108 109 if ( $has_gradient ) { 110 $class_names[] = "has-{$attributes['gradient']}-gradient-background"; 111 } 112 113 // Generate required CSS properties and their values. 114 if ( ! empty( $attributes['style']['border']['radius'] ) ) { 115 $styles_properties['border-radius'] = $attributes['style']['border']['radius']; 116 } 117 118 if ( ! empty( $attributes['style']['border']['width'] ) ) { 119 $styles_properties['border-width'] = $attributes['style']['border']['width']; 120 } 121 122 if ( $has_custom_gradient ) { 123 $styles_properties['background-image'] = $attributes['customGradient']; 124 } 125 126 if ( $has_custom_overlay ) { 127 $styles_properties['background-color'] = $attributes['customOverlayColor']; 128 } 129 130 $styles = ''; 131 132 foreach ( $styles_properties as $style_attribute => $style_attribute_value ) { 133 $styles .= "{$style_attribute}: $style_attribute_value; "; 134 } 135 136 return sprintf( 137 '<span class="%s" style="%s" aria-hidden="true"></span>', 138 esc_attr( implode( ' ', $class_names ) ), 139 esc_attr( trim( $styles ) ) 140 ); 141 142 } 143 144 /** 145 * Generates class names and styles to apply the border support styles for 146 * the Post Featured Image block. 147 * 148 * @param array $attributes The block attributes. 149 * @return array The border-related classnames and styles for the block. 150 */ 151 function get_block_core_post_featured_image_border_attributes( $attributes ) { 152 $border_styles = array(); 153 $sides = array( 'top', 'right', 'bottom', 'left' ); 154 155 // Border radius. 156 if ( isset( $attributes['style']['border']['radius'] ) ) { 157 $border_styles['radius'] = $attributes['style']['border']['radius']; 158 } 159 160 // Border style. 161 if ( isset( $attributes['style']['border']['style'] ) ) { 162 $border_styles['style'] = $attributes['style']['border']['style']; 163 } 164 165 // Border width. 166 if ( isset( $attributes['style']['border']['width'] ) ) { 167 $border_styles['width'] = $attributes['style']['border']['width']; 168 } 169 170 // Border color. 171 $preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null; 172 $custom_color = _wp_array_get( $attributes, array( 'style', 'border', 'color' ), null ); 173 $border_styles['color'] = $preset_color ? $preset_color : $custom_color; 174 175 // Individual border styles e.g. top, left etc. 176 foreach ( $sides as $side ) { 177 $border = _wp_array_get( $attributes, array( 'style', 'border', $side ), null ); 178 $border_styles[ $side ] = array( 179 'color' => isset( $border['color'] ) ? $border['color'] : null, 180 'style' => isset( $border['style'] ) ? $border['style'] : null, 181 'width' => isset( $border['width'] ) ? $border['width'] : null, 182 ); 183 } 184 185 $styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) ); 186 $attributes = array(); 187 if ( ! empty( $styles['classnames'] ) ) { 188 $attributes['class'] = $styles['classnames']; 189 } 190 if ( ! empty( $styles['css'] ) ) { 191 $attributes['style'] = $styles['css']; 192 } 193 return $attributes; 52 194 } 53 195
Note: See TracChangeset
for help on using the changeset viewer.