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.
|
|
|
|
*
|
2024-05-23 15:39:28 -04:00
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-05-19 11:09:27 -04:00
|
|
|
* @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'];
|
|
|
|
|
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']};";
|
|
|
|
}
|
2024-05-23 15:39:28 -04:00
|
|
|
if ( ! empty( $attributes['style']['shadow'] ) ) {
|
|
|
|
$shadow_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) );
|
|
|
|
|
|
|
|
if ( ! empty( $shadow_styles['css'] ) ) {
|
|
|
|
$extra_styles .= $shadow_styles['css'];
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 10:24:19 -04:00
|
|
|
|
|
|
|
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 );
|
Editor: Update the WordPress packages to the Gutenberg 16.7 RC2 version.
This patch, somewhat small brings a lot to WordPress.
This includes features like:
- DataViews.
- Customization tools like box shadow, background size and repeat.
- UI improvements in the site editor.
- Preferences sharing between the post and site editors.
- Unified panels and editors between post and site editors.
- Improved template mode in the post editor.
- Iterations to multiple interactive blocks.
- Preparing the blocks and UI for pattern overrides.
- and a lot more.
Props luisherranz, gziolo, isabel_brison, costdev, jonsurrell, peterwilsoncc, get_dave, antonvlasenko, desrosj.
See #60315.
Built from https://develop.svn.wordpress.org/trunk@57377
git-svn-id: http://core.svn.wordpress.org/trunk@56883 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-29 16:07:12 -05:00
|
|
|
|
|
|
|
// Get the first image from the post.
|
|
|
|
if ( $attributes['useFirstImageFromPost'] && ! $featured_image ) {
|
|
|
|
$content_post = get_post( $post_ID );
|
|
|
|
$content = $content_post->post_content;
|
|
|
|
$processor = new WP_HTML_Tag_Processor( $content );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Transfer the image tag from the post into a new text snippet.
|
|
|
|
* Because the HTML API doesn't currently expose a way to extract
|
|
|
|
* HTML substrings this is necessary as a workaround. Of note, this
|
|
|
|
* is different than directly extracting the IMG tag:
|
|
|
|
* - If there are duplicate attributes in the source there will only be one in the output.
|
|
|
|
* - If there are single-quoted or unquoted attributes they will be double-quoted in the output.
|
|
|
|
* - If there are named character references in the attribute values they may be replaced with their direct code points. E.g. `…` becomes `…`.
|
|
|
|
* In the future there will likely be a mechanism to copy snippets of HTML from
|
|
|
|
* one document into another, via the HTML Processor's `get_outer_html()` or
|
|
|
|
* equivalent. When that happens it would be appropriate to replace this custom
|
|
|
|
* code with that canonical code.
|
|
|
|
*/
|
|
|
|
if ( $processor->next_tag( 'img' ) ) {
|
|
|
|
$tag_html = new WP_HTML_Tag_Processor( '<img>' );
|
|
|
|
$tag_html->next_tag();
|
|
|
|
foreach ( $processor->get_attribute_names_with_prefix( '' ) as $name ) {
|
|
|
|
$tag_html->set_attribute( $name, $processor->get_attribute( $name ) );
|
|
|
|
}
|
|
|
|
$featured_image = $tag_html->get_updated_html();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:09:27 -04:00
|
|
|
if ( ! $featured_image ) {
|
|
|
|
return '';
|
|
|
|
}
|
Editor: Update the WordPress packages to the Gutenberg 16.7 RC2 version.
This patch, somewhat small brings a lot to WordPress.
This includes features like:
- DataViews.
- Customization tools like box shadow, background size and repeat.
- UI improvements in the site editor.
- Preferences sharing between the post and site editors.
- Unified panels and editors between post and site editors.
- Improved template mode in the post editor.
- Iterations to multiple interactive blocks.
- Preparing the blocks and UI for pattern overrides.
- and a lot more.
Props luisherranz, gziolo, isabel_brison, costdev, jonsurrell, peterwilsoncc, get_dave, antonvlasenko, desrosj.
See #60315.
Built from https://develop.svn.wordpress.org/trunk@57377
git-svn-id: http://core.svn.wordpress.org/trunk@56883 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-29 16:07:12 -05:00
|
|
|
|
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.
|
|
|
|
*
|
2024-05-23 15:39:28 -04:00
|
|
|
* @since 6.1.0
|
|
|
|
*
|
2022-09-20 11:43:29 -04:00
|
|
|
* @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.
|
|
|
|
*
|
2024-05-23 15:39:28 -04:00
|
|
|
* @since 6.1.0
|
|
|
|
*
|
2022-09-20 11:43:29 -04:00
|
|
|
* @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;
|
2023-09-26 10:23:26 -04:00
|
|
|
$custom_color = $attributes['style']['border']['color'] ?? null;
|
2022-09-20 11:43:29 -04:00
|
|
|
$border_styles['color'] = $preset_color ? $preset_color : $custom_color;
|
|
|
|
|
|
|
|
// Individual border styles e.g. top, left etc.
|
|
|
|
foreach ( $sides as $side ) {
|
2023-09-26 10:23:26 -04:00
|
|
|
$border = $attributes['style']['border'][ $side ] ?? null;
|
2022-09-20 11:43:29 -04:00
|
|
|
$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.
|
2024-05-23 15:39:28 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
2021-05-19 11:09:27 -04:00
|
|
|
*/
|
|
|
|
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' );
|