PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.14
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.14
51.1.87 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 All 41 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.14, at includes/extensions/Popup_Builder/Popup_Builder.php

1,051 lines 56.0 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'])) {
338 update_option('king_addons_pb_popup_conditions', $this->sanitize_conditions($_POST['king_addons_pb_popup_conditions'])); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
339 }
340 }
341
342 public function sanitize_conditions($data)
343 {
344 return wp_unslash(json_encode(array_filter(json_decode(stripcslashes($data), true))));
345 }
346
347 public function king_addons_pb_create_template(): void
348 {
349 $nonce = $_POST['nonce'];
350
351 if (!wp_verify_nonce($nonce, 'king-addons-popup-builder-admin-script') || !current_user_can('manage_options')) {
352 exit;
353 }
354
355 /** @noinspection DuplicatedCode */
356 $user_template_type = isset($_POST['user_template_type']) ? sanitize_text_field(wp_unslash($_POST['user_template_type'])) : false;
357 $user_template_library = isset($_POST['user_template_library']) ? sanitize_text_field(wp_unslash($_POST['user_template_library'])) : false;
358 $user_template_title = isset($_POST['user_template_title']) ? sanitize_text_field(wp_unslash($_POST['user_template_title'])) : false;
359 $user_template_slug = isset($_POST['user_template_slug']) ? sanitize_text_field(wp_unslash($_POST['user_template_slug'])) : false;
360
361 $check_post_type = ($user_template_library == 'king_addons_ext_pb' || $user_template_library == 'elementor_library');
362
363 if ($user_template_title && $check_post_type) {
364
365 $template_id = wp_insert_post(array(
366 'post_type' => $user_template_library,
367 'post_title' => $user_template_title,
368 'post_name' => $user_template_slug,
369 'post_content' => '',
370 'post_status' => 'publish'
371 ));
372
373 if ('king_addons_ext_pb' === $_POST['user_template_library']) {
374
375 wp_set_object_terms($template_id, [$user_template_type, 'user'], 'king_addons_pb_template_type');
376
377 if ('popup' === $_POST['user_template_type']) {
378 update_post_meta($template_id, '_elementor_template_type', 'king-addons-pb-popups');
379 } else {
380 update_post_meta($template_id, '_elementor_template_type', 'king-addons-pb-theme-builder');
381 update_post_meta($template_id, '_king_addons_pb_template_type', $user_template_type);
382 }
383 } else {
384 update_post_meta($template_id, '_elementor_template_type', 'page');
385 }
386
387 update_post_meta($template_id, '_wp_page_template', 'elementor_canvas');
388
389 echo esc_html($template_id);
390 }
391 }
392
393 public static function renderPopupBuilder(): void
394 {
395 $screen = get_current_screen();
396 if ($screen->id === 'toplevel_page_king-addons-popup-builder') {
397 wp_enqueue_style('king-addons-popup-builder-admin-style', KING_ADDONS_URL . 'includes/extensions/Popup_Builder/admin.css', '', KING_ADDONS_VERSION);
398 wp_enqueue_script('jquery');
399 wp_enqueue_script('king-addons-popup-builder-admin-script', KING_ADDONS_URL . 'includes/extensions/Popup_Builder/admin.js', array('jquery'), KING_ADDONS_VERSION);
400 wp_localize_script(
401 'king-addons-popup-builder-admin-script',
402 'KingAddonsPopupBuilderOptions',
403 [
404 'nonce' => wp_create_nonce('king-addons-popup-builder-admin-script'),
405 ]
406 );
407 }
408 ?>
409 <div class="wrap king-addons-pb-settings-page-wrap">
410 <div class="king-addons-pb-settings-page-header">
411 <h1><?php esc_html_e('Elementor Popup Builder', 'king-addons'); ?></h1>
412 <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>
413 <div class="king-addons-pb-preview-buttons">
414 <div class="king-addons-pb-user-template">
415 <span><?php esc_html_e('Create Popup', 'king-addons'); ?></span>
416 <span class="plus-icon">+</span>
417 </div>
418 <?php if (!king_addons_freemius()->can_use_premium_code__premium_only()): ?>
419 <div class="kng-promo-btn-wrap">
420 <a href="https://kingaddons.com/pricing/?rel=king-addons-popup-builder" target="_blank">
421 <div class="kng-promo-btn-txt">
422 <?php esc_html_e('Unlock Premium Features & 650+ Templates Today!', 'king-addons'); ?>
423 </div>
424 <img width="16px"
425 src="<?php echo esc_url(KING_ADDONS_URL) . 'includes/admin/img/share-v2.svg'; ?>"
426 alt="<?php echo esc_html__('Open link in the new tab', 'king-addons'); ?>">
427 </a>
428 </div>
429 <?php endif; ?>
430 </div>
431 </div>
432 <div class="king-addons-pb-settings-page">
433 <!--suppress HtmlUnknownTarget -->
434 <form method="post" action="options.php">
435 <input type="hidden" name="king_addons_pb_template" id="king_addons_pb_template" value="">
436 <?php self::render_conditions_popup(); ?>
437 <?php self::render_create_template_popup(); ?>
438 <input type="hidden" name="king_addons_pb_popup_conditions" id="king_addons_pb_popup_conditions"
439 value="<?php echo esc_attr(get_option('king_addons_pb_popup_conditions', '[]')); ?>">
440 <?php self::render_theme_builder_templates('popup'); ?>
441 </form>
442 </div>
443 </div>
444 <?php
445 }
446
447 public static function render_conditions_popup(): void
448 {
449 $active_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'king_addons_pb_tab_header';
450 ?>
451 <div class="king-addons-pb-condition-popup-wrap king-addons-pb-admin-popup-wrap">
452 <div class="king-addons-pb-condition-popup king-addons-pb-admin-popup">
453 <header>
454 <h2><?php esc_html_e('Where would you like the popup to be displayed?', 'king-addons'); ?></h2>
455 <p>
456 <?php esc_html_e('Define the conditions that specify where the popup will be applied across your site. ', 'king-addons'); ?>
457 <br>
458 <?php esc_html_e('For instance, select Entire Site to display the template on all pages.', 'king-addons');
459
460 if (!king_addons_freemius()->can_use_premium_code__premium_only()) {
461 echo '<span><br>';
462 echo esc_html__('All conditions are available in the ', 'king-addons');
463 echo '<strong><a href="https://kingaddons.com/pricing/?utm_source=kng-module-popup-builder-change-conditions-window" target="_blank">';
464 echo esc_html__('Pro version.', 'king-addons');
465 echo '</a></strong></span>';
466 }
467 ?>
468 </p>
469 </header>
470 <span class="close-popup dashicons dashicons-no-alt"></span>
471 <div class="king-addons-pb-conditions-wrap">
472 <div class="king-addons-pb-conditions-sample">
473 <?php if (king_addons_freemius()->can_use_premium_code__premium_only()) : ?>
474
475 <!--suppress HtmlFormInputWithoutLabel -->
476 <select name="global_condition_select" class="global-condition-select">
477 <option value="global"><?php esc_html_e('Entire Site', 'king-addons'); ?></option>
478 <option value="archive"><?php esc_html_e('Archives', 'king-addons'); ?></option>
479 <option value="single"><?php esc_html_e('Singular', 'king-addons'); ?></option>
480 </select>
481
482 <!--suppress HtmlFormInputWithoutLabel -->
483 <select name="archives_condition_select" class="archives-condition-select">
484 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
485 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
486 <option value="all_archives"><?php esc_html_e('All Archives', 'king-addons'); ?></option>
487 <option value="posts"><?php esc_html_e('Posts Archive', 'king-addons'); ?></option>
488 <?php
489 $custom_post_types = self::get_custom_types_of('post');
490 foreach ($custom_post_types as $key => $value) {
491 if ('e-landing-page' === $key) {
492 continue;
493 }
494
495 if (king_addons_freemius()->can_use_premium_code__premium_only() || 'product' === $key) {
496 echo '<option value="' . esc_attr($key) . '">' . $value . ' ' . esc_html__('Archive', 'king-addons') . '</option>';
497 } else {
498 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '">' . $value . ' ' . esc_html__('Archive (PRO)', 'king-addons') . '</option>';
499 }
500 }
501 ?>
502 <option value="author"><?php esc_html_e('Author Archive', 'king-addons'); ?></option>
503 <option value="date"><?php esc_html_e('Date Archive', 'king-addons'); ?></option>
504 <option value="search"><?php esc_html_e('Search Results', 'king-addons'); ?></option>
505 </optgroup>
506
507 <optgroup label="<?php esc_html_e('Taxonomy Archives', 'king-addons'); ?>">
508 <option value="categories"
509 class="custom-ids"><?php esc_html_e('Post Categories', 'king-addons'); ?></option>
510 <option value="tags"
511 class="custom-ids"><?php esc_html_e('Post Tags', 'king-addons'); ?></option>
512 <?php
513 $custom_taxonomies = self::get_custom_types_of('tax');
514 foreach ($custom_taxonomies as $key => $value) {
515 if (king_addons_freemius()->can_use_premium_code__premium_only() || 'product_cat' === $key || 'product_tag' === $key) {
516 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
517 } else {
518 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
519 }
520 }
521 ?>
522 </optgroup>
523 <?php else: ?>
524 <?php if ('king_addons_pb_tab_archive' === $active_tab) : ?>
525 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
526 <option value="all_archives"><?php esc_html_e('All Archives', 'king-addons'); ?></option>
527 <option value="posts"><?php esc_html_e('Posts Archive', 'king-addons'); ?></option>
528 <?php
529 $custom_post_types = self::get_custom_types_of('post');
530 foreach ($custom_post_types as $key => $value) {
531 if ('product' === $key || 'e-landing-page' === $key) {
532 continue;
533 }
534
535 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
536 echo '<option value="' . esc_attr($key) . '">' . $value . ' ' . esc_html__('Archive', 'king-addons') . '</option>';
537 } else {
538 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '">' . $value . ' ' . esc_html__('Archive (PRO)', 'king-addons') . '</option>';
539 }
540 }
541 ?>
542 <option value="author"><?php esc_html_e('Author Archive', 'king-addons'); ?></option>
543 <option value="date"><?php esc_html_e('Date Archive', 'king-addons'); ?></option>
544 <option value="search"><?php esc_html_e('Search Results', 'king-addons'); ?></option>
545 </optgroup>
546
547 <optgroup label="<?php esc_html_e('Taxonomy Archives', 'king-addons'); ?>">
548 <option value="categories"
549 class="custom-ids"><?php esc_html_e('Post Categories', 'king-addons'); ?></option>
550 <option value="tags"
551 class="custom-ids"><?php esc_html_e('Post Tags', 'king-addons'); ?></option>
552 <?php
553 $custom_taxonomies = self::get_custom_types_of('tax');
554 foreach ($custom_taxonomies as $key => $value) {
555 if ('product_cat' === $key || 'product_tag' === $key) {
556 continue;
557 }
558
559 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
560 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
561 } else {
562 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
563 }
564 }
565 ?>
566 </optgroup>
567 <?php elseif ('king_addons_pb_tab_product_archive' === $active_tab): ?>
568 <option value="products"><?php esc_html_e('Products Archive', 'king-addons'); ?></option>
569 <option value="product_cat"
570 class="custom-type-ids"><?php esc_html_e('Products Categories', 'king-addons'); ?></option>
571 <option value="product_tag"
572 class="custom-type-ids"><?php esc_html_e('Products Tags', 'king-addons'); ?></option>
573 <option value="product_search"><?php esc_html_e('Products Search', 'king-addons'); ?></option>
574 <?php endif; ?>
575 <?php endif; ?>
576 </select>
577
578 <!--suppress HtmlFormInputWithoutLabel -->
579 <select name="singles_condition_select" class="singles-condition-select">
580 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
581 <option value="front_page"><?php esc_html_e('Front Page', 'king-addons'); ?></option>
582 <option value="page_404"><?php esc_html_e('404 Page', 'king-addons'); ?></option>
583 <option value="pages"
584 class="custom-ids"><?php esc_html_e('Pages', 'king-addons'); ?></option>
585 <option value="posts"
586 class="custom-ids"><?php esc_html_e('Posts', 'king-addons'); ?></option>
587 <?php
588 $custom_post_types = self::get_custom_types_of('post');
589 foreach ($custom_post_types as $key => $value) {
590 if ('e-landing-page' === $key) {
591 continue;
592 }
593
594 if (king_addons_freemius()->can_use_premium_code__premium_only() || 'product' === $key) {
595 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
596 } else {
597 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
598 }
599 }
600 ?>
601 <?php else: ?>
602 <?php if ('king_addons_pb_tab_single' === $active_tab) : ?>
603 <option value="front_page"><?php esc_html_e('Front Page', 'king-addons'); ?></option>
604 <option value="page_404"><?php esc_html_e('404 Page', 'king-addons'); ?></option>
605 <option value="pages"
606 class="custom-ids"><?php esc_html_e('Pages', 'king-addons'); ?></option>
607 <option value="posts"
608 class="custom-ids"><?php esc_html_e('Posts', 'king-addons'); ?></option>
609
610 <?php
611 $custom_post_types = self::get_custom_types_of('post');
612 foreach ($custom_post_types as $key => $value) {
613 if ('product' === $key || 'e-landing-page' === $key) {
614 continue;
615 }
616
617 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
618 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . '</option>';
619 } else {
620 echo '<option value="pro-' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
621 }
622 }
623 ?>
624 <?php elseif ('king_addons_pb_tab_product_single' === $active_tab): ?>
625 <option value="product"
626 class="custom-product-ids custom-type-ids"><?php esc_html_e('Products', 'king-addons'); ?></option>
627 <?php endif; ?>
628 <?php endif; ?>
629 </select>
630
631 <!--suppress HtmlFormInputWithoutLabel -->
632 <input type="text"
633 placeholder="<?php esc_html_e('Enter comma separated IDs', 'king-addons'); ?>"
634 name="condition_input_ids" class="king-addons-pb-condition-input-ids">
635 <span class="king-addons-pb-delete-template-conditions dashicons dashicons-no-alt"></span>
636
637 <?php else: ?>
638 <!--suppress HtmlFormInputWithoutLabel -->
639 <select name="global_condition_select" class="global-condition-select">
640 <option value="global"><?php esc_html_e('Entire Site', 'king-addons'); ?></option>
641 <option value="archive"><?php esc_html_e('Archives (Pro)', 'king-addons'); ?></option>
642 <option value="single"><?php esc_html_e('Singular (Pro)', 'king-addons'); ?></option>
643 </select>
644
645 <!--suppress HtmlFormInputWithoutLabel -->
646 <select name="archives_condition_select" class="archives-condition-select">
647 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
648 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
649 <option value="all_archives"><?php esc_html_e('All Archives (Pro)', 'king-addons'); ?></option>
650 <option value="posts"><?php esc_html_e('Posts Archive (Pro)', 'king-addons'); ?></option>
651 <option value="author"><?php esc_html_e('Author Archive (Pro)', 'king-addons'); ?></option>
652 <option value="date"><?php esc_html_e('Date Archive (Pro)', 'king-addons'); ?></option>
653 <option value="search"><?php esc_html_e('Search Results (Pro)', 'king-addons'); ?></option>
654 <option value="categories"
655 class="custom-ids"><?php esc_html_e('Post Categories (Pro)', 'king-addons'); ?></option>
656 <option value="tags"
657 class="custom-ids"><?php esc_html_e('Post Tags (Pro)', 'king-addons'); ?></option>
658 </optgroup>
659 <optgroup label="<?php esc_html_e('WooCommerce Archives', 'king-addons'); ?>">
660 <option value="products"
661 class="custom-ids"><?php esc_html_e('Products Archive (Pro)', 'king-addons'); ?></option>
662 <option value="products_cats"
663 class="custom-ids"><?php esc_html_e('Product Categories (Pro)', 'king-addons'); ?></option>
664 <option value="product_tags"
665 class="custom-ids"><?php esc_html_e('Product Tags (Pro)', 'king-addons'); ?></option>
666 </optgroup>
667 <optgroup label="<?php esc_html_e('Custom Post Type Archives', 'king-addons'); ?>">
668 <?php
669 $custom_post_types = self::get_custom_types_of('post');
670 foreach ($custom_post_types as $key => $value) {
671 if ('product' === $key || 'e-landing-page' === $key) {
672 continue;
673 }
674
675 echo '<option value="' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
676 }
677
678 $custom_taxonomies = self::get_custom_types_of('tax');
679 foreach ($custom_taxonomies as $key => $value) {
680 if ('product_cat' === $key || 'product_tag' === $key) {
681 continue;
682 }
683 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
684 }
685 ?>
686 </optgroup>
687 <?php else: ?>
688 <?php if ('king_addons_pb_tab_archive' === $active_tab) : ?>
689 <optgroup label="<?php esc_html_e('Archives', 'king-addons'); ?>">
690 <option value="all_archives"><?php esc_html_e('All Archives', 'king-addons'); ?></option>
691 <option value="posts"><?php esc_html_e('Posts Archive', 'king-addons'); ?></option>
692 <option value="author"><?php esc_html_e('Author Archive', 'king-addons'); ?></option>
693 <option value="date"><?php esc_html_e('Date Archive', 'king-addons'); ?></option>
694 <option value="search"><?php esc_html_e('Search Results', 'king-addons'); ?></option>
695 <option value="categories"
696 class="custom-ids"><?php esc_html_e('Post Categories', 'king-addons'); ?></option>
697 <option value="tags"
698 class="custom-ids"><?php esc_html_e('Post Tags', 'king-addons'); ?></option>
699 </optgroup>
700 <optgroup
701 label="<?php esc_html_e('Custom Post Type Archives', 'king-addons'); ?>">
702 <?php
703 $custom_post_types = self::get_custom_types_of('post');
704 foreach ($custom_post_types as $key => $value) {
705 if ('product' === $key || 'e-landing-page' === $key) {
706 continue;
707 }
708
709 echo '<option value="' . esc_attr(substr($key, 0, 3)) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
710 }
711 ?>
712 <?php
713 $custom_taxonomies = self::get_custom_types_of('tax');
714 foreach ($custom_taxonomies as $key => $value) {
715 if ('product_cat' === $key || 'product_tag' === $key) {
716 continue;
717 }
718
719 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
720 }
721 ?>
722 </optgroup>
723 <?php elseif ('king_addons_pb_tab_product_archive' === $active_tab): ?>
724 <option value="products"><?php esc_html_e('Products Archive', 'king-addons'); ?></option>
725 <option value="product_cat"
726 class="custom-type-ids"><?php esc_html_e('Products Categories (Pro)', 'king-addons'); ?></option>
727 <option value="product_tag"
728 class="custom-type-ids"><?php esc_html_e('Products Tags (Pro)', 'king-addons'); ?></option>
729 <option value="product_search"><?php esc_html_e('Products Search (Pro)', 'king-addons'); ?></option>
730 <?php endif; ?>
731 <?php endif; ?>
732 </select>
733
734 <!--suppress HtmlFormInputWithoutLabel -->
735 <select name="singles_condition_select" class="singles-condition-select">
736 <?php if ('king_addons_pb_tab_header' === $active_tab || 'king_addons_pb_tab_footer' === $active_tab) : ?>
737 <option value="front_page"><?php esc_html_e('Front Page (Pro)', 'king-addons'); ?></option>
738 <option value="page_404"><?php esc_html_e('404 Page (Pro)', 'king-addons'); ?></option>
739 <option value="pages"
740 class="custom-ids"><?php esc_html_e('Pages (Pro)', 'king-addons'); ?></option>
741 <option value="posts"
742 class="custom-ids"><?php esc_html_e('Posts (Pro)', 'king-addons'); ?></option>
743 <option value="product"
744 class="custom-ids"><?php esc_html_e('Product (Pro)', 'king-addons'); ?></option>
745 <?php
746 $custom_post_types = self::get_custom_types_of('post');
747 foreach ($custom_post_types as $key => $value) {
748 if ('product' === $key || 'e-landing-page' === $key) {
749 continue;
750 }
751
752 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
753 }
754 ?>
755 <?php else: ?>
756 <?php if ('king_addons_pb_tab_single' === $active_tab) : ?>
757 <option value="front_page"><?php esc_html_e('Front Page', 'king-addons'); ?></option>
758 <option value="page_404"><?php esc_html_e('404 Page', 'king-addons'); ?></option>
759 <option value="pages"
760 class="custom-ids"><?php esc_html_e('Pages', 'king-addons'); ?></option>
761 <option value="posts"
762 class="custom-ids"><?php esc_html_e('Posts', 'king-addons'); ?></option>
763
764 <?php
765 $custom_post_types = self::get_custom_types_of('post');
766 foreach ($custom_post_types as $key => $value) {
767 if ('product' === $key || 'e-landing-page' === $key) {
768 continue;
769 }
770
771 echo '<option value="' . esc_attr($key) . '" class="custom-type-ids">' . esc_html($value) . ' (PRO)</option>';
772 }
773 ?>
774 <?php elseif ('king_addons_pb_tab_product_single' === $active_tab): ?>
775 <option value="product"
776 class="custom-type-ids"><?php esc_html_e('Products', 'king-addons'); ?></option>
777 <?php endif; ?>
778 <?php endif; ?>
779 </select>
780
781 <!--suppress HtmlFormInputWithoutLabel -->
782 <input type="text"
783 placeholder="<?php esc_html_e('Enter comma separated IDs (Pro)', 'king-addons'); ?>"
784 name="condition_input_ids" class="king-addons-pb-condition-input-ids">
785 <span class="king-addons-pb-delete-template-conditions dashicons dashicons-no-alt"></span>
786
787 <?php endif; ?>
788 </div>
789 </div>
790 <span class="king-addons-pb-add-conditions"><?php esc_html_e('+ Add Conditions', 'king-addons'); ?></span>
791 <span class="king-addons-pb-save-conditions"><?php esc_html_e('Save Conditions', 'king-addons'); ?></span>
792 </div>
793 </div>
794
795 <?php
796 }
797
798 public static function render_create_template_popup(): void
799 {
800 ?>
801 <div class="king-addons-pb-user-template-popup-wrap king-addons-pb-admin-popup-wrap">
802 <div class="king-addons-pb-user-template-popup king-addons-pb-admin-popup">
803 <header>
804 <h2><?php esc_html_e('Creating Popup', 'king-addons'); ?></h2>
805 <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>
806 </header>
807 <!--suppress HtmlFormInputWithoutLabel -->
808 <input type="text" name="user_template_title" class="king-addons-pb-user-template-title"
809 placeholder="<?php esc_html_e('Enter Popup Title', 'king-addons'); ?>">
810 <input type="hidden" name="user_template_type" class="user-template-type">
811 <span class="king-addons-pb-create-template"><?php esc_html_e('Create Popup', 'king-addons'); ?></span>
812 <span class="close-popup dashicons dashicons-no-alt"></span>
813 </div>
814 </div>
815 <?php
816 }
817
818 public static function get_custom_types_of($query, $exclude_defaults = true): array
819 {
820 /** @noinspection DuplicatedCode */
821 if ('tax' === $query) {
822 $custom_types = get_taxonomies(['show_in_nav_menus' => true], 'objects');
823 } else {
824 $custom_types = get_post_types(['show_in_nav_menus' => true], 'objects');
825 }
826
827 $custom_type_list = [];
828
829 foreach ($custom_types as $key => $value) {
830 if ($exclude_defaults) {
831 if ($key != 'post' && $key != 'page' && $key != 'category' && $key != 'post_tag') {
832 $custom_type_list[$key] = $value->label;
833 }
834 } else {
835 $custom_type_list[$key] = $value->label;
836 }
837 }
838
839 return $custom_type_list;
840 }
841
842 public static function render_theme_builder_templates($template): void
843 {
844 $args = array(
845 'post_type' => array('king_addons_ext_pb'),
846 'post_status' => array('publish'),
847 'posts_per_page' => -1,
848 'tax_query' => array(
849 array(
850 'taxonomy' => 'king_addons_pb_template_type',
851 'field' => 'slug',
852 'terms' => [$template, 'user'],
853 'operator' => 'AND'
854 )
855 )
856 );
857
858 $user_templates = get_posts($args);
859
860 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()) . '">';
861
862 if (!empty($user_templates)) {
863 foreach ($user_templates as $user_template) {
864 $slug = $user_template->post_name;
865
866 if (!str_contains($slug, 'user-')) {
867 continue;
868 }
869
870 $edit_url = str_replace('edit', 'elementor', get_edit_post_link($user_template->ID));
871 $show_on_canvas = get_post_meta(self::get_template_id($slug), 'king_addons_pb_' . $template . '_show_on_canvas', true);
872
873 echo '<li>';
874 echo '<h3 class="king-addons-pb-title">' . esc_html($user_template->post_title) . '</h3>';
875 echo '<div class="king-addons-pb-action-buttons">';
876 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>';
877 echo '<a href="' . esc_url($edit_url) . '" class="king-addons-pb-edit-template button button-primary">' . esc_html__('Edit Popup', 'king-addons') . '</a>';
878
879 $one_time_nonce = wp_create_nonce('delete_post-' . $slug);
880 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>';
881
882 echo '</div>';
883 echo '</li>';
884 }
885 } else {
886 echo '<li class="king-addons-pb-no-templates">Create the first popup by clicking the \'Create Popup\' button.</li>';
887 }
888
889 echo '</ul>';
890
891 wp_reset_postdata();
892
893 }
894
895 public static function get_template_id($slug)
896 {
897 $template = get_page_by_path($slug, OBJECT, 'king_addons_ext_pb');
898 return $template->ID ?? false;
899 }
900
901 public static function archive_pages_popup_conditions($conditions): void
902 {
903 $term_id = '';
904 $term_name = '';
905 $queried_object = get_queried_object();
906
907 if (!is_null($queried_object)) {
908 if (isset($queried_object->term_id) && isset($queried_object->taxonomy)) {
909 $term_id = $queried_object->term_id;
910 $term_name = $queried_object->taxonomy;
911 }
912 }
913
914 if (is_archive() || is_search()) {
915 if (is_archive() && !is_search()) {
916 if (isset($conditions['archive/all_archives'])) {
917 self::display_popups_by_location($conditions, 'archive/all_archives');
918 }
919
920 if (is_author()) {
921 if (isset($conditions['archive/author'])) {
922 self::display_popups_by_location($conditions, 'archive/author');
923 }
924 }
925
926 if (is_date()) {
927 if (isset($conditions['archive/date'])) {
928 self::display_popups_by_location($conditions, 'archive/date');
929 }
930 }
931
932 if (is_category()) {
933 if (isset($conditions['archive/categories/' . $term_id])) {
934 self::display_popups_by_location($conditions, 'archive/categories/' . $term_id);
935 }
936
937 if (isset($conditions['archive/categories/all'])) {
938 self::display_popups_by_location($conditions, 'archive/categories/all');
939 }
940 }
941
942 if (is_tag()) {
943 if (isset($conditions['archive/tags/' . $term_id])) {
944 self::display_popups_by_location($conditions, 'archive/tags/' . $term_id);
945 }
946
947 if (isset($conditions['archive/tags/all'])) {
948 self::display_popups_by_location($conditions, 'archive/tags/all');
949 }
950 }
951
952 if (is_tax()) {
953 if (isset($conditions['archive/' . $term_name . '/' . $term_id])) {
954 self::display_popups_by_location($conditions, 'archive/' . $term_name . '/' . $term_id);
955 }
956
957 if (isset($conditions['archive/' . $term_name . '/all'])) {
958 self::display_popups_by_location($conditions, 'archive/' . $term_name . '/all');
959 }
960 }
961
962 if (class_exists('WooCommerce')) {
963 if (function_exists('is_shop')) {
964 /** @noinspection PhpUndefinedFunctionInspection */
965 if (is_shop()) {
966 if (isset($conditions['archive/product'])) {
967 self::display_popups_by_location($conditions, 'archive/product');
968 }
969 }
970 }
971 }
972
973 } else {
974 if (isset($conditions['archive/search'])) {
975 self::display_popups_by_location($conditions, 'archive/search');
976 }
977 }
978
979 } elseif (self::is_blog_archive()) {
980 if (isset($conditions['archive/posts'])) {
981 self::display_popups_by_location($conditions, 'archive/posts');
982 }
983 }
984 }
985
986 public static function is_blog_archive(): bool
987 {
988 /** @noinspection DuplicatedCode */
989 $result = false;
990 $front_page = get_option('page_on_front');
991 $posts_page = get_option('page_for_posts');
992
993 if (is_home() && '0' === $front_page && '0' === $posts_page || (intval($posts_page) === get_queried_object_id() && !is_404())) {
994 $result = true;
995 }
996
997 return $result;
998 }
999
1000 public static function single_pages_popup_conditions($conditions): void
1001 {
1002 global $post;
1003
1004 $post_id = is_null($post) ? '' : $post->ID;
1005 $post_type = is_null($post) ? '' : $post->post_type;
1006
1007 if (is_single() || is_front_page() || is_page() || is_404()) {
1008
1009 if (is_single()) {
1010 if ('post' == $post_type) {
1011 if (isset($conditions['single/posts/' . $post_id])) {
1012 self::display_popups_by_location($conditions, 'single/posts/' . $post_id);
1013 }
1014
1015 if (isset($conditions['single/posts/all'])) {
1016 self::display_popups_by_location($conditions, 'single/posts/all');
1017 }
1018
1019 } else {
1020 if (isset($conditions['single/' . $post_type . '/' . $post_id])) {
1021 self::display_popups_by_location($conditions, 'single/' . $post_type . '/' . $post_id);
1022 }
1023
1024 if (isset($conditions['single/' . $post_type . '/all'])) {
1025 self::display_popups_by_location($conditions, 'single/' . $post_type . '/all');
1026 }
1027 }
1028 } else {
1029 if (is_front_page()) {
1030 if (isset($conditions['single/front_page'])) {
1031 self::display_popups_by_location($conditions, 'single/front_page');
1032 }
1033 } elseif (is_404()) {
1034 if (isset($conditions['single/page_404'])) {
1035 self::display_popups_by_location($conditions, 'single/page_404');
1036 }
1037 } elseif (is_page()) {
1038 if (isset($conditions['single/pages/' . $post_id])) {
1039 self::display_popups_by_location($conditions, 'single/pages/' . $post_id);
1040 }
1041
1042 if (isset($conditions['single/pages/all'])) {
1043 self::display_popups_by_location($conditions, 'single/pages/all');
1044 }
1045 }
1046 }
1047
1048 }
1049 }
1050
1051 }