Skip to content

Commit 95bc6bd

Browse files
committed
feat: Resolves feedback.
1 parent b9a6ceb commit 95bc6bd

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

plugins/webp-uploads/hooks.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -755,11 +755,29 @@ function webp_uploads_render_generator(): void {
755755
*
756756
* @since n.e.x.t
757757
*
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.
759772
* @param array<string, mixed> $block The block.
760773
* @return string The filtered block content.
761774
*/
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+
763781
// Only run on frontend.
764782
if ( ! webp_uploads_in_frontend_body() || '' === $block_content ) {
765783
return $block_content;
@@ -771,7 +789,7 @@ function webp_uploads_filter_block_background_images( string $block_content, arr
771789
// Process Cover block.
772790
if ( 'core/cover' === $block['blockName'] && isset( $block['attrs']['id'] ) ) {
773791
$attachment_id = $block['attrs']['id'];
774-
$image_url = isset( $block['attrs']['url'] ) ? $block['attrs']['url'] : '';
792+
$image_url = $block['attrs']['url'] ?? '';
775793
}
776794

777795
// Process Group block with background image.
@@ -780,31 +798,41 @@ function webp_uploads_filter_block_background_images( string $block_content, arr
780798

781799
if ( isset( $bg_image['id'] ) ) {
782800
$attachment_id = $bg_image['id'];
783-
$image_url = isset( $bg_image['url'] ) ? $bg_image['url'] : '';
801+
$image_url = $bg_image['url'] ?? '';
784802
}
785803
}
786804

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 );
790811

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+
}
793822

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 );
796824

797-
foreach ( $target_mimes as $target_mime ) {
798-
if ( $target_mime === $original_mime ) {
799-
continue;
800-
}
825+
if ( is_string( $new_url ) ) {
801826

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' );
803830

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;
808836
}
809837
}
810838
}

0 commit comments

Comments
 (0)