Skip to content

Commit 8239f2a

Browse files
Handle more edge cases
1 parent 16016d7 commit 8239f2a

1 file changed

Lines changed: 47 additions & 34 deletions

File tree

php/integrations/class-elementor.php

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,38 @@
1616
*/
1717
class Elementor extends Integrations {
1818

19+
/**
20+
* List of Elementor background image settings keys, along with their device and CSS suffix.
21+
*
22+
* @var array
23+
*/
24+
const ELEMENTOR_BACKGROUND_IMAGES = array(
25+
'_background_image' => array(
26+
'device' => 'desktop',
27+
'suffix' => '',
28+
),
29+
'_background_hover_image' => array(
30+
'device' => 'desktop',
31+
'suffix' => ':hover',
32+
),
33+
'_background_image_tablet' => array(
34+
'device' => 'tablet',
35+
'suffix' => '',
36+
),
37+
'_background_hover_image_tablet' => array(
38+
'device' => 'tablet',
39+
'suffix' => ':hover',
40+
),
41+
'_background_image_mobile' => array(
42+
'device' => 'mobile',
43+
'suffix' => '',
44+
),
45+
'_background_hover_image_mobile' => array(
46+
'device' => 'mobile',
47+
'suffix' => ':hover',
48+
),
49+
);
50+
1951
/**
2052
* Check if the integration can be enabled.
2153
*
@@ -42,38 +74,16 @@ public function register_hooks() {
4274
* @return void
4375
*/
4476
public function replace_bg_images_in_css( $post_css, $element ) {
45-
$media = $this->plugin->get_component( 'media' );
46-
$settings = $element->get_settings_for_display();
77+
$settings = $element->get_settings_for_display();
78+
$media = $this->plugin->get_component( 'media' );
79+
$delivery = $this->plugin->get_component( 'delivery' );
4780

48-
// Define all background image related keys and their specificities.
49-
$background_keys = array(
50-
'_background_image' => array(
51-
'device' => 'desktop',
52-
'suffix' => '',
53-
),
54-
'_background_hover_image' => array(
55-
'device' => 'desktop',
56-
'suffix' => ':hover',
57-
),
58-
'_background_image_tablet' => array(
59-
'device' => 'tablet',
60-
'suffix' => '',
61-
),
62-
'_background_hover_image_tablet' => array(
63-
'device' => 'tablet',
64-
'suffix' => ':hover',
65-
),
66-
'_background_image_mobile' => array(
67-
'device' => 'mobile',
68-
'suffix' => '',
69-
),
70-
'_background_hover_image_mobile' => array(
71-
'device' => 'mobile',
72-
'suffix' => ':hover',
73-
),
74-
);
81+
if ( ! $media || ! $delivery ) {
82+
return;
83+
}
7584

76-
foreach ( $background_keys as $background_key => $background_data ) {
85+
foreach ( self::ELEMENTOR_BACKGROUND_IMAGES as $background_key => $background_data ) {
86+
// We need to have both URL and ID from the image to proceed.
7787
if ( ! isset( $settings[ $background_key ]['url'], $settings[ $background_key ]['id'] ) ) {
7888
continue;
7989
}
@@ -82,13 +92,16 @@ public function replace_bg_images_in_css( $post_css, $element ) {
8292
$media_id = $settings[ $background_key ]['id'];
8393
$media_size = isset( $settings[ $background_key ]['size'] ) ? $settings[ $background_key ]['size'] : array();
8494

85-
$cloudinary_url = $media->cloudinary_url( $media_id, $media_size );
86-
87-
// Skip if the URL is unchanged or the cloudinary URL cannot be generated somehow.
88-
if ( ! $cloudinary_url || $cloudinary_url === $original_url ) {
95+
// Skip if the media is not deliverable via Cloudinary.
96+
if ( ! $delivery->is_deliverable( $media_id ) ) {
8997
continue;
9098
}
9199

100+
// If the original URL is already a Cloudinary URL, use it directly; otherwise, generate the Cloudinary URL.
101+
$cloudinary_url = $media->is_cloudinary_url( $original_url )
102+
? $original_url
103+
: $media->cloudinary_url( $media_id, $media_size );
104+
92105
// Build the CSS selector and rule.
93106
$css_selector = $post_css->get_element_unique_selector( $element ) . $background_data['suffix'];
94107
$css_rule = array( 'background-image' => "url('$cloudinary_url')" );

0 commit comments

Comments
 (0)