display-conditions.php
5 months ago
equal-height.php
5 months ago
floating-effects.php
5 months ago
liquid-glass.php
5 months ago
shape-divider.php
5 months ago
shapes.php
5 months ago
tooltips.php
5 months ago
wrapper-link.php
5 months ago
wrapper-link.php
254 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class: Wrapper_Link |
| 4 | * Name: Wrapper Link |
| 5 | * Slug: premium-wrapper-link |
| 6 | */ |
| 7 | |
| 8 | namespace PremiumAddons\Addons; |
| 9 | |
| 10 | // Elementor Classes. |
| 11 | use Elementor\Controls_Manager; |
| 12 | |
| 13 | // Premium Addons Classes. |
| 14 | use PremiumAddons\Admin\Includes\Admin_Helper; |
| 15 | use PremiumAddons\Includes\Helper_Functions; |
| 16 | use PremiumAddons\Includes\Controls\Premium_Post_Filter; |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit; // If this file is called directly, abort. |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Class Wrapper_Link. |
| 24 | */ |
| 25 | class Wrapper_Link { |
| 26 | |
| 27 | /** |
| 28 | * Load Script |
| 29 | * |
| 30 | * @var $load_script |
| 31 | */ |
| 32 | private static $load_script = null; |
| 33 | |
| 34 | /** |
| 35 | * Class object |
| 36 | * |
| 37 | * @var instance |
| 38 | */ |
| 39 | private static $instance = null; |
| 40 | |
| 41 | /** |
| 42 | * Class Constructor Function. |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | |
| 46 | // Enqueue the required JS file. |
| 47 | add_action( 'elementor/preview/enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 48 | |
| 49 | // Creates Premium Wrapper Link tab at the end of layout/content tab. |
| 50 | add_action( 'elementor/element/section/section_layout/after_section_end', array( $this, 'register_controls' ) ); |
| 51 | add_action( 'elementor/element/column/section_advanced/after_section_end', array( $this, 'register_controls' ) ); |
| 52 | add_action( 'elementor/element/common/_section_style/after_section_end', array( $this, 'register_controls' ) ); |
| 53 | |
| 54 | add_action( 'elementor/frontend/before_render', array( $this, 'check_script_enqueue' ) ); |
| 55 | |
| 56 | add_action( 'elementor/element/container/section_layout/after_section_end', array( $this, 'register_controls' ) ); |
| 57 | add_action( 'elementor/frontend/before_render', array( $this, 'before_render' ), 100 ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Register Global Tooltip controls. |
| 62 | * |
| 63 | * @since 1.0.0 |
| 64 | * @access public |
| 65 | * @param object $element for current element. |
| 66 | */ |
| 67 | public function register_controls( $element ) { |
| 68 | |
| 69 | $tabs = Controls_Manager::TAB_CONTENT; |
| 70 | |
| 71 | if ( 'section' === $element->get_name() || 'column' === $element->get_name() || 'container' === $element->get_name() ) { |
| 72 | $tabs = Controls_Manager::TAB_LAYOUT; |
| 73 | } |
| 74 | |
| 75 | $element->start_controls_section( |
| 76 | 'section_premium_wrapper_link', |
| 77 | array( |
| 78 | 'label' => sprintf( '<i class="pa-extension-icon pa-dash-icon"></i> %s', __( 'Wrapper Link', 'premium-addons-for-elementor' ) ), |
| 79 | 'tab' => $tabs, |
| 80 | ) |
| 81 | ); |
| 82 | |
| 83 | $element->add_control( |
| 84 | 'premium_wrapper_link_switcher', |
| 85 | array( |
| 86 | 'label' => __( 'Enable Wrapper Link', 'premium-addons-for-elementor' ), |
| 87 | 'type' => Controls_Manager::SWITCHER, |
| 88 | 'prefix_class' => 'premium-wrapper-link-', |
| 89 | ) |
| 90 | ); |
| 91 | |
| 92 | $element->add_control( |
| 93 | 'wrapper_link_notice', |
| 94 | array( |
| 95 | 'raw' => __( 'Please note that Wrapper Link works on the frontend.', 'premium-addons-for-elementor' ), |
| 96 | 'type' => Controls_Manager::RAW_HTML, |
| 97 | 'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning', |
| 98 | 'condition' => array( |
| 99 | 'premium_wrapper_link_switcher' => 'yes', |
| 100 | ), |
| 101 | ) |
| 102 | ); |
| 103 | |
| 104 | $element->add_control( |
| 105 | 'premium_wrapper_link_selection', |
| 106 | array( |
| 107 | 'label' => __( 'Link Type', 'premium-addons-for-elementor' ), |
| 108 | 'type' => Controls_Manager::SELECT, |
| 109 | 'options' => array( |
| 110 | 'url' => __( 'URL', 'premium-addons-for-elementor' ), |
| 111 | 'link' => __( 'Existing Page', 'premium-addons-for-elementor' ), |
| 112 | ), |
| 113 | 'default' => 'url', |
| 114 | 'label_block' => true, |
| 115 | 'condition' => array( |
| 116 | 'premium_wrapper_link_switcher' => 'yes', |
| 117 | ), |
| 118 | ) |
| 119 | ); |
| 120 | |
| 121 | $element->add_control( |
| 122 | 'premium_wrapper_link', |
| 123 | array( |
| 124 | 'label' => __( 'Link', 'premium-addons-for-elementor' ), |
| 125 | 'type' => Controls_Manager::URL, |
| 126 | 'dynamic' => array( |
| 127 | 'active' => true, |
| 128 | ), |
| 129 | 'placeholder' => 'https://example.com', |
| 130 | 'condition' => array( |
| 131 | 'premium_wrapper_link_switcher' => 'yes', |
| 132 | 'premium_wrapper_link_selection' => 'url', |
| 133 | ), |
| 134 | ) |
| 135 | ); |
| 136 | |
| 137 | $element->add_control( |
| 138 | 'premium_wrapper_existing_link', |
| 139 | array( |
| 140 | 'label' => __( 'Existing Page', 'premium-addons-for-elementor' ), |
| 141 | 'type' => Premium_Post_Filter::TYPE, |
| 142 | 'label_block' => true, |
| 143 | 'multiple' => false, |
| 144 | 'source' => array( 'post', 'page' ), |
| 145 | 'condition' => array( |
| 146 | 'premium_wrapper_link_switcher' => 'yes', |
| 147 | 'premium_wrapper_link_selection' => 'link', |
| 148 | ), |
| 149 | ) |
| 150 | ); |
| 151 | |
| 152 | $element->end_controls_section(); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /** |
| 157 | * Render Wrapper Link output on the frontend. |
| 158 | * |
| 159 | * Written in PHP and used to generate the final HTML. |
| 160 | * |
| 161 | * @since 1.0.0 |
| 162 | * @access public |
| 163 | * @param object $element for current element. |
| 164 | */ |
| 165 | public function before_render( $element ) { |
| 166 | |
| 167 | $settings = $element->get_settings_for_display(); |
| 168 | |
| 169 | if ( 'yes' === $element->get_settings_for_display( 'premium_wrapper_link_switcher' ) ) { |
| 170 | |
| 171 | if ( 'link' === $settings['premium_wrapper_link_selection'] ) { |
| 172 | $href = get_permalink( $settings['premium_wrapper_existing_link'] ); |
| 173 | } else { |
| 174 | $href = $settings['premium_wrapper_link']['url']; |
| 175 | } |
| 176 | |
| 177 | $link_settings = array( |
| 178 | 'type' => $settings['premium_wrapper_link_selection'], |
| 179 | 'link' => $settings['premium_wrapper_link'], |
| 180 | 'href' => esc_url( $href ), |
| 181 | ); |
| 182 | |
| 183 | if ( ! empty( $href ) ) { |
| 184 | $element->add_render_attribute( |
| 185 | '_wrapper', |
| 186 | array( |
| 187 | 'data-premium-element-link' => wp_json_encode( $link_settings ), |
| 188 | 'style' => 'cursor: pointer', |
| 189 | ) |
| 190 | ); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Enqueue scripts. |
| 197 | * |
| 198 | * Registers required dependencies for the extension and enqueues them. |
| 199 | * |
| 200 | * @since 1.6.5 |
| 201 | * @access public |
| 202 | */ |
| 203 | public function enqueue_scripts() { |
| 204 | |
| 205 | if ( ! wp_script_is( 'pa-wrapper-link', 'enqueued' ) ) { |
| 206 | wp_enqueue_script( 'pa-wrapper-link' ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Check Script Enqueue |
| 212 | * |
| 213 | * Check if the script files should be loaded. |
| 214 | * |
| 215 | * @since 4.7.7 |
| 216 | * @access public |
| 217 | */ |
| 218 | public function check_script_enqueue( $element ) { |
| 219 | |
| 220 | if ( self::$load_script ) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | if ( 'yes' === $element->get_settings_for_display( 'premium_wrapper_link_switcher' ) ) { |
| 225 | |
| 226 | $this->enqueue_scripts(); |
| 227 | |
| 228 | self::$load_script = true; |
| 229 | |
| 230 | remove_action( 'elementor/frontend/before_render', array( $this, 'check_script_enqueue' ) ); |
| 231 | |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Creates and returns an instance of the class |
| 237 | * |
| 238 | * @since 4.2.5 |
| 239 | * @access public |
| 240 | * |
| 241 | * @return object |
| 242 | */ |
| 243 | public static function get_instance() { |
| 244 | |
| 245 | if ( ! isset( self::$instance ) ) { |
| 246 | |
| 247 | self::$instance = new self(); |
| 248 | |
| 249 | } |
| 250 | |
| 251 | return self::$instance; |
| 252 | } |
| 253 | } |
| 254 |