Skip to content

Commit 959f7a4

Browse files
Generate URL even if Elementor one seems to come from Cloudinary
The one coming from Elementor doesn't contain all the options from the settings and therefore isn't accurate enough
1 parent e8eae5e commit 959f7a4

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

php/integrations/class-elementor.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function can_enable() {
6363
* @return void
6464
*/
6565
public function register_hooks() {
66-
add_action( 'elementor/element/parse_css', array( $this, 'replace_bg_images_in_css' ), 10, 2 );
66+
add_action( 'elementor/element/parse_css', array( $this, 'replace_background_images_in_css' ), 10, 2 );
6767
add_action( 'cloudinary_flush_cache', array( $this, 'clear_elementor_css_cache' ) );
6868
}
6969

@@ -74,7 +74,7 @@ public function register_hooks() {
7474
* @param Element_Base $element The Elementor element.
7575
* @return void
7676
*/
77-
public function replace_bg_images_in_css( $post_css, $element ) {
77+
public function replace_background_images_in_css( $post_css, $element ) {
7878
$settings = $element->get_settings_for_display();
7979
$media = $this->plugin->get_component( 'media' );
8080
$delivery = $this->plugin->get_component( 'delivery' );
@@ -84,36 +84,33 @@ public function replace_bg_images_in_css( $post_css, $element ) {
8484
}
8585

8686
foreach ( self::ELEMENTOR_BACKGROUND_IMAGES as $background_key => $background_data ) {
87-
// We need to have both URL and ID from the image to proceed.
88-
if ( ! isset( $settings[ $background_key ]['url'], $settings[ $background_key ]['id'] ) ) {
87+
// We need to have the ID from the image to proceed.
88+
if ( ! isset( $settings[ $background_key ]['id'] ) ) {
8989
continue;
9090
}
9191

92-
$original_url = $settings[ $background_key ]['url'];
93-
$media_id = $settings[ $background_key ]['id'];
94-
$media_size = isset( $settings[ $background_key ]['size'] ) ? $settings[ $background_key ]['size'] : array();
92+
$media_id = $settings[ $background_key ]['id'];
93+
$media_size = isset( $settings[ $background_key ]['size'] ) ? $settings[ $background_key ]['size'] : array();
9594

9695
// Skip if the media is not deliverable via Cloudinary.
9796
if ( ! $delivery->is_deliverable( $media_id ) ) {
9897
continue;
9998
}
10099

101-
// If the original URL is already a Cloudinary URL, use it directly; otherwise, generate the Cloudinary URL.
102-
$cloudinary_url = $media->is_cloudinary_url( $original_url )
103-
? $original_url
104-
: $media->cloudinary_url( $media_id, $media_size );
100+
// Generate the Cloudinary URL.
101+
$cloudinary_url = $media->cloudinary_url( $media_id, $media_size );
105102

106103
// Build the CSS selector and rule.
107104
$css_selector = $post_css->get_element_unique_selector( $element ) . $background_data['suffix'];
108105
$css_rule = array( 'background-image' => "url('$cloudinary_url')" );
109106

110107
// Retrieve the specific media query rule for non-desktop devices.
111108
$media_query = null;
112-
$device = $background_data['device']; // either 'desktop', 'tablet' or 'mobile'.
113-
if ( 'desktop' !== $device ) {
114-
$media_query = array( 'max' => $device );
109+
if ( 'desktop' !== $background_data['device'] ) {
110+
$media_query = array( 'max' => $background_data['device'] );
115111
}
116112

113+
// Override the CSS rule in Elementor.
117114
$post_css->get_stylesheet()->add_rules( $css_selector, $css_rule, $media_query );
118115
}
119116
}

0 commit comments

Comments
 (0)