|
17 | 17 | class Elementor extends Integrations { |
18 | 18 |
|
19 | 19 | /** |
20 | | - * List of Elementor background image settings keys, along with their device and CSS suffix. |
| 20 | + * List of Elementor background image settings keys. |
21 | 21 | * |
22 | 22 | * @var array |
23 | 23 | */ |
24 | 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 | | - ), |
| 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', |
49 | 31 | ); |
50 | 32 |
|
51 | 33 | /** |
@@ -87,7 +69,7 @@ public function replace_background_images_in_css( $post_css, $element ) { |
87 | 69 | return; |
88 | 70 | } |
89 | 71 |
|
90 | | - foreach ( self::ELEMENTOR_BACKGROUND_IMAGES as $background_key => $background_data ) { |
| 72 | + foreach ( self::ELEMENTOR_BACKGROUND_IMAGES as $background_key ) { |
91 | 73 | $background = null; |
92 | 74 | $is_container = false; |
93 | 75 |
|
@@ -127,15 +109,18 @@ public function replace_background_images_in_css( $post_css, $element ) { |
127 | 109 | return; |
128 | 110 | } |
129 | 111 |
|
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 ); |
134 | 114 |
|
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. |
136 | 119 | $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' ); |
139 | 124 | } |
140 | 125 |
|
141 | 126 | $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 |
200 | 185 | $stylesheet->add_rules( $css_selector, $css_rule, $media_query ); |
201 | 186 | return true; |
202 | 187 | } |
| 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 | + } |
203 | 215 | } |
0 commit comments