PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.7
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.7
3.3.3 3.3.2 3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 158 releases
notificationx / includes / FrontEnd / FrontEnd.php

FrontEnd.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.7, at includes/FrontEnd/FrontEnd.php

973 lines 41.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * FrontEnd Class
5 *
6 * @package NotificationX\FrontEnd
7 */
8
9 namespace NotificationX\FrontEnd;
10
11 use NotificationX\Admin\Entries;
12 use NotificationX\Admin\Settings;
13 use NotificationX\Core\Analytics;
14 use NotificationX\Core\Database;
15 use NotificationX\Core\GetData;
16 use NotificationX\NotificationX;
17 use NotificationX\Core\Locations;
18 use NotificationX\Core\PostType;
19 use NotificationX\Core\REST;
20 use NotificationX\GetInstance;
21 use NotificationX\Extensions\PressBar\PressBar;
22 use NotificationX\Core\Helper;
23 use NotificationX\Extensions\ExtensionFactory;
24 use NotificationX\Types\GDPR;
25
26 /**
27 * This class is responsible for all Front-End actions.
28 * @method static FrontEnd get_instance($args = null)
29 */
30 class FrontEnd {
31 /**
32 * Instance of FrontEnd
33 *
34 * @var FrontEnd
35 */
36 use GetInstance;
37 /**
38 * Assets Path and URL
39 */
40 const ASSET_URL = NOTIFICATIONX_ASSETS . 'public/';
41 const ASSET_PATH = NOTIFICATIONX_ASSETS_PATH . 'public/';
42 protected $notificationXArr = [];
43
44 /**
45 * Initially Invoked
46 * when its initialized.
47 */
48 public function __construct() {
49 Analytics::get_instance();
50 if (!is_admin() || !empty($_GET['frontend'])) {
51 add_action('init', [$this, 'init'], 10);
52 }
53 add_filter('nx_frontend_localize_data', [$this, 'get_localize_data']);
54 Preview::get_instance();
55 }
56
57 /**
58 * This method is reponsible for Admin Menu of
59 * NotificationX
60 *
61 * @return void
62 */
63 public function init() {
64 add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts'], 10);
65 add_filter('nx_fallback_data', [$this, 'fallback_data'], 10, 3);
66 add_filter('nx_filtered_data', [$this, 'filtered_data'], 9999, 3);
67 add_filter('nx_filtered_post', [$this, 'filtered_post'], 9999, 2);
68 add_action('wp_print_footer_scripts', [$this, 'footer_scripts']);
69 add_filter('body_class', [ $this, 'nx_add_body_class' ] );
70
71 }
72
73 /**
74 * This method is responsible for enqueueing scripts for public use.
75 *
76 * @return void
77 */
78 public function enqueue_scripts() {
79 $custom_css = $this->generate_custom_css();
80 wp_register_script('notificationx-public', Helper::file('public/js/frontend.js', true), [], apply_filters('nx_frontend_js_version', NOTIFICATIONX_VERSION ), true);
81 wp_register_style('notificationx-public', Helper::file('public/css/frontend.css', true), [], apply_filters('nx_frontend_css_version', NOTIFICATIONX_VERSION ), 'all');
82 // wp_register_style('notificationx-icon-pack', Helper::file('public/icon/style.css', true), [], NOTIFICATIONX_VERSION, 'all');
83 // Localize scripts for frontend
84 wp_localize_script(
85 'notificationx-public',
86 'notificationxPublic',
87 array(
88 'necessary_tab_info' => [
89 'title' => __('Necessary', 'notificationx'),
90 'desc' => __('Necessary cookies are needed to ensure the basic functions of this site, like allowing secure log-ins and managing your consent settings. These cookies do not collect any personal information.', 'notificationx'),
91 ],
92 'functional_tab_info' => [
93 'title' => __('Functional', 'notificationx'),
94 'desc' => __('Functional cookies assist in performing tasks like sharing website content on social media, collecting feedback, and enabling other third-party features.', 'notificationx'),
95 ],
96 'analytics_tab_info' => [
97 'title' => __('Analytics', 'notificationx'),
98 'desc' => __('Analytical cookies help us understand how visitors use the website. They provide data on metrics like the number of visitors, bounce rate, traffic sources etc.', 'notificationx'),
99 ],
100 'performance_tab_info' => [
101 'title' => __('Performance', 'notificationx'),
102 'desc' => __("Performance cookies help analyze the website's key performance indicators, which in turn helps improve the user experience for visitors.", 'notificationx'),
103 ],
104 'advertising_tab_info' => [
105 'title' => __('Advertisement', 'notificationx'),
106 'desc' => __("Advertisement cookies help analyze the website's key advertising indicators, which in turn helps improve the user experience for visitors.", 'notificationx'),
107 ],
108 'uncategorized_tab_info' => [
109 'title' => __('Uncategorized', 'notificationx'),
110 'desc' => __("Uncategorized cookies are those that don't fall into any specific category but may still be used for various purposes on the site. These cookies help us improve user experience by tracking interactions that don't fit into other cookie types.", 'notificationx'),
111 ],
112 'is_enabled_wp_consent_api' => is_plugin_active('wp-consent-api/wp-consent-api.php'),
113 )
114 );
115
116 $exit = false;
117 if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'wp-admin/widgets.php') !== false){
118 $exit = ['total' => 0];
119 }
120
121 $exit = apply_filters('nx_before_enqueue_scripts', $exit);
122 if(!empty($exit)){
123 $this->notificationXArr = $exit;
124 return;
125 }
126
127 if (!$exit && empty($_GET['elementor-preview'])) {
128 $this->notificationXArr = $this->get_notifications_ids();
129 if ($this->notificationXArr['total'] > 0) {
130 $lang = get_locale();
131 $lang = str_replace('_', '-', $lang);
132 $lang = strtolower($lang);
133 $_lang = explode('-', $lang);
134 if ($lang !== "en" && $lang !== "en-us") {
135 $script = Helper::file("public/locale/$lang.js", false);
136 if (file_exists($script)) {
137 wp_enqueue_script('notificationx-moment-locale', Helper::file("public/locale/$lang.js", true), [], NOTIFICATIONX_VERSION, true);
138 } else if (!empty($_lang[1])) {
139 $lang = $_lang[0];
140 $script = Helper::file("public/locale/$lang.js", false);
141 if (file_exists($script)) {
142 wp_enqueue_script('notificationx-moment-locale', Helper::file("public/locale/$lang.js", true), [], NOTIFICATIONX_VERSION, true);
143 }
144 }
145 }
146
147 wp_enqueue_style('notificationx-public');
148 wp_enqueue_script('notificationx-public');
149 wp_enqueue_style('dashicons');
150 do_action('notificationx_scripts', $this->notificationXArr);
151 wp_add_inline_style( 'notificationx-public', $custom_css );
152 }
153 } else {
154 // @todo maybe elementor edit mode CSS. to move to top.
155 // LATER
156 }
157 }
158
159 public function nx_add_body_class($classes) {
160 $classes[] = 'has-notificationx';
161 return $classes;
162 }
163
164 private function separate_css($css) {
165 $media_css = '';
166 $normal_css = '';
167
168 // Match @media blocks
169 preg_match_all('/@media[^{]*{([^}]*{[^}]*})*[^}]*}/', $css, $media_matches);
170
171 // Extract normal CSS
172 $normal_css = preg_replace('/@media[^{]*{([^}]*{[^}]*})*[^}]*}/', '', $css);
173
174 // Extract @media CSS
175 if (!empty($media_matches[0])) {
176 $media_css = implode("\n", $media_matches[0]);
177 }
178
179 // Clean up normal CSS (remove extra whitespace)
180 $normal_css = trim($normal_css);
181
182 return [
183 'normal_css' => $normal_css,
184 'media_css' => $media_css
185 ];
186 }
187
188 public function generate_custom_css() {
189 $posts = Database::get_instance()->get_posts(Database::$table_posts, '*', ['enabled' => true] );
190 $combine_css = "";
191 foreach ($posts as $post) {
192 if( !empty( $post['data']['add_custom_css'] ) && !empty( $post['nx_id'] ) ) {
193 $separatedCss = $this->separate_css($post['data']['add_custom_css']);
194 if( !empty( $post['data']['source'] ) && $post['data']['source'] == 'press_bar' ) {
195 $combine_css .= "{$separatedCss['normal_css']} {$separatedCss['media_css']} ";
196 } else if( !empty( $post['data']['source'] ) && $post['data']['source'] == 'gdpr_notification' ) {
197 $combine_css .= "{$separatedCss['normal_css']} {$separatedCss['media_css']} ";
198 }
199 else {
200 $combine_css .= "{$separatedCss['normal_css']} {$separatedCss['media_css']} ";
201 }
202 }
203 }
204 return $combine_css;
205 }
206
207 public function footer_scripts() {
208 if (!empty($this->notificationXArr['total']) && $this->notificationXArr['total'] > 0) {
209 $this->notificationXArr = apply_filters('nx_frontend_localize_data', $this->notificationXArr);
210 ?>
211 <script data-no-optimize="1">
212 (function() {
213 window.notificationXArr = window.notificationXArr || [];
214 window.notificationXArr.push(<?php echo json_encode($this->notificationXArr); ?>);
215 })();
216 </script>
217 <?php
218 }
219 }
220
221 // @todo deprecated. use get_localize_data instead
222 public function localizeScripts() {
223 return [];
224 }
225
226 public function get_localize_data($data) {
227 $data['rest'] = REST::get_instance()->rest_data(false);
228 $data['assets'] = self::ASSET_URL;
229 $data['is_pro'] = false;
230 $data['gmt_offset'] = get_option('gmt_offset');
231 $data['lang'] = get_locale();
232 $data['common_assets'] = NOTIFICATIONX_COMMON_URL;
233 $data['extra'] = [
234 'is_singular' => is_singular(),
235 'query' => $GLOBALS['wp_query']->query,
236 'queried_id' => get_queried_object_id(),
237 'pid' => !empty($GLOBALS['post']->ID) ? $GLOBALS['post']->ID : 0,
238 ];
239 $data['localeData'] = load_script_textdomain('notificationx-public', 'notificationx');
240 return $data;
241 }
242
243 public function get_notifications_data($params) {
244 $_params = $params;
245 $result = [
246 'global' => [],
247 'active' => [],
248 'pressbar' => [],
249 'gdpr' => [],
250 'shortcode' => [],
251 'popup' => [],
252 'exit_intent' => [],
253 ];
254 if (!empty($_params['all_active'])) {
255 $params = $this->get_notifications_ids();
256 }
257 $params = wp_parse_args(
258 $params,
259 [
260 'global' => [],
261 'active' => [],
262 'pressbar' => [],
263 'gdpr' => [],
264 'popup' => [],
265 'exit_intent' => [],
266 'shortcode' => [],
267 'inline_shortcode' => false,
268 ]
269 );
270 $global = $params['global'];
271 $active = $params['active'];
272 $pressbar = $params['pressbar'];
273 $gdpr = $params['gdpr'];
274 $popup = $params['popup'];
275 $exit_intent = $params['exit_intent'];
276 $shortcode = $params['shortcode'];
277 $all = array_merge($global, $active, $shortcode);
278 $_defaults = array(
279 'none' => '',
280 'name' => __('Someone', 'notificationx'),
281 'first_name' => __('Someone', 'notificationx'),
282 'last_name' => __('Someone', 'notificationx'),
283 'anonymous_title' => __('Anonymous Title', 'notificationx'),
284 'sometime' => __('Some time ago', 'notificationx'),
285 );
286
287 // foreach (['global', 'active'] as $key => $type) {
288 // foreach ($params[$type] as $id) {
289 // $result[$type][$id] = [];
290 // }
291 // }
292
293 $device = isset($params['deviceType']) && !empty($params['deviceType']) ? $params['deviceType'] : '';
294 if (!empty($all)) {
295 $notifications = $this->get_notifications($all, $device);
296 $entries = $this->get_entries($all, $notifications, $params);
297
298 foreach ($entries as $entry) {
299 $nx_id = $entry['nx_id'];
300 $settings = $notifications[$nx_id];
301
302 // check if notification is enabled or not.
303 if (!$settings['enabled']) {
304 continue;
305 }
306
307 $type = $settings['type'];
308 $source = $settings['source'];
309
310 $should_continue = apply_filters("nx_entry_show_on_frontend_$source", false, $entry, $settings);
311 if ( $should_continue ) {
312 continue;
313 }
314
315 if (!empty($entry['timestamp'])) {
316 $timestamp = $entry['timestamp'];
317 $display_from = !empty($settings['display_from']) ? $settings['display_from'] : 2;
318 $display_from = Helper::generate_time_string($settings);
319 if (!is_numeric($timestamp)) {
320 $entry['timestamp'] = $timestamp = is_string($timestamp) ? strtotime($timestamp) : false;
321 }
322 if ($timestamp && $display_from > $timestamp) {
323 if (apply_filters("nx_entry_display_$source", true, $entry, $settings)) {
324 continue;
325 }
326 }
327 }
328
329 $defaults = apply_filters("nx_fallback_data_$source", $_defaults, $entry, $settings);
330 $defaults = apply_filters('nx_fallback_data', $defaults, $entry, $settings);
331
332 $entry = $this->apply_defaults($entry, $defaults);
333 $entry['image_data'] = $this->get_image_url($entry, $settings);
334 if (!empty($entry['title'])) {
335 $entry['title'] = strip_tags(html_entity_decode($entry['title']));
336 }
337
338 $entry = apply_filters("nx_filtered_entry_$type", $entry, $settings);
339 $entry = apply_filters("nx_filtered_entry_$source", $entry, $settings);
340 $entry = apply_filters('nx_filtered_entry', $entry, $settings);
341 $entry = $this->link_url($entry, $settings, $params);
342
343 // @todo shortcode
344 // @todo check if the current page have shortcode.
345 if (in_array($nx_id, $shortcode)) {
346 $position = $settings['position'];
347 $settings['position'] = "notificationx-shortcode-$nx_id";
348 if (empty($result['shortcode'][$nx_id]['post'])) {
349 $result['shortcode'][$nx_id]['post'] = $settings;
350 }
351 $result['shortcode'][$nx_id]['entries'][] = $entry;
352 $settings['position'] = $position;
353 if ($settings['show_on'] === 'only_shortcode' || 'inline' === $settings['type'] || 'woocommerce_sales_inline' == $settings['source']) {
354 continue;
355 }
356 if (empty($global) && empty($active)) {
357 continue;
358 }
359 }
360
361 if (!empty($settings['global_queue'])) {
362 if (empty($result['global'][$nx_id]['post'])) {
363 $result['global'][$nx_id]['post'] = $settings;
364 }
365 $result['global'][$nx_id]['entries'][] = $entry;
366 } else {
367 if (empty($result['active'][$nx_id]['post'])) {
368 $result['active'][$nx_id]['post'] = $settings;
369 }
370 $result['active'][$nx_id]['entries'][] = $entry;
371 }
372 }
373
374 foreach ($result as &$group) {
375 foreach ($group as &$value) {
376 $value['entries'] = apply_filters("nx_filtered_data_{$value['post']['type']}", $value['entries'], $value['post'], $params);
377 $value['entries'] = apply_filters("nx_filtered_data_{$value['post']['source']}", $value['entries'], $value['post'], $params);
378 $value['entries'] = apply_filters('nx_filtered_data', $value['entries'], $value['post'], $params);
379 $value['post'] = apply_filters('nx_filtered_post', $value['post'], $params);
380 }
381 }
382 $result = apply_filters('nx_filtered_notice', $result, $params);
383 }
384
385 if (!empty($pressbar)) {
386 $notifications = $this->get_notifications($pressbar, $device);
387 foreach ($notifications as $key => $settings) {
388 $_nx_id = $settings['nx_id'];
389 // check if position is bottom_left then modify it to top
390 if($settings['position'] == 'bottom_left'){
391 $settings['position'] = 'top';
392 }
393 $elementor_post_id = isset($settings['elementor_id']) ? $settings['elementor_id'] : '';
394 if ($elementor_post_id == '' || get_post_status($elementor_post_id) !== 'publish' | !class_exists('\Elementor\Plugin')) {
395 $settings['elementor_id'] = false;
396 }
397 if ( ( !empty( $_params['all_active']) && $elementor_post_id ) || !$settings['enabled'] ) {
398 continue;
399 }
400
401 // $settings['button_url'] = apply_filters("nx_notification_link_{$settings['source']}", $settings['button_url'], $settings);
402 $settings['button_url'] = apply_filters('nx_notification_link', $settings['button_url'], $settings);
403 if (!empty($settings['button_url']) && strpos($settings['button_url'], '//') === false && strpos($settings['button_url'], './') === false) {
404 $settings['button_url'] = "//{$settings['button_url']}";
405 }
406 $bar_content = $this->get_bar_content($settings, false, $params);
407 if ($bar_content !== '&nbsp;' || !empty($settings['enable_countdown'])) {
408 $settings = apply_filters('nx_filtered_post', $settings, $params);
409 $result['pressbar'][$_nx_id]['post'] = $settings;
410 $result['pressbar'][$_nx_id]['content'] = $bar_content;
411 }
412
413 unset($_nx_id);
414 }
415 }
416
417 if (!empty($gdpr)) {
418 $notifications = $this->get_notifications($gdpr);
419 foreach ($notifications as $key => $settings) {
420 $_nx_id = $settings['nx_id'];
421 if (!empty($_params['all_active'])) {
422 continue;
423 }
424
425 $settings = apply_filters('nx_filtered_post', $settings, $params);
426
427 $result['gdpr'][$_nx_id]['post'] = $settings;
428 $result['gdpr'][$_nx_id]['content'] = "";
429 unset($_nx_id);
430 }
431 }
432
433 // Popup Notification
434 if (!empty($popup)) {
435 $popup_notifications = $this->get_notifications($popup);
436 foreach ($popup_notifications as $key => $settings) {
437 $_nx_id = $settings['nx_id'];
438 if ( !$settings['enabled'] ) {
439 continue;
440 }
441
442 $settings = apply_filters('nx_filtered_post', $settings, $params);
443
444 $result['popup'][$_nx_id]['post'] = $settings;
445 $result['popup'][$_nx_id]['content'] = "";
446 unset($_nx_id);
447 }
448 }
449
450 // Exit Intent Notification
451 if (!empty($exit_intent)) {
452 $exit_intent_notifications = $this->get_notifications($exit_intent);
453 foreach ($exit_intent_notifications as $key => $settings) {
454 $_nx_id = $settings['nx_id'];
455 if ( !$settings['enabled'] ) {
456 continue;
457 }
458 $settings = apply_filters('nx_filtered_post', $settings, $params);
459 $result['exit_intent'][$_nx_id]['post'] = $settings;
460 $result['exit_intent'][$_nx_id]['content'] = "";
461 unset($_nx_id);
462 }
463 }
464
465 $result['settings'] = $this->get_settings();
466 return $result;
467 }
468
469 public function get_settings(){
470
471 $branding_url = apply_filters('nx_branding_url', NOTIFICATIONX_PLUGIN_URL . '?utm_source=' . esc_url(home_url()) . '&utm_medium=notificationx');
472 $settings = [
473 'disable_powered_by' => Settings::get_instance()->get('settings.disable_powered_by'),
474 'affiliate_link' => $branding_url,
475 'enable_analytics' => Settings::get_instance()->get('settings.enable_analytics', true),
476 'analytics_from' => Settings::get_instance()->get('settings.analytics_from'),
477 'delay_before' => Settings::get_instance()->get('settings.delay_before', 5),
478 'display_for' => Settings::get_instance()->get('settings.display_for', 5),
479 'delay_between' => Settings::get_instance()->get('settings.delay_between', 5),
480 'loop' => Settings::get_instance()->get('settings.loop', 5),
481 'random' => Settings::get_instance()->get('settings.random', 5),
482 'analytics_nonce' => wp_create_nonce('analytics_nonce'),
483 ];
484 return $settings;
485 }
486
487 public function get_notifications_ids($return_posts = false, $args = []) {
488 $args = wp_parse_args($args, [
489 'enabled' => true,
490 'updated_at' => ['<=', Helper::mysql_time()],
491 // 'global_queue' => true,
492 ]);
493 $notifications = PostType::get_instance()->get_posts($args);
494
495 $active_notifications = $global_notifications = $bar_notifications = $gdpr_notification = $popup_notifications = $exit_intent_notifications = array();
496
497 foreach ($notifications as $key => $settings) {
498 // $settings = NotificationX::get_instance()->normalize_post($post);
499
500 // IF PRESSBAR TIME RE_CONFIG THEN REMOVE COOKIE
501 // if( $settings['source'] == 'press_bar'){
502 // if( $_settings->enable_countdown && ( Helper::current_timestamp($_settings->countdown_start_date) < time() || Helper::current_timestamp($_settings->countdown_end_date) > time() ) ) {
503 // unset( $_COOKIE["notificationx_{$settings['nx_id']}"] );
504 // \setcookie("notificationx_{$settings['nx_id']}", null);
505 // }
506 // }
507
508 /**
509 * Check if it's a pro source and pro plugin is disabled.
510 */
511 if(!NotificationX::is_pro()){
512 $ext = ExtensionFactory::get_instance()->get($settings['source']);
513 if($ext && $ext->is_pro){
514 continue;
515 }
516 }
517
518 $countdown_rand = !empty($settings['countdown_rand']) ? "-{$settings['countdown_rand']}" : '';
519
520 if (!empty($_COOKIE["notificationx_{$settings['nx_id']}$countdown_rand"]) && $_COOKIE["notificationx_{$settings['nx_id']}$countdown_rand"] == true) {
521 unset($notifications[$key]);
522 continue;
523 }
524
525 if($this->is_logged_in($settings['show_on_display'])){
526 continue;
527 }
528
529 $locations = isset($settings['all_locations']) ? $settings['all_locations'] : [];
530 $custom_ids = isset($settings['custom_ids']) ? $settings['custom_ids'] : [];
531 $taxonomy_ids = isset($settings['taxonomy_ids']) ? $settings['taxonomy_ids'] : [];
532
533 if($this->check_show_on($locations, $custom_ids, $settings['show_on'], $taxonomy_ids)){
534 continue;
535 }
536
537 /**
538 * Check for hiding in mobile device
539 */
540 // if ($settings['hide_on_mobile'] && wp_is_mobile()) {
541 // continue;
542 // }
543
544 $show_on_exclude = apply_filters('nx_show_on_exclude', false, $settings);
545 if ($show_on_exclude) {
546 continue;
547 }
548
549 // excluding pressBar on rest request. PressBar is printed directly in head.
550 // if((empty($args['source']) || $args['source'] !== 'press_bar') && $settings['source'] == 'press_bar'){
551 // continue;
552 // }
553
554 $active_global_queue = boolval($settings['global_queue']);
555 if ($settings['source'] == 'press_bar') {
556 // if(
557 // !$_settings->elementor_id &&
558 // $_settings->enable_countdown &&
559 // !$_settings->evergreen_timer &&
560 // (
561 // strtotime($_settings->countdown_start_date) > time() ||
562 // strtotime($_settings->countdown_end_date) < time()
563 // )
564 // ){
565 // continue;
566 // }
567
568 $bar_notifications[] = $return_posts ? $settings : $settings['nx_id'];
569 if (!empty($settings['elementor_id']) && class_exists('\Elementor\Plugin')) {
570 // @todo Find a function to only load css instead of building content.
571 \Elementor\Plugin::$instance->frontend->get_builder_content($settings['elementor_id'], false);
572 }
573 } elseif($settings['source'] == 'gdpr_notification') {
574 $gdpr_notification[] = $return_posts ? $settings : $settings['nx_id'];
575 } elseif($settings['source'] == 'popup_notification') {
576 $popup_notifications[] = $settings['nx_id'];
577 } elseif($settings['source'] == 'exit_intent_custom') {
578 $exit_intent_notifications[] = $settings['nx_id'];
579 } elseif ($active_global_queue && NotificationX::is_pro()) {
580 $global_notifications[] = $return_posts ? $settings : $settings['nx_id'];
581 } else {
582 $active_notifications[] = $return_posts ? $settings : $settings['nx_id'];
583 }
584
585 unset($notifications[$key]);
586 }
587 // do_action('nx_active_notificationx', $notifications);
588
589 // @todo maybe combine two hooks.
590
591 return apply_filters(
592 'get_notifications_ids',
593 [
594 'global' => $global_notifications,
595 'active' => $active_notifications,
596 'pressbar' => $bar_notifications,
597 'gdpr' => $gdpr_notification,
598 'popup' => $popup_notifications,
599 'exit_intent' => $exit_intent_notifications,
600 'total' => (count($global_notifications) + count($active_notifications) + count($bar_notifications) + count($gdpr_notification) + count($popup_notifications) + count($exit_intent_notifications)),
601 ],
602 $notifications
603 );
604 }
605
606 public function is_logged_in($show_on_display){
607 $logged_in = is_user_logged_in();
608
609 if (($logged_in && 'logged_out_user' == $show_on_display) || (!$logged_in && 'logged_in_user' == $show_on_display)) {
610 return true;
611 }
612 return false;
613 }
614
615 /**
616 * @todo filter is not extensive enough.
617 */
618 public function check_show_on($locations, $custom_ids, $show_on, $taxonomy_ids = ''){
619 $check_location = false;
620
621 if ($locations == 'is_custom' || is_array($locations) && in_array('is_custom', $locations)) {
622 $custom_ids = !empty($custom_ids) ? $custom_ids : [];
623 }
624 if (!empty($locations)) {
625 // @todo need to pass url.
626 $check_location = Locations::get_instance()->check_location($locations, $custom_ids, $taxonomy_ids);
627 }
628
629 $check_location = apply_filters('nx_check_location', $check_location, $custom_ids, $show_on);
630
631 if ($show_on == 'on_selected') {
632 // show if the page is on selected
633 if (!$check_location) {
634 return true;
635 }
636 } elseif ($show_on == 'hide_on_selected') {
637 // hide if the page is on selected
638 if ($check_location) {
639 return true;
640 }
641 } elseif ($show_on === 'only_shortcode') {
642 return true;
643 }
644 return false;
645 }
646
647 public function get_notifications($ids, $device = '') {
648 $results = [];
649 $notifications = PostType::get_instance()->get_posts_by_ids($ids);
650
651 foreach ($notifications as $key => $value) {
652 /**
653 * Check for hiding in mobile device
654 */
655 if (empty($value['hide_on_mobile']) && $device === 'mobile') {
656 continue;
657 }
658
659 /**
660 * Check for hiding in tablet device
661 */
662 if (empty($value['hide_on_tab']) && $device === 'tablet') {
663 continue;
664 }
665
666 /**
667 * Check for hiding in desktop device
668 */
669 if (empty($value['hide_on_desktop']) && $device === 'desktop') {
670 continue;
671 }
672
673 /**
674 * Check if it's a pro source and pro plugin is disabled.
675 */
676 if(!NotificationX::is_pro()){
677 $ext = ExtensionFactory::get_instance()->get($value['source']);
678 if($ext && $ext->is_pro){
679 continue;
680 }
681 }
682
683 $results[$value['nx_id']] = $value;
684 }
685 return $results;
686 }
687
688 public function get_entries($ids, $notifications,$params) {
689 $entries = [];
690 if (!empty($ids) && is_array($ids)) {
691 $query = [];
692 foreach ($ids as $id) {
693 if (!empty($notifications[$id])) {
694 $post = $notifications[$id];
695 $global_query = " nx_id = " . absint($id) . " AND source = '" . esc_sql($post['source']) . "'";
696 $_q = apply_filters("nx_get_entries_query_part_{$notifications[$id]['source']}",$global_query, $notifications[$id], $params );
697 $query[$id] = " (" . $_q . ")";
698 }
699 }
700 if (!empty($query)) {
701 $entries = Entries::get_instance()->get_entries('WHERE' . implode(' OR ', $query));
702 foreach ($entries as $key => $value) {
703 if (!empty($value['data'])) {
704 $entries[$key] = array_merge($value, $value['data']);
705 unset($entries[$key]['data']);
706 }
707 }
708 }
709 }
710 if (!is_array($entries)) {
711 $entries = [];
712 }
713 $entries = apply_filters('nx_frontend_get_entries', $entries, $ids, $notifications,$params);
714 return $entries;
715 }
716
717 /**
718 * This function is responsible for make ready the link for notifications.
719 *
720 * @param array $data
721 * @return void
722 */
723 public static function link_url($entry, $post, $params = []) {
724 if (empty($entry)) {
725 return false;
726 }
727 $link = isset($entry['link']) ? $entry['link'] : '';
728 // removing link if link type is none.
729 if (empty($post['link_type']) || $post['link_type'] === 'none') {
730 $link = '';
731 }
732
733 $link = apply_filters("nx_notification_link_{$post['source']}", $link, $post, $entry, $params);
734 $entry['link'] = apply_filters('nx_notification_link', $link, $post, $entry, $params);
735 return $entry;
736 }
737
738 /**
739 * This function is responsible for getting the image url
740 * using Product ID or from default image settings.
741 *
742 * @param array $data
743 * @param array $settings
744 * @return array of image data, contains url and title as alt text
745 */
746 public function get_image_url($data, $settings) {
747 $source = $settings['source'];
748 $alt_title = isset($data['name']) ? $data['name'] : '';
749 $image_type = isset($settings['show_notification_image']) ? $settings['show_notification_image'] : false;
750 if (empty($alt_title)) {
751 $alt_title = isset($data['title']) ? $data['title'] : '';
752 }
753 $image_data = [
754 'url' => '',
755 'alt' => $alt_title,
756 ];
757
758 if ($settings['show_default_image']) {
759 if (!empty($settings['image_url']['url'])) {
760 $image_size = (string) Settings::get_instance()->get('settings.notification_image_size', '100_100');
761 $image_size = explode('_', $image_size);
762 $width = 100;
763 $height = 100;
764 if( !empty( $image_size[0] ) && !empty( $image_size[1] ) ) {
765 $width = $image_size[0];
766 $height = $image_size[1];
767 }
768 $image = wp_get_attachment_image_src($settings['image_url']['id'], [$width, $height], true);
769 $image_data['url'] = $image[0];
770 } else {
771 $default_avatar = $settings['default_avatar'];
772 $image_data['url'] = NOTIFICATIONX_PUBLIC_URL . 'image/icons/' . $default_avatar;
773 }
774 } else {
775 if ($image_type === 'gravatar') {
776 $_data = array_change_key_case($data);
777 if (isset($_data['email'])) {
778 $image_data['url'] = get_avatar_url($_data['email'], ['size' => '100']);
779 }
780 }
781 }
782
783 $image_data['classes'] = $image_type;
784 $image_data = apply_filters("nx_notification_image_$source", $image_data, $data, $settings);
785 $image_data = apply_filters('nx_notification_image', $image_data, $data, $settings);
786
787 if (!empty($image_data['url'])) {
788 return $image_data;
789 }
790
791 return false;
792 }
793
794 public function apply_defaults($entry, $defaults) {
795
796 foreach ($defaults as $key => $value) {
797 // @todo mukul remove `|| empty(trim($entry[$key]))`
798 if (empty($entry[$key]) || (is_string($entry[$key]) && empty(trim($entry[$key])))) {
799 $entry[$key] = $value;
800 }
801 }
802
803 return $entry;
804 }
805
806 public function fallback_data($data, $saved_data, $settings) {
807 if ((empty($saved_data['name']) || $data['name'] == __('Someone', 'notificationx')) && isset($saved_data['first_name']) || isset($saved_data['last_name'])) {
808 $first_name = isset($saved_data['first_name']) ? $saved_data['first_name'] : '';
809 $last_name = isset($saved_data['last_name']) ? $saved_data['last_name'] : '';
810 $data['name'] = Helper::name($first_name, $last_name);
811 }
812 if (!empty($saved_data['name']) && empty($saved_data['first_name']) && empty($saved_data['last_name'])) {
813 $data['first_name'] = $saved_data['name'];
814 }
815 $data['title'] = isset($saved_data['post_title']) ? $saved_data['post_title'] : (isset($data['title']) ? $data['title'] : '');
816 return $data;
817 }
818
819 /**
820 * Add NotificationX in Footer
821 *
822 * @return void
823 */
824 public function filtered_data($entries, $post, $params) {
825 if (is_array($entries) && (!defined('NX_DEBUG') || !NX_DEBUG)) {
826 if (!empty($post['display_last']) && !in_array($post['source'], ['google', 'woo_inline', 'edd_inline', 'tutor_inline', 'learndash_inline', 'google_reviews', 'youtube','woocommerce_sales_inline','fluentcart_inline'])) {
827 $entries = array_slice($entries, 0, $post['display_last']);
828 }
829 foreach ($entries as $index => $entry) {
830 $_entry = apply_filters("nx_frontend_keep_entry_{$post['source']}", [
831 'nx_id' => $entry['nx_id'],
832 'timestamp' => isset($entry['timestamp']) ? $entry['timestamp'] : Helper::current_timestamp($entry['updated_at']),
833 'updated_at' => isset( $entry['updated_at'] ) ? $entry['updated_at'] : '',
834 'image_data' => $entry['image_data'],
835 'link' => $entry['link'],
836 ], $entry, $post, $params);
837 if (!empty($params['inline_shortcode']) && isset($entry['product_id'])) {
838 $_entry['product_id'] = $entry['product_id'];
839 }
840
841 $template_arr = array_values($post['notification-template']);
842 if ($post['template_adv']) {
843 $adv_template = $post['advanced_template'];
844 $pattern = "/{{(.+?)}}/i";
845 if (preg_match_all($pattern, $adv_template, $matches)) {
846 $template_arr = $matches[1];
847 }
848 }
849 if (is_array($template_arr)) {
850 foreach ($template_arr as $entry_key) {
851 $_entry_key = $entry_key;
852 if ($entry_key == 'tag_siteview' || $entry_key == 'tag_realtime_siteview') {
853 $entry_key = 'views';
854 } elseif ($entry_key == 'ga_title' || $entry_key == 'ga_page_title') {
855 $entry_key = 'title';
856 } elseif (strpos($entry_key, 'tag_product_') === 0) {
857 $entry_key = str_replace('tag_product_', '', $entry_key);
858 } elseif (strpos($entry_key, 'tag_') === 0) {
859 $entry_key = str_replace('tag_', '', $entry_key);
860 } elseif (strpos($entry_key, 'product_') === 0) {
861 $entry_key = str_replace('product_', '', $entry_key);
862 }
863
864 if (isset($entry[$entry_key])) {
865 $_entry[$entry_key] = $entry[$entry_key];
866 }
867 if (isset($entry[$_entry_key])) {
868 $_entry[$_entry_key] = $entry[$_entry_key];
869 }
870 }
871 $entries[$index] = $_entry;
872 }
873 }
874 }
875 return $entries;
876 }
877
878 /**
879 * Remove unnecessary props from post in frontend.
880 *
881 * @return void
882 */
883 public function filtered_post($post, $params = null) {
884 if (is_array($post) && empty($params['inline_shortcode']) && (!defined('NX_DEBUG') || !NX_DEBUG)) {
885 $ignore_props = [
886 'all_locations',
887 'category_list',
888 'combine_multiorder_text',
889 'content_trim_length',
890 'convertkit_form',
891 'currentTab',
892 'custom_contents',
893 'custom_ids',
894 'default_avatar',
895 'elementor_edit_link',
896 'enabled',
897 'exclude_categories',
898 'exclude_products',
899 'form_list',
900 'freemius_item_type',
901 'freemius_plugins',
902 'freemius_themes',
903 'give_form_list',
904 'give_forms_control',
905 'image_url',
906 'inline_location',
907 'is_confirmed',
908 'is_elementor',
909 'is_inline',
910 'ld_course_list',
911 'ld_product_control',
912 'mailchimp_list',
913 'max_stock',
914 'nx-bar_with_elementor',
915 'nx-bar_with_elementor-remove',
916 'nx-bar_with_elementor_install',
917 'order_status',
918 'press_content',
919 'preview',
920 'product_control',
921 'product_exclude_by',
922 'product_list',
923 'rest_route',
924 'show_default_image',
925 'show_notification_image',
926 'show_on',
927 'show_on_display',
928 'source_error',
929 'utm_campaign',
930 'utm_medium',
931 'utm_source',
932 'wp_reviews_product_type',
933 'wp_reviews_slug',
934 'wp_stats_product_type',
935 'wp_stats_slug',
936 '_locale',
937 '__product_list',
938 '__form_list',
939 '__ld_course_list',
940 '__give_form_list',
941 ];
942 foreach ($ignore_props as $prop) {
943 if (isset($post[$prop])) {
944 unset($post[$prop]);
945 }
946 }
947 }
948 if (isset($post['template_adv'], $post['advanced_template'])) {
949 $post['advanced_template'] = do_shortcode($post['advanced_template']);
950 }
951 if( !empty( $post['notification-template'] ) ) {
952 $post['notification-template'] = array_map( 'esc_html', $post['notification-template'] );
953 }
954 return $post;
955 }
956
957 public function get_bar_content($settings, $suppress_filters = false, $params = []){
958 $bar_content = PressBar::get_instance()->print_bar_notice($settings);
959 if(!$suppress_filters){
960 $bar_content = apply_filters("nx_filtered_data_{$settings['source']}", $bar_content, $settings, $params);
961 }
962
963 // checking if content is empty
964 $_bar_content = str_replace(array("\r\n", "\n", "\r"), '', $bar_content);
965 $_bar_content = trim(strip_tags($_bar_content));
966 if (empty($_bar_content) && !empty($settings['enable_countdown'])) {
967 $bar_content = '&nbsp;';
968 }
969 return $bar_content;
970 }
971
972 }
973