PluginProbe
Popup Maker – Responsive popup, Exit Intent Pop up, Email Optins, Autoresponder & More / trunk
Popup Maker – Responsive popup, Exit Intent Pop up, Email Optins, Autoresponder & More vtrunk
1.4.5 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.6 1.0.7 1.0.8 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.6 1.1.8 1.1.9 1.1.9.1 1.1.9.3 1.1.9.4 1.1.9.6 1.2.1.4 1.2.2.1 1.2.2.2 All 39 releases
popup-maker-wp / com / classes / SGPMNotificationEngine.php

SGPMNotificationEngine.php in Popup Maker – Responsive popup, Exit Intent Pop up, Email Optins, Autoresponder & More trunk, at com/classes/SGPMNotificationEngine.php

309 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SGPMNotificationEngine
4 {
5 private static $instance = null;
6
7 const ACTIVE_NOTIFICATION = 1;
8
9 private $activeNotificationsCount = 0;
10 private $activeNotifications = array();
11 private $dismissedNotifications = array();
12 private $statusDisabled = '0';
13
14 private $reviewNotice = array(
15 'hash' => false,
16 'status' => true,
17 'type' => 'review',
18 'title' => 'Leave a Review',
19 'text' => 'Popup Maker team is happy you are using our popups for a while and we hope our plugin helps you to improve the conversion of your website. We’d be very thankful and inspired if you can support us and have your input with your positive feedback.',
20 'links' => array(
21 array(
22 'className' => 'sgpm-review',
23 'label' => 'Leave a Review Here!',
24 'url' => 'https://wordpress.org/support/plugin/popup-maker-wp/reviews/#new-post'
25 )
26 )
27 );
28
29 private $notificationBellIconSvg = '<svg class="sgpm-notification-bell-icon sgpm-bell-ring-animation" height="10pt" viewBox="-21 0 512 512" width="10pt" xmlns="http://www.w3.org/2000/svg"><path d="m448 232.148438c-11.777344 0-21.332031-9.554688-21.332031-21.332032 0-59.839844-23.296875-116.074218-65.601563-158.402344-8.339844-8.339843-8.339844-21.820312 0-30.164062 8.339844-8.339844 21.824219-8.339844 30.164063 0 50.371093 50.367188 78.101562 117.335938 78.101562 188.566406 0 11.777344-9.554687 21.332032-21.332031 21.332032zm0 0"/><path d="m21.332031 232.148438c-11.773437 0-21.332031-9.554688-21.332031-21.332032 0-71.230468 27.734375-138.199218 78.101562-188.566406 8.339844-8.339844 21.824219-8.339844 30.164063 0 8.34375 8.34375 8.34375 21.824219 0 30.164062-42.304687 42.304688-65.597656 98.5625-65.597656 158.402344 0 11.777344-9.558594 21.332032-21.335938 21.332032zm0 0"/><path d="m434.753906 360.8125c-32.257812-27.265625-50.753906-67.117188-50.753906-109.335938v-59.476562c0-75.070312-55.765625-137.214844-128-147.625v-23.042969c0-11.796875-9.558594-21.332031-21.332031-21.332031-11.777344 0-21.335938 9.535156-21.335938 21.332031v23.042969c-72.253906 10.410156-128 72.554688-128 147.625v59.476562c0 42.21875-18.496093 82.070313-50.941406 109.503907-8.300781 7.105469-13.058594 17.429687-13.058594 28.351562 0 20.589844 16.746094 37.335938 37.335938 37.335938h352c20.585937 0 37.332031-16.746094 37.332031-37.335938 0-10.921875-4.757812-21.246093-13.246094-28.519531zm0 0"/><path d="m234.667969 512c38.632812 0 70.953125-27.542969 78.378906-64h-156.757813c7.421876 36.457031 39.742188 64 78.378907 64zm0 0"/></svg>';
30
31 private $clearAllIconSvg = '<svg class="sgpm-clear-all-notifications" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" height="16pt" width="16pt" viewBox="0 0 384 384" style="enable-background:new 0 0 384 384;" xml:space="preserve"><g><g><g><rect x="42.667" y="170.667" width="298.667" height="42.667"/><rect x="0" y="256" width="298.667" height="42.667"/><rect x="85.333" y="85.333" width="298.667" height="42.667"/></g></g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g></svg>';
32
33 private $closeNotifictionIconSvg = '<svg class="sgpm-close-notifications" enable-background="new 0 0 386.667 386.667" height="10pt" viewBox="0 0 386.667 386.667" width="10pt" xmlns="http://www.w3.org/2000/svg"><path d="m386.667 45.564-45.564-45.564-147.77 147.769-147.769-147.769-45.564 45.564 147.769 147.769-147.769 147.77 45.564 45.564 147.769-147.769 147.769 147.769 45.564-45.564-147.768-147.77z"/></svg>';
34
35 public static function getInstance()
36 {
37 if (self::$instance === null) {
38 self::$instance = new self();
39 }
40
41 return self::$instance;
42 }
43
44 private function __construct()
45 {
46 $this->init();
47 }
48
49 private function init()
50 {
51 $this->addCustomCronSchedule();
52 $this->validateNotificationEngineOptions();
53
54 $notificationsSource = get_option('sgpm_popup_maker_notification_engine_source');
55 $notificationsSource = maybe_unserialize($notificationsSource);
56 $dismissedNotifications = get_option('sgpm_popup_maker_dismissed_notifacions');
57 $dismissedNotifications = !is_array($dismissedNotifications) ? array() : json_decode($dismissedNotifications);
58
59 $this->setActiveNotificationsCount($notificationsSource);
60 $this->activeNotifications = $notificationsSource;
61 $this->dismissedNotifications = $dismissedNotifications;
62 }
63
64 private function setActiveNotificationsCount($notificationsSource)
65 {
66 if ($notificationsSource && is_array($notificationsSource) && !empty($notificationsSource)) {
67 foreach ($notificationsSource as $notification) {
68 if ($notification['status'] == $this->statusDisabled) continue;
69 $this->activeNotificationsCount++;
70 }
71 }
72 }
73
74 private function validateNotificationEngineOptions()
75 {
76 $storedNotificationSource = get_option('sgpm_popup_maker_notification_engine_source');
77
78 if (!$storedNotificationSource) {
79 add_option('sgpm_popup_maker_notification_engine_source', maybe_serialize(array()));
80 add_option('sgpm_popup_maker_dismissed_notifacions', json_encode(array()));
81 }
82 }
83
84 private function addCustomCronSchedule()
85 {
86 /* temprory fix : Delete old crons */
87 wp_clear_scheduled_hook("sgpm_fetch_new_notifications");
88 wp_unschedule_event(time(), 'sgpm_check_for_new_notifications_every_6_hours');
89
90 //add_action('sgpm_fetch_notifications', array($this, 'fetchNewNotifications'));
91 //add_filter('cron_schedules', array($this, 'checkeForNewNotificationsCron')); // add new schedule
92
93 if (!wp_next_scheduled('sgpm_fetch_notifications')) {
94 //wp_schedule_event(time(), 'twicedaily', 'sgpm_fetch_notifications');
95 }
96 }
97
98 public function checkeForNewNotificationsCron($schedules)
99 {
100 $schedules['sgpm_check_for_new_notifications_every_6_hours'] = array(
101 'interval' => 60*60*6, // Every 6 hours (in seconds)
102 'display' => __('Popup Maker every 6 hours'),
103 );
104
105 return $schedules;
106 }
107
108 public function fetchNewNotifications()
109 {
110 $newNotifications = array();
111 $existingNotificationsSource = maybe_unserialize(get_option('sgpm_popup_maker_notification_engine_source'));
112 $dismissedNotifacions = json_decode(get_option('sgpm_popup_maker_dismissed_notifacions'), true);
113 $isValidExistingNotificationsSource = $this->validateExistingNotificationsSource($existingNotificationsSource);
114
115 if (!$isValidExistingNotificationsSource) {
116 update_option('sgpm_popup_maker_notification_engine_source', maybe_serialize(array()));
117 }
118
119 if (!is_array($dismissedNotifacions)) {
120 update_option('sgpm_popup_maker_dismissed_notifacions', json_encode(array()));
121 $dismissedNotifacions = array();
122 }
123
124 $response = wp_remote_get(SGPM_NOTIFICATIONS_API_URL);
125 $body = wp_remote_retrieve_body($response);
126
127 if (is_wp_error($response)) {
128 // wp_die("Something went wrong after fetching notifiactions!");
129 return;
130 }
131
132 $notificationsSource = json_decode($body, true);
133 $validBody = SGPMBase::validateNotificationsBody($notificationsSource);
134
135 if (isset($notificationsSource) && $validBody) {
136 foreach ($notificationsSource as $notification) {
137 $hash = $notification['hash'];
138 $status = $notification['status'];
139
140 if (in_array($hash, $dismissedNotifacions)) continue;
141
142 foreach ($existingNotificationsSource as $existingNotification) {
143 $existingHash = $existingNotification['hash'];
144 $existingStatus = $existingNotification['status'];
145
146 if ($existingHash == $hash && $existingStatus != $status) {
147 $existingNotification['status'] = $status;
148 $notification = $existingNotification; // update notification
149 }
150 }
151
152 array_push($newNotifications, $notification);
153 }
154
155 update_option('sgpm_popup_maker_notification_engine_source', maybe_serialize($newNotifications));
156 }
157 }
158
159 private function validateExistingNotificationsSource($existingNotificationsSource)
160 {
161 $serializedData = maybe_unserialize($existingNotificationsSource);
162
163 if (is_serialized($serializedData)) {
164 return true;
165 }
166 else {
167 return false;
168 }
169 }
170
171 public function clearAllNotifications()
172 {
173 $hashArray = array();
174 $notificationsSource = maybe_unserialize(get_option('sgpm_popup_maker_notification_engine_source'));
175 $dismissedNotifications = json_decode(get_option('sgpm_popup_maker_dismissed_notifacions'), true);
176
177 foreach ($notificationsSource as $notification) {
178 if (in_array($notification['hash'], $dismissedNotifications)) continue;
179 array_push($dismissedNotifications, $notification['hash']);
180 }
181
182 update_option('sgpm_popup_maker_notification_engine_source', maybe_serialize(array()));
183 update_option('sgpm_popup_maker_dismissed_notifacions', json_encode($dismissedNotifications));
184 }
185
186 public function removeNotification($hash, $notificationId)
187 {
188 if (isset($this->activeNotifications[$notificationId])) {
189 unset($this->activeNotifications[$notificationId]);
190 update_option('sgpm_popup_maker_notification_engine_source', maybe_serialize($this->activeNotifications));
191
192 array_push($this->dismissedNotifications, $hash);
193 update_option('sgpm_popup_maker_dismissed_notifacions', json_encode($this->dismissedNotifications));
194 }
195 }
196
197 public function setNotificationBadgeData()
198 {
199 $showReviewNotice = $this->allowedToShowReviewNotice();
200 if ($showReviewNotice) $this->activeNotificationsCount += 1;
201
202 if ($this->activeNotificationsCount) {
203 ob_start();
204 ?>
205 <script>
206 jQuery(document).ready(function() {
207 jQuery('.sgpm-menu-item-notification-badge').remove();
208 jQuery('#toplevel_page_popup-maker-api-settings > .toplevel_page_popup-maker-api-settings > .wp-menu-name').append('<span class="sgpm-menu-item-notification-badge"><?php echo esc_html($this->activeNotificationsCount)?></span>');
209 })
210 </script>
211 <?php
212 $badge = ob_get_contents();
213 ob_end_clean();
214 }
215 }
216
217 public function create()
218 {
219 $this->validateNotificationEngineOptions();
220 $innerHtml = $this->getNotificationBody();
221 if ($innerHtml) {
222 $this->getNotificationShadeWrapper($innerHtml);
223 }
224 }
225
226 private function allowedToShowReviewNotice()
227 {
228 $showReviewNotice = true;
229 $pastDate = strtotime('-30 days');
230
231 $activationDate = get_option('sgpm_popup_maker_activation_date');
232 $noticeDismissed = get_option('sgpm_popup_maker_dismiss_review_notice');
233
234 if ($noticeDismissed == 'true') {
235 $showReviewNotice = false;
236 }
237 else if (!current_user_can('install_plugins')) {
238 $showReviewNotice = false;
239 }
240 else if (!$activationDate || $activationDate > $pastDate) {
241 $showReviewNotice = false;
242 }
243
244 return $showReviewNotice;
245 }
246
247 private function getNotificationShadeWrapper($innerHtml)
248 {
249 $wrapper = '<div class="sgpm-notification-shade-wrapper">';
250 $wrapper .= '<div class="sgpm-notification-shade-header">';
251 $wrapper .= '<h3 class="sgpm-notification-shade-title"><span><span><div class="sgpm-bell-icon-container">'.$this->notificationBellIconSvg.'</div>&nbsp;Notifications (<span class="sgpm-notifications-count">'.$this->activeNotificationsCount.'</span>)</span></h3>';
252 $wrapper .= '<h3 class="sgpm-notification-shade-buttons">';
253 $wrapper .= '<span class="sgpm-collapce-all sgpm-action-icon-container" onclick="clearAllNotifications()">';
254 $wrapper .= $this->clearAllIconSvg;
255 $wrapper .= '</span>';
256 $wrapper .= '</h3>';
257 $wrapper .= '</div>';
258 $wrapper .= '<div class="sgpm-notifications-container">';
259 $wrapper .= $innerHtml;
260 $wrapper .= '</div>';
261 $wrapper .= '</div>';
262
263 return $wrapper;
264 }
265
266 private function getNotificationBody()
267 {
268 $body = '';
269 $showReviewNotice = $this->allowedToShowReviewNotice();
270 if ($showReviewNotice && is_array($this->activeNotifications)) {
271 array_push($this->activeNotifications, $this->reviewNotice);
272 (int)$this->activeNotificationsCount = count($this->activeNotifications);
273 }
274 if (empty($this->activeNotifications)) return false;
275
276 foreach ($this->activeNotifications as $id => $notification) {
277 // dismiss disabled notifications
278 if ($notification['status'] == $this->statusDisabled) continue;
279
280 $body .= '<div class="sgpm-notification-body sgpm-notification-'.$id.'">';
281 $body .= '<div class="sgpm-notification-header">';
282 $body .= '<h3 class="sgpm-notification-title">';
283 $body .= '<span>'.$notification['title'].'</span>';
284 $body .= '</h3>';
285
286 $body .= '<h3 class="sgpm-notification-buttons">';
287 $body .= '<span class="sgpm-collapce sgpm-action-icon-container" onclick="removeNotification(\''.$notification['hash'].'\',\''.$notification['type'].'\','.$id.')">';
288 $body .= $this->closeNotifictionIconSvg;
289 $body .= '</span>';
290 $body .= '</h3>';
291 $body .= '</div>';
292
293 $body .= '<div class="sgpm-notification-text">';
294 $body .= $notification['text'];
295 $body .= '</div>';
296
297 $body .= '<div class="sgpm-notification-footer">';
298 foreach ($notification['links'] as $link) {
299 $body .= '<a class="sgpm-info-link '.$link['className'].'" target="_blank" href="'.$link['url'].'">'.$link['label'].'</a>';
300 }
301 $body .= '</div>';
302
303 $body .= '</div>';
304 }
305
306 return $body;
307 }
308 }
309