-
Notifications
You must be signed in to change notification settings - Fork 144
Reduce maximum image size used as background image based on container width #2258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
a985956
dfa6c45
5ed9644
ea61a9c
3fc31d1
134af1c
cf42623
f49fdc6
7748bca
119aded
b0e5312
d97926b
5331e16
a2e4ea7
4c1d7f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -402,3 +402,62 @@ function auto_sizes_get_featured_image_attachment_id( int $post_id ): int { | |
|
|
||
| return (int) get_post_thumbnail_id( $post_id ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds data attributes for background image attachment ID to Group and Cover blocks. | ||
| * | ||
| * This exposes the attachment ID from block attributes as data attributes on the HTML element, | ||
| * allowing Image Prioritizer to access this information without using attachment_url_to_postid(). | ||
| * This is particularly important for Cover blocks that may use sizes other than "full". | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string|mixed $content The block content about to be rendered. | ||
| * @param array{ attrs?: array<string, mixed>, blockName?: string } $parsed_block The parsed block. | ||
| * @return string The updated block content. | ||
| */ | ||
| function auto_sizes_add_background_image_data_attributes( $content, array $parsed_block ): string { | ||
| if ( ! is_string( $content ) || '' === $content ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $block_name = $parsed_block['blockName'] ?? ''; | ||
| $attrs = $parsed_block['attrs'] ?? array(); | ||
| $attachment_id = null; | ||
| $image_url = null; | ||
|
|
||
| // Extract background image data based on block type. | ||
| if ( 'core/cover' === $block_name ) { | ||
| $attachment_id = $attrs['id'] ?? null; | ||
| $image_url = $attrs['url'] ?? null; | ||
| } elseif ( 'core/group' === $block_name ) { | ||
| $attachment_id = $attrs['style']['background']['backgroundImage']['id'] ?? null; | ||
| $image_url = $attrs['style']['background']['backgroundImage']['url'] ?? null; | ||
| } | ||
|
|
||
| // Normalize attachment ID type if possible. | ||
| if ( isset( $attachment_id ) && is_numeric( $attachment_id ) ) { | ||
| $attachment_id = (int) $attachment_id; | ||
| } | ||
|
|
||
| // Validate extracted data and update the element with background image. | ||
| if ( | ||
| isset( $attachment_id, $image_url ) && | ||
| is_int( $attachment_id ) && | ||
| $attachment_id > 0 && | ||
| '' !== $image_url && | ||
| is_array( wp_get_attachment_metadata( $attachment_id ) ) | ||
| ) { | ||
| $processor = new WP_HTML_Tag_Processor( $content ); | ||
| while ( $processor->next_tag() ) { | ||
| $style = $processor->get_attribute( 'style' ); | ||
| if ( is_string( $style ) && str_contains( $style, 'background-image:' ) && str_contains( $style, $image_url ) ) { | ||
| $processor->set_attribute( 'data-bg-attachment-id', (string) $attachment_id ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted below, this attribute is being added here exclusively for the use of another plugin: Image Prioritizer. This dependency needs to be removed. Instead, this needs to be determining the size of the block as informed by the layout it has access to based on the block structure on a block template. In other words, I believe all of the logic here in |
||
| $content = $processor->get_updated_html(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $content; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -92,6 +92,9 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { | |||||||||||||||||||||||||||||||||||||||
| $this->add_image_preload_link( $context->link_collection, $group, $background_image_url ); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Reduce the background image size if URL Metrics are available. | ||||||||||||||||||||||||||||||||||||||||
| $this->reduce_background_image_size( $background_image_url, $context ); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $this->lazy_load_bg_images( $context ); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -238,4 +241,40 @@ private function lazy_load_bg_images( OD_Tag_Visitor_Context $context ): void { | |||||||||||||||||||||||||||||||||||||||
| $this->added_lazy_assets = true; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * Reduces background image size by choosing one that fits the element dimensions more closely. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * This is similar to how VIDEO poster images are optimized in the Video Tag Visitor. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @since n.e.x.t | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @param non-empty-string $background_image_url Background image URL. | ||||||||||||||||||||||||||||||||||||||||
| * @param OD_Tag_Visitor_Context $context Tag visitor context, with the cursor currently at an element with a background image. | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| private function reduce_background_image_size( string $background_image_url, OD_Tag_Visitor_Context $context ): void { | ||||||||||||||||||||||||||||||||||||||||
| $processor = $context->processor; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $max_element_width = $this->get_max_element_width( $context ); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // If the element wasn't present in any URL Metrics gathered for desktop, then abort downsizing the background image. | ||||||||||||||||||||||||||||||||||||||||
| if ( null === $max_element_width ) { | ||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Try to get the attachment ID from the data attribute (populated via filter from block attributes). | ||||||||||||||||||||||||||||||||||||||||
| $attachment_id = $processor->get_attribute( 'data-bg-attachment-id' ); | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+265
to
+266
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a plugin dependency issue here. Currently this requires that the |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if ( is_numeric( $attachment_id ) && $attachment_id > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
| $smaller_image_url = wp_get_attachment_image_url( (int) $attachment_id, array( (int) $max_element_width, 0 ) ); | ||||||||||||||||||||||||||||||||||||||||
| if ( is_string( $smaller_image_url ) && $smaller_image_url !== $background_image_url ) { | ||||||||||||||||||||||||||||||||||||||||
| // Replace the background image URL in the style attribute. | ||||||||||||||||||||||||||||||||||||||||
| $style = $processor->get_attribute( 'style' ); | ||||||||||||||||||||||||||||||||||||||||
| if ( is_string( $style ) ) { | ||||||||||||||||||||||||||||||||||||||||
| $updated_style = str_replace( $background_image_url, $smaller_image_url, $style ); | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+271
to
+274
|
||||||||||||||||||||||||||||||||||||||||
| // Replace the background image URL in the style attribute. | |
| $style = $processor->get_attribute( 'style' ); | |
| if ( is_string( $style ) ) { | |
| $updated_style = str_replace( $background_image_url, $smaller_image_url, $style ); | |
| // Replace the background image URL in the background/background-image declarations in the style attribute. | |
| $style = $processor->get_attribute( 'style' ); | |
| if ( is_string( $style ) ) { | |
| $declarations = explode( ';', $style ); | |
| foreach ( $declarations as &$declaration ) { | |
| $trimmed_declaration = ltrim( $declaration ); | |
| if ( | |
| 0 === stripos( $trimmed_declaration, 'background:' ) || | |
| 0 === stripos( $trimmed_declaration, 'background-image:' ) | |
| ) { | |
| $declaration = str_replace( $background_image_url, $smaller_image_url, $declaration ); | |
| } | |
| } | |
| unset( $declaration ); | |
| $updated_style = implode( ';', $declarations ); |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new reduce_background_image_size method lacks test coverage. Consider adding snapshot tests similar to the existing video poster tests (e.g., video-with-large-poster-and-desktop-url-metrics-collected) to verify the background image size reduction functionality works correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking
auto_sizes_filter_image_tag()for inspiration, something like:A new
auto_sizes_filter_background_image_style()function will be needed modeled afterauto_sizes_filter_image_tag().