Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.15% covered (success)
98.15%
53 / 54
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Expiry_Notice_Dismiss
98.15% covered (success)
98.15%
53 / 54
91.67% covered (success)
91.67%
11 / 12
28
0.00% covered (danger)
0.00%
0 / 1
 register_user_meta
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
 meta_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 banner_meta_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dismiss
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 is_dismissible
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 should_show_banner
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 should_show_modal
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 modal_meta_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 modal_dismissal
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 is_dismissed
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 term_expiry_ts
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_dismissed_at
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
1<?php
2/**
3 * Expiry_Notice_Dismiss: who has already closed which expiry notice.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices;
11
12/**
13 * Dismissals live in user meta written through core's `/wp/v2/users/me`, so
14 * wp-admin and the front end read and write the same record.
15 *
16 * The `META_*` constants are base names; the stored key is per site (see meta_key()).
17 */
18class Expiry_Notice_Dismiss {
19
20    // One key for every banner surface.
21    const META_BANNER = 'wpcom_plan_expiry_notice_dismiss';
22    const META_MODAL  = 'wpcom_plan_expiry_modal_dismiss';
23    // Separate from META_MODAL: a grace dismissal is stamped after `expiry_ts`
24    // and would otherwise satisfy the post-grace check for a modal never seen.
25    const META_MODAL_GRACE = 'wpcom_plan_expiry_modal_dismiss_grace';
26
27    const FINAL_WINDOW_DAYS = 7;
28
29    // Stands in for the "browser session" the design asks for: wp-admin and
30    // Calypso are separate origins, so the dismissal has to live server-side.
31    const MODAL_GRACE_DISMISS_TTL = DAY_IN_SECONDS;
32
33    /**
34     * Register the dismiss meta keys for REST writes by the user themselves.
35     *
36     * The stored value is always the server's clock, whatever the client sent:
37     * it is compared against the term's expiry to tell one lapse from the next.
38     */
39    public static function register_user_meta(): void {
40        foreach ( array( self::META_BANNER, self::META_MODAL, self::META_MODAL_GRACE ) as $base ) {
41            register_meta(
42                'user',
43                self::meta_key( $base ),
44                array(
45                    'show_in_rest'      => true,
46                    'single'            => true,
47                    'type'              => 'integer',
48                    'sanitize_callback' => static function () {
49                        return time();
50                    },
51                    'auth_callback'     => static function () {
52                        return current_user_can( 'manage_options' );
53                    },
54                )
55            );
56        }
57    }
58
59    /**
60     * The stored meta key for a base name, prefixed per site the way core
61     * keys per-site user settings: on Simple every site shares one usermeta
62     * table, and a dismissal on one site must not silence another.
63     *
64     * @param string $base One of the `META_*` constants.
65     */
66    public static function meta_key( string $base ): string {
67        global $wpdb;
68        return $wpdb->get_blog_prefix() . $base;
69    }
70
71    /**
72     * The key the banner dismisses to.
73     */
74    public static function banner_meta_key(): string {
75        return self::meta_key( self::META_BANNER );
76    }
77
78    /**
79     * Stamp a dismissal for a user, now. False for a key that is not one of
80     * this site's dismissal keys, so nothing can write other user meta through
81     * it, and for a user who does not exist.
82     *
83     * @param int    $user_id  The dismissing user.
84     * @param string $meta_key One of this site's dismissal keys, as the surface was given it.
85     */
86    public static function dismiss( int $user_id, string $meta_key ): bool {
87        $allowed = array_map( array( self::class, 'meta_key' ), array( self::META_BANNER, self::META_MODAL, self::META_MODAL_GRACE ) );
88        if ( ! $user_id || ! in_array( $meta_key, $allowed, true ) ) {
89            return false;
90        }
91        $now = time();
92        update_user_meta( $user_id, $meta_key, $now );
93        return (int) get_user_meta( $user_id, $meta_key, true ) === $now;
94    }
95
96    /**
97     * Whether the banner for this state can be dismissed at all.
98     *
99     * Only once the revert has happened: before that the site still has
100     * something to lose.
101     *
102     * @param array<string,mixed> $expiry_state State from Expiry_Data::get_expiry_state().
103     */
104    public static function is_dismissible( array $expiry_state ): bool {
105        return Expiry_Data::STATE_EXPIRED === ( $expiry_state['state'] ?? '' );
106    }
107
108    /**
109     * Whether the banner should show for this user right now.
110     *
111     * @param array<string,mixed> $expiry_state State from Expiry_Data::get_expiry_state().
112     * @param int|null            $user_id      Defaults to the current user.
113     */
114    public static function should_show_banner( array $expiry_state, ?int $user_id = null ): bool {
115        return ! self::is_dismissible( $expiry_state )
116            || ! self::is_dismissed( $user_id, self::banner_meta_key(), self::term_expiry_ts( $expiry_state ) );
117    }
118
119    /**
120     * Whether the modal should show for this user right now.
121     *
122     * @param array<string,mixed> $expiry_state State from Expiry_Data::get_expiry_state().
123     * @param int|null            $user_id      Defaults to the current user.
124     */
125    public static function should_show_modal( array $expiry_state, ?int $user_id = null ): bool {
126        $dismissal = self::modal_dismissal( $expiry_state );
127        if ( null === $dismissal ) {
128            return false;
129        }
130        return ! self::is_dismissed( $user_id, $dismissal['key'], self::term_expiry_ts( $expiry_state ), $dismissal['ttl'] );
131    }
132
133    /**
134     * The meta key the modal dismisses to in this state, or null where it never shows.
135     *
136     * @param array<string,mixed> $expiry_state State from Expiry_Data::get_expiry_state().
137     */
138    public static function modal_meta_key( array $expiry_state ): ?string {
139        return self::modal_dismissal( $expiry_state )['key'] ?? null;
140    }
141
142    /**
143     * How the modal dismisses in this state: in grace it comes back after a
144     * day, after the revert saying so once is enough.
145     *
146     * @param array<string,mixed> $expiry_state State from Expiry_Data::get_expiry_state().
147     * @return array{key:string,ttl:int|null}|null
148     */
149    private static function modal_dismissal( array $expiry_state ): ?array {
150        switch ( $expiry_state['state'] ?? '' ) {
151            case Expiry_Data::STATE_EXPIRED_GRACE:
152                return array(
153                    'key' => self::meta_key( self::META_MODAL_GRACE ),
154                    'ttl' => self::MODAL_GRACE_DISMISS_TTL,
155                );
156            case Expiry_Data::STATE_EXPIRED:
157                return array(
158                    'key' => self::meta_key( self::META_MODAL ),
159                    'ttl' => null,
160                );
161            default:
162                return null;
163        }
164    }
165
166    /**
167     * Whether this user has dismissed the notice for the term the state describes.
168     *
169     * A stamp older than the term's own expiry belongs to a purchase since
170     * renewed and does not count: once the plan is gone, expiry_ts is the
171     * revert time itself, so a dismissal from before the revert never
172     * carries over into the state after it.
173     *
174     * @param int|null $user_id   Defaults to the current user.
175     * @param string   $meta_key  A stored key, from meta_key().
176     * @param int|null $expiry_ts Expiry of the term being judged; null counts any stored dismissal.
177     * @param int|null $ttl       Seconds a dismissal holds for; null never lapses.
178     */
179    public static function is_dismissed( ?int $user_id, string $meta_key, ?int $expiry_ts = null, ?int $ttl = null ): bool {
180        $dismissed_at = self::get_dismissed_at( $user_id, $meta_key );
181        if ( null === $dismissed_at ) {
182            return false;
183        }
184        if ( null !== $ttl && $dismissed_at < time() - $ttl ) {
185            return false;
186        }
187        return null === $expiry_ts || $dismissed_at >= $expiry_ts;
188    }
189
190    /**
191     * The expiry timestamp a state carries, or null when it has none.
192     *
193     * @param array<string,mixed> $expiry_state State from Expiry_Data::get_expiry_state().
194     */
195    private static function term_expiry_ts( array $expiry_state ): ?int {
196        return isset( $expiry_state['expiry_ts'] ) ? (int) $expiry_state['expiry_ts'] : null;
197    }
198
199    /**
200     * The stored dismissal timestamp, or null if none.
201     *
202     * @param int|null $user_id  Defaults to the current user.
203     * @param string   $meta_key A stored key, from meta_key().
204     */
205    private static function get_dismissed_at( ?int $user_id, string $meta_key ): ?int {
206        $user_id ??= get_current_user_id();
207        if ( ! $user_id ) {
208            return null;
209        }
210        $raw = get_user_meta( $user_id, $meta_key, true );
211        return is_numeric( $raw ) && (int) $raw > 0 ? (int) $raw : null;
212    }
213}