Skip to content

Commit 56848db

Browse files
Override background image specific rules instead of adding on top
1 parent f290e72 commit 56848db

1 file changed

Lines changed: 45 additions & 33 deletions

File tree

php/integrations/class-elementor.php

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,17 @@
1717
class Elementor extends Integrations {
1818

1919
/**
20-
* List of Elementor background image settings keys, along with their device and CSS suffix.
20+
* List of Elementor background image settings keys.
2121
*
2222
* @var array
2323
*/
2424
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-
),
25+
'background_image',
26+
'background_hover_image',
27+
'background_image_tablet',
28+
'background_hover_image_tablet',
29+
'background_image_mobile',
30+
'background_hover_image_mobile',
4931
);
5032

5133
/**
@@ -87,7 +69,7 @@ public function replace_background_images_in_css( $post_css, $element ) {
8769
return;
8870
}
8971

90-
foreach ( self::ELEMENTOR_BACKGROUND_IMAGES as $background_key => $background_data ) {
72+
foreach ( self::ELEMENTOR_BACKGROUND_IMAGES as $background_key ) {
9173
$background = null;
9274
$is_container = false;
9375

@@ -127,15 +109,18 @@ public function replace_background_images_in_css( $post_css, $element ) {
127109
return;
128110
}
129111

130-
// Elementor applies this suffix rule to container background images to avoid conflicts with motion effects backgrounds.
131-
$default_suffix = $is_container ? ':not(.elementor-motion-effects-element-type-background)' : '';
132-
$css_selector = $unique_selector . $default_suffix . $background_data['suffix'];
133-
$css_rule = array( 'background-image' => "url('$cloudinary_url')" );
112+
// Determine if it's a hover background.
113+
$is_hover = ( strpos( $background_key, 'hover' ) !== false );
134114

135-
// Retrieve the specific media query rule for non-desktop devices.
115+
$css_selector = $this->build_background_image_css_selector( $unique_selector, $is_container, $is_hover );
116+
$css_rule = array( 'background-image' => "url('$cloudinary_url')" );
117+
118+
// Retrieve the specific media query rule for non-desktop devices based on the setting key.
136119
$media_query = null;
137-
if ( 'desktop' !== $background_data['device'] ) {
138-
$media_query = array( 'max' => $background_data['device'] );
120+
if ( strpos( $background_key, 'tablet' ) !== false ) {
121+
$media_query = array( 'max' => 'tablet' );
122+
} elseif ( strpos( $background_key, 'mobile' ) !== false ) {
123+
$media_query = array( 'max' => 'mobile' );
139124
}
140125

141126
$success = $this->override_elementor_css_rule( $post_css, $css_selector, $css_rule, $media_query );
@@ -200,4 +185,31 @@ private function override_elementor_css_rule( $post_css, $css_selector, $css_rul
200185
$stylesheet->add_rules( $css_selector, $css_rule, $media_query );
201186
return true;
202187
}
188+
189+
/**
190+
* Build the full CSS selector for background image replacement.
191+
*
192+
* @param string $unique_selector The unique selector for the element.
193+
* @param bool $is_container Whether the element is a container (section/column).
194+
* @param bool $is_hover Whether the background is for hover state.
195+
*
196+
* @return string
197+
*/
198+
private function build_background_image_css_selector( $unique_selector, $is_container, $is_hover ) {
199+
// For hover backgrounds, we simply append :hover to the unique selector.
200+
if ( $is_hover ) {
201+
return $unique_selector . ':hover';
202+
}
203+
204+
// For non-container elements, we can return the unique selector as is.
205+
if ( ! $is_container ) {
206+
return $unique_selector;
207+
}
208+
209+
// For container elements, we need to replicate the specific selector Elementor uses for background images.
210+
return sprintf(
211+
'%1$s:not(.elementor-motion-effects-element-type-background), %1$s > .elementor-motion-effects-container > .elementor-motion-effects-layer',
212+
$unique_selector
213+
);
214+
}
203215
}

0 commit comments

Comments
 (0)