Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.97% |
32 / 33 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Dashboard_Announcements | |
96.97% |
32 / 33 |
|
83.33% |
5 / 6 |
13 | |
0.00% |
0 / 1 |
| configure | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| is_enabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add_script_data | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| register | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
| get_announcements | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| predates_current_user | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The announcements the dashboard publishes on the What's new surface it hosts. |
| 4 | * |
| 5 | * @package automattic/jetpack-premium-analytics |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 | |
| 10 | use Automattic\Jetpack\Feature_Flags\Feature_Flags; |
| 11 | use function Automattic\Jetpack\Announcements\register_announcement; |
| 12 | |
| 13 | /** |
| 14 | * Registers the dashboard's announcements with the Jetpack Announcements package. |
| 15 | */ |
| 16 | class Dashboard_Announcements { |
| 17 | |
| 18 | /** |
| 19 | * Channel every page of the dashboard subscribes to, alone and followed by `/` and its section. |
| 20 | * |
| 21 | * @see docs/whats-new-channels.md |
| 22 | */ |
| 23 | const CHANNEL = 'premium-analytics'; |
| 24 | |
| 25 | /** |
| 26 | * Flag that turns the dashboard's What's new surface and its announcements on. |
| 27 | */ |
| 28 | const FLAG = 'jetpack-stats-whats-new'; |
| 29 | |
| 30 | /** |
| 31 | * Register the flag, the registrant, and the flag's answer for the dashboard's client. |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public static function configure() { |
| 36 | Feature_Flags::register( |
| 37 | self::FLAG, |
| 38 | array( |
| 39 | 'default' => false, |
| 40 | 'description' => "Show the What's new teaser and reader on Stats v2, with the announcements the dashboard registers.", |
| 41 | 'owner' => 'jetpack-stats', |
| 42 | ) |
| 43 | ); |
| 44 | |
| 45 | add_action( 'jetpack_register_announcements', array( __CLASS__, 'register' ) ); |
| 46 | add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'add_script_data' ), 20 ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Whether the dashboard shows What's new. |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | public static function is_enabled() { |
| 55 | return Feature_Flags::is_enabled( self::FLAG ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Tell the dashboard's client whether to mount the What's new surface. |
| 60 | * |
| 61 | * @param array $data The script data passed by the assets package. |
| 62 | * @return array |
| 63 | */ |
| 64 | public static function add_script_data( array $data ): array { |
| 65 | if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 66 | $data['premium_analytics'] = array(); |
| 67 | } |
| 68 | |
| 69 | $data['premium_analytics']['whats_new_enabled'] = self::is_enabled(); |
| 70 | |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Register the dashboard's announcements. |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public static function register() { |
| 80 | if ( ! self::is_enabled() ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Another plugin may load an older copy of the package first. |
| 85 | if ( ! function_exists( 'Automattic\\Jetpack\\Announcements\\register_announcement' ) ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // The dashboard's own rules: what shipped before a user arrived is no news to them, |
| 90 | // and its news is for whoever can read the dashboard. |
| 91 | foreach ( self::get_announcements() as $id => $announcement ) { |
| 92 | if ( ! self::predates_current_user( $announcement['date'] ) ) { |
| 93 | register_announcement( $id, $announcement + array( 'capability' => Capabilities::VIEW_ANALYTICS ) ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * The announcements of the dashboard, as `$id => $args` pairs. |
| 100 | * |
| 101 | * @return array[] |
| 102 | */ |
| 103 | private static function get_announcements() { |
| 104 | return array( |
| 105 | 'premium-analytics/arrange-tabs' => array( |
| 106 | 'date' => '2026-11-12', |
| 107 | 'title' => __( 'Arrange your tabs', 'jetpack-premium-analytics-pkg' ), |
| 108 | 'excerpt' => __( 'Reorder the dashboard tabs and choose the one that opens first.', 'jetpack-premium-analytics-pkg' ), |
| 109 | 'content' => '<p>' . esc_html__( 'Open the Stats options menu and choose Arrange tabs to reorder the tabs and to choose the one that opens first.', 'jetpack-premium-analytics-pkg' ) . '</p>', |
| 110 | 'channels' => array( self::CHANNEL . '/main' ), |
| 111 | ), |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Whether a date is older than the current user's registration. |
| 117 | * |
| 118 | * @param string $date Date of an announcement. |
| 119 | * @return bool |
| 120 | */ |
| 121 | private static function predates_current_user( $date ) { |
| 122 | $registered = strtotime( wp_get_current_user()->user_registered . ' +0000' ); |
| 123 | |
| 124 | return false !== $registered && strtotime( $date . ' +0000' ) < $registered; |
| 125 | } |
| 126 | } |