| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Admin; |
| 4 |
|
| 5 |
use WP_Screen; |
| 6 |
|
| 7 |
if (! defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Class ReviewNotice |
| 13 |
* |
| 14 |
* @ignore |
| 15 |
*/ |
| 16 |
class ReviewNotice |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $meta_key_dismissed = '_boxzilla_review_notice_dismissed'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Add action & filter hooks. |
| 25 |
*/ |
| 26 |
public function init() |
| 27 |
{ |
| 28 |
add_action('admin_init', [ $this, 'listen_for_dismiss' ]); |
| 29 |
add_action('admin_notices', [ $this, 'show' ]); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Listen for notice dismissal request. |
| 34 |
*/ |
| 35 |
public function listen_for_dismiss() |
| 36 |
{ |
| 37 |
if (empty($_POST['_boxzilla_review_notice_action'])) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$action = sanitize_key(wp_unslash($_POST['_boxzilla_review_notice_action'])); |
| 42 |
if ($action !== 'dismiss_review_notice') { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if (! current_user_can('edit_posts')) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if (empty($_POST['_boxzilla_review_notice_nonce'])) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$nonce = sanitize_text_field(wp_unslash($_POST['_boxzilla_review_notice_nonce'])); |
| 55 |
if (! wp_verify_nonce($nonce, 'boxzilla_dismiss_review_notice')) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$this->dismiss(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Set flag in user meta so notice won't be shown. |
| 64 |
*/ |
| 65 |
public function dismiss() |
| 66 |
{ |
| 67 |
$user = wp_get_current_user(); |
| 68 |
update_user_meta($user->ID, $this->meta_key_dismissed, 1); |
| 69 |
} |
| 70 |
|
| 71 |
public function show() |
| 72 |
{ |
| 73 |
$screen = get_current_screen(); |
| 74 |
if (! $screen instanceof WP_Screen) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// on some boxzilla screen? |
| 79 |
if ($screen->post_type !== 'boxzilla-box') { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
// authorized? |
| 84 |
if (! current_user_can('edit_posts')) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// only show if 2 weeks have passed since first use. |
| 89 |
$two_weeks_in_seconds = 60 * 60 * 24 * 14; |
| 90 |
if ($this->time_since_first_use() <= $two_weeks_in_seconds) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// only show if user did not dismiss before |
| 95 |
$user = wp_get_current_user(); |
| 96 |
if (get_user_meta($user->ID, $this->meta_key_dismissed, true)) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
echo '<div class="notice notice-info boxzilla-is-dismissible">'; |
| 101 |
echo '<p>'; |
| 102 |
echo '<strong>'; |
| 103 |
echo esc_html__('Has Boxzilla been helpful on your site?', 'boxzilla'), ' <br />'; |
| 104 |
echo '</strong>'; |
| 105 |
echo esc_html__('Could you spare 60 seconds to leave a quick, honest review on WordPress.org?', 'boxzilla'), ' '; |
| 106 |
echo '<a href="https://wordpress.org/support/plugin/boxzilla/reviews/?rate=5#new-post">', esc_html__('Leave a quick review', 'boxzilla'), '</a><br />'; |
| 107 |
echo esc_html__('Your review helps other site owners discover Boxzilla and helps us keep improving it.', 'boxzilla'); |
| 108 |
echo '</p>'; |
| 109 |
echo '<form method="post"><button type="submit" class="notice-dismiss"><span class="screen-reader-text">', esc_html__('Dismiss this notice.', 'boxzilla'), '</span></button><input type="hidden" name="_boxzilla_review_notice_action" value="dismiss_review_notice"/>'; |
| 110 |
wp_nonce_field('boxzilla_dismiss_review_notice', '_boxzilla_review_notice_nonce', false); |
| 111 |
echo '</form>'; |
| 112 |
echo '</div>'; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @return int |
| 117 |
*/ |
| 118 |
private function time_since_first_use() |
| 119 |
{ |
| 120 |
$options = (array) get_option('boxzilla_settings', []); |
| 121 |
|
| 122 |
// option was never added before, do it now. |
| 123 |
if (empty($options['first_activated_on'])) { |
| 124 |
$options['first_activated_on'] = time(); |
| 125 |
update_option('boxzilla_settings', $options); |
| 126 |
} |
| 127 |
|
| 128 |
return time() - $options['first_activated_on']; |
| 129 |
} |
| 130 |
} |
| 131 |
|