PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.11
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.11
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 / Admin / Notice.php

Notice.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.11, at includes/Admin/Notice.php

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