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