-
Notifications
You must be signed in to change notification settings - Fork 11
/
review.php
421 lines (337 loc) · 9.04 KB
/
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* WP Review Me
*
* A lightweight library to help you get more reviews for your WordPress theme/plugin.
*
* LICENSE: This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details. You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://opensource.org/licenses/gpl-license.php>
*
* @package WP Review Me
* @author Julien Liabeuf <julien@liabeuf.fr>
* @version 2.0.1
* @license GPL-2.0+
* @link https://julienliabeuf.com
* @copyright 2016 Julien Liabeuf
*/
if ( ! class_exists( 'WP_Review_Me' ) ) {
class WP_Review_Me {
/**
* Library version
*
* @since 1.0
* @var string
*/
public $version = '2.0.1';
/**
* Required version of PHP.
*
* @since 1.0
* @var string
*/
public $php_version_required = '5.5';
/**
* Minimum version of WordPress required to use the library
*
* @since 1.0
* @var string
*/
public $wordpress_version_required = '4.2';
/**
* Holds the unique identifying key for this particular instance
*
* @since 1.0
* @var string
*/
protected $key;
/**
* Link unique ID
*
* @since 1.0
* @var string
*/
public $link_id;
/**
* WP_Review_Me constructor.
*
* @since 1.0
*
* @param array $args Object settings
*/
public function __construct( $args ) {
$args = wp_parse_args( $args, $this->get_defaults() );
$this->days = $args['days_after'];
$this->type = $args['type'];
$this->slug = $args['slug'];
$this->rating = $args['rating'];
$this->message = $args['message'];
$this->link_label = $args['link_label'];
$this->cap = $args['cap'];
$this->scope = $args['scope'];
// Set the unique identifying key for this instance
$this->key = 'wrm_' . substr( md5( plugin_basename( __FILE__ ) ), 0, 20 );
$this->link_id = 'wrm-review-link-' . $this->key;
// Instantiate
$this->init();
}
/**
* Get default object settings
*
* @since 1.0
* @return array
*/
protected function get_defaults() {
$defaults = array(
'days_after' => 10,
'type' => '',
'slug' => '',
'rating' => 5,
'message' => sprintf( esc_html__( 'Hey! It's been a little while that you've been using this product. You might not realize it, but user reviews are such a great help to us. We would be so grateful if you could take a minute to leave a review on WordPress.org. Many thanks in advance :)', 'wp-review-me' ) ),
'link_label' => esc_html__( 'Click here to leave your review', 'wp-review-me' ),
// Parameters used in WP Dismissible Notices Handler
'cap' => 'administrator',
'scope' => 'global',
);
return $defaults;
}
/**
* Initialize the library
*
* @since 1.0
* @return void
*/
private function init() {
// Make sure WordPress is compatible
if ( ! $this->is_wp_compatible() ) {
$this->spit_error(
sprintf(
esc_html__( 'The library can not be used because your version of WordPress is too old. You need version %s at least.', 'wp-review-me' ),
$this->wordpress_version_required
)
);
return;
}
// Make sure PHP is compatible
if ( ! $this->is_php_compatible() ) {
$this->spit_error(
sprintf(
esc_html__( 'The library can not be used because your version of PHP is too old. You need version %s at least.', 'wp-review-me' ),
$this->php_version_required
)
);
return;
}
// Make sure the dependencies are loaded
if ( ! function_exists( 'dnh_register_notice' ) ) {
$dnh_file = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'vendor/julien731/wp-dismissible-notices-handler/handler.php';
if ( file_exists( $dnh_file ) ) {
require( $dnh_file );
}
if ( ! function_exists( 'dnh_register_notice' ) ) {
$this->spit_error(
sprintf(
esc_html__( 'Dependencies are missing. Please run a %s.', 'wp-review-me' ),
'<code>composer install</code>'
)
);
return;
}
}
add_action( 'admin_footer', array( $this, 'script' ) );
add_action( 'wp_ajax_wrm_clicked_review', array( $this, 'dismiss_notice' ) );
// And let's roll... maybe.
$this->maybe_prompt();
}
/**
* Check if the current WordPress version fits the requirements
*
* @since 1.0
* @return boolean
*/
private function is_wp_compatible() {
if ( version_compare( get_bloginfo( 'version' ), $this->wordpress_version_required, '<' ) ) {
return false;
}
return true;
}
/**
* Check if the version of PHP is compatible with this library
*
* @since 1.0
* @return boolean
*/
private function is_php_compatible() {
if ( version_compare( phpversion(), $this->php_version_required, '<' ) ) {
return false;
}
return true;
}
/**
* Spits an error message at the top of the admin screen
*
* @since 1.0
*
* @param string $error Error message to spit
*
* @return void
*/
protected function spit_error( $error ) {
printf(
'<div style="margin: 20px; text-align: center;"><strong>%1$s</strong> %2$s</pre></div>',
esc_html__( 'WP Review Me Error:', 'wp-review-me' ),
wp_kses_post( $error )
);
}
/**
* Check if it is time to ask for a review
*
* @since 1.0
* @return bool
*/
public function is_time() {
$installed = (int) get_option( $this->key, 0 );
if ( 0 === $installed ) {
$this->setup_date();
$installed = time();
}
if ( $installed + ( $this->days * 86400 ) > time() ) {
return false;
}
return true;
}
/**
* Save the current date as the installation date
*
* @since 1.0
* @return void
*/
protected function setup_date() {
update_option( $this->key, time() );
}
/**
* Get the review link
*
* @since 1.0
* @return string
*/
protected function get_review_link() {
$link = 'https://wordpress.org/support/';
switch ( $this->type ) {
case 'theme':
$link .= 'theme/';
break;
case 'plugin':
$link .= 'plugin/';
break;
}
$link .= $this->slug . '/reviews';
$link = add_query_arg( 'rate', $this->rating, $link );
$link = esc_url( $link . '#new-post' );
return $link;
}
/**
* Get the complete link tag
*
* @since 1.0
* @return string
*/
protected function get_review_link_tag() {
$link = $this->get_review_link();
return "<a href='$link' target='_blank' id='$this->link_id'>$this->link_label</a>";
}
/**
* Trigger the notice if it is time to ask for a review
*
* @since 1.0
* @return void
*/
protected function maybe_prompt() {
if ( ! $this->is_time() ) {
return;
}
dnh_register_notice( $this->key, 'updated', $this->get_message(), array(
'scope' => $this->scope,
'cap' => $this->cap
) );
}
/**
* Echo the JS script in the admin footer
*
* @since 1.0
* @return void
*/
public function script() { ?>
<script>
jQuery(document).ready(function($) {
$('#<?php echo $this->link_id; ?>').on('click', wrmDismiss);
function wrmDismiss() {
var data = {
action: 'wrm_clicked_review',
id: '<?php echo $this->link_id; ?>'
};
jQuery.ajax({
type:'POST',
url: ajaxurl,
data: data,
success:function( data ){
console.log(data);
}
});
}
});
</script>
<?php }
/**
* Dismiss the notice when the review link is clicked
*
* @since 1.0
* @return void
*/
public function dismiss_notice() {
if ( empty( $_POST ) ) {
echo 'missing POST';
die();
}
if ( ! isset( $_POST['id'] ) ) {
echo 'missing ID';
die();
}
$id = sanitize_text_field( $_POST['id'] );
if ( $id !== $this->link_id ) {
echo "not this instance's job";
die();
}
// Get the DNH notice ID ready
$notice_id = DNH()->get_id( str_replace( 'wrm-review-link-', '', $id ) );
$dismissed = DNH()->dismiss_notice( $notice_id );
echo $dismissed;
/**
* Fires right after the notice has been dismissed. This allows for various integrations to perform additional tasks.
*
* @since 1.0
*
* @param string $id The notice ID
* @param string $notice_id The notice ID as defined by the DNH class
*/
do_action( 'wrm_after_notice_dismissed', $id, $notice_id );
// Stop execution here
die();
}
/**
* Get the review prompt message
*
* @since 1.0
* @return string
*/
protected function get_message() {
$message = $this->message;
$link = $this->get_review_link_tag();
$message = $message . ' ' . $link;
return wp_kses_post( $message );
}
}
}