| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Admin; |
| 4 |
|
| 5 |
use WP_Screen; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class ReviewNotice |
| 9 |
* |
| 10 |
* @ignore |
| 11 |
*/ |
| 12 |
class ReviewNotice |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $meta_key_dismissed = '_boxzilla_review_notice_dismissed'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Add action & filter hooks. |
| 21 |
*/ |
| 22 |
public function init() |
| 23 |
{ |
| 24 |
add_action('admin_notices', [ $this, 'show' ]); |
| 25 |
add_action('boxzilla_admin_dismiss_review_notice', [ $this, 'dismiss' ]); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Set flag in user meta so notice won't be shown. |
| 30 |
*/ |
| 31 |
public function dismiss() |
| 32 |
{ |
| 33 |
$user = wp_get_current_user(); |
| 34 |
update_user_meta($user->ID, $this->meta_key_dismissed, 1); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
public function show() |
| 41 |
{ |
| 42 |
$screen = get_current_screen(); |
| 43 |
if (! $screen instanceof WP_Screen) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
// on some boxzilla screen? |
| 48 |
if ($screen->post_type !== 'boxzilla-box') { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// authorized? |
| 53 |
if (! current_user_can('edit_posts')) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// only show if 2 weeks have passed since first use. |
| 58 |
$two_weeks_in_seconds = ( 60 * 60 * 24 * 14 ); |
| 59 |
if ($this->time_since_first_use() <= $two_weeks_in_seconds) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
// only show if user did not dismiss before |
| 64 |
$user = wp_get_current_user(); |
| 65 |
if (get_user_meta($user->ID, $this->meta_key_dismissed, true)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
echo '<div class="notice notice-info boxzilla-is-dismissible">'; |
| 70 |
echo '<p>'; |
| 71 |
echo esc_html__('You\'ve been using Boxzilla for some time now; we hope you love it!', 'boxzilla'), ' <br />'; |
| 72 |
echo '<a href="https://wordpress.org/support/view/plugin-reviews/boxzilla?rate=5#new-post">', esc_html__('If you do, please leave us a nice plugin review on WordPress.org.', 'boxzilla'), '</a>'; |
| 73 |
echo esc_html__('It would be of great help to us.', 'boxzilla'); |
| 74 |
echo '</p>'; |
| 75 |
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_admin_action" value="dismiss_review_notice"/></form>'; |
| 76 |
echo '</div>'; |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return int |
| 82 |
*/ |
| 83 |
private function time_since_first_use() |
| 84 |
{ |
| 85 |
$options = get_option('boxzilla_settings'); |
| 86 |
|
| 87 |
// option was never added before, do it now. |
| 88 |
if (empty($options['first_activated_on'])) { |
| 89 |
$options['first_activated_on'] = time(); |
| 90 |
update_option('boxzilla_settings', $options); |
| 91 |
} |
| 92 |
|
| 93 |
return time() - $options['first_activated_on']; |
| 94 |
} |
| 95 |
} |
| 96 |
|