Admin
3 years ago
Commands
4 years ago
Db
4 years ago
Ecommerce
3 years ago
Report
4 years ago
Site
3 years ago
TrackingCode
4 years ago
Updater
4 years ago
User
3 years ago
WpStatistics
4 years ago
views
4 years ago
API.php
4 years ago
Access.php
4 years ago
AjaxTracker.php
5 years ago
Annotations.php
4 years ago
Bootstrap.php
4 years ago
Capabilities.php
4 years ago
Compatibility.php
4 years ago
Email.php
4 years ago
Installer.php
4 years ago
Logger.php
4 years ago
OptOut.php
4 years ago
Paths.php
4 years ago
PrivacyBadge.php
4 years ago
RedirectOnActivation.php
4 years ago
Referral.php
4 years ago
Roles.php
4 years ago
ScheduledTasks.php
4 years ago
Settings.php
4 years ago
Site.php
3 years ago
TrackingCode.php
4 years ago
Uninstaller.php
4 years ago
Updater.php
4 years ago
User.php
4 years ago
Referral.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // if accessed directly |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Every 90 days we show a please review notice until the user dismisses this notice or clicks on rate us. |
| 18 | * We only show this notice on Matomo screens. |
| 19 | * |
| 20 | * @todo validate the nonce |
| 21 | * phpcs:disable WordPress.Security.NonceVerification.Missing |
| 22 | */ |
| 23 | class Referral { |
| 24 | const OPTION_NAME_REFERRAL_DISMISSED = 'matomo-referral-dismissed'; |
| 25 | |
| 26 | /** |
| 27 | * @var int |
| 28 | */ |
| 29 | private $time; |
| 30 | |
| 31 | public function __construct() { |
| 32 | $this->time = time(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param int $time |
| 37 | * |
| 38 | * @internal for tests only |
| 39 | */ |
| 40 | public function set_time( $time ) { |
| 41 | $this->time = $time; |
| 42 | } |
| 43 | |
| 44 | public function register_hooks() { |
| 45 | $self = $this; |
| 46 | |
| 47 | add_action( |
| 48 | 'wp_ajax_matomo_referral_dismiss_admin_notice', |
| 49 | function () use ( $self ) { |
| 50 | if ( is_admin() && $self->should_show() && $self->can_refer() ) { |
| 51 | // no need for an nonce check here as it's nothing critical |
| 52 | if ( ! empty( $_POST['forever'] ) ) { |
| 53 | $self->dismiss_forever(); |
| 54 | } else { |
| 55 | $self->dismiss(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | ); |
| 60 | add_action( |
| 61 | 'admin_notices', |
| 62 | function () use ( $self ) { |
| 63 | if ( $self->can_refer() && $self->should_show_on_screen() ) { |
| 64 | $self->render(); |
| 65 | } |
| 66 | } |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | public function render() { |
| 71 | include 'views/referral.php'; |
| 72 | } |
| 73 | |
| 74 | public function should_show_on_screen() { |
| 75 | if ( ! is_admin() ) { |
| 76 | return false; |
| 77 | } |
| 78 | $screen = get_current_screen(); |
| 79 | |
| 80 | return $screen && $screen->id && strpos( $screen->id, 'matomo-' ) === 0; |
| 81 | } |
| 82 | |
| 83 | public function can_refer() { |
| 84 | return current_user_can( Capabilities::KEY_VIEW ); |
| 85 | } |
| 86 | |
| 87 | public function dismiss_forever() { |
| 88 | $ten_years = 60 * 60 * 24 * 365 * 10; |
| 89 | update_option( self::OPTION_NAME_REFERRAL_DISMISSED, $this->time + $ten_years ); |
| 90 | } |
| 91 | |
| 92 | public function dismiss() { |
| 93 | update_option( self::OPTION_NAME_REFERRAL_DISMISSED, $this->time, true ); |
| 94 | } |
| 95 | |
| 96 | public function get_last_dismissed() { |
| 97 | return get_option( self::OPTION_NAME_REFERRAL_DISMISSED ); |
| 98 | } |
| 99 | |
| 100 | private function get_days_in_seconds( $num_days ) { |
| 101 | return 60 * 60 * 24 * $num_days; |
| 102 | } |
| 103 | |
| 104 | public function should_show() { |
| 105 | $dismissed = $this->get_last_dismissed(); |
| 106 | |
| 107 | if ( ! $dismissed ) { |
| 108 | // the first time we check... we set it back 30 days cause we want to see first rating after 60 days |
| 109 | $this->time = $this->time - $this->get_days_in_seconds( 30 ); |
| 110 | $this->dismiss(); |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | $ninety_days_in_seconds = $this->get_days_in_seconds( 90 ); |
| 116 | |
| 117 | if ( $this->time > ( $dismissed + $ninety_days_in_seconds ) ) { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 |