Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.86% |
87 / 88 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Notices | |
98.86% |
87 / 88 |
|
85.71% |
6 / 7 |
26 | |
0.00% |
0 / 1 |
| update_notice | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
1 | |||
| get_notices_to_show | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
11 | |||
| to_detail_records | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| get_notices_from_wpcom | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
4 | |||
| is_notice_hidden | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| is_hidden | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| to_detail_record | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * A class that handles the notices for the Stats Admin dashboard. |
| 4 | * |
| 5 | * @package automattic/jetpack-stats-admin |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Stats_Admin; |
| 9 | |
| 10 | use Automattic\Jetpack\Stats\Options as Stats_Options; |
| 11 | use Jetpack_Options; |
| 12 | |
| 13 | /** |
| 14 | * The Notices class handles the notices for the Stats Admin dashboard. |
| 15 | * |
| 16 | * @package Automattic\Jetpack\Stats_Admin |
| 17 | */ |
| 18 | class Notices { |
| 19 | const STATS_DASHBOARD_NOTICES_CACHE_KEY = 'jetpack_stats_dashboard_notices_cache_key'; |
| 20 | |
| 21 | /** |
| 22 | * The flat map and the detail records are different shapes of the same resource, so they |
| 23 | * cannot share a transient. |
| 24 | */ |
| 25 | const STATS_DASHBOARD_NOTICES_DETAILS_CACHE_KEY = self::STATS_DASHBOARD_NOTICES_CACHE_KEY . '_details'; |
| 26 | |
| 27 | const OPT_OUT_NEW_STATS_NOTICE_ID = 'opt_out_new_stats'; |
| 28 | const NEW_STATS_FEEDBACK_NOTICE_ID = 'new_stats_feedback'; |
| 29 | const OPT_IN_NEW_STATS_NOTICE_ID = 'opt_in_new_stats'; |
| 30 | const GDPR_COOKIE_CONSENT_NOTICE_ID = 'gdpr_cookie_consent'; |
| 31 | |
| 32 | const VIEWS_TO_SHOW_FEEDBACK = 3; |
| 33 | const POSTPONE_OPT_IN_NOTICE_DAYS = 30; |
| 34 | |
| 35 | /** |
| 36 | * Update notice status. |
| 37 | * |
| 38 | * @param mixed $id ID of the notice. |
| 39 | * @param mixed $status Status of the notice. |
| 40 | * @param int $postponed_for Postponed for how many seconds. |
| 41 | * @return bool |
| 42 | */ |
| 43 | public function update_notice( $id, $status, $postponed_for = 0 ) { |
| 44 | delete_transient( self::STATS_DASHBOARD_NOTICES_CACHE_KEY ); |
| 45 | delete_transient( self::STATS_DASHBOARD_NOTICES_DETAILS_CACHE_KEY ); |
| 46 | return WPCOM_Client::request_as_blog( |
| 47 | sprintf( |
| 48 | '/sites/%d/jetpack-stats-dashboard/notices', |
| 49 | Jetpack_Options::get_option( 'id' ) |
| 50 | ), |
| 51 | 'v2', |
| 52 | array( |
| 53 | 'timeout' => 5, |
| 54 | 'method' => 'POST', |
| 55 | 'headers' => array( |
| 56 | 'Content-Type' => 'application/json', |
| 57 | ), |
| 58 | ), |
| 59 | wp_json_encode( |
| 60 | array( |
| 61 | 'id' => $id, |
| 62 | 'status' => $status, |
| 63 | 'postponed_for' => $postponed_for, |
| 64 | ), |
| 65 | JSON_UNESCAPED_SLASHES |
| 66 | ), |
| 67 | 'wpcom' |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Return an array of notices IDs as keys and their value to flag whether to show them. |
| 73 | * |
| 74 | * @param bool $include_details Return a detail record per notice instead of a bare flag. |
| 75 | * @return array |
| 76 | */ |
| 77 | public function get_notices_to_show( bool $include_details = false ) { |
| 78 | // Reuse the one fetch for every flag below, so details mode doesn't also pull the flat map. |
| 79 | $notices_wpcom = $this->get_notices_from_wpcom( $include_details ); |
| 80 | |
| 81 | $new_stats_enabled = Stats_Options::get_option( 'enable_odyssey_stats' ); |
| 82 | $stats_views = intval( Stats_Options::get_option( 'views' ) ); |
| 83 | $odyssey_stats_changed_at = intval( Stats_Options::get_option( 'odyssey_stats_changed_at' ) ); |
| 84 | |
| 85 | // Check if Jetpack is integrated with the Complianz plugin, which blocks the Stats. |
| 86 | $complianz_options_integrations = get_option( 'complianz_options_integrations' ); |
| 87 | $is_jetpack_blocked_by_complianz = ! isset( $complianz_options_integrations['jetpack'] ) || $complianz_options_integrations['jetpack']; |
| 88 | |
| 89 | $local_notices = array( |
| 90 | // Show Opt-in notice 30 days after the new stats being disabled. |
| 91 | self::OPT_IN_NEW_STATS_NOTICE_ID => ! $new_stats_enabled |
| 92 | && $odyssey_stats_changed_at < time() - self::POSTPONE_OPT_IN_NOTICE_DAYS * DAY_IN_SECONDS |
| 93 | && ! $this->is_hidden( $notices_wpcom, self::OPT_IN_NEW_STATS_NOTICE_ID ), |
| 94 | |
| 95 | // Show feedback notice after 3 views of the new stats. |
| 96 | self::NEW_STATS_FEEDBACK_NOTICE_ID => $new_stats_enabled |
| 97 | && $stats_views >= self::VIEWS_TO_SHOW_FEEDBACK |
| 98 | && ! $this->is_hidden( $notices_wpcom, self::NEW_STATS_FEEDBACK_NOTICE_ID ), |
| 99 | |
| 100 | // Show opt-out notice before 3 views of the new stats, where 3 is included. |
| 101 | self::OPT_OUT_NEW_STATS_NOTICE_ID => $new_stats_enabled |
| 102 | && $stats_views < self::VIEWS_TO_SHOW_FEEDBACK |
| 103 | && ! $this->is_hidden( $notices_wpcom, self::OPT_OUT_NEW_STATS_NOTICE_ID ), |
| 104 | |
| 105 | // GDPR cookie consent notice for Complianz users. |
| 106 | self::GDPR_COOKIE_CONSENT_NOTICE_ID => class_exists( 'COMPLIANZ' ) && $is_jetpack_blocked_by_complianz |
| 107 | && ! $this->is_hidden( $notices_wpcom, self::GDPR_COOKIE_CONSENT_NOTICE_ID ), |
| 108 | ); |
| 109 | |
| 110 | if ( $include_details ) { |
| 111 | return $this->to_detail_records( $notices_wpcom, $local_notices ); |
| 112 | } |
| 113 | |
| 114 | return array_merge( $notices_wpcom, $local_notices ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Normalize every notice to the detail shape, whichever shape WPCOM answered in. |
| 119 | * |
| 120 | * The package ships to self-hosted sites and can run for months against a WPCOM that does not |
| 121 | * serve `include_details` yet, so a passed-through flat value would leave the caller parsing |
| 122 | * two shapes in one response. |
| 123 | * |
| 124 | * @param array $notices_wpcom The WPCOM response, flat map or detail records. |
| 125 | * @param array $local_notices The locally-computed visibility flags. |
| 126 | * @return array |
| 127 | */ |
| 128 | private function to_detail_records( array $notices_wpcom, array $local_notices ) { |
| 129 | $notices = array(); |
| 130 | |
| 131 | foreach ( array_keys( array_merge( $notices_wpcom, $local_notices ) ) as $id ) { |
| 132 | // A local notice keeps its own visibility; its escalation fields still come from WPCOM. |
| 133 | $show = array_key_exists( $id, $local_notices ) |
| 134 | ? (bool) $local_notices[ $id ] |
| 135 | : ! $this->is_hidden( $notices_wpcom, $id ); |
| 136 | |
| 137 | $notices[ $id ] = $this->to_detail_record( $notices_wpcom[ $id ] ?? null, $show ); |
| 138 | } |
| 139 | |
| 140 | return $notices; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the array of hidden notices from WPCOM. |
| 145 | * |
| 146 | * @param bool $include_details Ask WPCOM for detail records instead of a flat map. |
| 147 | * @return array |
| 148 | */ |
| 149 | public function get_notices_from_wpcom( bool $include_details = false ) { |
| 150 | $path = sprintf( |
| 151 | '/sites/%d/jetpack-stats-dashboard/notices', |
| 152 | Jetpack_Options::get_option( 'id' ) |
| 153 | ); |
| 154 | |
| 155 | if ( $include_details ) { |
| 156 | $path .= '?include_details=true'; |
| 157 | } |
| 158 | |
| 159 | $notices_wpcom = WPCOM_Client::request_as_blog_cached( |
| 160 | $path, |
| 161 | 'v2', |
| 162 | array( |
| 163 | 'timeout' => 5, |
| 164 | ), |
| 165 | null, |
| 166 | 'wpcom', |
| 167 | true, |
| 168 | $include_details |
| 169 | ? static::STATS_DASHBOARD_NOTICES_DETAILS_CACHE_KEY |
| 170 | : static::STATS_DASHBOARD_NOTICES_CACHE_KEY |
| 171 | ); |
| 172 | |
| 173 | if ( is_wp_error( $notices_wpcom ) ) { |
| 174 | return array(); |
| 175 | } |
| 176 | return $notices_wpcom; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Checks if a notice is hidden. |
| 181 | * |
| 182 | * @param mixed $id ID of the notice. |
| 183 | * @return bool |
| 184 | */ |
| 185 | public function is_notice_hidden( $id ) { |
| 186 | return $this->is_hidden( $this->get_notices_from_wpcom(), $id ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Whether a notice is hidden in an already-fetched WPCOM response, in either shape. |
| 191 | * |
| 192 | * @param array $notices_wpcom The WPCOM response, flat map or detail records. |
| 193 | * @param mixed $id ID of the notice. |
| 194 | * @return bool |
| 195 | */ |
| 196 | private function is_hidden( array $notices_wpcom, $id ) { |
| 197 | if ( ! array_key_exists( $id, $notices_wpcom ) ) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | $record = $notices_wpcom[ $id ]; |
| 202 | |
| 203 | return is_array( $record ) ? empty( $record['show'] ) : $record === false; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Wrap a locally-computed flag in the detail shape, filling the escalation fields from WPCOM. |
| 208 | * |
| 209 | * @param mixed $wpcom_record The WPCOM detail record for this notice, or null when it has none. |
| 210 | * @param bool $show The locally-computed visibility flag. |
| 211 | * @return array |
| 212 | */ |
| 213 | private function to_detail_record( $wpcom_record, bool $show ) { |
| 214 | $record = is_array( $wpcom_record ) ? $wpcom_record : array(); |
| 215 | |
| 216 | return array( |
| 217 | 'show' => $show, |
| 218 | 'status' => $record['status'] ?? null, |
| 219 | 'postponed_count' => (int) ( $record['postponed_count'] ?? 0 ), |
| 220 | // A non-numeric value would cast to 0, which reads as "due now" rather than "not scheduled". |
| 221 | 'next_show_at' => is_numeric( $record['next_show_at'] ?? null ) ? (int) $record['next_show_at'] : null, |
| 222 | ); |
| 223 | } |
| 224 | } |