@@ -747,13 +747,84 @@ function webp_uploads_render_generator(): void {
747
747
}
748
748
add_action ( 'wp_head ' , 'webp_uploads_render_generator ' );
749
749
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
+
750
816
/**
751
817
* Initializes custom functionality for handling image uploads and content filters.
752
818
*
753
819
* @since 2.1.0
754
820
*/
755
821
function webp_uploads_init (): void {
822
+ // Filter regular image tags.
756
823
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 );
757
828
}
758
829
add_action ( 'init ' , 'webp_uploads_init ' );
759
830
0 commit comments