From 3bcee2c8ebea2211ca8b866975bc7381d7ce2f41 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 19 Sep 2024 13:22:54 +0100 Subject: [PATCH 1/2] Update class-spectra.php Replace Spectra background image URLs with Cloudflare URLs --- app/integrations/class-spectra.php | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/app/integrations/class-spectra.php b/app/integrations/class-spectra.php index b095505..f8f42c2 100644 --- a/app/integrations/class-spectra.php +++ b/app/integrations/class-spectra.php @@ -14,6 +14,8 @@ namespace CF_Images\App\Integrations; +use CF_Images\App\Image; + if ( ! defined( 'WPINC' ) ) { die; } @@ -31,6 +33,7 @@ class Spectra { */ public function __construct() { add_filter( 'cf_images_content_attachment_id', array( $this, 'detect_image_id' ), 10, 2 ); + add_filter( 'uagb_block_attributes_for_css_and_js', array( $this, 'replace_background_images' ), 10, 2 ); } /** @@ -58,4 +61,35 @@ public function detect_image_id( int $attachment_id, string $filtered_image ): i return $attachment_id; } + + /** + * Replace background images in Spectra blocks. + * + * @param array $attributes Block attributes. + * @param array $block Block data. + * + * @return array + */ + public function replace_background_images( $attributes, $block ) { + $device_aliases = [ 'Desktop', 'Tablet', 'Mobile' ]; + + // Check background images for all devices. + foreach ( $device_aliases as $device ) { + if ( isset( $attributes['backgroundImage' . $device] ) ) { + // Create a fake tag with the standard WordPress class to indicate the attachment ID and the URL being passed from Spectra prior to creating inline CSS. + $image = new Image( '', $attributes['backgroundImage' . $device]['url'], '' ); + + // Do the CF Images magic. + $image_dom = $image->get_processed(); + + // Get the src attribute from the image. + preg_match( '/src=[\'"]([^\'"]+)/i', $image_dom, $src ); + + // Replace the background image URL. + $attributes['backgroundImage' . $device]['url'] = $src[1]; + } + } + + return $attributes; + } } From 27e9816e9ee34d4b94cf842d1b8256ded4b3841c Mon Sep 17 00:00:00 2001 From: Anton Vanyukov Date: Mon, 7 Oct 2024 14:15:11 +1000 Subject: [PATCH 2/2] Update class-spectra.php Simplify code --- app/integrations/class-spectra.php | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/app/integrations/class-spectra.php b/app/integrations/class-spectra.php index f8f42c2..67aa78d 100644 --- a/app/integrations/class-spectra.php +++ b/app/integrations/class-spectra.php @@ -14,8 +14,6 @@ namespace CF_Images\App\Integrations; -use CF_Images\App\Image; - if ( ! defined( 'WPINC' ) ) { die; } @@ -33,7 +31,7 @@ class Spectra { */ public function __construct() { add_filter( 'cf_images_content_attachment_id', array( $this, 'detect_image_id' ), 10, 2 ); - add_filter( 'uagb_block_attributes_for_css_and_js', array( $this, 'replace_background_images' ), 10, 2 ); + add_filter( 'uagb_block_attributes_for_css_and_js', array( $this, 'replace_background_images' ) ); } /** @@ -66,28 +64,23 @@ public function detect_image_id( int $attachment_id, string $filtered_image ): i * Replace background images in Spectra blocks. * * @param array $attributes Block attributes. - * @param array $block Block data. * * @return array */ - public function replace_background_images( $attributes, $block ) { - $device_aliases = [ 'Desktop', 'Tablet', 'Mobile' ]; + public function replace_background_images( array $attributes ): array { + $device_aliases = array( 'Desktop', 'Tablet', 'Mobile' ); // Check background images for all devices. foreach ( $device_aliases as $device ) { - if ( isset( $attributes['backgroundImage' . $device] ) ) { - // Create a fake tag with the standard WordPress class to indicate the attachment ID and the URL being passed from Spectra prior to creating inline CSS. - $image = new Image( '', $attributes['backgroundImage' . $device]['url'], '' ); - - // Do the CF Images magic. - $image_dom = $image->get_processed(); + $key = 'backgroundImage' . $device; + if ( ! isset( $attributes[ $key ] ) ) { + continue; + } - // Get the src attribute from the image. - preg_match( '/src=[\'"]([^\'"]+)/i', $image_dom, $src ); + $image = apply_filters( 'wp_get_attachment_image_src', array( $attributes[ $key ]['url'] ), $attributes[ $key ]['id'], '', false ); - // Replace the background image URL. - $attributes['backgroundImage' . $device]['url'] = $src[1]; - } + // Replace the background image URL. + $attributes[ $key ]['url'] = $image[0]; } return $attributes;