summaryrefslogtreecommitdiffstats
path: root/wp-includes/blocks/post-featured-image.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:57:30 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:57:30 +0000
commitfa9a33d818470a5796f0ff8797f98b510ed8de18 (patch)
treebde6a1eede376f9b5df5898ce812330152984d8e /wp-includes/blocks/post-featured-image.php
parentReleasing progress-linux version 6.4.3+dfsg1-1~progress7.99u1. (diff)
downloadwordpress-fa9a33d818470a5796f0ff8797f98b510ed8de18.tar.xz
wordpress-fa9a33d818470a5796f0ff8797f98b510ed8de18.zip
Merging upstream version 6.5+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--wp-includes/blocks/post-featured-image.php31
1 files changed, 31 insertions, 0 deletions
diff --git a/wp-includes/blocks/post-featured-image.php b/wp-includes/blocks/post-featured-image.php
index 4a7aa2f..9a1fd31 100644
--- a/wp-includes/blocks/post-featured-image.php
+++ b/wp-includes/blocks/post-featured-image.php
@@ -54,9 +54,40 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
}
$featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr );
+
+ // 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. `&hellip;` 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();
+ }
+ }
+
if ( ! $featured_image ) {
return '';
}
+
if ( $is_link ) {
$link_target = $attributes['linkTarget'];
$rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';