Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.45% covered (danger)
45.45%
5 / 11
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_retire_classic_stats
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
wpcomsh_enable_woocommerce_analytics_proxy_tracking
100.00% covered (success)
100.00%
2 / 2
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 classic Stats page retirement, so Atomic sites open only Stats v2.
30 *
31 * At file scope because Jetpack resolves the flag on `plugins_loaded`. Priority 5 leaves the last word to the
32 * a8c Feature Flags screen's per-site override at 10, which a `jetpack_feature_flag_enabled_{flag}` filter would beat.
33 *
34 * @param bool   $enabled Whether the flag is enabled.
35 * @param string $name    Feature flag name.
36 * @return bool
37 */
38function wpcomsh_retire_classic_stats( $enabled, $name ) {
39    if ( 'jetpack-stats-classic-page-retired' !== $name ) {
40        return $enabled;
41    }
42
43    // Jetpack updates apart from wpcomsh; before it lists Stats v2 in Calypso's menu, retiring would drop Stats from there.
44    // @phan-suppress-next-line PhanUndeclaredClassReference -- Premium Analytics ships with the Jetpack plugin, not wpcomsh.
45    return method_exists( 'Automattic\Jetpack\PremiumAnalytics\Analytics', 'register_replayed_admin_menu' ) ? true : $enabled;
46}
47add_filter( 'jetpack_feature_flag_enabled', 'wpcomsh_retire_classic_stats', 5, 2 );
48
49/**
50 * Turns on WooCommerce Analytics proxy tracking while Stats v2 runs.
51 *
52 * Asked when the filter is read rather than at file scope, because Jetpack loads after wpcomsh.
53 *
54 * @param bool $enabled Whether proxy tracking is enabled.
55 * @return bool
56 */
57function wpcomsh_enable_woocommerce_analytics_proxy_tracking( $enabled ) {
58    $analytics = 'Automattic\Jetpack\PremiumAnalytics\Analytics';
59
60    return $enabled || ( is_callable( array( $analytics, 'is_initialized' ) ) && $analytics::is_initialized() );
61}
62add_filter( 'woocommerce_analytics_experimental_proxy_tracking_enabled', 'wpcomsh_enable_woocommerce_analytics_proxy_tracking' );