|
| 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