Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
4 / 10
66.67% covered (warning)
66.67%
2 / 3
CRAP
n/a
0 / 0
wpcomsh_extend_stats_menu_item_access
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_enable_premium_analytics
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
wpcomsh_retire_classic_stats
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Jetpack Stats.
4 *
5 * @package wpcomsh
6 */
7
8/**
9 * Extends the default value for which roles can access the Stats menu item.
10 *
11 * This enables the `view_stats` capability to work with all roles that are available on Simple sites.
12 *
13 * @see https://github.com/Automattic/jetpack/pull/33255
14 * @see Automattic\Jetpack\Stats\Options::get_defaults()
15 *
16 * @param array $default_value The default value for stats options.
17 * @return array
18 */
19function wpcomsh_extend_stats_menu_item_access( $default_value ) {
20    if ( is_array( $default_value ) ) {
21        $default_value['roles'] = array( 'administrator', 'editor', 'author', 'contributor' );
22    }
23
24    return $default_value;
25}
26add_filter( 'default_option_stats_options', 'wpcomsh_extend_stats_menu_item_access' );
27
28/**
29 * Turns on the bundled Premium Analytics dashboard for Atomic sites carrying the blog sticker.
30 *
31 * The filter is registered here, at file scope, because Jetpack resolves the flag once per request
32 * from `Jetpack::configure()` on `plugins_loaded` — a callback added on `plugins_loaded` or later
33 * would be too late to be seen.
34 *
35 * @param bool $enabled Whether Premium Analytics is already enabled.
36 * @return bool
37 */
38function wpcomsh_enable_premium_analytics( $enabled ) {
39    return $enabled || wpcomsh_is_site_sticker_active( 'jetpack-premium-analytics' );
40}
41add_filter( 'jetpack_premium_analytics_enabled', 'wpcomsh_enable_premium_analytics' );
42
43/**
44 * Turns on the classic Stats page retirement, so Atomic sites open only Stats v2.
45 *
46 * At file scope for the same reason as above. Priority 5 leaves the last word to the a8c Feature Flags
47 * screen's per-site override at 10, which a `jetpack_feature_flag_enabled_{flag}` filter would beat.
48 *
49 * @param bool   $enabled Whether the flag is enabled.
50 * @param string $name    Feature flag name.
51 * @return bool
52 */
53function wpcomsh_retire_classic_stats( $enabled, $name ) {
54    if ( 'jetpack-stats-classic-page-retired' !== $name ) {
55        return $enabled;
56    }
57
58    // Jetpack updates apart from wpcomsh; before it lists Stats v2 in Calypso's menu, retiring would drop Stats from there.
59    // @phan-suppress-next-line PhanUndeclaredClassReference -- Premium Analytics ships with the Jetpack plugin, not wpcomsh.
60    return method_exists( 'Automattic\Jetpack\PremiumAnalytics\Analytics', 'register_replayed_admin_menu' ) ? true : $enabled;
61}
62add_filter( 'jetpack_feature_flag_enabled', 'wpcomsh_retire_classic_stats', 5, 2 );