Skip to content

Commit 16016d7

Browse files
Handle elementor background image replacement
1 parent 9a4fbff commit 16016d7

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

php/class-plugin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cloudinary\Delivery\Lazy_Load;
1515
use Cloudinary\Delivery\Responsive_Breakpoints;
1616
use Cloudinary\Assets as CLD_Assets;
17+
use Cloudinary\Integrations\Elementor;
1718
use Cloudinary\Integrations\WPML;
1819
use Cloudinary\Media\Gallery;
1920
use Cloudinary\Sync\Storage;
@@ -31,7 +32,7 @@ final class Plugin {
3132
*
3233
* @since 0.1
3334
*
34-
* @var Admin|CLD_Assets|Connect|Dashboard|Deactivation|Delivery|Extensions|Gallery|Lazy_Load|Media|Meta_Box|Relate|Report|Responsive_Breakpoints|REST_API|State|Storage|SVG|Sync|URL[]|WPML|null
35+
* @var Admin|CLD_Assets|Connect|Dashboard|Deactivation|Delivery|Extensions|Gallery|Lazy_Load|Media|Meta_Box|Relate|Report|Responsive_Breakpoints|REST_API|State|Storage|SVG|Sync|URL[]|WPML|Elementor|null
3536
*/
3637
public $components;
3738
/**
@@ -136,6 +137,7 @@ public function plugins_loaded() {
136137
$this->components['metabox'] = new Meta_Box( $this );
137138
$this->components['url'] = new URL( $this );
138139
$this->components['wpml'] = new WPML( $this );
140+
$this->components['elementor'] = new Elementor( $this );
139141
$this->components['special_offer'] = new Special_Offer( $this );
140142
}
141143

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Elementor integration class for the Cloudinary plugin.
4+
*
5+
* @package Cloudinary
6+
*/
7+
8+
namespace Cloudinary\Integrations;
9+
10+
use Elementor\Core\Files\CSS\Post;
11+
use Elementor\Element_Base;
12+
use Elementor\Plugin;
13+
14+
/**
15+
* Class Elementor
16+
*/
17+
class Elementor extends Integrations {
18+
19+
/**
20+
* Check if the integration can be enabled.
21+
*
22+
* @return bool
23+
*/
24+
public function can_enable() {
25+
return class_exists( 'Elementor\Plugin' );
26+
}
27+
28+
/**
29+
* Register hooks for the integration.
30+
*
31+
* @return void
32+
*/
33+
public function register_hooks() {
34+
add_action( 'elementor/element/parse_css', array( $this, 'replace_bg_images_in_css' ), 10, 2 );
35+
}
36+
37+
/**
38+
* Replace all background images URLs with Cloudinary URLs, within the generated Elementor CSS file.
39+
*
40+
* @param Post $post_css The post CSS object.
41+
* @param Element_Base $element The Elementor element.
42+
* @return void
43+
*/
44+
public function replace_bg_images_in_css( $post_css, $element ) {
45+
$media = $this->plugin->get_component( 'media' );
46+
$settings = $element->get_settings_for_display();
47+
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+
);
75+
76+
foreach ( $background_keys as $background_key => $background_data ) {
77+
if ( ! isset( $settings[ $background_key ]['url'], $settings[ $background_key ]['id'] ) ) {
78+
continue;
79+
}
80+
81+
$original_url = $settings[ $background_key ]['url'];
82+
$media_id = $settings[ $background_key ]['id'];
83+
$media_size = isset( $settings[ $background_key ]['size'] ) ? $settings[ $background_key ]['size'] : array();
84+
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 ) {
89+
continue;
90+
}
91+
92+
// Build the CSS selector and rule.
93+
$css_selector = $post_css->get_element_unique_selector( $element ) . $background_data['suffix'];
94+
$css_rule = array( 'background-image' => "url('$cloudinary_url')" );
95+
96+
// Retrieve the specific media query rule for non-desktop devices.
97+
$media_query = null;
98+
$device = $background_data['device']; // either 'desktop', 'tablet' or 'mobile'.
99+
if ( 'desktop' !== $device ) {
100+
$media_query = array( 'max' => $device );
101+
}
102+
103+
$post_css->get_stylesheet()->add_rules( $css_selector, $css_rule, $media_query );
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)