| @@ -1,0 +1,197 @@ | ||
| 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( | |
| 76 | + esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER'])), | |
| 77 | + isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : '' | |
| 78 | + ) !== false) { | |
| 79 | + return true; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + // 2. Schedule Settings | |
| 84 | + if (!empty($settings['schedule_type'])) { | |
| 85 | + $current_time = current_time('timestamp', true); | |
| 86 | + | |
| 87 | + // Daily schedule check | |
| 88 | + if ($settings['schedule_type'] === 'daily' && | |
| 89 | + !empty($settings['daily_from_time']) && | |
| 90 | + !empty($settings['daily_to_time'])) { | |
| 91 | + | |
| 92 | + $from_time = strtotime(gmdate('Y-m-d ') . gmdate('H:i:s', strtotime($settings['daily_from_time']))); | |
| 93 | + $to_time = strtotime(gmdate('Y-m-d ') . gmdate('H:i:s', strtotime($settings['daily_to_time']))); | |
| 94 | + | |
| 95 | + // Handle case where to_time is on the next day | |
| 96 | + if ($to_time < $from_time) { | |
| 97 | + $to_time += 86400; // Add 24 hours | |
| 98 | + } | |
| 99 | + | |
| 100 | + if ($current_time < $from_time || $current_time > $to_time) { | |
| 101 | + return true; | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + // Weekly schedule check | |
| 106 | + if ($settings['schedule_type'] === 'weekly' && | |
| 107 | + !empty($settings['weekly_days']) && | |
| 108 | + !empty($settings['weekly_from_time']) && | |
| 109 | + !empty($settings['weekly_to_time'])) { | |
| 110 | + | |
| 111 | + $current_day = strtolower(gmdate('l', $current_time)); | |
| 112 | + $from_time = strtotime(gmdate('Y-m-d ') . gmdate('H:i:s', strtotime($settings['weekly_from_time']))); | |
| 113 | + $to_time = strtotime(gmdate('Y-m-d ') . gmdate('H:i:s', strtotime($settings['weekly_to_time']))); | |
| 114 | + | |
| 115 | + // Handle case where to_time is on the next day | |
| 116 | + if ($to_time < $from_time) { | |
| 117 | + $to_time += 86400; // Add 24 hours | |
| 118 | + } | |
| 119 | + | |
| 120 | + // Check if current day is in the selected days | |
| 121 | + $show_today = false; | |
| 122 | + foreach ($settings['weekly_days'] as $day) { | |
| 123 | + if (strtolower($day) === $current_day) { | |
| 124 | + $show_today = true; | |
| 125 | + break; | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + if (!$show_today || $current_time < $from_time || $current_time > $to_time) { | |
| 130 | + return true; | |
| 131 | + } | |
| 132 | + } | |
| 133 | + // Custom schedule check | |
| 134 | + if ( | |
| 135 | + $settings['schedule_type'] === 'custom' && | |
| 136 | + !empty($settings['custom_schedule']['startDate']) && | |
| 137 | + !empty($settings['custom_schedule']['endDate']) && | |
| 138 | + !empty($settings['custom_from_time']) && | |
| 139 | + !empty($settings['custom_to_time']) | |
| 140 | + ) { | |
| 141 | + // Parse date range (strip timezone) | |
| 142 | + $start_date = strtotime(substr($settings['custom_schedule']['startDate'], 0, 10)); | |
| 143 | + $end_date = strtotime(substr($settings['custom_schedule']['endDate'], 0, 10)); | |
| 144 | + $current_date = strtotime(gmdate('Y-m-d', $current_time)); // Current day in GMT | |
| 145 | + $custom_from_time = $settings['custom_from_time']; | |
| 146 | + $custom_to_time = $settings['custom_to_time']; | |
| 147 | + // Check if current date is outside range | |
| 148 | + if ($current_date < $start_date || $current_date > $end_date) { | |
| 149 | + return true; | |
| 150 | + } | |
| 151 | + | |
| 152 | + // Combine current date with from/to times | |
| 153 | + $from_time = strtotime(gmdate('Y-m-d ') . gmdate('H:i:s', strtotime($custom_from_time))); | |
| 154 | + $to_time = strtotime(gmdate('Y-m-d ') . gmdate('H:i:s', strtotime($custom_to_time))); | |
| 155 | + | |
| 156 | + // Handle overnight time ranges (e.g., 10 PM - 6 AM) | |
| 157 | + if ($to_time < $from_time) { | |
| 158 | + $to_time += 86400; // add 24 hours | |
| 159 | + if ($current_time < $from_time) { | |
| 160 | + $current_time += 86400; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + | |
| 164 | + // Check if current timestamp is outside the daily time range | |
| 165 | + if ($current_time < $from_time || $current_time > $to_time) { | |
| 166 | + return true; | |
| 167 | + } | |
| 168 | + } | |
| 169 | + } | |
| 170 | + | |
| 171 | + // Country & user-role targeting are enforced globally for every type in | |
| 172 | + // NotificationX\Core\Targeting (also hooked on nx_show_on_exclude). | |
| 173 | + | |
| 174 | + // If we've made it here, don't exclude the notification | |
| 175 | + return $exclude; | |
| 176 | + } | |
| 177 | + | |
| 178 | + public static function restResponse($params) { | |
| 179 | + if (empty($params['inputValue'])) { | |
| 180 | + return []; | |
| 181 | + } | |
| 182 | + return array_values(GlobalFields::get_instance()->normalize_fields(Helper::nx_get_all_country($params['inputValue']))); | |
| 183 | + } | |
| 184 | + | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Runs when modules is enabled. | |
| 188 | + * | |
| 189 | + * @return void | |
| 190 | + */ | |
| 191 | + public function init() { | |
| 192 | + parent::init(); | |
| 193 | + $this->title = __('Notification Bar', 'notificationx'); | |
| 194 | + } | |
| 195 | + | |
| 196 | + | |
| 197 | +} | |