| 1 |
<?php /** @noinspection PhpUnused */ |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use Elementor\Element_Base; |
| 7 |
|
| 8 |
/** @noinspection SpellCheckingInspection */ |
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Wrapper_Link |
| 14 |
{ |
| 15 |
private static ?Wrapper_Link $_instance = null; |
| 16 |
|
| 17 |
public static function instance(): Wrapper_Link |
| 18 |
{ |
| 19 |
if (is_null(self::$_instance)) { |
| 20 |
self::$_instance = new self(); |
| 21 |
} |
| 22 |
return self::$_instance; |
| 23 |
} |
| 24 |
|
| 25 |
public function __construct() |
| 26 |
{ |
| 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/frontend/before_render', [__CLASS__, 'renderLink'], 1); |
| 32 |
} |
| 33 |
|
| 34 |
public static function addControls(Element_Base $element): void |
| 35 |
{ |
| 36 |
$element->start_controls_section( |
| 37 |
'kng_wrapper_link_section', |
| 38 |
[ |
| 39 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Wrapper Link', 'king-addons'), |
| 40 |
'tab' => Controls_Manager::TAB_ADVANCED |
| 41 |
] |
| 42 |
); |
| 43 |
|
| 44 |
$element->add_control( |
| 45 |
'kng_wrapper_link_switch', |
| 46 |
[ |
| 47 |
'label' => esc_html__('Enable Wrapper Link', 'king-addons'), |
| 48 |
'type' => Controls_Manager::SWITCHER |
| 49 |
] |
| 50 |
); |
| 51 |
|
| 52 |
$element->add_control( |
| 53 |
'kng_wrapper_link', |
| 54 |
[ |
| 55 |
'label' => esc_html__('Link', 'king-addons'), |
| 56 |
'type' => Controls_Manager::URL, |
| 57 |
'placeholder' => esc_html__('https://example.com', 'king-addons'), |
| 58 |
'options' => [ |
| 59 |
'url', |
| 60 |
'is_external' |
| 61 |
], |
| 62 |
'condition' => [ |
| 63 |
'kng_wrapper_link_switch!' => '' |
| 64 |
] |
| 65 |
] |
| 66 |
); |
| 67 |
|
| 68 |
$element->end_controls_section(); |
| 69 |
} |
| 70 |
|
| 71 |
public static function renderLink(Element_Base $element): void |
| 72 |
{ |
| 73 |
if (!empty($element->get_settings_for_display('kng_wrapper_link_switch'))) { |
| 74 |
|
| 75 |
$wrapper_link_settings = $element->get_settings_for_display('kng_wrapper_link'); |
| 76 |
|
| 77 |
if (!empty($wrapper_link_settings['url'])) { |
| 78 |
|
| 79 |
$link_target = ($wrapper_link_settings['is_external']) ? '_blank' : '_self'; |
| 80 |
|
| 81 |
$element->add_render_attribute( |
| 82 |
'_wrapper', |
| 83 |
[ |
| 84 |
'style' => 'cursor: pointer;', |
| 85 |
'onclick' => "window.open('" . esc_url($wrapper_link_settings['url']) . "', '" . esc_attr($link_target) . "');" |
| 86 |
] |
| 87 |
); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |