| 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 |
add_action( 'admin_notices', array( $this, 'show' ) ); |
| 24 |
add_action( 'boxzilla_admin_dismiss_review_notice', array( $this, 'dismiss' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Set flag in user meta so notice won't be shown. |
| 29 |
*/ |
| 30 |
public function dismiss() { |
| 31 |
$user = wp_get_current_user(); |
| 32 |
update_user_meta( $user->ID, $this->meta_key_dismissed, 1 ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @return bool |
| 37 |
*/ |
| 38 |
public function show() { |
| 39 |
$screen = get_current_screen(); |
| 40 |
if ( ! $screen instanceof WP_Screen ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
// on some boxzilla screen? |
| 45 |
if ( $screen->post_type !== 'boxzilla-box' ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
// authorized? |
| 50 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
// only show if 2 weeks have passed since first use. |
| 55 |
$two_weeks_in_seconds = ( 60 * 60 * 24 * 14 ); |
| 56 |
if ( $this->time_since_first_use() <= $two_weeks_in_seconds ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
// only show if user did not dismiss before |
| 61 |
$user = wp_get_current_user(); |
| 62 |
if ( get_user_meta( $user->ID, $this->meta_key_dismissed, true ) ) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
echo '<div class="notice notice-info boxzilla-is-dismissible">'; |
| 67 |
echo '<p>'; |
| 68 |
echo esc_html__( 'You\'ve been using Boxzilla for some time now; we hope you love it!', 'boxzilla' ), ' <br />'; |
| 69 |
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>'; |
| 70 |
echo esc_html__('It would be of great help to us.', 'boxzilla' ); |
| 71 |
echo '</p>'; |
| 72 |
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>'; |
| 73 |
echo '</div>'; |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return int |
| 79 |
*/ |
| 80 |
private function time_since_first_use() { |
| 81 |
$options = get_option( 'boxzilla_settings' ); |
| 82 |
|
| 83 |
// option was never added before, do it now. |
| 84 |
if ( empty( $options['first_activated_on'] ) ) { |
| 85 |
$options['first_activated_on'] = time(); |
| 86 |
update_option( 'boxzilla_settings', $options ); |
| 87 |
} |
| 88 |
|
| 89 |
return time() - $options['first_activated_on']; |
| 90 |
} |
| 91 |
} |
| 92 |
|