Skip to content

Commit b9a6ceb

Browse files
committed
feat: Adds function to process BG image for cover and group block.
1 parent 42bb977 commit b9a6ceb

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

plugins/webp-uploads/hooks.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,13 +747,84 @@ function webp_uploads_render_generator(): void {
747747
}
748748
add_action( 'wp_head', 'webp_uploads_render_generator' );
749749

750+
/**
751+
* Process a block's content to handle background images for specific block types.
752+
*
753+
* This function targets blocks like Cover and Group that may use background images,
754+
* converting them to modern image formats when appropriate.
755+
*
756+
* @since n.e.x.t
757+
*
758+
* @param string $block_content The block content.
759+
* @param array<string, mixed> $block The block.
760+
* @return string The filtered block content.
761+
*/
762+
function webp_uploads_filter_block_background_images( string $block_content, array $block ): string {
763+
// Only run on frontend.
764+
if ( ! webp_uploads_in_frontend_body() || '' === $block_content ) {
765+
return $block_content;
766+
}
767+
768+
$attachment_id = null;
769+
$image_url = '';
770+
771+
// Process Cover block.
772+
if ( 'core/cover' === $block['blockName'] && isset( $block['attrs']['id'] ) ) {
773+
$attachment_id = $block['attrs']['id'];
774+
$image_url = isset( $block['attrs']['url'] ) ? $block['attrs']['url'] : '';
775+
}
776+
777+
// Process Group block with background image.
778+
if ( 'core/group' === $block['blockName'] && isset( $block['attrs']['style']['background']['backgroundImage'] ) ) {
779+
$bg_image = $block['attrs']['style']['background']['backgroundImage'];
780+
781+
if ( isset( $bg_image['id'] ) ) {
782+
$attachment_id = $bg_image['id'];
783+
$image_url = isset( $bg_image['url'] ) ? $bg_image['url'] : '';
784+
}
785+
}
786+
787+
// If we have a valid attachment and URL, process it.
788+
if ( ! is_null( $attachment_id ) && '' !== $image_url ) {
789+
$metadata = wp_get_attachment_metadata( $attachment_id );
790+
791+
if ( is_array( $metadata ) ) {
792+
$original_mime = get_post_mime_type( $attachment_id );
793+
794+
if ( null !== $original_mime && '' !== $original_mime ) {
795+
$target_mimes = webp_uploads_get_content_image_mimes( $attachment_id, 'background_image' );
796+
797+
foreach ( $target_mimes as $target_mime ) {
798+
if ( $target_mime === $original_mime ) {
799+
continue;
800+
}
801+
802+
$new_url = webp_uploads_get_mime_type_image( $attachment_id, $image_url, $target_mime );
803+
804+
if ( null !== $new_url && '' !== $new_url ) {
805+
// Replace the URL in the content.
806+
$block_content = str_replace( $image_url, $new_url, $block_content );
807+
}
808+
}
809+
}
810+
}
811+
}
812+
813+
return $block_content;
814+
}
815+
750816
/**
751817
* Initializes custom functionality for handling image uploads and content filters.
752818
*
753819
* @since 2.1.0
754820
*/
755821
function webp_uploads_init(): void {
822+
// Filter regular image tags.
756823
add_filter( 'wp_content_img_tag', webp_uploads_is_picture_element_enabled() ? 'webp_uploads_wrap_image_in_picture' : 'webp_uploads_filter_image_tag', 10, 3 );
824+
825+
// Filter blocks that may contain background images.
826+
add_filter( 'render_block_core/cover', 'webp_uploads_filter_block_background_images', 10, 2 );
827+
add_filter( 'render_block_core/group', 'webp_uploads_filter_block_background_images', 10, 2 );
757828
}
758829
add_action( 'init', 'webp_uploads_init' );
759830

0 commit comments

Comments
 (0)