PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 1.1.3
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v1.1.3
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.3, at includes/class-nx-message.php

984 lines 37.3 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'] ) ) {
245 $plugin = sanitize_text_field( $_GET['plugin'] );
246 if( $plugin === $this->plugin_name ) {
247 $options_data = $this->get_options_data();
248 $clicked_from = current( $this->next_notice() );
249 if( isset( $_GET['plugin_action'] ) ) {
250 $plugin_action = sanitize_text_field( $_GET['plugin_action'] );
251 }
252 if( isset( $_GET['dismiss'] ) ) {
253 $dismiss = sanitize_text_field( $_GET['dismiss'] );
254 }
255 if( isset( $_GET['later'] ) ) {
256 $later = sanitize_text_field( $_GET['later'] );
257 }
258
259 $later_time = '';
260
261 switch( $clicked_from ) {
262
263 case 'opt_in' :
264 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
265 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
266 break;
267
268 case 'first_install' :
269 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
270 break;
271
272 case 'update' :
273 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
274 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
275 break;
276
277 case 'review' :
278 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
279 break;
280
281 case 'upsale' :
282 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
283 break;
284 }
285
286 if( isset( $later ) && $later == true ) {
287 $options_data[ $this->plugin_name ]['notice_will_show'][ $clicked_from ] = $later_time;
288 }
289 if( isset( $dismiss ) && $dismiss == true ) {
290 $this->update( $clicked_from );
291 }
292 $this->update_options_data( $options_data[ $this->plugin_name ] );
293 }
294 }
295 }
296 /**
297 * For Redirecting Current Page without Arguments!
298 *
299 * @return void
300 */
301 private function redirect_to(){
302 $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
303 $query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
304 parse_str( $query_string, $current_url );
305
306 $unset_array = array( 'dismiss', 'plugin', '_wpnonce', 'later', 'plugin_action', 'marketing_optin' );
307
308 foreach( $unset_array as $value ) {
309 if( isset( $current_url[ $value ] ) ) {
310 unset( $current_url[ $value ] );
311 }
312 }
313
314 $current_url = http_build_query($current_url);
315 $redirect_url = $request_uri . '?' . $current_url;
316 return $redirect_url;
317 }
318 /**
319 * Before Notice
320 * @return void
321 */
322 public function before(){
323 $current_notice = current( $this->next_notice() );
324 $classes = 'notice notice-info put-dismiss-notice';
325 if( isset( $this->data['classes'] ) ) {
326 if( isset( $this->data['classes'][ $current_notice ] ) ) {
327 $classes = $this->data['classes'][ $current_notice ];
328 }
329 }
330
331 if( $this->has_thumbnail( $current_notice ) ) {
332 $classes .= 'notice-has-thumbnail';
333 }
334
335 echo '<div class="'. $classes .' wpdeveloper-'. $current_notice .'-notice">';
336 }
337 /**
338 * After Notice
339 * @return void
340 */
341 public function after(){
342 echo '</div>';
343 }
344 /**
345 * Content generation & Hooks Funciton.
346 * @return void
347 */
348 public function content(){
349 $options_data = $this->get_options_data();
350 $notice = current( $this->next_notice() );
351
352 switch( $notice ) {
353 case 'opt_in' :
354 do_action('wpdeveloper_optin_notice_for_' . $this->plugin_name );
355 break;
356 case 'first_install' :
357 if( $options_data[ $this->plugin_name ]['first_install'] !== 'deactivated' ) {
358 do_action( 'wpdeveloper_first_install_notice_for_' . $this->plugin_name );
359 $this->get_thumbnail( 'first_install' );
360 $this->get_message( 'first_install' );
361 }
362 break;
363 case 'update' :
364 do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name );
365 $this->get_thumbnail( 'update' );
366 $this->get_message( 'update' );
367 $this->dismiss_button_scripts();
368 break;
369 case 'review' :
370 do_action( 'wpdeveloper_review_notice_for_' . $this->plugin_name );
371 $this->get_thumbnail( 'review' );
372 $this->get_message( 'review' );
373 break;
374 }
375 }
376 /**
377 * Before Upsale Notice
378 * @return void
379 */
380 public function before_upsale(){
381 $classes = '';
382 if( $this->has_thumbnail('upsale') ) {
383 $classes = 'notice-has-thumbnail';
384 }
385 echo '<div class="error notice is-dismissible wpdeveloper-upsale-notice '. $classes .'">';
386 }
387 /**
388 * Upsale Notice
389 */
390 public function upsale_notice(){
391 do_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name );
392 do_action('wpdeveloper_upsale_notice_for_' . $this->plugin_name);
393 $this->get_thumbnail( 'upsale' );
394 $this->get_message( 'upsale' );
395 do_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name );
396 $this->upsale_button_script();
397 }
398 /**
399 * Get upsale arguments.
400 * @return void
401 */
402 private function get_upsale_args(){
403 return ( empty( $this->upsale_args ) ) ? array() : $this->upsale_args;
404 }
405 /**
406 * This function is responsible for making the button visible to the upsale notice.
407 */
408 private function upsale_button(){
409 $upsale_args = $this->get_upsale_args();
410 $plugin_slug = ( isset( $upsale_args['slug'] )) ? $upsale_args['slug'] : '' ;
411 if( empty( $plugin_slug ) ) {
412 return;
413 }
414 echo '<button data-slug="'. $plugin_slug .'" id="plugin-install-core-'. $this->plugin_name .'" class="button button-primary">'. __( 'Install Now!', $this->text_domain ) .'</button>';
415 }
416 /**
417 * This methods is responsible for get notice image.
418 *
419 * @param string $msg_for
420 * @return void
421 */
422 protected function get_thumbnail( $msg_for ){
423 $output = '';
424 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
425 $output = '<div class="wpdeveloper-notice-thumbnail">';
426 $output .= '<img src="'. $this->data['thumbnail'][ $msg_for ] .'" alt="">';
427 $output .= '</div>';
428 }
429 echo $output;
430 }
431 /**
432 * Has Thumbnail Check
433 *
434 * @param string $msg_for
435 * @return boolean
436 */
437 protected function has_thumbnail( $msg_for = '' ){
438 if( empty( $msg_for ) ) {
439 return false;
440 }
441 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
442 return true;
443 }
444 return false;
445 }
446 /**
447 * This method is responsible for get messages.
448 *
449 * @param string $msg_for
450 * @return void
451 */
452 protected function get_message( $msg_for ){
453 if( isset( $this->data['message'] ) && isset( $this->data['message'][ $msg_for ] ) ) {
454 echo '<div class="wpdeveloper-notice-message">';
455 echo $this->data['message'][ $msg_for ];
456 if( $msg_for === 'upsale' ) {
457 $this->upsale_button();
458 }
459 $this->dismissible_notice( $msg_for );
460 echo '</div>';
461 }
462 }
463 /**
464 * Detect which notice will show @ next.
465 * @return void
466 */
467 protected function next_notice(){
468 $options_data = $this->get_options_data();
469 if( ! $options_data ) {
470 $args = $this->get_args();
471 $return_notice = $args['notice_will_show'];
472 } else {
473 $return_notice = $options_data[ $this->plugin_name ]['notice_will_show'];
474 }
475
476 if( is_array( $return_notice ) ) {
477 $return_notice = array_flip( $return_notice );
478 ksort( $return_notice );
479 }
480
481 return $return_notice;
482 }
483 /**
484 * Which notice is deserve to show in next slot.
485 * @param string $notice
486 * @return boolean
487 */
488 private function deserve_notice( $notice ) {
489 $notices = $this->get_user_notices();
490 if( $notice === false ) {
491 return false;
492 }
493 if( empty( $notices ) ) {
494 return true;
495 } else {
496 if( isset( $notices[ $this->notice_id ] ) && isset( $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
497 if( in_array( $notice, $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
498 return false;
499 } else {
500 return true;
501 }
502 } else {
503 return true;
504 }
505 }
506 }
507 /**
508 * This is the main methods for generate the notice.
509 * @return void
510 */
511 public function admin_notices(){
512 $current_notice = current( $this->next_notice() );
513 if( $current_notice == 'opt_in' ) {
514 do_action( $this->do_notice_action );
515 return;
516 }
517 do_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name );
518 do_action( $this->do_notice_action );
519 do_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name );
520 }
521 /**
522 * This method is responsible for all dismissible links generation.
523 * @param string $links_for
524 * @return void
525 */
526 public function dismissible_notice( $links_for = '' ){
527 if( empty( $links_for ) ) {
528 return;
529 }
530 $links = isset( $this->data['links'][ $links_for ] ) ? $this->data['links'][ $links_for ] : false;
531 if( $links ) :
532 $output = '<ul class="wpdeveloper-notice-link">';
533 foreach( $links as $key => $link_value ) {
534 if( ! empty( $link_value['label'] ) ) {
535 $output .= '<li>';
536 if( isset( $link_value['link'] ) ) {
537 $link = $link_value['link'];
538 $target = isset( $link_value['target'] ) ? 'target="'. $link_value['target'] .'"' : '';
539 if( isset( $link_value['data_args'] ) && is_array( $link_value['data_args'] ) ) {
540 $data_args = [];
541 foreach( $link_value['data_args'] as $key => $args_value ) {
542 $data_args[ $key ] = $args_value;
543 }
544 $data_args[ 'plugin' ] = $this->plugin_name;
545 $normal_link = add_query_arg( $data_args, $link );
546 $link = wp_nonce_url( $normal_link, 'wpdeveloper-nonce' );
547 }
548 $class = '';
549 if( isset( $link_value['link_class'] ) ) {
550 $class = 'class="' . implode( ' ', $link_value['link_class'] ) . '"';
551 }
552 $output .= '<a '. $class .' href="'. esc_url( $link ) .'" '. $target .'>';
553 }
554 if( isset( $link_value['icon_class'] ) ) {
555 $output .= '<span class="'. $link_value['icon_class'] .'"></span>';
556 }
557 if( isset( $link_value['icon_img'] ) ) {
558 $output .= '<img src="'. $link_value['icon_img'] .'" />';
559 }
560 $output .= $link_value['label'];
561 if( isset( $link_value['link'] ) ) {
562 $output .= '</a>';
563 }
564 $output .= '</li>';
565 }
566 }
567 $output .= '</ul>';
568 echo $output;
569 endif;
570 }
571 /**
572 * First Installation Track
573 * @return void
574 */
575 public function first_install_track( $args = array() ){
576 if( ! current_user_can( 'manage_options' ) ) {
577 return;
578 }
579 if( empty( $args ) ) {
580 $args = array(
581 'time' => $this->timestamp,
582 'version' => $this->version,
583 );
584 }
585 $options_data = $this->get_options_data();
586 $args = wp_parse_args( $args, $this->get_args() );
587 if( ! isset( $options_data[ $this->plugin_name ] )
588 || ( isset( $options_data[ $this->plugin_name ]['version'] ) && version_compare( $options_data[ $this->plugin_name ]['version'], $this->version, '!=' ) ) ) {
589 $this->update_options_data( $args );
590 }
591 }
592 /**
593 * First Installation Deactive Track
594 *
595 * @return void
596 */
597 public function first_install_end(){
598 // $args = array(
599 // 'first_install' => 'deactivated'
600 // );
601 // $options_data = $this->get_options_data();
602 // if( isset( $options_data[ $this->plugin_name ] ) ) {
603 // $args = wp_parse_args( $args, $options_data[ $this->plugin_name ] );
604 // $this->update_options_data( $args );
605 // }
606 delete_option( 'wpdeveloper_plugins_data' );
607 }
608 /**
609 * Get all options from database!
610 * @return void
611 */
612 protected function get_options_data( $key = ''){
613 $options_data = get_option( 'wpdeveloper_plugins_data' );
614 if( empty( $key ) ) {
615 return $options_data;
616 }
617
618 if( isset( $options_data[ $this->plugin_name ][ $key ] ) ) {
619 return $options_data[ $this->plugin_name ][ $key ];
620 }
621 return false;
622 }
623 /**
624 * This will update the options table for plugins.
625 *
626 * @param mixed $new_data
627 * @param array $args
628 * @return void
629 */
630 protected function update_options_data( $args = array() ){
631 $options_data = $this->get_options_data();
632 $options_data[ $this->plugin_name ] = $args;
633 update_option( 'wpdeveloper_plugins_data', $options_data );
634 }
635 /**
636 * Set properties data, for some selected properties.
637 *
638 * @param string $name
639 * @param mixed $value
640 */
641 public function __set( $name, $value ){
642 if( in_array( $name, $this->properties ) ) {
643 $this->data[ $name ] = $value;
644 }
645 }
646 /**
647 * Invoked when some selected methods are called
648 *
649 * @param string $name
650 * @param array $values
651 * @return void
652 */
653 public function __call( $name, $values ){
654 if( in_array( $name, $this->methods ) ) {
655 $this->data[ $name ][ $values[0] ] = $values[1];
656 }
657 }
658 /**
659 * Get all option arguments.
660 * @param string $key
661 * @return array
662 */
663 private function get_args( $key = '' ){
664 if( empty( $key ) ) {
665 return $this->options_args;
666 }
667
668 if( isset( $this->options_args[ $key ] ) ) {
669 return $this->options_args[ $key ];
670 }
671
672 return false;
673 }
674 /**
675 * Resetting data on update.
676 * @return void
677 */
678 private function set_args_on_update(){
679 $args = $this->get_args();
680 $options_data = $this->get_options_data();
681 $set_data = $options_data[ $this->plugin_name ];
682 $args = wp_parse_args( $set_data, $args );
683 $this->update_options_data( $args );
684 }
685 /**
686 * When upgrade is complete. it will fired.
687 * @param WP_Upgrader $upgrader_object
688 * @param array $options
689 * @return void
690 */
691 public function upgrade_completed( $upgrader_object, $options ) {
692 // If an update has taken place and the updated type is plugins and the plugins element exists
693 if( isset( $options['action'] ) && $options['action'] == 'update' && $options['type'] == 'plugin' ) {
694 if( ! isset( $options['plugin'] ) && isset( $options['plugins'] ) ) {
695 foreach( $options['plugins'] as $plugin ) {
696 if( $plugin == $this->plugin_name ) {
697 $this->set_args_on_update();
698 }
699 }
700 }
701
702 if( isset( $options['plugin'] ) && $options['plugin'] == $this->plugin_name ) {
703 $this->set_args_on_update();
704 }
705 }
706 }
707 /**
708 * This function is responsible for get_user_notices
709 * @return void
710 */
711 private function get_user_notices() {
712 $notices = get_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, true );
713 return ! $notices ? array() : $notices;
714 }
715 /**
716 * This function is responsible for update meta information.
717 *
718 * @param string $notice
719 * @return void
720 */
721 private function update( $notice ){
722 if( empty( $notice ) ) {
723 return;
724 }
725 $options_data = $this->get_options_data();
726 $user_notices = $this->get_user_notices();
727 $user_notices[ $this->notice_id ][ $this->plugin_name ][] = $notice;
728 // Remove the upsale from notice_will_show field in options DB.
729 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] );
730 $this->update_options_data( $options_data[ $this->plugin_name ] );
731 // Set users meta, not to show again current_version notice.
732 update_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, $user_notices);
733 }
734
735 public function notice_dissmiss(){
736 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_notice_dissmiss' ) ) {
737 return;
738 }
739
740 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_notice_dissmiss_for_' . $this->plugin_name ) ) {
741 return;
742 }
743
744 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
745 $notice = isset( $_POST['notice'] ) ? $_POST['notice'] : false;
746 if( $dismiss ) {
747 $this->update( $notice );
748 echo 'success';
749 } else {
750 echo 'failed';
751 }
752 die();
753 }
754
755 /**
756 * This function is responsible for do action when
757 * the dismiss button clicked in upsale notice.
758 */
759 public function upsale_notice_dissmiss(){
760
761 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_upsale_notice_dissmiss' ) ) {
762 return;
763 }
764
765 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name ) ) {
766 return;
767 }
768
769 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
770 if( $dismiss ) {
771 $this->update( 'upsale' );
772 echo 'success';
773 } else {
774 echo 'failed';
775 }
776 die();
777 }
778
779 public function dismiss_button_scripts(){
780 ?>
781 <script type="text/javascript">
782 jQuery(document).ready( function($) {
783 if( $('.notice').length > 0 ) {
784 if( $('.notice').find('.notice-dismiss').length > 0 ) {
785 $('.notice').on('click', 'button.notice-dismiss', function (e) {
786 e.preventDefault();
787 $.ajax({
788 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
789 type: 'post',
790 data: {
791 action: 'wpdeveloper_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
792 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_notice_dissmiss'); ?>',
793 dismiss: true,
794 notice: $(this).data('notice'),
795 },
796 success: function(response) {
797 $('.notice').hide();
798 console.log('Successfully saved!');
799 },
800 error: function(error) {
801 console.log('Something went wrong!');
802 },
803 complete: function() {
804 console.log('Its Complete.');
805 }
806 });
807 });
808 }
809 }
810 } );
811 </script>
812 <?php
813 }
814
815 /**
816 * Upsale Button Script.
817 * When install button is clicked, it will do its own things.
818 * also for dismiss button JS.
819 * @return void
820 */
821 public function upsale_button_script(){
822 $upsale_args = $this->get_upsale_args();
823
824 $plugin_slug = ( isset( $upsale_args['slug'] ) ) ? $upsale_args['slug'] : '';
825 $plugin_file = ( isset( $upsale_args['file'] ) ) ? $upsale_args['file'] : '';
826 $page_slug = ( isset( $upsale_args['page_slug'] ) ) ? $upsale_args['page_slug'] : '';
827
828 ?>
829 <script type="text/javascript">
830 jQuery(document).ready( function($) {
831 <?php if( ! empty( $plugin_slug ) && ! empty( $plugin_file ) ) : ?>
832 $('#plugin-install-core-<?php echo $this->plugin_name; ?>').on('click', function (e) {
833 var self = $(this);
834 e.preventDefault();
835 self.addClass('install-now updating-message');
836 self.text('<?php echo esc_js( 'Installing...' ); ?>');
837
838 $.ajax({
839 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
840 type: 'POST',
841 data: {
842 action: 'wpdeveloper_upsale_core_install_<?php echo $this->plugin_name; ?>',
843 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_core_install_' . $this->plugin_name); ?>',
844 slug : '<?php echo $plugin_slug; ?>',
845 file : '<?php echo $plugin_file; ?>'
846 },
847 success: function(response) {
848 self.text('<?php echo esc_js( 'Installed' ); ?>');
849 <?php if( ! empty( $page_slug ) ) : ?>
850 window.location.href = '<?php echo admin_url( "admin.php?page={$page_slug}" ); ?>';
851 <?php endif; ?>
852 },
853 error: function(error) {
854 self.removeClass('install-now updating-message');
855 alert( error );
856 },
857 complete: function() {
858 self.attr('disabled', 'disabled');
859 self.removeClass('install-now updating-message');
860 }
861 });
862 });
863
864 <?php endif; ?>
865
866 $('.wpdeveloper-upsale-notice').on('click', 'button.notice-dismiss', function (e) {
867 e.preventDefault();
868 console.log( e );
869 return;
870 $.ajax({
871 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
872 type: 'post',
873 data: {
874 action: 'wpdeveloper_upsale_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
875 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_notice_dissmiss'); ?>',
876 dismiss: true
877 },
878 success: function(response) {
879 console.log('Successfully saved!');
880 },
881 error: function(error) {
882 console.log('Something went wrong!');
883 },
884 complete: function() {
885 console.log('Its Complete.');
886 }
887 });
888 });
889 } );
890 </script>
891
892 <?php
893 }
894 }
895
896 // Initialization.
897 $notice = new NotificationX_Notice(NOTIFICATIONX_BASENAME, NOTIFICATIONX_VERSION);
898 $scheme = (parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY )) ? '&' : '?';
899 $url = $_SERVER['REQUEST_URI'] . $scheme;
900 $notice->links = [
901 'review' => array(
902 'later' => array(
903 'link' => 'https://wpdeveloper.net/review-notificationx',
904 'target' => '_blank',
905 'label' => __( 'Ok, you deserve it!', $notice->text_domain ),
906 'icon_class' => 'dashicons dashicons-external',
907 ),
908 'allready' => array(
909 'link' => $url,
910 'label' => __( 'I already did', $notice->text_domain ),
911 'icon_class' => 'dashicons dashicons-smiley',
912 'data_args' => [
913 'dismiss' => true,
914 ]
915 ),
916 'maybe_later' => array(
917 'link' => $url,
918 'label' => __( 'Maybe Later', $notice->text_domain ),
919 'icon_class' => 'dashicons dashicons-calendar-alt',
920 'data_args' => [
921 'later' => true,
922 ]
923 ),
924 'support' => array(
925 'link' => 'https://wpdeveloper.net/support',
926 'label' => __( 'I need help', $notice->text_domain ),
927 'icon_class' => 'dashicons dashicons-sos',
928 ),
929 'never_show_again' => array(
930 'link' => $url,
931 'label' => __( 'Never show again', $notice->text_domain ),
932 'icon_class' => 'dashicons dashicons-dismiss',
933 'data_args' => [
934 'dismiss' => true,
935 ]
936 ),
937 )
938 ];
939
940 /**
941 * This is upsale notice settings
942 * classes for wrapper,
943 * Message message for showing.
944 */
945 $notice->classes( 'upsale', 'notice is-dismissible' );
946 $notice->message( 'upsale', '<p>'. __( 'Get the missing feature for NotificationX by installing PRO!', $notice->text_domain ) .'</p>' );
947 $notice->thumbnail( 'upsale', plugins_url( 'admin/assets/img/nx-icon.svg', NOTIFICATIONX_BASENAME ) );
948
949 /**
950 * This is review message and thumbnail.
951 */
952 $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>' );
953 $notice->thumbnail( 'review', plugins_url( 'admin/assets/img/nx-icon.svg', NOTIFICATIONX_BASENAME ) );
954
955 /**
956 * Current Notice End Time.
957 * Notice will dismiss in 3 days if user does nothing.
958 */
959 $notice->cne_time = '3 Day';
960 /**
961 * Current Notice Maybe Later Time.
962 * Notice will show again in 7 days
963 */
964 $notice->maybe_later_time = '7 Day';
965
966 $notice->upsale_args = array(
967 'slug' => 'notificationx-pro',
968 'page_slug' => 'nx-builder',
969 'file' => 'nx-pro.php',
970 'condition' => [
971 'by' => 'class',
972 'class' => 'NotificationXPro'
973 ],
974 );
975
976 $notice->options_args = array(
977 'notice_will_show' => [
978 'opt_in' => $notice->timestamp,
979 'review' => $notice->makeTime( $notice->timestamp, '7 Day' ), // after 4 days
980 // 'upsale' => $notice->makeTime( $notice->timestamp, '4 Day' ), // will be after 4 day
981 ]
982 );
983
984 $notice->init();