PluginProbe
Gutenberg / 16.6.0
Gutenberg v16.6.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / post-featured-image.php

post-featured-image.php in Gutenberg 16.6.0, at build/block-library/blocks/post-featured-image.php

223 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_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 $is_link = isset( $attributes['isLink'] ) && $attributes['isLink'];
23 $size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
24 $attr = gutenberg_get_block_core_post_featured_image_border_attributes( $attributes );
25 $overlay_markup = gutenberg_get_block_core_post_featured_image_overlay_element_markup( $attributes );
26
27 if ( $is_link ) {
28 if ( get_the_title( $post_ID ) ) {
29 $attr['alt'] = trim( strip_tags( get_the_title( $post_ID ) ) );
30 } else {
31 $attr['alt'] = sprintf(
32 // translators: %d is the post ID.
33 __( 'Untitled post %d' ),
34 $post_ID
35 );
36 }
37 }
38
39 $extra_styles = '';
40
41 // Aspect ratio with a height set needs to override the default width/height.
42 if ( ! empty( $attributes['aspectRatio'] ) ) {
43 $extra_styles .= 'width:100%;height:100%;';
44 } elseif ( ! empty( $attributes['height'] ) ) {
45 $extra_styles .= "height:{$attributes['height']};";
46 }
47
48 if ( ! empty( $attributes['scale'] ) ) {
49 $extra_styles .= "object-fit:{$attributes['scale']};";
50 }
51
52 if ( ! empty( $extra_styles ) ) {
53 $attr['style'] = empty( $attr['style'] ) ? $extra_styles : $attr['style'] . $extra_styles;
54 }
55
56 $featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr );
57 if ( ! $featured_image ) {
58 return '';
59 }
60 if ( $is_link ) {
61 $link_target = $attributes['linkTarget'];
62 $rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
63 $height = ! empty( $attributes['height'] ) ? 'style="' . esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . '"' : '';
64 $featured_image = sprintf(
65 '<a href="%1$s" target="%2$s" %3$s %4$s>%5$s%6$s</a>',
66 get_the_permalink( $post_ID ),
67 esc_attr( $link_target ),
68 $rel,
69 $height,
70 $featured_image,
71 $overlay_markup
72 );
73 } else {
74 $featured_image = $featured_image . $overlay_markup;
75 }
76
77 $aspect_ratio = ! empty( $attributes['aspectRatio'] )
78 ? esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';'
79 : '';
80 $width = ! empty( $attributes['width'] )
81 ? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';'
82 : '';
83 $height = ! empty( $attributes['height'] )
84 ? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';'
85 : '';
86 if ( ! $height && ! $width && ! $aspect_ratio ) {
87 $wrapper_attributes = get_block_wrapper_attributes();
88 } else {
89 $wrapper_attributes = get_block_wrapper_attributes( array( 'style' => $aspect_ratio . $width . $height ) );
90 }
91 return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
92 }
93
94 /**
95 * Generate markup for the HTML element that will be used for the overlay.
96 *
97 * @param array $attributes Block attributes.
98 *
99 * @return string HTML markup in string format.
100 */
101 function gutenberg_get_block_core_post_featured_image_overlay_element_markup( $attributes ) {
102 $has_dim_background = isset( $attributes['dimRatio'] ) && $attributes['dimRatio'];
103 $has_gradient = isset( $attributes['gradient'] ) && $attributes['gradient'];
104 $has_custom_gradient = isset( $attributes['customGradient'] ) && $attributes['customGradient'];
105 $has_solid_overlay = isset( $attributes['overlayColor'] ) && $attributes['overlayColor'];
106 $has_custom_overlay = isset( $attributes['customOverlayColor'] ) && $attributes['customOverlayColor'];
107 $class_names = array( 'wp-block-post-featured-image__overlay' );
108 $styles = array();
109
110 if ( ! $has_dim_background ) {
111 return '';
112 }
113
114 // Apply border classes and styles.
115 $border_attributes = gutenberg_get_block_core_post_featured_image_border_attributes( $attributes );
116
117 if ( ! empty( $border_attributes['class'] ) ) {
118 $class_names[] = $border_attributes['class'];
119 }
120
121 if ( ! empty( $border_attributes['style'] ) ) {
122 $styles[] = $border_attributes['style'];
123 }
124
125 // Apply overlay and gradient classes.
126 if ( $has_dim_background ) {
127 $class_names[] = 'has-background-dim';
128 $class_names[] = "has-background-dim-{$attributes['dimRatio']}";
129 }
130
131 if ( $has_solid_overlay ) {
132 $class_names[] = "has-{$attributes['overlayColor']}-background-color";
133 }
134
135 if ( $has_gradient || $has_custom_gradient ) {
136 $class_names[] = 'has-background-gradient';
137 }
138
139 if ( $has_gradient ) {
140 $class_names[] = "has-{$attributes['gradient']}-gradient-background";
141 }
142
143 // Apply background styles.
144 if ( $has_custom_gradient ) {
145 $styles[] = sprintf( 'background-image: %s;', $attributes['customGradient'] );
146 }
147
148 if ( $has_custom_overlay ) {
149 $styles[] = sprintf( 'background-color: %s;', $attributes['customOverlayColor'] );
150 }
151
152 return sprintf(
153 '<span class="%s" style="%s" aria-hidden="true"></span>',
154 esc_attr( implode( ' ', $class_names ) ),
155 esc_attr( safecss_filter_attr( implode( ' ', $styles ) ) )
156 );
157 }
158
159 /**
160 * Generates class names and styles to apply the border support styles for
161 * the Post Featured Image block.
162 *
163 * @param array $attributes The block attributes.
164 * @return array The border-related classnames and styles for the block.
165 */
166 function gutenberg_get_block_core_post_featured_image_border_attributes( $attributes ) {
167 $border_styles = array();
168 $sides = array( 'top', 'right', 'bottom', 'left' );
169
170 // Border radius.
171 if ( isset( $attributes['style']['border']['radius'] ) ) {
172 $border_styles['radius'] = $attributes['style']['border']['radius'];
173 }
174
175 // Border style.
176 if ( isset( $attributes['style']['border']['style'] ) ) {
177 $border_styles['style'] = $attributes['style']['border']['style'];
178 }
179
180 // Border width.
181 if ( isset( $attributes['style']['border']['width'] ) ) {
182 $border_styles['width'] = $attributes['style']['border']['width'];
183 }
184
185 // Border color.
186 $preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
187 $custom_color = _wp_array_get( $attributes, array( 'style', 'border', 'color' ), null );
188 $border_styles['color'] = $preset_color ? $preset_color : $custom_color;
189
190 // Individual border styles e.g. top, left etc.
191 foreach ( $sides as $side ) {
192 $border = _wp_array_get( $attributes, array( 'style', 'border', $side ), null );
193 $border_styles[ $side ] = array(
194 'color' => isset( $border['color'] ) ? $border['color'] : null,
195 'style' => isset( $border['style'] ) ? $border['style'] : null,
196 'width' => isset( $border['width'] ) ? $border['width'] : null,
197 );
198 }
199
200 $styles = gutenberg_style_engine_get_styles( array( 'border' => $border_styles ) );
201 $attributes = array();
202 if ( ! empty( $styles['classnames'] ) ) {
203 $attributes['class'] = $styles['classnames'];
204 }
205 if ( ! empty( $styles['css'] ) ) {
206 $attributes['style'] = $styles['css'];
207 }
208 return $attributes;
209 }
210
211 /**
212 * Registers the `core/post-featured-image` block on the server.
213 */
214 function gutenberg_register_block_core_post_featured_image() {
215 register_block_type_from_metadata(
216 __DIR__ . '/post-featured-image',
217 array(
218 'render_callback' => 'gutenberg_render_block_core_post_featured_image',
219 )
220 );
221 }
222 add_action( 'init', 'gutenberg_register_block_core_post_featured_image', 20 );
223