display-conditions.php
2 weeks ago
equal-height.php
2 weeks ago
floating-effects.php
2 weeks ago
liquid-glass.php
2 weeks ago
shape-divider.php
2 weeks ago
shapes.php
2 weeks ago
tooltips.php
2 days ago
wrapper-link.php
2 weeks ago
display-conditions.php
382 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class: Display_Conditions |
| 4 | * Name : Display Conditions |
| 5 | * Slug : display-conditions |
| 6 | */ |
| 7 | |
| 8 | namespace PremiumAddons\Addons; |
| 9 | |
| 10 | // PremiumAddons Classes. |
| 11 | use PremiumAddons\Includes\Helper_Functions; |
| 12 | use PremiumAddons\Includes\PA_Controls_Handler; |
| 13 | |
| 14 | // Elementor Classes. |
| 15 | use Elementor\Repeater; |
| 16 | use Elementor\Controls_Manager; |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Class Display_Conditions |
| 24 | * |
| 25 | * @since 4.11.58 |
| 26 | * @access public |
| 27 | * @package PremiumAddons\Addons |
| 28 | */ |
| 29 | class Display_Conditions { |
| 30 | |
| 31 | /** |
| 32 | * Load Script |
| 33 | * |
| 34 | * @var bool|null |
| 35 | */ |
| 36 | private static $load_script = null; |
| 37 | |
| 38 | /** |
| 39 | * Class object |
| 40 | * |
| 41 | * @var self|null |
| 42 | */ |
| 43 | private static $instance = null; |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | */ |
| 48 | public function __construct() { |
| 49 | |
| 50 | // Enqueue the required JS file. |
| 51 | add_action( 'elementor/preview/enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 52 | |
| 53 | add_action( 'elementor/element/section/section_advanced/after_section_end', array( $this, 'register_controls' ), 10 ); |
| 54 | add_action( 'elementor/element/column/section_advanced/after_section_end', array( $this, 'register_controls' ), 10 ); |
| 55 | add_action( 'elementor/element/common/_section_style/after_section_end', array( $this, 'register_controls' ), 10 ); |
| 56 | |
| 57 | add_action( 'elementor/frontend/before_render', array( $this, 'check_script_enqueue' ) ); |
| 58 | |
| 59 | add_action( 'elementor/element/container/section_layout/after_section_end', array( $this, 'register_controls' ), 10 ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Enqueue scripts. |
| 64 | * |
| 65 | * Registers required dependencies for the extension and enqueues them. |
| 66 | * |
| 67 | * @since 4.9.21 |
| 68 | * @access public |
| 69 | */ |
| 70 | public function enqueue_scripts() { |
| 71 | |
| 72 | if ( ! wp_script_is( 'pa-dis-conditions', 'enqueued' ) ) { |
| 73 | wp_enqueue_script( 'pa-dis-conditions' ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Register PA Display Conditions controls. |
| 79 | * |
| 80 | * @access public |
| 81 | * @param object $element for current element. |
| 82 | */ |
| 83 | public function register_controls( $element ) { |
| 84 | |
| 85 | $element->start_controls_section( |
| 86 | 'section_pa_display_conditions', |
| 87 | array( |
| 88 | 'label' => sprintf( '<i class="pa-extension-icon pa-dash-icon"></i> %s', __( 'Display Conditions', 'premium-addons-for-elementor' ) ), |
| 89 | 'tab' => Controls_Manager::TAB_ADVANCED, |
| 90 | ) |
| 91 | ); |
| 92 | |
| 93 | $element->add_control( |
| 94 | 'pa_dc_cache_notice', |
| 95 | array( |
| 96 | 'type' => Controls_Manager::RAW_HTML, |
| 97 | 'raw' => __( 'Please note that you need to <b>deactivate</b> the <a href="https://elementor.com/help/element-caching-help/" style="color: var(--e-a-color-warning);" target="_blank">Element Caching</a> feature to avoid any conflicts', 'premium-addons-for-elementor' ), |
| 98 | 'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning', |
| 99 | 'condition' => array( |
| 100 | 'pa_display_conditions_switcher' => 'yes', |
| 101 | ), |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | $controls_obj = new PA_Controls_Handler(); |
| 106 | |
| 107 | $options = PA_Controls_Handler::$conditions; |
| 108 | |
| 109 | $options['acf'] = array( |
| 110 | 'label' => __( 'ACF (PRO)', 'premium-addons-for-elementor' ), |
| 111 | 'options' => array( |
| 112 | 'acf_choice' => __( 'Choice', 'premium-addons-for-elementor' ), |
| 113 | 'acf_text' => __( 'Text', 'premium-addons-for-elementor' ), |
| 114 | 'acf_boolean' => __( 'True/False', 'premium-addons-for-elementor' ), |
| 115 | ), |
| 116 | ); |
| 117 | |
| 118 | $options['woocommerce'] = array( |
| 119 | 'label' => __( 'WooCommerce (PRO)', 'premium-addons-for-elementor' ), |
| 120 | 'options' => array( |
| 121 | 'woo_cat_page' => __( 'Current Category Page', 'premium-addons-for-elementor' ), |
| 122 | 'woo_product_cat' => __( 'Current Product Category', 'premium-addons-for-elementor' ), |
| 123 | 'woo_product_price' => __( 'Current Product Price', 'premium-addons-for-elementor' ), |
| 124 | 'woo_product_stock' => __( 'Current Product Stock', 'premium-addons-for-elementor' ), |
| 125 | 'woo_orders' => __( 'Purchased/In Cart Orders', 'premium-addons-for-elementor' ), |
| 126 | 'woo_category' => __( 'Purchased/In Cart Categories', 'premium-addons-for-elementor' ), |
| 127 | 'woo_last_purchase' => __( 'Last Purchase In Cart', 'premium-addons-for-elementor' ), |
| 128 | 'woo_total_price' => __( 'Amount In Cart', 'premium-addons-for-elementor' ), |
| 129 | 'woo_cart_products' => __( 'Products In Cart', 'premium-addons-for-elementor' ), |
| 130 | 'woo_purchase_products' => __( 'Purchased Products', 'premium-addons-for-elementor' ), |
| 131 | ), |
| 132 | ); |
| 133 | |
| 134 | $options = apply_filters( 'pa_display_conditions', $options ); |
| 135 | |
| 136 | $element->add_control( |
| 137 | 'pa_display_conditions_switcher', |
| 138 | array( |
| 139 | 'label' => __( 'Enable Display Conditions', 'premium-addons-for-elementor' ), |
| 140 | 'type' => Controls_Manager::SWITCHER, |
| 141 | 'return_value' => 'yes', |
| 142 | 'render_type' => 'template', |
| 143 | 'prefix_class' => 'pa-display-conditions-', |
| 144 | 'frontend_available' => true, |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | $element->add_control( |
| 149 | 'pa_display_action', |
| 150 | array( |
| 151 | 'label' => __( 'Action', 'premium-addons-for-elementor' ), |
| 152 | 'type' => Controls_Manager::SELECT, |
| 153 | 'default' => 'show', |
| 154 | 'options' => array( |
| 155 | 'show' => __( 'Show Element', 'premium-addons-for-elementor' ), |
| 156 | 'hide' => __( 'Hide Element', 'premium-addons-for-elementor' ), |
| 157 | ), |
| 158 | 'condition' => array( |
| 159 | 'pa_display_conditions_switcher' => 'yes', |
| 160 | ), |
| 161 | ) |
| 162 | ); |
| 163 | |
| 164 | $element->add_control( |
| 165 | 'pa_display_when', |
| 166 | array( |
| 167 | 'label' => __( 'Display When', 'premium-addons-for-elementor' ), |
| 168 | 'type' => Controls_Manager::SELECT, |
| 169 | 'default' => 'any', |
| 170 | 'options' => array( |
| 171 | 'any' => __( 'Any Condition is Met', 'premium-addons-for-elementor' ), |
| 172 | 'all' => __( 'All Conditions Are Met', 'premium-addons-for-elementor' ), |
| 173 | ), |
| 174 | 'condition' => array( |
| 175 | 'pa_display_conditions_switcher' => 'yes', |
| 176 | ), |
| 177 | ) |
| 178 | ); |
| 179 | |
| 180 | $repeater = new Repeater(); |
| 181 | |
| 182 | $repeater->add_control( |
| 183 | 'pa_condition_key', |
| 184 | array( |
| 185 | 'label' => __( 'Type', 'premium-addons-for-elementor' ), |
| 186 | 'type' => Controls_Manager::SELECT, |
| 187 | 'groups' => $options, |
| 188 | 'default' => 'browser', |
| 189 | 'label_block' => true, |
| 190 | ) |
| 191 | ); |
| 192 | |
| 193 | $options_conditions = apply_filters( |
| 194 | 'pa_pro_display_conditions', |
| 195 | array( |
| 196 | 'url_string', |
| 197 | 'url_referer', |
| 198 | 'shortcode', |
| 199 | 'woo_orders', |
| 200 | 'woo_cat_page', |
| 201 | 'woo_category', |
| 202 | 'woo_product_price', |
| 203 | 'woo_product_stock', |
| 204 | 'woo_product_cat', |
| 205 | 'woo_last_purchase', |
| 206 | 'woo_total_price', |
| 207 | 'woo_purchase_products', |
| 208 | 'woo_cart_products', |
| 209 | 'acf_choice', |
| 210 | 'acf_text', |
| 211 | 'acf_boolean', |
| 212 | ) |
| 213 | ); |
| 214 | |
| 215 | $get_pro = Helper_Functions::get_campaign_link( 'https://premiumaddons.com/pro', 'conditions-addon', 'wp-editor', 'get-pro' ); |
| 216 | |
| 217 | $repeater->add_control( |
| 218 | 'display_conditions_notice', |
| 219 | array( |
| 220 | 'type' => Controls_Manager::RAW_HTML, |
| 221 | 'raw' => __( 'This option is available in Premium Addons Pro. ', 'premium-addons-for-elementor' ) . '<a href="' . esc_url( $get_pro ) . '" target="_blank">' . __( 'Upgrade now!', 'premium-addons-for-elementor' ) . '</a>', |
| 222 | 'content_classes' => 'papro-upgrade-notice', |
| 223 | 'condition' => array( |
| 224 | 'pa_condition_key' => $options_conditions, |
| 225 | ), |
| 226 | ) |
| 227 | ); |
| 228 | |
| 229 | $controls_obj->add_repeater_source_controls( $repeater ); |
| 230 | |
| 231 | $repeater->add_control( |
| 232 | 'pa_condition_operator', |
| 233 | array( |
| 234 | 'type' => Controls_Manager::SELECT, |
| 235 | 'default' => 'is', |
| 236 | 'label_block' => true, |
| 237 | 'options' => array( |
| 238 | 'is' => __( 'Is', 'premium-addons-for-elementor' ), |
| 239 | 'not' => __( 'Is Not', 'premium-addons-for-elementor' ), |
| 240 | ), |
| 241 | 'condition' => array( |
| 242 | 'pa_condition_key!' => $options_conditions, |
| 243 | ), |
| 244 | ) |
| 245 | ); |
| 246 | |
| 247 | $controls_obj->add_repeater_compare_controls( $repeater ); |
| 248 | |
| 249 | $repeater->add_control( |
| 250 | 'pa_condition_timezone', |
| 251 | array( |
| 252 | 'label' => 'Timezone', |
| 253 | 'type' => Controls_Manager::SELECT, |
| 254 | 'default' => 'server', |
| 255 | 'label_block' => true, |
| 256 | 'options' => array( |
| 257 | 'local' => __( 'Local Time', 'premium-addons-for-elementor' ), |
| 258 | 'server' => __( 'Server Timezone', 'premium-addons-for-elementor' ), |
| 259 | ), |
| 260 | 'condition' => array( |
| 261 | 'pa_condition_key' => array( 'date_range', 'time_range', 'date', 'day' ), |
| 262 | ), |
| 263 | ) |
| 264 | ); |
| 265 | |
| 266 | $values = $repeater->get_controls(); |
| 267 | |
| 268 | $element->add_control( |
| 269 | 'pa_condition_repeater', |
| 270 | array( |
| 271 | 'label' => __( 'Conditions', 'premium-addons-for-elementor' ), |
| 272 | 'type' => Controls_Manager::REPEATER, |
| 273 | 'label_block' => true, |
| 274 | 'fields' => $values, |
| 275 | 'title_field' => '<# print( pa_condition_key.replace(/_/g, " ").split(" ").map((s) => s.charAt(0).toUpperCase() + s.substring(1)).join(" ")) #>', |
| 276 | 'prevent_empty' => false, |
| 277 | 'condition' => array( |
| 278 | 'pa_display_conditions_switcher' => 'yes', |
| 279 | ), |
| 280 | ) |
| 281 | ); |
| 282 | |
| 283 | $this->add_helpful_information( $element ); |
| 284 | |
| 285 | $element->end_controls_section(); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Add Help & Docs |
| 290 | * |
| 291 | * @since 4.9.39 |
| 292 | * @access private |
| 293 | * @param object $element for current element. |
| 294 | * |
| 295 | * @return void |
| 296 | */ |
| 297 | private function add_helpful_information( $element ) { |
| 298 | |
| 299 | $element->add_control( |
| 300 | 'pa_condition_info', |
| 301 | array( |
| 302 | 'label' => __( 'Help & Docs', 'premium-addons-for-elementor' ), |
| 303 | 'separator' => 'before', |
| 304 | 'type' => Controls_Manager::HEADING, |
| 305 | 'condition' => array( |
| 306 | 'pa_display_conditions_switcher' => 'yes', |
| 307 | ), |
| 308 | ) |
| 309 | ); |
| 310 | |
| 311 | $docs = array( |
| 312 | 'https://premiumaddons.com/docs/elementor-display-conditions-tutorial/' => __( 'Getting started »', 'premium-addons-for-elementor' ), |
| 313 | 'https://www.youtube.com/watch?v=ydv343MTf4w/' => __( 'Check the video tutorial »', 'premium-addons-for-elementor' ), |
| 314 | 'https://premiumaddons.com/docs/elementor-editor-not-loading-with-display-conditions/' => __( 'Fix editor not loading with Display Conditions enabled »', 'premium-addons-for-elementor' ), |
| 315 | 'https://premiumaddons.com/docs/how-to-show-hide-element-based-on-browser-elementor-display-conditions/' => __( 'Show/Hide Element Based on Browser »', 'premium-addons-for-elementor' ), |
| 316 | 'https://premiumaddons.com/docs/how-to-show-hide-element-on-specific-time-range-elementor-display-conditions/' => __( 'Show/Hide Element Based on Time Range »', 'premium-addons-for-elementor' ), |
| 317 | 'https://premiumaddons.com/docs/how-to-show-hide-element-with-location-elementor-display-conditions/' => __( 'Show/Hide Element Based on Location »', 'premium-addons-for-elementor' ), |
| 318 | ); |
| 319 | |
| 320 | $doc_index = 1; |
| 321 | foreach ( $docs as $url => $title ) { |
| 322 | |
| 323 | $doc_url = Helper_Functions::get_campaign_link( $url, 'conditions-addon', 'wp-editor', 'get-support' ); |
| 324 | |
| 325 | $element->add_control( |
| 326 | 'pa_condition_doc_' . $doc_index, |
| 327 | array( |
| 328 | 'type' => Controls_Manager::RAW_HTML, |
| 329 | 'raw' => sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( $doc_url ), esc_html( $title ) ), |
| 330 | 'content_classes' => 'editor-pa-doc', |
| 331 | 'condition' => array( |
| 332 | 'pa_display_conditions_switcher' => 'yes', |
| 333 | ), |
| 334 | ) |
| 335 | ); |
| 336 | |
| 337 | ++$doc_index; |
| 338 | |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Check Script Enqueue |
| 344 | * |
| 345 | * Check if the script files should be loaded. |
| 346 | * |
| 347 | * @since 4.9.21 |
| 348 | * @access public |
| 349 | * |
| 350 | * @param object $element for current element. |
| 351 | */ |
| 352 | public function check_script_enqueue( $element ) { |
| 353 | |
| 354 | if ( self::$load_script ) { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | $settings = $element->get_active_settings(); |
| 359 | |
| 360 | if ( ! empty( $settings['pa_display_conditions_switcher'] ) ) { |
| 361 | |
| 362 | $this->enqueue_scripts(); |
| 363 | |
| 364 | self::$load_script = true; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Returns an instance of this class. |
| 370 | * |
| 371 | * @access public |
| 372 | */ |
| 373 | public static function get_instance() { |
| 374 | |
| 375 | if ( ! isset( self::$instance ) ) { |
| 376 | self::$instance = new self(); |
| 377 | } |
| 378 | |
| 379 | return self::$instance; |
| 380 | } |
| 381 | } |
| 382 |