-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-dotorg-plugin-review.php
182 lines (149 loc) · 4.84 KB
/
class-dotorg-plugin-review.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Plugin review class.
* Prompts users to give a review of the plugin on WordPress.org after a period of usage.
*
* Heavily based on code by Rhys Wynne
* https://winwar.co.uk/2014/10/ask-wordpress-plugin-reviews-week/
*
* @version 1.0
* @copyright Copyright (c), Ryan Hellyer
* @author Ryan Hellyer <[email protected]>
*/
if ( ! class_exists( 'DotOrg_Plugin_Review' ) ) :
class DotOrg_Plugin_Review {
/**
* Private variables.
*
* These should be customised for each project.
*/
private $slug; // The plugin slug
private $name; // The plugin name
private $time_limit; // The time limit at which notice is shown
/**
* Variables.
*/
public $nobug_option;
/**
* Fire the constructor up :)
*/
public function __construct( $args ) {
$this->slug = $args['slug'];
$this->name = $args['name'];
if ( isset( $args['time_limit'] ) ) {
$this->time_limit = $args['time_limit'];
} else {
$this->time_limit = WEEK_IN_SECONDS;
}
$this->nobug_option = $this->slug . '-no-bug';
// Loading main functionality.
add_action( 'admin_init', array( $this, 'check_installation_date' ) );
add_action( 'admin_init', array( $this, 'set_no_bug' ), 5 );
}
/**
* Seconds to words.
*/
public function seconds_to_words( $seconds ) {
// Get the years.
$years = ( intval( $seconds ) / YEAR_IN_SECONDS ) % 100;
if ( $years > 1 ) {
return sprintf( __( '%s years', $this->slug ), $years );
} elseif ( $years > 0) {
return __( 'a year', $this->slug );
}
// Get the weeks.
$weeks = ( intval( $seconds ) / WEEK_IN_SECONDS ) % 52;
if ( $weeks > 1 ) {
return sprintf( __( '%s weeks', $this->slug ), $weeks );
} elseif ( $weeks > 0) {
return __( 'a week', $this->slug );
}
// Get the days.
$days = ( intval( $seconds ) / DAY_IN_SECONDS ) % 7;
if ( $days > 1 ) {
return sprintf( __( '%s days', $this->slug ), $days );
} elseif ( $days > 0) {
return __( 'a day', $this->slug );
}
// Get the hours.
$hours = ( intval( $seconds ) / HOUR_IN_SECONDS ) % 24;
if ( $hours > 1 ) {
return sprintf( __( '%s hours', $this->slug ), $hours );
} elseif ( $hours > 0) {
return __( 'an hour', $this->slug );
}
// Get the minutes.
$minutes = ( intval( $seconds ) / MINUTE_IN_SECONDS ) % 60;
if ( $minutes > 1 ) {
return sprintf( __( '%s minutes', $this->slug ), $minutes );
} elseif ( $minutes > 0) {
return __( 'a minute', $this->slug );
}
// Get the seconds.
$seconds = intval( $seconds ) % 60;
if ( $seconds > 1 ) {
return sprintf( __( '%s seconds', $this->slug ), $seconds );
} elseif ( $seconds > 0) {
return __( 'a second', $this->slug );
}
return;
}
/**
* Check date on admin initiation and add to admin notice if it was more than the time limit.
*/
public function check_installation_date() {
if ( true != get_site_option( $this->nobug_option ) ) {
// If not installation date set, then add it.
$install_date = get_site_option( $this->slug . '-activation-date' );
if ( '' == $install_date ) {
add_site_option( $this->slug . '-activation-date', time() );
$install_date = get_site_option( $this->slug . '-activation-date' );
}
// If difference between install date and now is greater than time limit, then display notice.
if ( ( time() - $install_date ) > $this->time_limit ) {
add_action( 'admin_notices', array( $this, 'display_admin_notice' ) );
}
}
}
/**
* Display Admin Notice, asking for a review.
*/
public function display_admin_notice() {
$screen = get_current_screen();
if ( isset( $screen->base ) && 'plugins' == $screen->base ) {
$no_bug_url = wp_nonce_url( admin_url( '?' . $this->nobug_option . '=true' ), 'review-nonce' );
$time = $this->seconds_to_words( time() - get_site_option( $this->slug . '-activation-date' ) );
echo '
<div class="updated">
<p>' . sprintf( __( 'You have been using the %s plugin for %s now, do you like it? If so, please leave us a review with your feedback!', 'spam-destroyer' ), $this->name, $time ) . '
<br /><br />
<a onclick="location.href=\'' . esc_url( $no_bug_url ) . '\';" class="button button-primary" href="' . esc_url( 'https://wordpress.org/support/view/plugin-reviews/' . $this->slug . '#postform' ) . '" target="_blank">' . __( 'Leave A Review', 'spam-destroyer' ) . '</a>
<a href="' . esc_url( $no_bug_url ) . '">' . __( 'No thanks.', 'spam-destroyer' ) . '</a>
</p>
</div>';
}
}
/**
* Set the plugin to no longer bug users if user asks not to be.
*/
public function set_no_bug() {
// Bail out if not on correct page.
if (
! isset( $_GET['_wpnonce'] )
||
(
! wp_verify_nonce( $_GET['_wpnonce'], 'review-nonce' )
||
! is_admin()
||
! isset( $_GET[$this->nobug_option] )
||
! current_user_can( 'manage_options' )
)
) {
return;
}
add_site_option( $this->nobug_option, true );
}
}
endif;