Passed
Push — master ( edd4c4...9643cd )
by Chris
03:53
created

MonsterInsights_Review::review_dismiss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * Ask for some love.
4
 *
5
 * @package    MonsterInsights
6
 * @author     MonsterInsights
7
 * @since      7.0.7
8
 * @license    GPL-2.0+
9
 * @copyright  Copyright (c) 2018, MonsterInsights LLC
10
 */
11
class MonsterInsights_Review {
12
	/**
13
	 * Primary class constructor.
14
	 *
15
	 * @since 7.0.7
16
	 */
17
	public function __construct() {
18
		// Admin notice requesting review.
19
		add_action( 'admin_notices', array( $this, 'review_request' ) );
20
		add_action( 'wp_ajax_monsterinsights_review_dismiss', array( $this, 'review_dismiss' ) );
21
	}
22
	/**
23
	 * Add admin notices as needed for reviews.
24
	 *
25
	 * @since 7.0.7
26
	 */
27
	public function review_request() {
28
		// Only consider showing the review request to admin users.
29
		if ( ! is_super_admin() ) {
30
			return;
31
		}
32
33
		// Don't show to lite users
34
		if ( ! monsterinsights_is_pro_version() ) {
35
			return;
36
		}
37
38
		// If the user has opted out of product annoucement notifications, don't
39
		// display the review request.
40
		if ( monsterinsights_get_option( 'hide_am_notices', false ) || monsterinsights_get_option( 'network_hide_am_notices', false ) ) {
41
			return;
42
		}
43
		// Verify that we can do a check for reviews.
44
		$review = get_option( 'monsterinsights_review' );
45
		$time   = time();
46
		$load   = false;
47
48
		if ( ! $review ) {
49
			$review = array(
50
				'time'      => $time,
51
				'dismissed' => false,
52
			);
53
			update_option( 'monsterinsights_review', $review );
54
		} else {
55
			// Check if it has been dismissed or not.
56
			if ( ( isset( $review['dismissed'] ) && ! $review['dismissed'] ) && ( isset( $review['time'] ) && ( ( $review['time'] + DAY_IN_SECONDS ) <= $time ) ) ) {
57
				$load = true;
58
			}
59
		}
60
61
		// If we cannot load, return early.
62
		if ( ! $load ) {
63
			return;
64
		}
65
66
		$this->review();
67
	}
68
69
	/**
70
	 * Maybe show review request.
71
	 *
72
	 * @since 7.0.7
73
	 */
74
	public function review() {
75
		// Fetch when plugin was initially installed.
76
		$activated = get_option( 'monsterinsights_over_time', array() );
77
		if ( ! empty( $activated['installed_date'] ) ) {
78
			// Only continue if plugin has been installed for at least 14 days.
79
			if ( ( $activated['installed_date'] + ( DAY_IN_SECONDS * 14 ) ) > time() ) {
80
				return;
81
			}
82
		} else {
83
			$data = array(
84
				'installed_version' => MONSTERINSIGHTS_VERSION,
85
				'installed_date'    => time(),
86
				'installed_pro'     => monsterinsights_is_pro_version(),
87
			);
88
89
			update_option( 'monsterinsights_over_time', $data );
90
			return;
91
		}
92
		// Only proceed with displaying if the user is tracking.
93
		$ua_code = monsterinsights_get_ua_to_output();
94
		if ( empty( $ua_code ) ) {
95
			return;
96
		}
97
		// We have a candidate! Output a review message.
98
		?>
99
		<div class="notice notice-info is-dismissible monsterinsights-review-notice">
100
			<p><?php esc_html_e( 'Hey, I noticed you created a contact form with MonsterInsights - that’s awesome! Could you please do me a BIG favor and give it a 5-star rating on WordPress to help us spread the word and boost our motivation?', 'google-analytics-for-wordpress' ); ?></p>
101
			<p><strong><?php echo wp_kses( __( '~ Syed Balkhi<br>Co-Founder of MonsterInsights', 'google-analytics-for-wordpress' ), array( 'br' => array() ) ); ?></strong></p>
102
			<p>
103
				<a href="https://wordpress.org/support/plugin/google-analytics-for-wordpress/reviews/?filter=5#new-post" class="monsterinsights-dismiss-review-notice monsterinsights-review-out" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Ok, you deserve it', 'google-analytics-for-wordpress' ); ?></a><br>
104
				<a href="#" class="monsterinsights-dismiss-review-notice" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Nope, maybe later', 'google-analytics-for-wordpress' ); ?></a><br>
105
				<a href="#" class="monsterinsights-dismiss-review-notice" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'I already did', 'google-analytics-for-wordpress' ); ?></a>
106
			</p>
107
		</div>
108
		<script type="text/javascript">
109
			jQuery( document ).ready( function ( $ ) {
110
				$( document ).on( 'click', '.monsterinsights-dismiss-review-notice, .monsterinsights-review-notice button', function ( event ) {
111
					if ( ! $( this ).hasClass( 'monsterinsights-review-out' ) ) {
112
						event.preventDefault();
113
					}
114
					$.post( ajaxurl, {
115
						action: 'monsterinsights_review_dismiss'
116
					} );
117
					$( '.monsterinsights-review-notice' ).remove();
118
				} );
119
			} );
120
		</script>
121
		<?php
122
	}
123
	/**
124
	 * Dismiss the review admin notice
125
	 *
126
	 * @since 7.0.7
127
	 */
128
	public function review_dismiss() {
129
		$review              = get_option( 'monsterinsights_review', array() );
130
		$review['time']      = time();
131
		$review['dismissed'] = true;
132
		update_option( 'monsterinsights_review', $review );
133
		die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
134
	}
135
}
136
new MonsterInsights_Review;