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.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 1.2.0 1.2.1 All 156 releases
notificationx / includes / Types / NotificationBar.php

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

239 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Extension Abstract
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Types;
10
11 use NotificationX\Core\Helper;
12 use NotificationX\Extensions\GlobalFields;
13 use NotificationX\GetInstance;
14 use NotificationX\Modules;
15
16 /**
17 * Extension Abstract for all Extension.
18 * @method static NotificationBar get_instance($args = null)
19 */
20 class NotificationBar extends Types {
21 /**
22 * Instance of Admin
23 *
24 * @var Admin
25 */
26 use GetInstance;
27
28 public $priority = 15;
29 public $themes = [];
30 public $module = [];
31 public $default_source = 'press_bar';
32 public $link_type = '-1';
33
34
35 /**
36 * Initially Invoked when initialized.
37 */
38 public function __construct() {
39 parent::__construct();
40 $this->id = 'notification_bar';
41 add_filter('nx_show_on_exclude', [$this, 'show_on_exclude'], 10, 2);
42 }
43
44
45
46 /**
47 * Determines whether to exclude the notification bar from showing
48 * based on various conditions like bar reappearance, schedule settings,
49 * country targeting, and user role targeting.
50 *
51 * @param bool $exclude Current exclusion status
52 * @param array $settings Notification settings
53 * @return bool True if should be excluded, false otherwise
54 */
55 public function show_on_exclude($exclude, $settings) {
56 // Only process if this is a PressBar notification
57 // if (empty($settings['type']) || $settings['type'] !== $this->id) {
58 // return $exclude;
59 // }
60
61 // 1. Bar Reappearance settings
62 if (!empty($settings['bar_reappearance'])) {
63 $cookie_name = 'nx_bar_' . $settings['nx_id'];
64
65 // Check if the bar should be permanently hidden for this user
66 if ($settings['bar_reappearance'] === 'dont_show_welcomebar' && isset($_COOKIE[$cookie_name])) {
67 return true;
68 }
69
70 // Check if the bar should only show on new visits (not refreshes/page changes)
71 if ($settings['bar_reappearance'] === 'show_welcomebar_next_visit' &&
72 isset($_COOKIE[$cookie_name]) &&
73 $_COOKIE[$cookie_name] === 'shown' &&
74 !empty($_SERVER['HTTP_REFERER']) &&
75 strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== false) {
76 return true;
77 }
78 }
79
80 // 2. Schedule Settings
81 if (!empty($settings['schedule_type'])) {
82 $current_time = current_time('timestamp', true);
83
84 // Daily schedule check
85 if ($settings['schedule_type'] === 'daily' &&
86 !empty($settings['daily_from_time']) &&
87 !empty($settings['daily_to_time'])) {
88
89 $from_time = strtotime(date('Y-m-d ') . date('H:i:s', strtotime($settings['daily_from_time'])));
90 $to_time = strtotime(date('Y-m-d ') . date('H:i:s', strtotime($settings['daily_to_time'])));
91
92 // Handle case where to_time is on the next day
93 if ($to_time < $from_time) {
94 $to_time += 86400; // Add 24 hours
95 }
96
97 if ($current_time < $from_time || $current_time > $to_time) {
98 return true;
99 }
100 }
101
102 // Weekly schedule check
103 if ($settings['schedule_type'] === 'weekly' &&
104 !empty($settings['weekly_days']) &&
105 !empty($settings['weekly_from_time']) &&
106 !empty($settings['weekly_to_time'])) {
107
108 $current_day = strtolower(date('l', $current_time));
109 $from_time = strtotime(date('Y-m-d ') . date('H:i:s', strtotime($settings['weekly_from_time'])));
110 $to_time = strtotime(date('Y-m-d ') . date('H:i:s', strtotime($settings['weekly_to_time'])));
111
112 // Handle case where to_time is on the next day
113 if ($to_time < $from_time) {
114 $to_time += 86400; // Add 24 hours
115 }
116
117 // Check if current day is in the selected days
118 $show_today = false;
119 foreach ($settings['weekly_days'] as $day) {
120 if (strtolower($day) === $current_day) {
121 $show_today = true;
122 break;
123 }
124 }
125
126 if (!$show_today || $current_time < $from_time || $current_time > $to_time) {
127 return true;
128 }
129 }
130 // Custom schedule check
131 if (
132 $settings['schedule_type'] === 'custom' &&
133 !empty($settings['custom_schedule']['startDate']) &&
134 !empty($settings['custom_schedule']['endDate']) &&
135 !empty($settings['custom_from_time']) &&
136 !empty($settings['custom_to_time'])
137 ) {
138 // Parse date range (strip timezone)
139 $start_date = strtotime(substr($settings['custom_schedule']['startDate'], 0, 10));
140 $end_date = strtotime(substr($settings['custom_schedule']['endDate'], 0, 10));
141 $current_date = strtotime(gmdate('Y-m-d', $current_time)); // Current day in GMT
142 $custom_from_time = $settings['custom_from_time'];
143 $custom_to_time = $settings['custom_to_time'];
144 // Check if current date is outside range
145 if ($current_date < $start_date || $current_date > $end_date) {
146 return true;
147 }
148
149 // Combine current date with from/to times
150 $from_time = strtotime(date('Y-m-d ') . date('H:i:s', strtotime($custom_from_time)));
151 $to_time = strtotime(date('Y-m-d ') . date('H:i:s', strtotime($custom_to_time)));
152
153 // Handle overnight time ranges (e.g., 10 PM - 6 AM)
154 if ($to_time < $from_time) {
155 $to_time += 86400; // add 24 hours
156 if ($current_time < $from_time) {
157 $current_time += 86400;
158 }
159 }
160
161 // Check if current timestamp is outside the daily time range
162 if ($current_time < $from_time || $current_time > $to_time) {
163 return true;
164 }
165 }
166 }
167
168 // 3. Country Targeting
169 if ( !empty($settings['country_targeting']) && is_array($settings['country_targeting']) && !in_array('all', $settings['country_targeting'])) {
170 $visitor_country = Helper::nx_get_visitor_country_code();
171 // If we couldn't determine the country or it's not in the target list
172 $countryValues = array_column( $settings['country_targeting'], 'value' );
173 $normalizedCountries = array_map('strtoupper', $countryValues);
174 $visitor_country = strtoupper($visitor_country);
175 if (empty($visitor_country)) {
176 return true;
177 }
178 // Only apply country filtering if 'ALL' is not in the list
179 if (!in_array('ALL', $normalizedCountries)) {
180 if (empty($visitor_country) || !in_array($visitor_country, $normalizedCountries)) {
181 return true;
182 }
183 }
184 }
185
186 // 4. User Role Targeting
187 if (!empty($settings['targeting_user_roles']) &&
188 !in_array('all_users', $settings['targeting_user_roles'])) {
189
190 // For logged out users
191 if (!is_user_logged_in() && !in_array('guest', $settings['targeting_user_roles'])) {
192 return true;
193 }
194
195 // For logged in users
196 if (is_user_logged_in()) {
197 $user = wp_get_current_user();
198 $user_roles = (array) $user->roles;
199
200 // Check if any of the user's roles match the targeted roles
201 $has_targeted_role = false;
202 foreach ($user_roles as $role) {
203 if (in_array($role, $settings['targeting_user_roles'])) {
204 $has_targeted_role = true;
205 break;
206 }
207 }
208
209 if (!$has_targeted_role) {
210 return true;
211 }
212 }
213 }
214
215 // If we've made it here, don't exclude the notification
216 return $exclude;
217 }
218
219 public static function restResponse($params) {
220 if (empty($params['inputValue'])) {
221 return [];
222 }
223 return array_values(GlobalFields::get_instance()->normalize_fields(Helper::nx_get_all_country($params['inputValue'])));
224 }
225
226
227 /**
228 * Runs when modules is enabled.
229 *
230 * @return void
231 */
232 public function init() {
233 parent::init();
234 $this->title = __('Notification Bar', 'notificationx');
235 }
236
237
238 }
239