@@ -755,11 +755,29 @@ function webp_uploads_render_generator(): void {
755
755
*
756
756
* @since n.e.x.t
757
757
*
758
- * @param string $block_content The block content.
758
+ * @phpstan-param array{
759
+ * blockName: string|null,
760
+ * attrs: array{
761
+ * id?: positive-int,
762
+ * url?: string,
763
+ * style?: array{
764
+ * background?: array{
765
+ * backgroundImage?: string
766
+ * }
767
+ * }
768
+ * }
769
+ * } $block
770
+ *
771
+ * @param string|mixed $block_content The block content.
759
772
* @param array<string, mixed> $block The block.
760
773
* @return string The filtered block content.
761
774
*/
762
- function webp_uploads_filter_block_background_images ( string $ block_content , array $ block ): string {
775
+ function webp_uploads_filter_block_background_images ( $ block_content , array $ block ): string {
776
+ // Because plugins can do bad things.
777
+ if ( ! is_string ( $ block_content ) ) {
778
+ $ block_content = '' ;
779
+ }
780
+
763
781
// Only run on frontend.
764
782
if ( ! webp_uploads_in_frontend_body () || '' === $ block_content ) {
765
783
return $ block_content ;
@@ -771,7 +789,7 @@ function webp_uploads_filter_block_background_images( string $block_content, arr
771
789
// Process Cover block.
772
790
if ( 'core/cover ' === $ block ['blockName ' ] && isset ( $ block ['attrs ' ]['id ' ] ) ) {
773
791
$ attachment_id = $ block ['attrs ' ]['id ' ];
774
- $ image_url = isset ( $ block ['attrs ' ]['url ' ] ) ? $ block [ ' attrs ' ][ ' url ' ] : '' ;
792
+ $ image_url = $ block ['attrs ' ]['url ' ] ?? '' ;
775
793
}
776
794
777
795
// Process Group block with background image.
@@ -780,31 +798,41 @@ function webp_uploads_filter_block_background_images( string $block_content, arr
780
798
781
799
if ( isset ( $ bg_image ['id ' ] ) ) {
782
800
$ attachment_id = $ bg_image ['id ' ];
783
- $ image_url = isset ( $ bg_image ['url ' ] ) ? $ bg_image [ ' url ' ] : '' ;
801
+ $ image_url = $ bg_image ['url ' ] ?? '' ;
784
802
}
785
803
}
786
804
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 );
805
+ // Abort if there is no associated background image.
806
+ if ( is_null ( $ attachment_id ) || '' === $ image_url || ! is_array ( wp_get_attachment_metadata ( $ attachment_id ) ) ) {
807
+ return $ block_content ;
808
+ }
809
+
810
+ $ original_mime = get_post_mime_type ( $ attachment_id );
790
811
791
- if ( is_array ( $ metadata ) ) {
792
- $ original_mime = get_post_mime_type ( $ attachment_id );
812
+ if ( ! is_string ( $ original_mime ) ) {
813
+ return $ block_content ;
814
+ }
815
+
816
+ $ target_mimes = webp_uploads_get_content_image_mimes ( $ attachment_id , 'background_image ' );
817
+
818
+ foreach ( $ target_mimes as $ target_mime ) {
819
+ if ( $ target_mime === $ original_mime ) {
820
+ continue ;
821
+ }
793
822
794
- if ( null !== $ original_mime && '' !== $ original_mime ) {
795
- $ target_mimes = webp_uploads_get_content_image_mimes ( $ attachment_id , 'background_image ' );
823
+ $ new_url = webp_uploads_get_mime_type_image ( $ attachment_id , $ image_url , $ target_mime );
796
824
797
- foreach ( $ target_mimes as $ target_mime ) {
798
- if ( $ target_mime === $ original_mime ) {
799
- continue ;
800
- }
825
+ if ( is_string ( $ new_url ) ) {
801
826
802
- $ new_url = webp_uploads_get_mime_type_image ( $ attachment_id , $ image_url , $ target_mime );
827
+ $ processor = new WP_HTML_Tag_Processor ( $ block_content );
828
+ while ( $ processor ->next_tag () ) {
829
+ $ style = $ processor ->get_attribute ( 'style ' );
803
830
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
- }
831
+ if ( is_string ( $ style ) && str_contains ( $ style , 'background-image: ' ) && str_contains ( $ style , $ image_url ) ) {
832
+ $ updated_style = str_replace ( $ image_url , $ new_url , $ style );
833
+ $ processor ->set_attribute ( 'style ' , $ updated_style );
834
+ $ block_content = $ processor ->get_updated_html ();
835
+ break 2 ;
808
836
}
809
837
}
810
838
}
0 commit comments