← All changes
|
includes/features/Parallax_Background/Parallax_Background.php
+194
-0
51.1.2
→
51.1.87
View file →
| @@ -1,0 +1,194 @@ | ||
| 1 | +<?php /** @noinspection PhpUnused, CssUnusedSymbol, DuplicatedCode, SpellCheckingInspection */ | |
| 2 | + | |
| 3 | +namespace King_Addons; | |
| 4 | + | |
| 5 | +use Elementor\Controls_Manager; | |
| 6 | +use Elementor\Element_Base; | |
| 7 | +use Elementor\Group_Control_Image_Size; | |
| 8 | +use Elementor\Utils; | |
| 9 | + | |
| 10 | +if (!defined('ABSPATH')) { | |
| 11 | + exit; // Exit if accessed directly. | |
| 12 | +} | |
| 13 | + | |
| 14 | +class Parallax_Background | |
| 15 | +{ | |
| 16 | + private static ?Parallax_Background $_instance = null; | |
| 17 | + | |
| 18 | + public static function instance(): Parallax_Background | |
| 19 | + { | |
| 20 | + if (is_null(self::$_instance)) { | |
| 21 | + self::$_instance = new self(); | |
| 22 | + } | |
| 23 | + return self::$_instance; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public function __construct() | |
| 27 | + { | |
| 28 | + add_action('elementor/preview/enqueue_scripts', [__CLASS__, 'enqueueScripts'], 1); | |
| 29 | + add_action('elementor/element/section/section_advanced/after_section_end', [__CLASS__, 'addControls'], 1); | |
| 30 | + add_action('elementor/frontend/section/after_render', [__CLASS__, 'renderAnimation'], 1); | |
| 31 | + add_action('elementor/element/container/section_layout/after_section_end', array($this, 'addControls'), 10); | |
| 32 | + add_action('elementor/frontend/before_render', [__CLASS__, 'disableLazyLoading'], 1); | |
| 33 | + add_action('elementor/frontend/container/after_render', array($this, 'renderAnimation'), 10); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static function disableLazyLoading(Element_Base $element): void | |
| 37 | + { | |
| 38 | + if ($element->get_settings('kng_parallax_bg_switch')) { | |
| 39 | + $element->add_render_attribute( | |
| 40 | + '_wrapper', | |
| 41 | + [ | |
| 42 | + 'class' => 'e-lazyloaded', | |
| 43 | + ] | |
| 44 | + ); | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + public static function enqueueScripts(): void | |
| 49 | + { | |
| 50 | + if (!wp_script_is(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'jarallax' . '-' . 'jarallax')) { | |
| 51 | + wp_enqueue_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'jarallax' . '-' . 'jarallax', '', array('jquery'), KING_ADDONS_VERSION); | |
| 52 | + } | |
| 53 | + | |
| 54 | + if (!wp_script_is(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'parallax-background' . '-' . 'preview-handler')) { | |
| 55 | + wp_enqueue_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'parallax-background' . '-' . 'preview-handler', '', array('jquery'), KING_ADDONS_VERSION); | |
| 56 | + } | |
| 57 | + } | |
| 58 | + | |
| 59 | + public static function addControls(Element_Base $element): void | |
| 60 | + { | |
| 61 | + $element->start_controls_section( | |
| 62 | + 'kng_parallax_bg_section', | |
| 63 | + [ | |
| 64 | + 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Parallax Background', 'king-addons'), | |
| 65 | + 'tab' => Controls_Manager::TAB_ADVANCED | |
| 66 | + ] | |
| 67 | + ); | |
| 68 | + | |
| 69 | + $element->add_control( | |
| 70 | + 'kng_parallax_bg_switch', | |
| 71 | + [ | |
| 72 | + 'label' => esc_html__('Enable Parallax', 'king-addons'), | |
| 73 | + 'type' => Controls_Manager::SWITCHER, | |
| 74 | + 'prefix_class' => 'kng-parallax-bg-', | |
| 75 | + 'frontend_available' => true, | |
| 76 | + 'render_type' => 'template', | |
| 77 | + ] | |
| 78 | + ); | |
| 79 | + | |
| 80 | + $element->add_control( | |
| 81 | + 'kng_parallax_bg_notice', | |
| 82 | + [ | |
| 83 | + 'type' => Controls_Manager::NOTICE, | |
| 84 | + 'notice_type' => 'info', | |
| 85 | + 'dismissible' => false, | |
| 86 | + 'heading' => esc_html__( 'Note', 'king-addons' ), | |
| 87 | + 'content' => esc_html__( 'If images are not showing, try disabling the Lazy Load Background Images option in Elementor settings', 'king-addons' ), | |
| 88 | + 'condition' => [ | |
| 89 | + 'kng_parallax_bg_switch!' => '' | |
| 90 | + ], | |
| 91 | + ] | |
| 92 | + ); | |
| 93 | + | |
| 94 | + $element->add_control( | |
| 95 | + 'kng_parallax_bg_image', | |
| 96 | + [ | |
| 97 | + 'label' => esc_html__('Image', 'king-addons'), | |
| 98 | + 'type' => Controls_Manager::MEDIA, | |
| 99 | + 'default' => [ | |
| 100 | + 'url' => Utils::get_placeholder_image_src(), | |
| 101 | + ], | |
| 102 | + 'frontend_available' => true, | |
| 103 | + 'condition' => [ | |
| 104 | + 'kng_parallax_bg_switch!' => '' | |
| 105 | + ] | |
| 106 | + ] | |
| 107 | + ); | |
| 108 | + | |
| 109 | + $element->add_group_control( | |
| 110 | + Group_Control_Image_Size::get_type(), | |
| 111 | + [ | |
| 112 | + 'name' => 'kng_parallax_bg_image_size', | |
| 113 | + 'default' => 'full', | |
| 114 | + 'frontend_available' => true, | |
| 115 | + 'condition' => [ | |
| 116 | + 'kng_parallax_bg_switch!' => '' | |
| 117 | + ] | |
| 118 | + ] | |
| 119 | + ); | |
| 120 | + | |
| 121 | + $element->add_control( | |
| 122 | + 'kng_parallax_bg_speed', | |
| 123 | + [ | |
| 124 | + 'label' => esc_html__('Animation Speed and Moving Direction', 'king-addons'), | |
| 125 | + 'description' => esc_html__('From -1.0 to 2.0', 'king-addons'), | |
| 126 | + 'type' => Controls_Manager::NUMBER, | |
| 127 | + 'frontend_available' => true, | |
| 128 | + 'min' => -1, | |
| 129 | + 'max' => 2, | |
| 130 | + 'step' => 0.1, | |
| 131 | + 'default' => 1.3, | |
| 132 | + 'separator' => 'before', | |
| 133 | + 'condition' => [ | |
| 134 | + 'kng_parallax_bg_switch!' => '' | |
| 135 | + ] | |
| 136 | + ] | |
| 137 | + ); | |
| 138 | + | |
| 139 | + $element->add_control('kng_parallax_bg_type', | |
| 140 | + [ | |
| 141 | + 'label' => esc_html__('Type', 'king-addons'), | |
| 142 | + 'type' => Controls_Manager::SELECT, | |
| 143 | + 'options' => [ | |
| 144 | + 'scroll' => esc_html__('Scroll', 'king-addons'), | |
| 145 | + 'scale' => esc_html__('Scale', 'king-addons'), | |
| 146 | + 'opacity' => esc_html__('Opacity', 'king-addons'), | |
| 147 | + 'scroll-opacity' => esc_html__('Scroll-Opacity', 'king-addons'), | |
| 148 | + 'scale-opacity' => esc_html__('Scale-Opacity', 'king-addons'), | |
| 149 | + ], | |
| 150 | + 'default' => 'scroll', | |
| 151 | + 'frontend_available' => true, | |
| 152 | + 'label_block' => 'true', | |
| 153 | + 'separator' => 'before', | |
| 154 | + 'condition' => [ | |
| 155 | + 'kng_parallax_bg_switch!' => '' | |
| 156 | + ], | |
| 157 | + ] | |
| 158 | + ); | |
| 159 | + | |
| 160 | + $element->end_controls_section(); | |
| 161 | + } | |
| 162 | + | |
| 163 | + public static function renderAnimation(Element_Base $element): void | |
| 164 | + { | |
| 165 | + if (!empty($element->get_settings_for_display('kng_parallax_bg_switch'))) { | |
| 166 | + | |
| 167 | + if (!wp_script_is(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'jarallax' . '-' . 'jarallax')) { | |
| 168 | + wp_enqueue_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'jarallax' . '-' . 'jarallax', '', array('jquery'), KING_ADDONS_VERSION); | |
| 169 | + } | |
| 170 | + | |
| 171 | + if (('container' === $element->get_type() || 'section' === $element->get_type()) && 'yes' === $element->get_settings('kng_parallax_bg_switch')) { | |
| 172 | + | |
| 173 | + $settings = $element->get_settings(); | |
| 174 | + | |
| 175 | + $image_src = Group_Control_Image_Size::get_attachment_image_src($settings['kng_parallax_bg_image']['id'], 'kng_parallax_bg_image_size', $settings); | |
| 176 | + | |
| 177 | + if (!$image_src) { | |
| 178 | + $image_src = $settings['kng_parallax_bg_image']['url']; | |
| 179 | + } | |
| 180 | + | |
| 181 | + $element_ID = $element->get_id(); | |
| 182 | + | |
| 183 | + if ($image_src) { | |
| 184 | + $inline_style = ".king-addons-parallax-container {position: absolute;top: 0;left: 0;width: 100%;height: 100%;z-index: -100 !important;} .kng-parallax-bg-yes {background-image: none !important;} .jarallax div {will-change: transform;}"; | |
| 185 | + wp_enqueue_style('king-addons-parallax-background-' . $element_ID, KING_ADDONS_URL . 'includes/features/Parallax_Background/style.css', '', KING_ADDONS_VERSION); | |
| 186 | + wp_add_inline_style('king-addons-parallax-background-' . $element_ID, $inline_style); | |
| 187 | + | |
| 188 | + $inline_js = "document.querySelector('.elementor-element-" . esc_js($element_ID) . "').insertAdjacentHTML('beforeend', '" . '<div data-jarallax data-speed="' . esc_attr($settings['kng_parallax_bg_speed']) . '" data-type="' . esc_attr($settings['kng_parallax_bg_type']) . '" class="jarallax king-addons-parallax-container kng-parallax-bg-' . esc_js($element_ID) . '" style="background-image: url(' . esc_url($image_src) . ');"></div>' . "');"; | |
| 189 | + wp_print_inline_script_tag($inline_js); | |
| 190 | + } | |
| 191 | + } | |
| 192 | + } | |
| 193 | + } | |
| 194 | +} | |