Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update class-spectra.php #51

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/integrations/class-spectra.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +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' ) );
}

/**
Expand Down Expand Up @@ -58,4 +59,30 @@ 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.
*
* @return array
*/
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 ) {
$key = 'backgroundImage' . $device;
if ( ! isset( $attributes[ $key ] ) ) {
continue;
}

$image = apply_filters( 'wp_get_attachment_image_src', array( $attributes[ $key ]['url'] ), $attributes[ $key ]['id'], '', false );

// Replace the background image URL.
$attributes[ $key ]['url'] = $image[0];
}

return $attributes;
}
}