| @@ -1,0 +1,101 @@ | ||
| 1 | +<?php /** @noinspection SpellCheckingInspection, PhpUnused */ | |
| 2 | + | |
| 3 | +namespace King_Addons; | |
| 4 | + | |
| 5 | +use Elementor\Controls_Manager; | |
| 6 | +use Elementor\Element_Base; | |
| 7 | + | |
| 8 | +if (!defined('ABSPATH')) { | |
| 9 | + exit; // Exit if accessed directly. | |
| 10 | +} | |
| 11 | + | |
| 12 | +class Custom_CSS | |
| 13 | +{ | |
| 14 | + private static ?Custom_CSS $_instance = null; | |
| 15 | + | |
| 16 | + public static function instance(): Custom_CSS | |
| 17 | + { | |
| 18 | + if (is_null(self::$_instance)) { | |
| 19 | + self::$_instance = new self(); | |
| 20 | + } | |
| 21 | + return self::$_instance; | |
| 22 | + } | |
| 23 | + | |
| 24 | + public function __construct() | |
| 25 | + { | |
| 26 | + add_action('elementor/preview/enqueue_scripts', [__CLASS__, 'enqueueScripts'], 1); | |
| 27 | + add_action('elementor/element/container/section_layout/after_section_end', [__CLASS__, 'addControls'], 1); | |
| 28 | + add_action('elementor/element/column/section_advanced/after_section_end', [__CLASS__, 'addControls'], 1); | |
| 29 | + add_action('elementor/element/section/section_advanced/after_section_end', [__CLASS__, 'addControls'], 1); | |
| 30 | + add_action('elementor/element/common/_section_style/after_section_end', [__CLASS__, 'addControls'], 1); | |
| 31 | + add_action('elementor/element/parse_css', [__CLASS__, 'renderCSS'], 10, 2); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public static function enqueueScripts(): void | |
| 35 | + { | |
| 36 | + if (!wp_script_is(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'custom-css' . '-' . 'preview-handler')) { | |
| 37 | + wp_enqueue_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'custom-css' . '-' . 'preview-handler', '', array('jquery'), KING_ADDONS_VERSION); | |
| 38 | + } | |
| 39 | + } | |
| 40 | + | |
| 41 | + public static function addControls(Element_Base $element): void | |
| 42 | + { | |
| 43 | + $element->start_controls_section( | |
| 44 | + 'kng_custom_css_section', | |
| 45 | + [ | |
| 46 | + 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Custom CSS', 'king-addons'), | |
| 47 | + 'tab' => Controls_Manager::TAB_ADVANCED | |
| 48 | + ] | |
| 49 | + ); | |
| 50 | + | |
| 51 | + $element->add_control( | |
| 52 | + 'kng_custom_css_switch', | |
| 53 | + [ | |
| 54 | + 'label' => esc_html__('Enable Custom CSS', 'king-addons'), | |
| 55 | + 'type' => Controls_Manager::SWITCHER, | |
| 56 | + 'prefix_class' => 'kng-custom-css-', | |
| 57 | + 'frontend_available' => true | |
| 58 | + ] | |
| 59 | + ); | |
| 60 | + | |
| 61 | + $element->add_control( | |
| 62 | + 'kng_custom_css', | |
| 63 | + [ | |
| 64 | + 'type' => Controls_Manager::CODE, | |
| 65 | + 'label' => esc_html__('Custom CSS', 'king-addons'), | |
| 66 | + 'description' => esc_html__('Use "[current-element]" keyword as selector to target the current element. You can also use all CSS pseudo-classes, for example "[current-element]:hover" adds action when mouse hover over the current element.', 'king-addons'), | |
| 67 | + 'default' => '[current-element] { }', | |
| 68 | + 'language' => 'css', | |
| 69 | + 'render_type' => 'ui', | |
| 70 | + 'label_block' => true, | |
| 71 | + 'frontend_available' => true, | |
| 72 | + 'condition' => [ | |
| 73 | + 'kng_custom_css_switch!' => '' | |
| 74 | + ] | |
| 75 | + ] | |
| 76 | + ); | |
| 77 | + | |
| 78 | + $element->end_controls_section(); | |
| 79 | + } | |
| 80 | + | |
| 81 | + public static function renderCSS($raw_css, Element_Base $element): void | |
| 82 | + { | |
| 83 | + if (!empty($element->get_settings_for_display('kng_custom_css_switch'))) { | |
| 84 | + $element_settings = $element->get_settings(); | |
| 85 | + | |
| 86 | + if (empty($element_settings['kng_custom_css'])) { | |
| 87 | + return; | |
| 88 | + } | |
| 89 | + | |
| 90 | + $css = trim($element_settings['kng_custom_css']); | |
| 91 | + | |
| 92 | + if (empty($css)) { | |
| 93 | + return; | |
| 94 | + } | |
| 95 | + | |
| 96 | + $css = str_replace('[current-element]', $raw_css->get_element_unique_selector($element), $css); | |
| 97 | + | |
| 98 | + $raw_css->get_stylesheet()->add_raw_css($css); | |
| 99 | + } | |
| 100 | + } | |
| 101 | +} | |