2021-05-19 11:09:27 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Server-side rendering of the `core/post-featured-image` block.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the `core/post-featured-image` block on the server.
|
|
|
|
*
|
|
|
|
* @param array $attributes Block attributes.
|
|
|
|
* @param string $content Block default content.
|
|
|
|
* @param WP_Block $block Block instance.
|
|
|
|
* @return string Returns the featured image for the current post.
|
|
|
|
*/
|
|
|
|
function render_block_core_post_featured_image( $attributes, $content, $block ) {
|
|
|
|
if ( ! isset( $block->context['postId'] ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$post_ID = $block->context['postId'];
|
|
|
|
|
2023-01-16 14:59:36 -05:00
|
|
|
// Check is needed for backward compatibility with third-party plugins
|
|
|
|
// that might rely on the `in_the_loop` check; calling `the_post` sets it to true.
|
|
|
|
if ( ! in_the_loop() && have_posts() ) {
|
|
|
|
the_post();
|
|
|
|
}
|
|
|
|
|
2022-09-20 11:43:29 -04:00
|
|
|
$is_link = isset( $attributes['isLink'] ) && $attributes['isLink'];
|
2022-04-12 11:12:47 -04:00
|
|
|
$size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
|
2022-09-20 11:43:29 -04:00
|
|
|
$attr = get_block_core_post_featured_image_border_attributes( $attributes );
|
|
|
|
$overlay_markup = get_block_core_post_featured_image_overlay_element_markup( $attributes );
|
|
|
|
|
|
|
|
if ( $is_link ) {
|
2023-02-07 02:04:52 -05:00
|
|
|
if ( get_the_title( $post_ID ) ) {
|
|
|
|
$attr['alt'] = trim( strip_tags( get_the_title( $post_ID ) ) );
|
|
|
|
} else {
|
|
|
|
$attr['alt'] = sprintf(
|
|
|
|
// translators: %d is the post ID.
|
|
|
|
__( 'Untitled post %d' ),
|
|
|
|
$post_ID
|
|
|
|
);
|
|
|
|
}
|
2022-09-20 11:43:29 -04:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:24:19 -04:00
|
|
|
$extra_styles = '';
|
|
|
|
|
|
|
|
// Aspect ratio with a height set needs to override the default width/height.
|
|
|
|
if ( ! empty( $attributes['aspectRatio'] ) ) {
|
|
|
|
$extra_styles .= 'width:100%;height:100%;';
|
|
|
|
} elseif ( ! empty( $attributes['height'] ) ) {
|
|
|
|
$extra_styles .= "height:{$attributes['height']};";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $attributes['scale'] ) ) {
|
|
|
|
$extra_styles .= "object-fit:{$attributes['scale']};";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $extra_styles ) ) {
|
Editor: Update block editor packages to the latest patch releases.
This updates the block editor related npm dependencies to their latest patch versions ahead of WordPress 6.1.1.
For a full list of what’s included in this update, see https://github.com/WordPress/gutenberg/compare/432ed388f8d0614f9de775738b24b0ea96664715...6566f5fe9ece6ad5ae550349d3b1f0944a011040.
Props aaronrobertshaw, ntsekouras, bernhard-reiter, ramonopoly, isabel_brison, andrewserong, get_dave, scruffian, andraganescu, talldanwp, mciampini, noisysocks, cbravobernal, bph, tyxla, ellatrix, czapla, mcsf, ironprogrammer, wildworks, peterwilsoncc, mamaduka, mikachan, spacedmonkey, cybr, youknowriad, alexstine, aristath, kevin940726, ndiego, 0mirka00, poena, joen, ryankienstra, desrosj, vtad, nithins53, audrasjb, kacper3355, sabernhardt.
Fixes #57038, #56818, #56955, #56923.
Built from https://develop.svn.wordpress.org/trunk@54811
git-svn-id: http://core.svn.wordpress.org/trunk@54363 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-11 07:46:55 -05:00
|
|
|
$attr['style'] = empty( $attr['style'] ) ? $extra_styles : $attr['style'] . $extra_styles;
|
|
|
|
}
|
|
|
|
|
2022-09-20 11:43:29 -04:00
|
|
|
$featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr );
|
2021-05-19 11:09:27 -04:00
|
|
|
if ( ! $featured_image ) {
|
|
|
|
return '';
|
|
|
|
}
|
2022-09-20 11:43:29 -04:00
|
|
|
if ( $is_link ) {
|
|
|
|
$link_target = $attributes['linkTarget'];
|
|
|
|
$rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
|
2023-02-07 02:04:52 -05:00
|
|
|
$height = ! empty( $attributes['height'] ) ? 'style="' . esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . '"' : '';
|
2022-09-20 11:43:29 -04:00
|
|
|
$featured_image = sprintf(
|
2023-02-07 02:04:52 -05:00
|
|
|
'<a href="%1$s" target="%2$s" %3$s %4$s>%5$s%6$s</a>',
|
2022-09-20 11:43:29 -04:00
|
|
|
get_the_permalink( $post_ID ),
|
|
|
|
esc_attr( $link_target ),
|
|
|
|
$rel,
|
2023-02-07 02:04:52 -05:00
|
|
|
$height,
|
2022-09-20 11:43:29 -04:00
|
|
|
$featured_image,
|
|
|
|
$overlay_markup
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$featured_image = $featured_image . $overlay_markup;
|
2021-05-19 11:09:27 -04:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:24:19 -04:00
|
|
|
$aspect_ratio = ! empty( $attributes['aspectRatio'] )
|
|
|
|
? esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';'
|
|
|
|
: '';
|
|
|
|
$width = ! empty( $attributes['width'] )
|
|
|
|
? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';'
|
|
|
|
: '';
|
|
|
|
$height = ! empty( $attributes['height'] )
|
|
|
|
? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';'
|
|
|
|
: '';
|
|
|
|
if ( ! $height && ! $width && ! $aspect_ratio ) {
|
2023-02-07 02:04:52 -05:00
|
|
|
$wrapper_attributes = get_block_wrapper_attributes();
|
|
|
|
} else {
|
2023-06-27 10:24:19 -04:00
|
|
|
$wrapper_attributes = get_block_wrapper_attributes( array( 'style' => $aspect_ratio . $width . $height ) );
|
2023-02-07 02:04:52 -05:00
|
|
|
}
|
2022-09-20 11:43:29 -04:00
|
|
|
return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate markup for the HTML element that will be used for the overlay.
|
|
|
|
*
|
|
|
|
* @param array $attributes Block attributes.
|
|
|
|
*
|
|
|
|
* @return string HTML markup in string format.
|
|
|
|
*/
|
|
|
|
function get_block_core_post_featured_image_overlay_element_markup( $attributes ) {
|
|
|
|
$has_dim_background = isset( $attributes['dimRatio'] ) && $attributes['dimRatio'];
|
|
|
|
$has_gradient = isset( $attributes['gradient'] ) && $attributes['gradient'];
|
|
|
|
$has_custom_gradient = isset( $attributes['customGradient'] ) && $attributes['customGradient'];
|
|
|
|
$has_solid_overlay = isset( $attributes['overlayColor'] ) && $attributes['overlayColor'];
|
|
|
|
$has_custom_overlay = isset( $attributes['customOverlayColor'] ) && $attributes['customOverlayColor'];
|
2022-10-04 11:06:52 -04:00
|
|
|
$class_names = array( 'wp-block-post-featured-image__overlay' );
|
|
|
|
$styles = array();
|
2022-09-20 11:43:29 -04:00
|
|
|
|
|
|
|
if ( ! $has_dim_background ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-10-04 11:06:52 -04:00
|
|
|
// Apply border classes and styles.
|
|
|
|
$border_attributes = get_block_core_post_featured_image_border_attributes( $attributes );
|
|
|
|
|
|
|
|
if ( ! empty( $border_attributes['class'] ) ) {
|
|
|
|
$class_names[] = $border_attributes['class'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $border_attributes['style'] ) ) {
|
|
|
|
$styles[] = $border_attributes['style'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply overlay and gradient classes.
|
2022-09-20 11:43:29 -04:00
|
|
|
if ( $has_dim_background ) {
|
|
|
|
$class_names[] = 'has-background-dim';
|
|
|
|
$class_names[] = "has-background-dim-{$attributes['dimRatio']}";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $has_solid_overlay ) {
|
|
|
|
$class_names[] = "has-{$attributes['overlayColor']}-background-color";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $has_gradient || $has_custom_gradient ) {
|
|
|
|
$class_names[] = 'has-background-gradient';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $has_gradient ) {
|
|
|
|
$class_names[] = "has-{$attributes['gradient']}-gradient-background";
|
|
|
|
}
|
|
|
|
|
2022-10-04 11:06:52 -04:00
|
|
|
// Apply background styles.
|
2022-09-20 11:43:29 -04:00
|
|
|
if ( $has_custom_gradient ) {
|
2022-10-04 11:06:52 -04:00
|
|
|
$styles[] = sprintf( 'background-image: %s;', $attributes['customGradient'] );
|
2022-09-20 11:43:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $has_custom_overlay ) {
|
2022-10-04 11:06:52 -04:00
|
|
|
$styles[] = sprintf( 'background-color: %s;', $attributes['customOverlayColor'] );
|
2022-09-20 11:43:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'<span class="%s" style="%s" aria-hidden="true"></span>',
|
|
|
|
esc_attr( implode( ' ', $class_names ) ),
|
2022-10-04 11:06:52 -04:00
|
|
|
esc_attr( safecss_filter_attr( implode( ' ', $styles ) ) )
|
2022-09-20 11:43:29 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates class names and styles to apply the border support styles for
|
|
|
|
* the Post Featured Image block.
|
|
|
|
*
|
|
|
|
* @param array $attributes The block attributes.
|
|
|
|
* @return array The border-related classnames and styles for the block.
|
|
|
|
*/
|
|
|
|
function get_block_core_post_featured_image_border_attributes( $attributes ) {
|
|
|
|
$border_styles = array();
|
|
|
|
$sides = array( 'top', 'right', 'bottom', 'left' );
|
|
|
|
|
|
|
|
// Border radius.
|
|
|
|
if ( isset( $attributes['style']['border']['radius'] ) ) {
|
|
|
|
$border_styles['radius'] = $attributes['style']['border']['radius'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Border style.
|
|
|
|
if ( isset( $attributes['style']['border']['style'] ) ) {
|
|
|
|
$border_styles['style'] = $attributes['style']['border']['style'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Border width.
|
|
|
|
if ( isset( $attributes['style']['border']['width'] ) ) {
|
|
|
|
$border_styles['width'] = $attributes['style']['border']['width'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Border color.
|
|
|
|
$preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
|
|
|
|
$custom_color = _wp_array_get( $attributes, array( 'style', 'border', 'color' ), null );
|
|
|
|
$border_styles['color'] = $preset_color ? $preset_color : $custom_color;
|
|
|
|
|
|
|
|
// Individual border styles e.g. top, left etc.
|
|
|
|
foreach ( $sides as $side ) {
|
|
|
|
$border = _wp_array_get( $attributes, array( 'style', 'border', $side ), null );
|
|
|
|
$border_styles[ $side ] = array(
|
|
|
|
'color' => isset( $border['color'] ) ? $border['color'] : null,
|
|
|
|
'style' => isset( $border['style'] ) ? $border['style'] : null,
|
|
|
|
'width' => isset( $border['width'] ) ? $border['width'] : null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
|
|
|
|
$attributes = array();
|
|
|
|
if ( ! empty( $styles['classnames'] ) ) {
|
|
|
|
$attributes['class'] = $styles['classnames'];
|
|
|
|
}
|
|
|
|
if ( ! empty( $styles['css'] ) ) {
|
|
|
|
$attributes['style'] = $styles['css'];
|
|
|
|
}
|
|
|
|
return $attributes;
|
2021-05-19 11:09:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the `core/post-featured-image` block on the server.
|
|
|
|
*/
|
|
|
|
function register_block_core_post_featured_image() {
|
|
|
|
register_block_type_from_metadata(
|
|
|
|
__DIR__ . '/post-featured-image',
|
|
|
|
array(
|
|
|
|
'render_callback' => 'render_block_core_post_featured_image',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
add_action( 'init', 'register_block_core_post_featured_image' );
|