PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.38
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.38
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.38, at includes/extensions/Popup_Builder/Popup_Builder.php

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