PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 1.1.2
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v1.1.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 1.2.0 1.2.1 All 156 releases
notificationx / includes / class-nx-message.php

class-nx-message.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 1.1.2, at includes/class-nx-message.php

973 lines 36.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 class NotificationX_Notice {
3 /**
4 * Admin Notice Key
5 *
6 * @var array
7 */
8 const ADMIN_UPDATE_NOTICE_KEY = 'wpdeveloper_notices_seen';
9 public $text_domain = 'notificationx';
10 /**
11 * All Data
12 * @var array
13 */
14 private $data = array();
15 private $properties = array(
16 'links', 'message', 'thumbnail',
17 );
18 private $methods = array(
19 'message', 'thumbnail', 'classes'
20 );
21 /**
22 * cne_day == current_notice_end_day
23 *
24 * @var integer
25 */
26 public $cne_time = '2 day';
27 public $maybe_later_time = '7 day';
28 public $finish_time = [];
29 /**
30 * Plugin Name
31 *
32 * @var string
33 */
34 private $plugin_name;
35 /**
36 * Plugin File Name
37 * @var string
38 */
39 private $plugin_file;
40 /**
41 * First Install Version Of The Plugin
42 *
43 * @var string
44 */
45 private $version;
46 /**
47 * Saved Data in DB
48 * @var array
49 */
50 private $options_data;
51 /**
52 * Current Timestamp
53 * @var integer
54 */
55 public $timestamp;
56 /**
57 * Primary Notice Action
58 *
59 * @var string
60 */
61 private $do_notice_action;
62 /**
63 * Default Options Set
64 *
65 * @var array
66 */
67 public $options_args = array(
68 // 'first_install' => true,
69 // 'notice_will_show' => [
70 // 'opt_in' => true,
71 // 'first_install' => false,
72 // 'update' => true,
73 // 'review' => true,
74 // 'upsale' => true,
75 // ]
76 );
77 /**
78 * Notice ID for users.
79 * @var string
80 */
81 private $notice_id;
82 /**
83 * Upsale Notice Arguments
84 * @var array
85 */
86 public $upsale_args;
87 /**
88 * Revoke this function when the object is created.
89 *
90 * @param string $plugin_name
91 * @param string $version
92 */
93 public function __construct( $plugin_file = '', $version = '' ) {
94 $this->plugin_file = $plugin_file;
95 $this->plugin_name = basename( $plugin_file, '.php' );
96 $this->version = $version;
97 $this->timestamp = intval( current_time( 'timestamp' ) );
98 $this->notice_id = 'wpdeveloper_notice_' . str_replace( '.', '_', $this->version );
99
100 $this->do_notice_action = 'wpdeveloper_notices_for_' . $this->plugin_name;
101
102 if( ! class_exists( 'NotificationX_Core_Installer' ) ) {
103 require_once dirname( __FILE__ ) . '/class-nx-core-installer.php';
104 new NotificationX_Core_Installer( $this->plugin_name );
105 }
106 }
107 /**
108 * Initiate The Plugin
109 * @return void
110 */
111 public function init(){
112 add_action( 'init', array( $this, 'first_install_track') );
113 add_action( 'deactivate_' . $this->plugin_file, array( $this, 'first_install_end' ) );
114 add_action( 'init', array( $this, 'hooks' ) );
115 }
116 /**
117 * All Hooks
118 * @return void
119 */
120 public function hooks(){
121 add_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name, array( $this, 'clicked' ) );
122 add_action( 'wp_ajax_wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'upsale_notice_dissmiss' ) );
123 add_action( 'wp_ajax_wpdeveloper_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'notice_dissmiss' ) );
124 add_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name, array( $this, 'before' ) );
125 add_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
126 add_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name, array( $this, 'before_upsale' ) );
127 add_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
128 add_action( $this->do_notice_action, array( $this, 'content' ) );
129 if( current_user_can( 'install_plugins' ) ) {
130 if( isset( $_GET['plugin'] ) && $_GET['plugin'] == $this->plugin_name ) {
131 do_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name );
132 /**
133 * Redirect User To the Current URL, but without set query arguments.
134 */
135 wp_safe_redirect( $this->redirect_to() );
136 }
137 $return_notice = $this->next_notice();
138 $current_notice = current( $return_notice );
139 $next_notice = next( $return_notice );
140
141 $deserve_notice = $this->deserve_notice( $current_notice );
142 $options_data = $this->get_options_data();
143 $user_notices = $this->get_user_notices();
144
145 $notice_time = isset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] )
146 ? $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] : $this->timestamp;
147 $next_notice_time = $next_notice ? $options_data[ $this->plugin_name ]['notice_will_show'][ $next_notice ] : $this->timestamp;
148 $current_notice_end = $this->makeTime( $notice_time, $this->cne_time );
149
150 if( ! $deserve_notice ) {
151 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
152 $this->update_options_data( $options_data[ $this->plugin_name ] );
153 }
154
155 if( $deserve_notice ) {
156 /**
157 * TODO: automatic maybe later setup with time.
158 */
159 if( ( $this->timestamp >= $current_notice_end ) || ( $this->timestamp > $next_notice_time ) ) {
160 $this->maybe_later( $current_notice );
161 $notice_time = false;
162 }
163
164 if( isset( $this->finish_time[ $current_notice ] ) ) {
165 if( $this->timestamp >= strtotime( $this->finish_time[ $current_notice ] ) ) {
166 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
167 $this->update_options_data( $options_data[ $this->plugin_name ] );
168 $notice_time = false;
169 }
170 }
171
172 if( $notice_time != false ) {
173 if( $notice_time <= $this->timestamp ) {
174 if( $current_notice === 'upsale' ) {
175 $upsale_args = $this->get_upsale_args();
176 if( empty( $upsale_args ) ) {
177 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
178 $this->update_options_data( $options_data[ $this->plugin_name ] );
179 } else {
180 if( isset( $upsale_args['condition'], $upsale_args['condition']['by'] ) ) {
181 switch( $upsale_args['condition']['by'] ) {
182 case 'class' :
183 if( isset( $upsale_args['condition']['class'] ) && class_exists( $upsale_args['condition']['class'] ) ) {
184 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
185 $this->update_options_data( $options_data[ $this->plugin_name ] );
186 return;
187 }
188 break;
189 case 'function' :
190 if( isset( $upsale_args['condition']['function'] ) && function_exists( $upsale_args['condition']['function'] ) ) {
191 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
192 $this->update_options_data( $options_data[ $this->plugin_name ] );
193 return;
194 }
195 break;
196 }
197 }
198 if ( ! function_exists( 'get_plugins' ) ) {
199 include ABSPATH . '/wp-admin/includes/plugin.php';
200 }
201 $plugins = get_plugins();
202 $pkey = $upsale_args['slug'] . '/' . $upsale_args['file'];
203 if( isset( $plugins[ $pkey ] ) ) {
204 $this->update( $current_notice );
205 return;
206 }
207 add_action( 'admin_notices', array( $this, 'upsale_notice' ) );
208 }
209 } else {
210 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
211 }
212 }
213 }
214 }
215 }
216 }
217 /**
218 * Make time using timestamp and a string like 2 Hour, 2 Day, 30 Minutes, 1 Week, 1 year
219 * @param integer $current
220 * @param string $time
221 * @return integer
222 */
223 public function makeTime( $current, $time ) {
224 return intval( strtotime( date('Y-m-d h:i:s', $current) . " +$time" ) );
225 }
226 /**
227 * Automatice Maybe Later.
228 * @param string $notice
229 * @return void
230 */
231 private function maybe_later( $notice ){
232 if( empty( $notice ) ) {
233 return;
234 }
235 $options_data = $this->get_options_data();
236 $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] = $this->makeTime( $this->timestamp, $this->maybe_later_time );
237 $this->update_options_data( $options_data[ $this->plugin_name ] );
238 }
239 /**
240 * When links are clicked, this function will invoked.
241 * @return void
242 */
243 public function clicked(){
244 if( isset( $_GET['plugin'] ) && $_GET['plugin'] === $this->plugin_name ) {
245 $options_data = $this->get_options_data();
246 $clicked_from = current( $this->next_notice() );
247 extract($_GET);
248
249 $later_time = '';
250
251 switch( $clicked_from ) {
252
253 case 'opt_in' :
254 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
255 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
256 break;
257
258 case 'first_install' :
259 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
260 break;
261
262 case 'update' :
263 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
264 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
265 break;
266
267 case 'review' :
268 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
269 break;
270
271 case 'upsale' :
272 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
273 break;
274 }
275
276 if( isset( $later ) && $later == true ) {
277 $options_data[ $this->plugin_name ]['notice_will_show'][ $clicked_from ] = $later_time;
278 }
279 if( isset( $dismiss ) && $dismiss == true ) {
280 $this->update( $clicked_from );
281 }
282 $this->update_options_data( $options_data[ $this->plugin_name ] );
283 }
284 }
285 /**
286 * For Redirecting Current Page without Arguments!
287 *
288 * @return void
289 */
290 private function redirect_to(){
291 $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
292 $query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
293 parse_str( $query_string, $current_url );
294
295 $unset_array = array( 'dismiss', 'plugin', '_wpnonce', 'later', 'plugin_action', 'marketing_optin' );
296
297 foreach( $unset_array as $value ) {
298 if( isset( $current_url[ $value ] ) ) {
299 unset( $current_url[ $value ] );
300 }
301 }
302
303 $current_url = http_build_query($current_url);
304 $redirect_url = $request_uri . '?' . $current_url;
305 return $redirect_url;
306 }
307 /**
308 * Before Notice
309 * @return void
310 */
311 public function before(){
312 $current_notice = current( $this->next_notice() );
313 $classes = 'notice notice-info put-dismiss-notice';
314 if( isset( $this->data['classes'] ) ) {
315 if( isset( $this->data['classes'][ $current_notice ] ) ) {
316 $classes = $this->data['classes'][ $current_notice ];
317 }
318 }
319
320 if( $this->has_thumbnail( $current_notice ) ) {
321 $classes .= 'notice-has-thumbnail';
322 }
323
324 echo '<div class="'. $classes .' wpdeveloper-'. $current_notice .'-notice">';
325 }
326 /**
327 * After Notice
328 * @return void
329 */
330 public function after(){
331 echo '</div>';
332 }
333 /**
334 * Content generation & Hooks Funciton.
335 * @return void
336 */
337 public function content(){
338 $options_data = $this->get_options_data();
339 $notice = current( $this->next_notice() );
340
341 switch( $notice ) {
342 case 'opt_in' :
343 do_action('wpdeveloper_optin_notice_for_' . $this->plugin_name );
344 break;
345 case 'first_install' :
346 if( $options_data[ $this->plugin_name ]['first_install'] !== 'deactivated' ) {
347 do_action( 'wpdeveloper_first_install_notice_for_' . $this->plugin_name );
348 $this->get_thumbnail( 'first_install' );
349 $this->get_message( 'first_install' );
350 }
351 break;
352 case 'update' :
353 do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name );
354 $this->get_thumbnail( 'update' );
355 $this->get_message( 'update' );
356 $this->dismiss_button_scripts();
357 break;
358 case 'review' :
359 do_action( 'wpdeveloper_review_notice_for_' . $this->plugin_name );
360 $this->get_thumbnail( 'review' );
361 $this->get_message( 'review' );
362 break;
363 }
364 }
365 /**
366 * Before Upsale Notice
367 * @return void
368 */
369 public function before_upsale(){
370 $classes = '';
371 if( $this->has_thumbnail('upsale') ) {
372 $classes = 'notice-has-thumbnail';
373 }
374 echo '<div class="error notice is-dismissible wpdeveloper-upsale-notice '. $classes .'">';
375 }
376 /**
377 * Upsale Notice
378 */
379 public function upsale_notice(){
380 do_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name );
381 do_action('wpdeveloper_upsale_notice_for_' . $this->plugin_name);
382 $this->get_thumbnail( 'upsale' );
383 $this->get_message( 'upsale' );
384 do_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name );
385 $this->upsale_button_script();
386 }
387 /**
388 * Get upsale arguments.
389 * @return void
390 */
391 private function get_upsale_args(){
392 return ( empty( $this->upsale_args ) ) ? array() : $this->upsale_args;
393 }
394 /**
395 * This function is responsible for making the button visible to the upsale notice.
396 */
397 private function upsale_button(){
398 $upsale_args = $this->get_upsale_args();
399 $plugin_slug = ( isset( $upsale_args['slug'] )) ? $upsale_args['slug'] : '' ;
400 if( empty( $plugin_slug ) ) {
401 return;
402 }
403 echo '<button data-slug="'. $plugin_slug .'" id="plugin-install-core-'. $this->plugin_name .'" class="button button-primary">'. __( 'Install Now!', $this->text_domain ) .'</button>';
404 }
405 /**
406 * This methods is responsible for get notice image.
407 *
408 * @param string $msg_for
409 * @return void
410 */
411 protected function get_thumbnail( $msg_for ){
412 $output = '';
413 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
414 $output = '<div class="wpdeveloper-notice-thumbnail">';
415 $output .= '<img src="'. $this->data['thumbnail'][ $msg_for ] .'" alt="">';
416 $output .= '</div>';
417 }
418 echo $output;
419 }
420 /**
421 * Has Thumbnail Check
422 *
423 * @param string $msg_for
424 * @return boolean
425 */
426 protected function has_thumbnail( $msg_for = '' ){
427 if( empty( $msg_for ) ) {
428 return false;
429 }
430 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
431 return true;
432 }
433 return false;
434 }
435 /**
436 * This method is responsible for get messages.
437 *
438 * @param string $msg_for
439 * @return void
440 */
441 protected function get_message( $msg_for ){
442 if( isset( $this->data['message'] ) && isset( $this->data['message'][ $msg_for ] ) ) {
443 echo '<div class="wpdeveloper-notice-message">';
444 echo $this->data['message'][ $msg_for ];
445 if( $msg_for === 'upsale' ) {
446 $this->upsale_button();
447 }
448 $this->dismissible_notice( $msg_for );
449 echo '</div>';
450 }
451 }
452 /**
453 * Detect which notice will show @ next.
454 * @return void
455 */
456 protected function next_notice(){
457 $options_data = $this->get_options_data();
458 if( ! $options_data ) {
459 $args = $this->get_args();
460 $return_notice = $args['notice_will_show'];
461 } else {
462 $return_notice = $options_data[ $this->plugin_name ]['notice_will_show'];
463 }
464
465 if( is_array( $return_notice ) ) {
466 $return_notice = array_flip( $return_notice );
467 ksort( $return_notice );
468 }
469
470 return $return_notice;
471 }
472 /**
473 * Which notice is deserve to show in next slot.
474 * @param string $notice
475 * @return boolean
476 */
477 private function deserve_notice( $notice ) {
478 $notices = $this->get_user_notices();
479 if( $notice === false ) {
480 return false;
481 }
482 if( empty( $notices ) ) {
483 return true;
484 } else {
485 if( isset( $notices[ $this->notice_id ] ) && isset( $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
486 if( in_array( $notice, $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
487 return false;
488 } else {
489 return true;
490 }
491 } else {
492 return true;
493 }
494 }
495 }
496 /**
497 * This is the main methods for generate the notice.
498 * @return void
499 */
500 public function admin_notices(){
501 $current_notice = current( $this->next_notice() );
502 if( $current_notice == 'opt_in' ) {
503 do_action( $this->do_notice_action );
504 return;
505 }
506 do_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name );
507 do_action( $this->do_notice_action );
508 do_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name );
509 }
510 /**
511 * This method is responsible for all dismissible links generation.
512 * @param string $links_for
513 * @return void
514 */
515 public function dismissible_notice( $links_for = '' ){
516 if( empty( $links_for ) ) {
517 return;
518 }
519 $links = isset( $this->data['links'][ $links_for ] ) ? $this->data['links'][ $links_for ] : false;
520 if( $links ) :
521 $output = '<ul class="wpdeveloper-notice-link">';
522 foreach( $links as $key => $link_value ) {
523 if( ! empty( $link_value['label'] ) ) {
524 $output .= '<li>';
525 if( isset( $link_value['link'] ) ) {
526 $link = $link_value['link'];
527 $target = isset( $link_value['target'] ) ? 'target="'. $link_value['target'] .'"' : '';
528 if( isset( $link_value['data_args'] ) && is_array( $link_value['data_args'] ) ) {
529 $data_args = [];
530 foreach( $link_value['data_args'] as $key => $args_value ) {
531 $data_args[ $key ] = $args_value;
532 }
533 $data_args[ 'plugin' ] = $this->plugin_name;
534 $normal_link = add_query_arg( $data_args, $link );
535 $link = wp_nonce_url( $normal_link, 'wpdeveloper-nonce' );
536 }
537 $class = '';
538 if( isset( $link_value['link_class'] ) ) {
539 $class = 'class="' . implode( ' ', $link_value['link_class'] ) . '"';
540 }
541 $output .= '<a '. $class .' href="'. esc_url( $link ) .'" '. $target .'>';
542 }
543 if( isset( $link_value['icon_class'] ) ) {
544 $output .= '<span class="'. $link_value['icon_class'] .'"></span>';
545 }
546 if( isset( $link_value['icon_img'] ) ) {
547 $output .= '<img src="'. $link_value['icon_img'] .'" />';
548 }
549 $output .= $link_value['label'];
550 if( isset( $link_value['link'] ) ) {
551 $output .= '</a>';
552 }
553 $output .= '</li>';
554 }
555 }
556 $output .= '</ul>';
557 echo $output;
558 endif;
559 }
560 /**
561 * First Installation Track
562 * @return void
563 */
564 public function first_install_track( $args = array() ){
565 if( ! current_user_can( 'manage_options' ) ) {
566 return;
567 }
568 if( empty( $args ) ) {
569 $args = array(
570 'time' => $this->timestamp,
571 'version' => $this->version,
572 );
573 }
574 $options_data = $this->get_options_data();
575 $args = wp_parse_args( $args, $this->get_args() );
576 if( ! isset( $options_data[ $this->plugin_name ] )
577 || ( isset( $options_data[ $this->plugin_name ]['version'] ) && version_compare( $options_data[ $this->plugin_name ]['version'], $this->version, '!=' ) ) ) {
578 $this->update_options_data( $args );
579 }
580 }
581 /**
582 * First Installation Deactive Track
583 *
584 * @return void
585 */
586 public function first_install_end(){
587 // $args = array(
588 // 'first_install' => 'deactivated'
589 // );
590 // $options_data = $this->get_options_data();
591 // if( isset( $options_data[ $this->plugin_name ] ) ) {
592 // $args = wp_parse_args( $args, $options_data[ $this->plugin_name ] );
593 // $this->update_options_data( $args );
594 // }
595 delete_option( 'wpdeveloper_plugins_data' );
596 }
597 /**
598 * Get all options from database!
599 * @return void
600 */
601 protected function get_options_data( $key = ''){
602 $options_data = get_option( 'wpdeveloper_plugins_data' );
603 if( empty( $key ) ) {
604 return $options_data;
605 }
606
607 if( isset( $options_data[ $this->plugin_name ][ $key ] ) ) {
608 return $options_data[ $this->plugin_name ][ $key ];
609 }
610 return false;
611 }
612 /**
613 * This will update the options table for plugins.
614 *
615 * @param mixed $new_data
616 * @param array $args
617 * @return void
618 */
619 protected function update_options_data( $args = array() ){
620 $options_data = $this->get_options_data();
621 $options_data[ $this->plugin_name ] = $args;
622 update_option( 'wpdeveloper_plugins_data', $options_data );
623 }
624 /**
625 * Set properties data, for some selected properties.
626 *
627 * @param string $name
628 * @param mixed $value
629 */
630 public function __set( $name, $value ){
631 if( in_array( $name, $this->properties ) ) {
632 $this->data[ $name ] = $value;
633 }
634 }
635 /**
636 * Invoked when some selected methods are called
637 *
638 * @param string $name
639 * @param array $values
640 * @return void
641 */
642 public function __call( $name, $values ){
643 if( in_array( $name, $this->methods ) ) {
644 $this->data[ $name ][ $values[0] ] = $values[1];
645 }
646 }
647 /**
648 * Get all option arguments.
649 * @param string $key
650 * @return array
651 */
652 private function get_args( $key = '' ){
653 if( empty( $key ) ) {
654 return $this->options_args;
655 }
656
657 if( isset( $this->options_args[ $key ] ) ) {
658 return $this->options_args[ $key ];
659 }
660
661 return false;
662 }
663 /**
664 * Resetting data on update.
665 * @return void
666 */
667 private function set_args_on_update(){
668 $args = $this->get_args();
669 $options_data = $this->get_options_data();
670 $set_data = $options_data[ $this->plugin_name ];
671 $args = wp_parse_args( $set_data, $args );
672 $this->update_options_data( $args );
673 }
674 /**
675 * When upgrade is complete. it will fired.
676 * @param WP_Upgrader $upgrader_object
677 * @param array $options
678 * @return void
679 */
680 public function upgrade_completed( $upgrader_object, $options ) {
681 // If an update has taken place and the updated type is plugins and the plugins element exists
682 if( isset( $options['action'] ) && $options['action'] == 'update' && $options['type'] == 'plugin' ) {
683 if( ! isset( $options['plugin'] ) && isset( $options['plugins'] ) ) {
684 foreach( $options['plugins'] as $plugin ) {
685 if( $plugin == $this->plugin_name ) {
686 $this->set_args_on_update();
687 }
688 }
689 }
690
691 if( isset( $options['plugin'] ) && $options['plugin'] == $this->plugin_name ) {
692 $this->set_args_on_update();
693 }
694 }
695 }
696 /**
697 * This function is responsible for get_user_notices
698 * @return void
699 */
700 private function get_user_notices() {
701 $notices = get_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, true );
702 return ! $notices ? array() : $notices;
703 }
704 /**
705 * This function is responsible for update meta information.
706 *
707 * @param string $notice
708 * @return void
709 */
710 private function update( $notice ){
711 if( empty( $notice ) ) {
712 return;
713 }
714 $options_data = $this->get_options_data();
715 $user_notices = $this->get_user_notices();
716 $user_notices[ $this->notice_id ][ $this->plugin_name ][] = $notice;
717 // Remove the upsale from notice_will_show field in options DB.
718 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] );
719 $this->update_options_data( $options_data[ $this->plugin_name ] );
720 // Set users meta, not to show again current_version notice.
721 update_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, $user_notices);
722 }
723
724 public function notice_dissmiss(){
725 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_notice_dissmiss' ) ) {
726 return;
727 }
728
729 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_notice_dissmiss_for_' . $this->plugin_name ) ) {
730 return;
731 }
732
733 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
734 $notice = isset( $_POST['notice'] ) ? $_POST['notice'] : false;
735 if( $dismiss ) {
736 $this->update( $notice );
737 echo 'success';
738 } else {
739 echo 'failed';
740 }
741 die();
742 }
743
744 /**
745 * This function is responsible for do action when
746 * the dismiss button clicked in upsale notice.
747 */
748 public function upsale_notice_dissmiss(){
749
750 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_upsale_notice_dissmiss' ) ) {
751 return;
752 }
753
754 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name ) ) {
755 return;
756 }
757
758 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
759 if( $dismiss ) {
760 $this->update( 'upsale' );
761 echo 'success';
762 } else {
763 echo 'failed';
764 }
765 die();
766 }
767
768 public function dismiss_button_scripts(){
769 ?>
770 <script type="text/javascript">
771 jQuery(document).ready( function($) {
772 if( $('.notice').length > 0 ) {
773 if( $('.notice').find('.notice-dismiss').length > 0 ) {
774 $('.notice').on('click', 'button.notice-dismiss', function (e) {
775 e.preventDefault();
776 $.ajax({
777 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
778 type: 'post',
779 data: {
780 action: 'wpdeveloper_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
781 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_notice_dissmiss'); ?>',
782 dismiss: true,
783 notice: $(this).data('notice'),
784 },
785 success: function(response) {
786 $('.notice').hide();
787 console.log('Successfully saved!');
788 },
789 error: function(error) {
790 console.log('Something went wrong!');
791 },
792 complete: function() {
793 console.log('Its Complete.');
794 }
795 });
796 });
797 }
798 }
799 } );
800 </script>
801 <?php
802 }
803
804 /**
805 * Upsale Button Script.
806 * When install button is clicked, it will do its own things.
807 * also for dismiss button JS.
808 * @return void
809 */
810 public function upsale_button_script(){
811 $upsale_args = $this->get_upsale_args();
812
813 $plugin_slug = ( isset( $upsale_args['slug'] ) ) ? $upsale_args['slug'] : '';
814 $plugin_file = ( isset( $upsale_args['file'] ) ) ? $upsale_args['file'] : '';
815 $page_slug = ( isset( $upsale_args['page_slug'] ) ) ? $upsale_args['page_slug'] : '';
816
817 ?>
818 <script type="text/javascript">
819 jQuery(document).ready( function($) {
820 <?php if( ! empty( $plugin_slug ) && ! empty( $plugin_file ) ) : ?>
821 $('#plugin-install-core-<?php echo $this->plugin_name; ?>').on('click', function (e) {
822 var self = $(this);
823 e.preventDefault();
824 self.addClass('install-now updating-message');
825 self.text('<?php echo esc_js( 'Installing...' ); ?>');
826
827 $.ajax({
828 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
829 type: 'POST',
830 data: {
831 action: 'wpdeveloper_upsale_core_install_<?php echo $this->plugin_name; ?>',
832 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_core_install_' . $this->plugin_name); ?>',
833 slug : '<?php echo $plugin_slug; ?>',
834 file : '<?php echo $plugin_file; ?>'
835 },
836 success: function(response) {
837 self.text('<?php echo esc_js( 'Installed' ); ?>');
838 <?php if( ! empty( $page_slug ) ) : ?>
839 window.location.href = '<?php echo admin_url( "admin.php?page={$page_slug}" ); ?>';
840 <?php endif; ?>
841 },
842 error: function(error) {
843 self.removeClass('install-now updating-message');
844 alert( error );
845 },
846 complete: function() {
847 self.attr('disabled', 'disabled');
848 self.removeClass('install-now updating-message');
849 }
850 });
851 });
852
853 <?php endif; ?>
854
855 $('.wpdeveloper-upsale-notice').on('click', 'button.notice-dismiss', function (e) {
856 e.preventDefault();
857 console.log( e );
858 return;
859 $.ajax({
860 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
861 type: 'post',
862 data: {
863 action: 'wpdeveloper_upsale_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
864 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_notice_dissmiss'); ?>',
865 dismiss: true
866 },
867 success: function(response) {
868 console.log('Successfully saved!');
869 },
870 error: function(error) {
871 console.log('Something went wrong!');
872 },
873 complete: function() {
874 console.log('Its Complete.');
875 }
876 });
877 });
878 } );
879 </script>
880
881 <?php
882 }
883 }
884
885 // Initialization.
886 $notice = new NotificationX_Notice(NOTIFICATIONX_BASENAME, NOTIFICATIONX_VERSION);
887 $scheme = (parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY )) ? '&' : '?';
888 $url = $_SERVER['REQUEST_URI'] . $scheme;
889 $notice->links = [
890 'review' => array(
891 'later' => array(
892 'link' => 'https://wpdeveloper.net/review-notificationx',
893 'target' => '_blank',
894 'label' => __( 'Ok, you deserve it!', $notice->text_domain ),
895 'icon_class' => 'dashicons dashicons-external',
896 ),
897 'allready' => array(
898 'link' => $url,
899 'label' => __( 'I already did', $notice->text_domain ),
900 'icon_class' => 'dashicons dashicons-smiley',
901 'data_args' => [
902 'dismiss' => true,
903 ]
904 ),
905 'maybe_later' => array(
906 'link' => $url,
907 'label' => __( 'Maybe Later', $notice->text_domain ),
908 'icon_class' => 'dashicons dashicons-calendar-alt',
909 'data_args' => [
910 'later' => true,
911 ]
912 ),
913 'support' => array(
914 'link' => 'https://wpdeveloper.net/support',
915 'label' => __( 'I need help', $notice->text_domain ),
916 'icon_class' => 'dashicons dashicons-sos',
917 ),
918 'never_show_again' => array(
919 'link' => $url,
920 'label' => __( 'Never show again', $notice->text_domain ),
921 'icon_class' => 'dashicons dashicons-dismiss',
922 'data_args' => [
923 'dismiss' => true,
924 ]
925 ),
926 )
927 ];
928
929 /**
930 * This is upsale notice settings
931 * classes for wrapper,
932 * Message message for showing.
933 */
934 $notice->classes( 'upsale', 'notice is-dismissible' );
935 $notice->message( 'upsale', '<p>'. __( 'Get the missing feature for NotificationX by installing PRO!', $notice->text_domain ) .'</p>' );
936 $notice->thumbnail( 'upsale', plugins_url( 'admin/assets/img/nx-icon.svg', NOTIFICATIONX_BASENAME ) );
937
938 /**
939 * This is review message and thumbnail.
940 */
941 $notice->message( 'review', '<p>'. __( 'We hope you\'re enjoying NotificationX! Could you please do us a BIG favor and give it a 5-star rating on WordPress to help us spread the word and boost our motivation?', $notice->text_domain ) .'</p>' );
942 $notice->thumbnail( 'review', plugins_url( 'admin/assets/img/nx-icon.svg', NOTIFICATIONX_BASENAME ) );
943
944 /**
945 * Current Notice End Time.
946 * Notice will dismiss in 3 days if user does nothing.
947 */
948 $notice->cne_time = '3 Day';
949 /**
950 * Current Notice Maybe Later Time.
951 * Notice will show again in 7 days
952 */
953 $notice->maybe_later_time = '7 Day';
954
955 $notice->upsale_args = array(
956 'slug' => 'notificationx-pro',
957 'page_slug' => 'nx-builder',
958 'file' => 'nx-pro.php',
959 'condition' => [
960 'by' => 'class',
961 'class' => 'NotificationXPro'
962 ],
963 );
964
965 $notice->options_args = array(
966 'notice_will_show' => [
967 'opt_in' => $notice->timestamp,
968 'review' => $notice->makeTime( $notice->timestamp, '7 Day' ), // after 4 days
969 // 'upsale' => $notice->makeTime( $notice->timestamp, '4 Day' ), // will be after 4 day
970 ]
971 );
972
973 $notice->init();