PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.86
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.86
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / extensions / Popup_Builder / Popup_Builder.php

Popup_Builder.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.86, at includes/extensions/Popup_Builder/Popup_Builder.php

1,093 lines 57.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace King_Addons;
4
5 use Elementor\Plugin;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 final class Popup_Builder
12 {
13 private static ?Popup_Builder $instance = null;
14 private static ?Plugin $elementor_instance;
15
16 public static function instance(): ?Popup_Builder
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('init', [$this, 'register_templates_library_cpt']);
27 add_action('current_screen', [$this, 'redirect_to_options_page']);
28
29 add_action('elementor/documents/register', [$this, 'register_elementor_document_type']);
30
31 // Frontend-only hooks
32 if (!is_admin()) {
33 add_action('template_redirect', [$this, 'block_template_frontend']);
34 add_action('wp_enqueue_scripts', [$this, 'enqueue_styles'], 998);
35 // Enqueue frontend JS even on non-Elementor pages (when popups are configured).
36 add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts'], 998);
37 add_action('template_include', [$this, 'set_post_type_template'], 9999);
38 add_action('wp_footer', [$this, 'render_popups']);
39 }
40
41 add_action('elementor/editor/before_enqueue_scripts', [$this, 'enqueueScriptPreviewHandler'], 988);
42
43 self::$elementor_instance = Plugin::instance();
44
45 add_action('wp_ajax_king_addons_pb_save_template_conditions', [$this, 'king_addons_pb_save_template_conditions']);
46 add_action('wp_ajax_king_addons_pb_create_template', [$this, 'king_addons_pb_create_template']);
47 add_action('wp_ajax_king_addons_pb_delete_template', [$this, 'king_addons_pb_delete_template']);
48
49 }
50
51 public function enqueueScriptPreviewHandler(): void
52 {
53 wp_enqueue_script('king-addons-popup-builder-popup-preview-script', KING_ADDONS_URL . 'includes/extensions/Popup_Builder/preview-handler.js', array('jquery'), KING_ADDONS_VERSION, true);
54 }
55
56
57 public function register_elementor_document_type($documents_manager): void
58 {
59 require_once(KING_ADDONS_PATH . 'includes/extensions/Popup_Builder/Popup_Module.php');
60
61 if (king_addons_freemius()->can_use_premium_code__premium_only() && defined('KING_ADDONS_PRO_PATH')) {
62 require_once(KING_ADDONS_PRO_PATH . 'includes/extensions/Popup_Builder_Pro/Popup_Module_Pro.php');
63 $documents_manager->register_document_type('king-addons-pb-popups', 'King_Addons\King_Addons_Popup_Module_Pro');
64 } else {
65 $documents_manager->register_document_type('king-addons-pb-popups', 'King_Addons\King_Addons_Popup_Module');
66 }
67 }
68
69 public function enqueue_styles(): void
70 {
71 wp_enqueue_style('king-addons-popup-builder-popup-module-style', KING_ADDONS_URL . 'includes/extensions/Popup_Builder/popup-module.css', '', KING_ADDONS_VERSION);
72 }
73
74 public function enqueue_scripts(): void
75 {
76 // Avoid loading the popup runtime if there are no configured conditions.
77 $raw_conditions = get_option('king_addons_pb_popup_conditions');
78 if (empty($raw_conditions) || $raw_conditions === '[]') {
79 return;
80 }
81
82 $deps = ['jquery'];
83 if (wp_script_is('elementor-frontend', 'registered')) {
84 $deps[] = 'elementor-frontend';
85 }
86
87 wp_enqueue_script(
88 'king-addons-popup-builder-popup-module-script',
89 KING_ADDONS_URL . 'includes/extensions/Popup_Builder/popup-module.js',
90 $deps,
91 KING_ADDONS_VERSION,
92 true
93 );
94 }
95
96 public function render_popups(): void
97 {
98 $conditions = json_decode(get_option('king_addons_pb_popup_conditions'), true);
99
100 if (!empty($conditions)) {
101 $conditions = self::reverse_template_conditions($conditions);
102
103 if (isset($conditions['global'])) {
104 self::display_popups_by_location($conditions, 'global');
105 }
106
107 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
108 self::archive_pages_popup_conditions($conditions);
109 self::single_pages_popup_conditions($conditions);
110 }
111
112 if (!wp_script_is(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'perfectscrollbar' . '-' . 'perfectscrollbar')) {
113 wp_enqueue_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'perfectscrollbar' . '-' . 'perfectscrollbar', '', '', KING_ADDONS_VERSION);
114 }
115 }
116 }
117
118 public static function display_popups_by_location($conditions, $page): void
119 {
120 foreach ($conditions[$page] as $popup) {
121 self::render_popup_content($popup);
122 }
123 }
124
125 public static function render_popup_content($slug): void
126 {
127
128 $template_id = self::get_template_id($slug);
129
130 // Check if WPML is active. Find WPML Object ID for current page.
131 /** @noinspection SpellCheckingInspection */
132 if (defined('ICL_SITEPRESS_VERSION')) {
133 $default_lang = apply_filters('wpml_default_language', '');
134 $current_lang = apply_filters('wpml_current_language', '');
135
136 if ($default_lang !== $current_lang) {
137 $template_id = apply_filters('wpml_object_id', $template_id, 'king_addons_ext_pb', true, $current_lang);
138 }
139 }
140
141 $get_settings = self::get_template_settings($slug);
142 $get_elementor_content = self::$elementor_instance->frontend->get_builder_content($template_id);
143
144 if ('' === $get_elementor_content) {
145 return;
146 }
147
148 $get_encoded_settings = !empty($get_settings) ? wp_json_encode($get_settings) : '[]';
149 $template_settings_attr = "data-settings='" . esc_attr($get_encoded_settings) . "'";
150
151 if (!self::check_available_user_roles($get_settings['popup_show_for_roles'])) {
152 return;
153 }
154
155 if (!self::$elementor_instance->preview->is_preview_mode()) {
156 echo '<div id="king-addons-pb-popup-id-' . esc_attr($template_id) . '" class="king-addons-pb-template-popup" ' . $template_settings_attr . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
157 echo '<div class="king-addons-pb-template-popup-inner">';
158 echo '<div class="king-addons-pb-popup-overlay"></div>';
159 echo '<div class="king-addons-pb-popup-container">';
160
161 if (Plugin::$instance->experiments->is_feature_active('e_font_icon_svg')) {
162 echo '<div class="king-addons-pb-popup-close-btn"><i class="fa fa-times"></i></div>';
163 } else {
164 echo '<div class="king-addons-pb-popup-close-btn"><i class="eicon-close"></i></div>';
165 }
166
167 echo '<div class="king-addons-pb-popup-container-inner">';
168 echo $get_elementor_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
169 echo '</div>';
170 echo '</div>';
171 echo '</div>';
172 echo '</div>';
173 }
174 }
175
176 public static function get_template_settings($slug): array
177 {
178 $settings = [];
179 $defaults = [];
180
181 $template_id = self::get_template_id($slug);
182 /** @noinspection DuplicatedCode */
183 $meta_settings = get_post_meta($template_id, '_elementor_page_settings', true);
184
185 $popup_defaults = [
186 'popup_trigger' => 'load',
187 'popup_load_delay' => 1,
188 'popup_scroll_progress' => 10,
189 'popup_inactivity_time' => 15,
190 'popup_element_scroll' => '',
191 'popup_custom_trigger' => '',
192 'popup_specific_date' => date('Y-m-d H:i', strtotime('+1 month') + (get_option('gmt_offset') * HOUR_IN_SECONDS)),
193 'popup_stop_after_date' => false,
194 'popup_stop_after_date_select' => date('Y-m-d H:i', strtotime('+1 day') + (get_option('gmt_offset') * HOUR_IN_SECONDS)),
195 'popup_show_again_delay' => 1,
196 'popup_disable_esc_key' => false,
197 'popup_automatic_close_switch' => false,
198 'popup_automatic_close_delay' => 10,
199 'popup_animation' => 'fade',
200 'popup_animation_duration' => 1,
201 'popup_show_for_roles' => '',
202 'popup_show_via_referral' => false,
203 'popup_referral_keyword' => '',
204 'popup_display_as' => 'modal',
205 'popup_show_on_device' => true,
206 'popup_show_on_device_mobile' => true,
207 'popup_show_on_device_tablet' => true,
208 'popup_disable_page_scroll' => true,
209 'popup_overlay_disable_close' => false,
210 'popup_close_button_display_delay' => 0,
211 ];
212
213 if (strpos($slug, 'popup')) {
214 $defaults = $popup_defaults;
215 }
216
217 foreach ($defaults as $option => $value) {
218 if (isset($meta_settings[$option])) {
219 $settings[$option] = $meta_settings[$option];
220 }
221 }
222
223 return array_merge($defaults, $settings);
224 }
225
226 public static function check_available_user_roles($selected_roles): bool
227 {
228 if (empty($selected_roles)) {
229 return true;
230 }
231
232 $current_user = wp_get_current_user();
233
234 if (!empty($current_user->roles)) {
235 $role = $current_user->roles[0];
236 } else {
237 $role = 'guest';
238 }
239
240 if (in_array($role, $selected_roles)) {
241 return true;
242 }
243
244 return false;
245 }
246
247 public function reverse_template_conditions($conditions): array
248 {
249 $reverse = [];
250
251 foreach ($conditions as $key => $condition) {
252 foreach ($condition as $location) {
253 if (!isset($reverse[$location])) {
254 $reverse[$location] = [$key];
255 } else {
256 $reverse[$location][] = $key;
257 }
258 }
259 }
260
261 return $reverse;
262 }
263
264 public function set_post_type_template($template)
265 {
266 if (is_singular('king_addons_ext_pb')) {
267 if ('king-addons-pb-popups' === self::get_elementor_template_type(get_the_ID()) && self::$elementor_instance->preview->is_preview_mode()) {
268 $template = KING_ADDONS_PATH . 'includes/extensions/Popup_Builder/preview.php';
269 }
270 return $template;
271 }
272 return $template;
273 }
274
275 public function block_template_frontend(): void
276 {
277 if (is_singular('king_addons_ext_pb') && !current_user_can('edit_posts')) {
278 wp_redirect(site_url(), 301);
279 die;
280 }
281 }
282
283 public static function get_elementor_template_type($id)
284 {
285 $post_meta = get_post_meta($id);
286 return $post_meta['_elementor_template_type'][0] ?? false;
287 }
288
289 public function redirect_to_options_page(): void
290 {
291 if (get_current_screen()->post_type == 'king_addons_ext_pb' && isset($_GET['action']) && $_GET['action'] == 'edit') {
292 wp_redirect('admin.php?page=king-addons-popup-builder');
293 }
294 }
295
296 public function register_templates_library_cpt(): void
297 {
298
299 $args = array(
300 'label' => esc_html__('King Addons Templates', 'king-addons'),
301 'public' => true,
302 'rewrite' => false,
303 'show_ui' => true,
304 'show_in_menu' => false,
305 'show_in_nav_menus' => false,
306 'exclude_from_search' => true,
307 'capability_type' => 'post',
308 'supports' => ['title', 'elementor'],
309 'hierarchical' => false,
310 );
311
312 register_post_type('king_addons_ext_pb', $args);
313
314 $tax_args = [
315 'hierarchical' => true,
316 'show_ui' => true,
317 'show_in_nav_menus' => false,
318 'show_admin_column' => true,
319 'query_var' => is_admin(),
320 'rewrite' => false,
321 'public' => false,
322 ];
323
324 register_taxonomy('king_addons_pb_template_type', 'king_addons_ext_pb', $tax_args);
325
326 }
327
328 public function king_addons_pb_delete_template(): void
329 {
330 /** @noinspection DuplicatedCode */
331 $nonce = $_POST['nonce'];
332
333 if (!wp_verify_nonce($nonce, 'delete_post-' . $_POST['template_slug']) || !current_user_can('manage_options')) {
334 exit;
335 }
336
337 $template_slug = isset($_POST['template_slug']) ? sanitize_text_field(wp_unslash($_POST['template_slug'])) : '';
338 $template_library = isset($_POST['template_library']) ? sanitize_text_field(wp_unslash($_POST['template_library'])) : '';
339
340 $post = get_page_by_path($template_slug, OBJECT, $template_library);
341
342 if (get_post_type($post->ID) == 'king_addons_ext_pb' || get_post_type($post->ID) == 'elementor_library') {
343 wp_delete_post($post->ID, true);
344 }
345 }
346
347 public function king_addons_pb_save_template_conditions(): void
348 {
349
350 $nonce = $_POST['nonce'];
351
352 if (!wp_verify_nonce($nonce, 'king-addons-popup-builder-admin-script') || !current_user_can('manage_options')) {
353 exit;
354 }
355
356 if (isset($_POST['king_addons_pb_popup_conditions']) && isset($_POST['king_addons_popup_nonce'])) {
357 if (wp_verify_nonce($_POST['king_addons_popup_nonce'], 'king_addons_popup_settings')) {
358 update_option('king_addons_pb_popup_conditions', $this->sanitize_conditions($_POST['king_addons_pb_popup_conditions']));
359
360 // Persist "show on canvas" toggle per-template (used by the admin UI).
361 if (isset($_POST['template']) && isset($_POST['king_addons_pb_popup_show_on_canvas'])) {
362 $template_slug = sanitize_text_field(wp_unslash($_POST['template']));
363 $template_id = self::get_template_id($template_slug);
364 if ($template_id) {
365 $show_on_canvas = filter_var(wp_unslash($_POST['king_addons_pb_popup_show_on_canvas']), FILTER_VALIDATE_BOOLEAN);
366 update_post_meta($template_id, 'king_addons_pb_popup_show_on_canvas', $show_on_canvas ? 'true' : 'false');
367 }
368 }
369 }
370 }
371 }
372
373 public function sanitize_conditions($data)
374 {
375 $decoded = json_decode(wp_unslash($data), true);
376 if (!is_array($decoded)) {
377 return '[]';
378 }
379
380 // Remove empty entries (templates with no locations).
381 $decoded = array_filter($decoded, static function ($value) {
382 return !empty($value);
383 });
384
385 return wp_json_encode($decoded);
386 }
387
388 public function king_addons_pb_create_template(): void
389 {
390 $nonce = $_POST['nonce'];
391
392 if (!wp_verify_nonce($nonce, 'king-addons-popup-builder-admin-script') || !current_user_can('manage_options')) {
393 exit;
394 }
395
396 /** @noinspection DuplicatedCode */
397 $user_template_type = isset($_POST['user_template_type']) ? sanitize_text_field(wp_unslash($_POST['user_template_type'])) : false;
398 $user_template_library = isset($_POST['user_template_library']) ? sanitize_text_field(wp_unslash($_POST['user_template_library'])) : false;
399 $user_template_title = isset($_POST['user_template_title']) ? sanitize_text_field(wp_unslash($_POST['user_template_title'])) : false;
400 $user_template_slug = isset($_POST['user_template_slug']) ? sanitize_text_field(wp_unslash($_POST['user_template_slug'])) : false;
401
402 $check_post_type = ($user_template_library == 'king_addons_ext_pb' || $user_template_library == 'elementor_library');
403
404 if ($user_template_title && $check_post_type) {
405
406 $template_id = wp_insert_post(array(
407 'post_type' => $user_template_library,
408 'post_title' => $user_template_title,
409 'post_name' => $user_template_slug,
410 'post_content' => '',
411 'post_status' => 'publish'
412 ));
413
414 if ('king_addons_ext_pb' === $_POST['user_template_library']) {
415
416 wp_set_object_terms($template_id, [$user_template_type, 'user'], 'king_addons_pb_template_type');
417
418 if ('popup' === $_POST['user_template_type']) {
419 update_post_meta($template_id, '_elementor_template_type', 'king-addons-pb-popups');
420 } else {
421 update_post_meta($template_id, '_elementor_template_type', 'king-addons-pb-theme-builder');
422 update_post_meta($template_id, '_king_addons_pb_template_type', $user_template_type);
423 }
424 } else {
425 update_post_meta($template_id, '_elementor_template_type', 'page');
426 }
427
428 update_post_meta($template_id, '_wp_page_template', 'elementor_canvas');
429
430 echo esc_html($template_id);
431 }
432 }
433
434 public static function renderPopupBuilder(): void
435 {
436 $screen = get_current_screen();
437 if ($screen->id === 'toplevel_page_king-addons-popup-builder') {
438 wp_enqueue_style('king-addons-popup-builder-admin-style', KING_ADDONS_URL . 'includes/extensions/Popup_Builder/admin.css', '', KING_ADDONS_VERSION);
439 wp_enqueue_script('jquery');
440 wp_enqueue_script('king-addons-popup-builder-admin-script', KING_ADDONS_URL . 'includes/extensions/Popup_Builder/admin.js', array('jquery'), KING_ADDONS_VERSION);
441 wp_localize_script(
442 'king-addons-popup-builder-admin-script',
443 'KingAddonsPopupBuilderOptions',
444 [
445 'nonce' => wp_create_nonce('king-addons-popup-builder-admin-script'),
446 ]
447 );
448 }
449 ?>
450 <div class="wrap king-addons-pb-settings-page-wrap">
451 <div class="king-addons-pb-settings-page-header">
452 <h1><?php esc_html_e('Elementor Popup Builder', 'king-addons'); ?></h1>
453 <p><?php esc_html_e('Design and customize eye-catching popups, ideal for creating promotional messages, announcements, or subscription forms that capture visitor attention', 'king-addons'); ?></p>
454 <div class="king-addons-pb-preview-buttons">
455 <div class="king-addons-pb-user-template">
456 <span><?php esc_html_e('Create Popup', 'king-addons'); ?></span>
457 <span class="plus-icon">+</span>
458 </div>
459 <?php if (!king_addons_freemius()->can_use_premium_code__premium_only()): ?>
460 <div class="kng-promo-btn-wrap">
461 <a href="https://kingaddons.com/pricing/?utm_source=king-addons-popup-builder" target="_blank">
462 <div class="kng-promo-btn-txt">
463 <?php esc_html_e('Unlock Premium Features & 650+ Templates Today!', 'king-addons'); ?>
464 </div>
465 <img width="16px"
466 src="<?php echo esc_url(KING_ADDONS_URL) . 'includes/admin/img/share-v2.svg'; ?>"
467 alt="<?php echo esc_html__('Open link in the new tab', 'king-addons'); ?>">
468 </a>
469 </div>
470 <?php endif; ?>
471 </div>
472 </div>
473 <div class="king-addons-pb-settings-page">
474 <!--suppress HtmlUnknownTarget -->
475 <form method="post" action="options.php">
476 <?php wp_nonce_field('king_addons_popup_settings', 'king_addons_popup_nonce'); ?>
477 <input type="hidden" name="king_addons_pb_template" id="king_addons_pb_template" value="">
478 <?php self::render_conditions_popup(); ?>
479 <?php self::render_create_template_popup(); ?>
480 <input type="hidden" name="king_addons_pb_popup_conditions" id="king_addons_pb_popup_conditions"
481 value="<?php echo esc_attr(get_option('king_addons_pb_popup_conditions', '[]')); ?>">
482 <?php self::render_theme_builder_templates('popup'); ?>
483 </form>
484 </div>
485 </div>
486 <?php
487 }
488
489 public static function render_conditions_popup(): void
490 {
491 $active_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'king_addons_pb_tab_header';
492 ?>
493 <div class="king-addons-pb-condition-popup-wrap king-addons-pb-admin-popup-wrap">
494 <div class="king-addons-pb-condition-popup king-addons-pb-admin-popup">
495 <header>
496 <h2><?php esc_html_e('Where would you like the popup to be displayed?', 'king-addons'); ?></h2>
497 <p>
498 <?php esc_html_e('Define the conditions that specify where the popup will be applied across your site. ', 'king-addons'); ?>
499 <br>
500 <?php esc_html_e('For instance, select Entire Site to display the template on all pages.', 'king-addons');
501
502 if (!king_addons_freemius()->can_use_premium_code__premium_only()) {
503 echo '<span><br>';
504 echo esc_html__('All conditions are available in the ', 'king-addons');
505 echo '<strong><a href="https://kingaddons.com/pricing/?utm_source=kng-module-popup-builder-change-conditions-window" target="_blank">';
506 echo esc_html__('Pro version.', 'king-addons');
507 echo '</a></strong></span>';
508 }
509 ?>
510 </p>
511 </header>
512 <span class="close-popup dashicons dashicons-no-alt"></span>
513 <div class="king-addons-pb-conditions-wrap">
514 <div class="king-addons-pb-conditions-sample">
515 <?php if (king_addons_freemius()->can_use_premium_code__premium_only()) : ?>
516
517 <!--suppress HtmlFormInputWithoutLabel -->
518 <select name="global_condition_select" class="global-condition-select">
519 <option value="global"><?php esc_html_e('Entire Site', 'king-addons'); ?></option>
520 <option value="archive"><?php esc_html_e('Archives', 'king-addons'); ?></option>
521 <option value="single"><?php esc_html_e('Singular', 'king-addons'); ?></option>
522 </select>
523
524 <!--suppress HtmlFormInputWithoutLabel -->
525 <select name="archives_condition_select" class="archives-condition-select">
526 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
527 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
528 <option value="all_archives"><?php esc_html_e('All Archives', 'king-addons'); ?></option>
529 <option value="posts"><?php esc_html_e('Posts Archive', 'king-addons'); ?></option>
530 <?php
531 $custom_post_types = self::get_custom_types_of('post');
532 foreach ($custom_post_types as $key => $value) {
533 if ('e-landing-page' === $key) {
534 continue;
535 }
536
537 if (king_addons_freemius()->can_use_premium_code__premium_only() || 'product' === $key) {
538 echo '<option value="' . esc_attr($key) . '">' . $value . ' ' . esc_html__('Archive', 'king-addons') . '</option>';
539 } else {
540 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '">' . $value . ' ' . esc_html__('Archive (PRO)', 'king-addons') . '</option>';
541 }
542 }
543 ?>
544 <option value="author"><?php esc_html_e('Author Archive', 'king-addons'); ?></option>
545 <option value="date"><?php esc_html_e('Date Archive', 'king-addons'); ?></option>
546 <option value="search"><?php esc_html_e('Search Results', 'king-addons'); ?></option>
547 </optgroup>
548
549 <optgroup label="<?php esc_html_e('Taxonomy Archives', 'king-addons'); ?>">
550 <option value="categories"
551 class="custom-ids"><?php esc_html_e('Post Categories', 'king-addons'); ?></option>
552 <option value="tags"
553 class="custom-ids"><?php esc_html_e('Post Tags', 'king-addons'); ?></option>
554 <?php
555 $custom_taxonomies = self::get_custom_types_of('tax');
556 foreach ($custom_taxonomies as $key => $value) {
557 if (king_addons_freemius()->can_use_premium_code__premium_only() || 'product_cat' === $key || 'product_tag' === $key) {
558 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
559 } else {
560 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
561 }
562 }
563 ?>
564 </optgroup>
565 <?php else: ?>
566 <?php if ('king_addons_pb_tab_archive' === $active_tab) : ?>
567 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
568 <option value="all_archives"><?php esc_html_e('All Archives', 'king-addons'); ?></option>
569 <option value="posts"><?php esc_html_e('Posts Archive', 'king-addons'); ?></option>
570 <?php
571 $custom_post_types = self::get_custom_types_of('post');
572 foreach ($custom_post_types as $key => $value) {
573 if ('product' === $key || 'e-landing-page' === $key) {
574 continue;
575 }
576
577 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
578 echo '<option value="' . esc_attr($key) . '">' . $value . ' ' . esc_html__('Archive', 'king-addons') . '</option>';
579 } else {
580 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '">' . $value . ' ' . esc_html__('Archive (PRO)', 'king-addons') . '</option>';
581 }
582 }
583 ?>
584 <option value="author"><?php esc_html_e('Author Archive', 'king-addons'); ?></option>
585 <option value="date"><?php esc_html_e('Date Archive', 'king-addons'); ?></option>
586 <option value="search"><?php esc_html_e('Search Results', 'king-addons'); ?></option>
587 </optgroup>
588
589 <optgroup label="<?php esc_html_e('Taxonomy Archives', 'king-addons'); ?>">
590 <option value="categories"
591 class="custom-ids"><?php esc_html_e('Post Categories', 'king-addons'); ?></option>
592 <option value="tags"
593 class="custom-ids"><?php esc_html_e('Post Tags', 'king-addons'); ?></option>
594 <?php
595 $custom_taxonomies = self::get_custom_types_of('tax');
596 foreach ($custom_taxonomies as $key => $value) {
597 if ('product_cat' === $key || 'product_tag' === $key) {
598 continue;
599 }
600
601 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
602 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
603 } else {
604 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
605 }
606 }
607 ?>
608 </optgroup>
609 <?php elseif ('king_addons_pb_tab_product_archive' === $active_tab): ?>
610 <option value="products"><?php esc_html_e('Products Archive', 'king-addons'); ?></option>
611 <option value="product_cat"
612 class="custom-type-ids"><?php esc_html_e('Products Categories', 'king-addons'); ?></option>
613 <option value="product_tag"
614 class="custom-type-ids"><?php esc_html_e('Products Tags', 'king-addons'); ?></option>
615 <option value="product_search"><?php esc_html_e('Products Search', 'king-addons'); ?></option>
616 <?php endif; ?>
617 <?php endif; ?>
618 </select>
619
620 <!--suppress HtmlFormInputWithoutLabel -->
621 <select name="singles_condition_select" class="singles-condition-select">
622 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
623 <option value="front_page"><?php esc_html_e('Front Page', 'king-addons'); ?></option>
624 <option value="page_404"><?php esc_html_e('404 Page', 'king-addons'); ?></option>
625 <option value="pages"
626 class="custom-ids"><?php esc_html_e('Pages', 'king-addons'); ?></option>
627 <option value="posts"
628 class="custom-ids"><?php esc_html_e('Posts', 'king-addons'); ?></option>
629 <?php
630 $custom_post_types = self::get_custom_types_of('post');
631 foreach ($custom_post_types as $key => $value) {
632 if ('e-landing-page' === $key) {
633 continue;
634 }
635
636 if (king_addons_freemius()->can_use_premium_code__premium_only() || 'product' === $key) {
637 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
638 } else {
639 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
640 }
641 }
642 ?>
643 <?php else: ?>
644 <?php if ('king_addons_pb_tab_single' === $active_tab) : ?>
645 <option value="front_page"><?php esc_html_e('Front Page', 'king-addons'); ?></option>
646 <option value="page_404"><?php esc_html_e('404 Page', 'king-addons'); ?></option>
647 <option value="pages"
648 class="custom-ids"><?php esc_html_e('Pages', 'king-addons'); ?></option>
649 <option value="posts"
650 class="custom-ids"><?php esc_html_e('Posts', 'king-addons'); ?></option>
651
652 <?php
653 $custom_post_types = self::get_custom_types_of('post');
654 foreach ($custom_post_types as $key => $value) {
655 if ('product' === $key || 'e-landing-page' === $key) {
656 continue;
657 }
658
659 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
660 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
661 } else {
662 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
663 }
664 }
665 ?>
666 <?php elseif ('king_addons_pb_tab_product_single' === $active_tab): ?>
667 <option value="product"
668 class="custom-product-ids custom-type-ids"><?php esc_html_e('Products', 'king-addons'); ?></option>
669 <?php endif; ?>
670 <?php endif; ?>
671 </select>
672
673 <!--suppress HtmlFormInputWithoutLabel -->
674 <input type="text"
675 placeholder="<?php esc_html_e('Enter comma separated IDs', 'king-addons'); ?>"
676 name="condition_input_ids" class="king-addons-pb-condition-input-ids">
677 <span class="king-addons-pb-delete-template-conditions dashicons dashicons-no-alt"></span>
678
679 <?php else: ?>
680 <!--suppress HtmlFormInputWithoutLabel -->
681 <select name="global_condition_select" class="global-condition-select">
682 <option value="global"><?php esc_html_e('Entire Site', 'king-addons'); ?></option>
683 <option value="archive"><?php esc_html_e('Archives (Pro)', 'king-addons'); ?></option>
684 <option value="single"><?php esc_html_e('Singular (Pro)', 'king-addons'); ?></option>
685 </select>
686
687 <!--suppress HtmlFormInputWithoutLabel -->
688 <select name="archives_condition_select" class="archives-condition-select">
689 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
690 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
691 <option value="all_archives"><?php esc_html_e('All Archives (Pro)', 'king-addons'); ?></option>
692 <option value="posts"><?php esc_html_e('Posts Archive (Pro)', 'king-addons'); ?></option>
693 <option value="author"><?php esc_html_e('Author Archive (Pro)', 'king-addons'); ?></option>
694 <option value="date"><?php esc_html_e('Date Archive (Pro)', 'king-addons'); ?></option>
695 <option value="search"><?php esc_html_e('Search Results (Pro)', 'king-addons'); ?></option>
696 <option value="categories"
697 class="custom-ids"><?php esc_html_e('Post Categories (Pro)', 'king-addons'); ?></option>
698 <option value="tags"
699 class="custom-ids"><?php esc_html_e('Post Tags (Pro)', 'king-addons'); ?></option>
700 </optgroup>
701 <optgroup label="<?php esc_html_e('WooCommerce Archives', 'king-addons'); ?>">
702 <option value="products"
703 class="custom-ids"><?php esc_html_e('Products Archive (Pro)', 'king-addons'); ?></option>
704 <option value="products_cats"
705 class="custom-ids"><?php esc_html_e('Product Categories (Pro)', 'king-addons'); ?></option>
706 <option value="product_tags"
707 class="custom-ids"><?php esc_html_e('Product Tags (Pro)', 'king-addons'); ?></option>
708 </optgroup>
709 <optgroup label="<?php esc_html_e('Custom Post Type Archives', 'king-addons'); ?>">
710 <?php
711 $custom_post_types = self::get_custom_types_of('post');
712 foreach ($custom_post_types as $key => $value) {
713 if ('product' === $key || 'e-landing-page' === $key) {
714 continue;
715 }
716
717 echo '<option value="' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
718 }
719
720 $custom_taxonomies = self::get_custom_types_of('tax');
721 foreach ($custom_taxonomies as $key => $value) {
722 if ('product_cat' === $key || 'product_tag' === $key) {
723 continue;
724 }
725 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
726 }
727 ?>
728 </optgroup>
729 <?php else: ?>
730 <?php if ('king_addons_pb_tab_archive' === $active_tab) : ?>
731 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
732 <option value="all_archives"><?php esc_html_e('All Archives', 'king-addons'); ?></option>
733 <option value="posts"><?php esc_html_e('Posts Archive', 'king-addons'); ?></option>
734 <option value="author"><?php esc_html_e('Author Archive', 'king-addons'); ?></option>
735 <option value="date"><?php esc_html_e('Date Archive', 'king-addons'); ?></option>
736 <option value="search"><?php esc_html_e('Search Results', 'king-addons'); ?></option>
737 <option value="categories"
738 class="custom-ids"><?php esc_html_e('Post Categories', 'king-addons'); ?></option>
739 <option value="tags"
740 class="custom-ids"><?php esc_html_e('Post Tags', 'king-addons'); ?></option>
741 </optgroup>
742 <optgroup
743 label="<?php esc_html_e('Custom Post Type Archives', 'king-addons'); ?>">
744 <?php
745 $custom_post_types = self::get_custom_types_of('post');
746 foreach ($custom_post_types as $key => $value) {
747 if ('product' === $key || 'e-landing-page' === $key) {
748 continue;
749 }
750
751 echo '<option value="' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
752 }
753 ?>
754 <?php
755 $custom_taxonomies = self::get_custom_types_of('tax');
756 foreach ($custom_taxonomies as $key => $value) {
757 if ('product_cat' === $key || 'product_tag' === $key) {
758 continue;
759 }
760
761 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
762 }
763 ?>
764 </optgroup>
765 <?php elseif ('king_addons_pb_tab_product_archive' === $active_tab): ?>
766 <option value="products"><?php esc_html_e('Products Archive', 'king-addons'); ?></option>
767 <option value="product_cat"
768 class="custom-type-ids"><?php esc_html_e('Products Categories (Pro)', 'king-addons'); ?></option>
769 <option value="product_tag"
770 class="custom-type-ids"><?php esc_html_e('Products Tags (Pro)', 'king-addons'); ?></option>
771 <option value="product_search"><?php esc_html_e('Products Search (Pro)', 'king-addons'); ?></option>
772 <?php endif; ?>
773 <?php endif; ?>
774 </select>
775
776 <!--suppress HtmlFormInputWithoutLabel -->
777 <select name="singles_condition_select" class="singles-condition-select">
778 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
779 <option value="front_page"><?php esc_html_e('Front Page (Pro)', 'king-addons'); ?></option>
780 <option value="page_404"><?php esc_html_e('404 Page (Pro)', 'king-addons'); ?></option>
781 <option value="pages"
782 class="custom-ids"><?php esc_html_e('Pages (Pro)', 'king-addons'); ?></option>
783 <option value="posts"
784 class="custom-ids"><?php esc_html_e('Posts (Pro)', 'king-addons'); ?></option>
785 <option value="product"
786 class="custom-ids"><?php esc_html_e('Product (Pro)', 'king-addons'); ?></option>
787 <?php
788 $custom_post_types = self::get_custom_types_of('post');
789 foreach ($custom_post_types as $key => $value) {
790 if ('product' === $key || 'e-landing-page' === $key) {
791 continue;
792 }
793
794 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
795 }
796 ?>
797 <?php else: ?>
798 <?php if ('king_addons_pb_tab_single' === $active_tab) : ?>
799 <option value="front_page"><?php esc_html_e('Front Page', 'king-addons'); ?></option>
800 <option value="page_404"><?php esc_html_e('404 Page', 'king-addons'); ?></option>
801 <option value="pages"
802 class="custom-ids"><?php esc_html_e('Pages', 'king-addons'); ?></option>
803 <option value="posts"
804 class="custom-ids"><?php esc_html_e('Posts', 'king-addons'); ?></option>
805
806 <?php
807 $custom_post_types = self::get_custom_types_of('post');
808 foreach ($custom_post_types as $key => $value) {
809 if ('product' === $key || 'e-landing-page' === $key) {
810 continue;
811 }
812
813 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
814 }
815 ?>
816 <?php elseif ('king_addons_pb_tab_product_single' === $active_tab): ?>
817 <option value="product"
818 class="custom-type-ids"><?php esc_html_e('Products', 'king-addons'); ?></option>
819 <?php endif; ?>
820 <?php endif; ?>
821 </select>
822
823 <!--suppress HtmlFormInputWithoutLabel -->
824 <input type="text"
825 placeholder="<?php esc_html_e('Enter comma separated IDs (Pro)', 'king-addons'); ?>"
826 name="condition_input_ids" class="king-addons-pb-condition-input-ids">
827 <span class="king-addons-pb-delete-template-conditions dashicons dashicons-no-alt"></span>
828
829 <?php endif; ?>
830 </div>
831 </div>
832 <span class="king-addons-pb-add-conditions"><?php esc_html_e('+ Add Conditions', 'king-addons'); ?></span>
833 <span class="king-addons-pb-save-conditions"><?php esc_html_e('Save Conditions', 'king-addons'); ?></span>
834 </div>
835 </div>
836
837 <?php
838 }
839
840 public static function render_create_template_popup(): void
841 {
842 ?>
843 <div class="king-addons-pb-user-template-popup-wrap king-addons-pb-admin-popup-wrap">
844 <div class="king-addons-pb-user-template-popup king-addons-pb-admin-popup">
845 <header>
846 <h2><?php esc_html_e('Creating Popup', 'king-addons'); ?></h2>
847 <p><?php esc_html_e('Design and customize eye-catching popups, ideal for creating promotional messages, announcements, or subscription forms that capture visitor attention', 'king-addons'); ?></p>
848 </header>
849 <!--suppress HtmlFormInputWithoutLabel -->
850 <input type="text" name="user_template_title" class="king-addons-pb-user-template-title"
851 placeholder="<?php esc_html_e('Enter Popup Title', 'king-addons'); ?>">
852 <input type="hidden" name="user_template_type" class="user-template-type">
853 <span class="king-addons-pb-create-template"><?php esc_html_e('Create Popup', 'king-addons'); ?></span>
854 <span class="close-popup dashicons dashicons-no-alt"></span>
855 </div>
856 </div>
857 <?php
858 }
859
860 public static function get_custom_types_of($query, $exclude_defaults = true): array
861 {
862 /** @noinspection DuplicatedCode */
863 if ('tax' === $query) {
864 $custom_types = get_taxonomies(['show_in_nav_menus' => true], 'objects');
865 } else {
866 $custom_types = get_post_types(['show_in_nav_menus' => true], 'objects');
867 }
868
869 $custom_type_list = [];
870
871 foreach ($custom_types as $key => $value) {
872 if ($exclude_defaults) {
873 if ($key != 'post' && $key != 'page' && $key != 'category' && $key != 'post_tag') {
874 $custom_type_list[$key] = $value->label;
875 }
876 } else {
877 $custom_type_list[$key] = $value->label;
878 }
879 }
880
881 return $custom_type_list;
882 }
883
884 public static function render_theme_builder_templates($template): void
885 {
886 $args = array(
887 'post_type' => array('king_addons_ext_pb'),
888 'post_status' => array('publish'),
889 'posts_per_page' => -1,
890 'tax_query' => array(
891 array(
892 'taxonomy' => 'king_addons_pb_template_type',
893 'field' => 'slug',
894 'terms' => [$template, 'user'],
895 'operator' => 'AND'
896 )
897 )
898 );
899
900 $user_templates = get_posts($args);
901
902 echo '<ul class="king-addons-pb-' . esc_attr($template) . '-templates-list king-addons-pb-my-templates-list" data-pro="' . esc_attr(king_addons_freemius()->can_use_premium_code__premium_only()) . '">';
903
904 if (!empty($user_templates)) {
905 foreach ($user_templates as $user_template) {
906 $slug = $user_template->post_name;
907
908 if (!str_contains($slug, 'user-')) {
909 continue;
910 }
911
912 $edit_url = str_replace('edit', 'elementor', get_edit_post_link($user_template->ID));
913 $show_on_canvas = get_post_meta(self::get_template_id($slug), 'king_addons_pb_' . $template . '_show_on_canvas', true);
914
915 echo '<li>';
916 echo '<h3 class="king-addons-pb-title">' . esc_html($user_template->post_title) . '</h3>';
917 echo '<div class="king-addons-pb-action-buttons">';
918 echo '<span class="king-addons-pb-template-conditions button button-primary" data-slug="' . esc_attr($slug) . '" data-show-on-canvas="' . esc_attr($show_on_canvas) . '">' . esc_html__('Manage Conditions', 'king-addons') . '</span>';
919 echo '<a href="' . esc_url($edit_url) . '" class="king-addons-pb-edit-template button button-primary">' . esc_html__('Edit Popup', 'king-addons') . '</a>';
920
921 $one_time_nonce = wp_create_nonce('delete_post-' . $slug);
922 echo '<span class="king-addons-pb-delete-template button button-primary" data-nonce="' . esc_attr($one_time_nonce) . '" data-slug="' . esc_attr($slug) . '" data-warning="' . esc_html__('Are you sure you want to delete this popup?', 'king-addons') . '"><span class="dashicons dashicons-no-alt"></span></span>';
923
924 echo '</div>';
925 echo '</li>';
926 }
927 } else {
928 echo '<li class="king-addons-pb-no-templates">Create the first popup by clicking the \'Create Popup\' button.</li>';
929 }
930
931 echo '</ul>';
932
933 wp_reset_postdata();
934
935 }
936
937 public static function get_template_id($slug)
938 {
939 $template = get_page_by_path($slug, OBJECT, 'king_addons_ext_pb');
940 return $template->ID ?? false;
941 }
942
943 public static function archive_pages_popup_conditions($conditions): void
944 {
945 $term_id = '';
946 $term_name = '';
947 $queried_object = get_queried_object();
948
949 if (!is_null($queried_object)) {
950 if (isset($queried_object->term_id) && isset($queried_object->taxonomy)) {
951 $term_id = $queried_object->term_id;
952 $term_name = $queried_object->taxonomy;
953 }
954 }
955
956 if (is_archive() || is_search()) {
957 if (is_archive() && !is_search()) {
958 if (isset($conditions['archive/all_archives'])) {
959 self::display_popups_by_location($conditions, 'archive/all_archives');
960 }
961
962 if (is_author()) {
963 if (isset($conditions['archive/author'])) {
964 self::display_popups_by_location($conditions, 'archive/author');
965 }
966 }
967
968 if (is_date()) {
969 if (isset($conditions['archive/date'])) {
970 self::display_popups_by_location($conditions, 'archive/date');
971 }
972 }
973
974 if (is_category()) {
975 if (isset($conditions['archive/categories/' . $term_id])) {
976 self::display_popups_by_location($conditions, 'archive/categories/' . $term_id);
977 }
978
979 if (isset($conditions['archive/categories/all'])) {
980 self::display_popups_by_location($conditions, 'archive/categories/all');
981 }
982 }
983
984 if (is_tag()) {
985 if (isset($conditions['archive/tags/' . $term_id])) {
986 self::display_popups_by_location($conditions, 'archive/tags/' . $term_id);
987 }
988
989 if (isset($conditions['archive/tags/all'])) {
990 self::display_popups_by_location($conditions, 'archive/tags/all');
991 }
992 }
993
994 if (is_tax()) {
995 if (isset($conditions['archive/' . $term_name . '/' . $term_id])) {
996 self::display_popups_by_location($conditions, 'archive/' . $term_name . '/' . $term_id);
997 }
998
999 if (isset($conditions['archive/' . $term_name . '/all'])) {
1000 self::display_popups_by_location($conditions, 'archive/' . $term_name . '/all');
1001 }
1002 }
1003
1004 if (class_exists('WooCommerce')) {
1005 if (function_exists('is_shop')) {
1006 /** @noinspection PhpUndefinedFunctionInspection */
1007 if (function_exists('is_shop') && is_shop()) {
1008 if (isset($conditions['archive/product'])) {
1009 self::display_popups_by_location($conditions, 'archive/product');
1010 }
1011 }
1012 }
1013 }
1014
1015 } else {
1016 if (isset($conditions['archive/search'])) {
1017 self::display_popups_by_location($conditions, 'archive/search');
1018 }
1019 }
1020
1021 } elseif (self::is_blog_archive()) {
1022 if (isset($conditions['archive/posts'])) {
1023 self::display_popups_by_location($conditions, 'archive/posts');
1024 }
1025 }
1026 }
1027
1028 public static function is_blog_archive(): bool
1029 {
1030 /** @noinspection DuplicatedCode */
1031 $result = false;
1032 $front_page = get_option('page_on_front');
1033 $posts_page = get_option('page_for_posts');
1034
1035 if (is_home() && '0' === $front_page && '0' === $posts_page || (intval($posts_page) === get_queried_object_id() && !is_404())) {
1036 $result = true;
1037 }
1038
1039 return $result;
1040 }
1041
1042 public static function single_pages_popup_conditions($conditions): void
1043 {
1044 global $post;
1045
1046 $post_id = is_null($post) ? '' : $post->ID;
1047 $post_type = is_null($post) ? '' : $post->post_type;
1048
1049 if (is_single() || is_front_page() || is_page() || is_404()) {
1050
1051 if (is_single()) {
1052 if ('post' == $post_type) {
1053 if (isset($conditions['single/posts/' . $post_id])) {
1054 self::display_popups_by_location($conditions, 'single/posts/' . $post_id);
1055 }
1056
1057 if (isset($conditions['single/posts/all'])) {
1058 self::display_popups_by_location($conditions, 'single/posts/all');
1059 }
1060
1061 } else {
1062 if (isset($conditions['single/' . $post_type . '/' . $post_id])) {
1063 self::display_popups_by_location($conditions, 'single/' . $post_type . '/' . $post_id);
1064 }
1065
1066 if (isset($conditions['single/' . $post_type . '/all'])) {
1067 self::display_popups_by_location($conditions, 'single/' . $post_type . '/all');
1068 }
1069 }
1070 } else {
1071 if (is_front_page()) {
1072 if (isset($conditions['single/front_page'])) {
1073 self::display_popups_by_location($conditions, 'single/front_page');
1074 }
1075 } elseif (is_404()) {
1076 if (isset($conditions['single/page_404'])) {
1077 self::display_popups_by_location($conditions, 'single/page_404');
1078 }
1079 } elseif (is_page()) {
1080 if (isset($conditions['single/pages/' . $post_id])) {
1081 self::display_popups_by_location($conditions, 'single/pages/' . $post_id);
1082 }
1083
1084 if (isset($conditions['single/pages/all'])) {
1085 self::display_popups_by_location($conditions, 'single/pages/all');
1086 }
1087 }
1088 }
1089
1090 }
1091 }
1092
1093 }