PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 1.1.2
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v1.1.2
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Admin / Reviews.php

Reviews.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 1.1.2, at classes/Admin/Reviews.php

487 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace JP\CC\Admin;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Class JP\CC\Admin\Reviews
11 *
12 * This class adds a review request system for your plugin or theme to the WP dashboard.
13 *
14 * @since 1.1.0
15 */
16 class Reviews {
17
18 /**
19 * Tracking API Endpoint.
20 *
21 * @var string
22 */
23 public static $api_url;
24
25 /**
26 *
27 */
28 public static function init() {
29 add_action( 'init', array( __CLASS__, 'hooks' ) );
30 add_action( 'wp_ajax_jp_cc_review_action', array( __CLASS__, 'ajax_handler' ) );
31 }
32
33 /**
34 * Hook into relevant WP actions.
35 */
36 public static function hooks() {
37 if ( is_admin() && current_user_can( 'edit_posts' ) ) {
38 self::installed_on();
39 add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
40 add_action( 'network_admin_notices', array( __CLASS__, 'admin_notices' ) );
41 add_action( 'user_admin_notices', array( __CLASS__, 'admin_notices' ) );
42 }
43 }
44
45 /**
46 * Get the install date for comparisons. Sets the date to now if none is found.
47 *
48 * @return false|string
49 */
50 public static function installed_on() {
51 $installed_on = get_option( 'jp_cc_reviews_installed_on', false );
52
53 if ( ! $installed_on ) {
54 $installed_on = current_time( 'mysql' );
55 update_option( 'jp_cc_reviews_installed_on', $installed_on );
56 }
57
58 return $installed_on;
59 }
60
61 /**
62 * AJAX Handler
63 */
64 public static function ajax_handler() {
65 $args = wp_parse_args( $_REQUEST, array(
66 'group' => self::get_trigger_group(),
67 'code' => self::get_trigger_code(),
68 'pri' => self::get_current_trigger( 'pri' ),
69 'reason' => 'maybe_later',
70 ) );
71
72 if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'jp_cc_review_action' ) ) {
73 wp_send_json_error();
74 }
75
76 try {
77 $user_id = get_current_user_id();
78
79 $dismissed_triggers = self::dismissed_triggers();
80 $dismissed_triggers[ $args['group'] ] = $args['pri'];
81 update_user_meta( $user_id, '_jp_cc_reviews_dismissed_triggers', $dismissed_triggers );
82 update_user_meta( $user_id, '_jp_cc_reviews_last_dismissed', current_time( 'mysql' ) );
83
84 switch ( $args['reason'] ) {
85 case 'maybe_later':
86 update_user_meta( $user_id, '_jp_cc_reviews_last_dismissed', current_time( 'mysql' ) );
87 break;
88 case 'am_now':
89 case 'already_did':
90 self::already_did( true );
91 break;
92 }
93
94 wp_send_json_success();
95
96 } catch ( \Exception $e ) {
97 wp_send_json_error( $e );
98 }
99 }
100
101 /**
102 * @return int|string
103 */
104 public static function get_trigger_group() {
105 static $selected;
106
107 if ( ! isset( $selected ) ) {
108
109 $dismissed_triggers = self::dismissed_triggers();
110
111 $triggers = self::triggers();
112
113 foreach ( $triggers as $g => $group ) {
114 foreach ( $group['triggers'] as $t => $trigger ) {
115 if ( ! in_array( false, $trigger['conditions'] ) && ( empty( $dismissed_triggers[ $g ] ) || $dismissed_triggers[ $g ] < $trigger['pri'] ) ) {
116 $selected = $g;
117 break;
118 }
119 }
120
121 if ( isset( $selected ) ) {
122 break;
123 }
124 }
125 }
126
127 return $selected;
128 }
129
130 /**
131 * @return int|string
132 */
133 public static function get_trigger_code() {
134 static $selected;
135
136 if ( ! isset( $selected ) ) {
137
138 $dismissed_triggers = self::dismissed_triggers();
139
140 foreach ( self::triggers() as $g => $group ) {
141 foreach ( $group['triggers'] as $t => $trigger ) {
142 if ( ! in_array( false, $trigger['conditions'] ) && ( empty( $dismissed_triggers[ $g ] ) || $dismissed_triggers[ $g ] < $trigger['pri'] ) ) {
143 $selected = $t;
144 break;
145 }
146 }
147
148 if ( isset( $selected ) ) {
149 break;
150 }
151 }
152 }
153
154 return $selected;
155 }
156
157 /**
158 * @param null $key
159 *
160 * @return bool|mixed
161 */
162 public static function get_current_trigger( $key = null ) {
163 $group = self::get_trigger_group();
164 $code = self::get_trigger_code();
165
166 if ( ! $group || ! $code ) {
167 return false;
168 }
169
170 $trigger = self::triggers( $group, $code );
171
172 return empty( $key ) ? $trigger : ( isset( $trigger[ $key ] ) ? $trigger[ $key ] : false );
173 }
174
175 /**
176 * Returns an array of dismissed trigger groups.
177 *
178 * Array contains the group key and highest priority trigger that has been shown previously for each group.
179 *
180 * $return = array(
181 * 'group1' => 20
182 * );
183 *
184 * @return array|mixed
185 */
186 public static function dismissed_triggers() {
187 $user_id = get_current_user_id();
188
189 $dismissed_triggers = get_user_meta( $user_id, '_jp_cc_reviews_dismissed_triggers', true );
190
191 if ( ! $dismissed_triggers ) {
192 $dismissed_triggers = array();
193 }
194
195 return $dismissed_triggers;
196 }
197
198 /**
199 * Returns true if the user has opted to never see this again. Or sets the option.
200 *
201 * @param bool $set If set this will mark the user as having opted to never see this again.
202 *
203 * @return bool
204 */
205 public static function already_did( $set = false ) {
206 $user_id = get_current_user_id();
207
208 if ( $set ) {
209 update_user_meta( $user_id, '_jp_cc_reviews_already_did', true );
210
211 return true;
212 }
213
214 return (bool) get_user_meta( $user_id, '_jp_cc_reviews_already_did', true );
215 }
216
217 /**
218 * Gets a list of triggers.
219 *
220 * @param null $group
221 * @param null $code
222 *
223 * @return bool|mixed
224 */
225 public static function triggers( $group = null, $code = null ) {
226 static $triggers;
227
228 if ( ! isset( $triggers ) ) {
229
230 $link = 'https://wordpress.org/support/plugin/content-control/reviews/?rate=5#rate-response';
231
232 $time_message = __( 'Hi there! You\'ve been using the Content Control plugin on your site for %s now - We hope it\'s been helpful. If you\'re enjoying the plugin, would you mind rating it 5-stars to help spread the word?', 'content-control' );
233 $triggers = array(
234 'time_installed' => array(
235 'triggers' => array(
236 'one_week' => array(
237 'message' => sprintf( $time_message, __( '1 week', 'content-control' ) ),
238 'conditions' => array(
239 strtotime( self::installed_on() . ' +1 week' ) < time(),
240 ),
241 'link' => $link,
242 'pri' => 10,
243 ),
244 'one_month' => array(
245 'message' => sprintf( $time_message, __( '1 month', 'content-control' ) ),
246 'conditions' => array(
247 strtotime( self::installed_on() . ' +1 month' ) < time(),
248 ),
249 'link' => $link,
250 'pri' => 20,
251 ),
252 'three_months' => array(
253 'message' => sprintf( $time_message, __( '3 months', 'content-control' ) ),
254 'conditions' => array(
255 strtotime( self::installed_on() . ' +3 months' ) < time(),
256 ),
257 'link' => $link,
258 'pri' => 30,
259 ),
260
261 ),
262 'pri' => 10,
263 ),
264 );
265
266 $triggers = apply_filters( 'jp_cc_reviews_triggers', $triggers );
267
268 // Sort Groups
269 uasort( $triggers, array( __CLASS__, 'rsort_by_priority' ) );
270
271 // Sort each groups triggers.
272 foreach ( $triggers as $k => $v ) {
273 uasort( $triggers[ $k ]['triggers'], array( __CLASS__, 'rsort_by_priority' ) );
274 }
275 }
276
277 if ( isset( $group ) ) {
278 if ( ! isset( $triggers[ $group ] ) ) {
279 return false;
280 }
281
282 return ! isset( $code ) ? $triggers[ $group ] : isset( $triggers[ $group ]['triggers'][ $code ] ) ? $triggers[ $group ]['triggers'][ $code ] : false;
283 }
284
285 return $triggers;
286 }
287
288 /**
289 * Render admin notices if available.
290 */
291 public static function admin_notices() {
292 if ( self::hide_notices() ) {
293 return;
294 }
295
296 $group = self::get_trigger_group();
297 $code = self::get_trigger_code();
298 $pri = self::get_current_trigger( 'pri' );
299 $trigger = self::get_current_trigger();
300
301 // Used to anonymously distinguish unique site+user combinations in terms of effectiveness of each trigger.
302 $uuid = wp_hash( home_url() . '-' . get_current_user_id() );
303
304 ?>
305
306 <script type="text/javascript">
307 (function ($) {
308 var trigger = {
309 group: '<?php echo $group; ?>',
310 code: '<?php echo $code; ?>',
311 pri: '<?php echo $pri; ?>'
312 };
313
314 function dismiss(reason) {
315 $.ajax({
316 method: "POST",
317 dataType: "json",
318 url: ajaxurl,
319 data: {
320 action: 'jp_cc_review_action',
321 nonce: '<?php echo wp_create_nonce( 'jp_cc_review_action' ); ?>',
322 group: trigger.group,
323 code: trigger.code,
324 pri: trigger.pri,
325 reason: reason
326 }
327 });
328
329 <?php if ( ! empty( self::$api_url ) ) : ?>
330 $.ajax({
331 method: "POST",
332 dataType: "json",
333 url: '<?php echo self::$api_url; ?>',
334 data: {
335 trigger_group: trigger.group,
336 trigger_code: trigger.code,
337 reason: reason,
338 uuid: '<?php echo $uuid; ?>'
339 }
340 });
341 <?php endif; ?>
342 }
343
344 $(document)
345 .on('click', '.jp-cc-notice .jp-cc-dismiss', function (event) {
346 var $this = $(this),
347 reason = $this.data('reason'),
348 notice = $this.parents('.jp-cc-notice');
349
350 notice.fadeTo(100, 0, function () {
351 notice.slideUp(100, function () {
352 notice.remove();
353 });
354 });
355
356 dismiss(reason);
357 })
358 .ready(function () {
359 setTimeout(function () {
360 $('.jp-cc-notice button.notice-dismiss').click(function (event) {
361 dismiss('maybe_later');
362 });
363 }, 1000);
364 });
365 }(jQuery));
366 </script>
367
368 <style>
369 .jp-cc-notice p {
370 margin-bottom: 0;
371 }
372
373 .jp-cc-notice img.logo {
374 float: right;
375 margin-left: 10px;
376 width: 75px;
377 padding: 0.25em;
378 border: 1px solid #ccc;
379 }
380 </style>
381
382 <div class="notice notice-success is-dismissible jp-cc-notice">
383
384 <p>
385 <img class="logo" src="<?php echo \JP_Content_Control::$URL; ?>assets/images/icon-128x128.png" />
386 <strong>
387 <?php echo $trigger['message']; ?>
388 <br />
389 <?php
390
391 $names = array(
392 '<a target="_blank" href="https://twitter.com/danieliser" title="Follow Daniel on Twitter">@danieliser</a>',
393 '<a target="_blank" href="https://twitter.com/calumallison" title="Follow Calum on Twitter">@calumallison</a>',
394 );
395
396 shuffle( $names );
397
398 echo '~ ' . implode( ' & ', $names );
399
400 ?>
401 </strong>
402 </p>
403 <ul>
404 <li>
405 <a class="jp-cc-dismiss" target="_blank" href="<?php echo $trigger['link']; ?>>" data-reason="am_now">
406 <strong><?php _e( 'Ok, you deserve it', 'content-control' ); ?></strong>
407 </a>
408 </li>
409 <li>
410 <a href="#" class="jp-cc-dismiss" data-reason="maybe_later">
411 <?php _e( 'Nope, maybe later', 'content-control' ); ?>
412 </a>
413 </li>
414 <li>
415 <a href="#" class="jp-cc-dismiss" data-reason="already_did">
416 <?php _e( 'I already did', 'content-control' ); ?>
417 </a>
418 </li>
419 </ul>
420
421 </div>
422
423 <?php
424 }
425
426 /**
427 * Checks if notices should be shown.
428 *
429 * @return bool
430 */
431 public static function hide_notices() {
432 $code = self::get_trigger_code();
433
434 $conditions = array(
435 self::already_did(),
436 self::last_dismissed() && strtotime( self::last_dismissed() . ' +2 weeks' ) > time(),
437 empty( $code ),
438 );
439
440 return in_array( true, $conditions );
441 }
442
443 /**
444 * Gets the last dismissed date.
445 *
446 * @return false|string
447 */
448 public static function last_dismissed() {
449 $user_id = get_current_user_id();
450
451 return get_user_meta( $user_id, '_jp_cc_reviews_last_dismissed', true );
452 }
453
454 /**
455 * Sort array by priority value
456 *
457 * @param $a
458 * @param $b
459 *
460 * @return int
461 */
462 public static function sort_by_priority( $a, $b ) {
463 if ( ! isset( $a['pri'] ) || ! isset( $b['pri'] ) || $a['pri'] === $b['pri'] ) {
464 return 0;
465 }
466
467 return ( $a['pri'] < $b['pri'] ) ? - 1 : 1;
468 }
469
470 /**
471 * Sort array in reverse by priority value
472 *
473 * @param $a
474 * @param $b
475 *
476 * @return int
477 */
478 public static function rsort_by_priority( $a, $b ) {
479 if ( ! isset( $a['pri'] ) || ! isset( $b['pri'] ) || $a['pri'] === $b['pri'] ) {
480 return 0;
481 }
482
483 return ( $a['pri'] < $b['pri'] ) ? 1 : - 1;
484 }
485
486 }
487