| 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 |
/** |
| 72 |
* Loads the click handler, and only for pages that actually use the feature. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
private static function enqueueScript(): void |
| 77 |
{ |
| 78 |
$handle = KING_ADDONS_ASSETS_UNIQUE_KEY . '-wrapper-link-wrapper-link'; |
| 79 |
|
| 80 |
if (!wp_script_is($handle, 'enqueued')) { |
| 81 |
wp_enqueue_script($handle); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public static function renderLink(Element_Base $element): void |
| 86 |
{ |
| 87 |
if (!empty($element->get_settings_for_display('kng_wrapper_link_switch'))) { |
| 88 |
|
| 89 |
$wrapper_link_settings = $element->get_settings_for_display('kng_wrapper_link'); |
| 90 |
|
| 91 |
if (!empty($wrapper_link_settings['url'])) { |
| 92 |
|
| 93 |
$url = esc_url_raw($wrapper_link_settings['url']); |
| 94 |
if (empty($url)) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$link_target = (!empty($wrapper_link_settings['is_external'])) ? '_blank' : '_self'; |
| 99 |
|
| 100 |
/** |
| 101 |
* The URL travels in a data attribute and the click is handled by |
| 102 |
* wrapper-link.js. An inline onclick fired on every click that |
| 103 |
* bubbled up, so a link inside the container navigated twice — |
| 104 |
* once itself and once through the wrapper. |
| 105 |
*/ |
| 106 |
$element->add_render_attribute( |
| 107 |
'_wrapper', |
| 108 |
[ |
| 109 |
'style' => 'cursor: pointer;', |
| 110 |
'data-kng-wrapper-link' => $url, |
| 111 |
'data-kng-wrapper-link-target' => $link_target, |
| 112 |
] |
| 113 |
); |
| 114 |
|
| 115 |
self::enqueueScript(); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |