Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
46.15% |
6 / 13 |
|
75.00% |
3 / 4 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_extend_stats_menu_item_access | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| wpcomsh_retire_classic_stats | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| wpcomsh_enable_woocommerce_stats | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| wpcomsh_enable_woocommerce_analytics_proxy_tracking | |
100.00% |
2 / 2 |
|
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 | */ |
| 19 | function 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 | } |
| 26 | add_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 | */ |
| 38 | function 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 | } |
| 47 | add_filter( 'jetpack_feature_flag_enabled', 'wpcomsh_retire_classic_stats', 5, 2 ); |
| 48 | |
| 49 | /** |
| 50 | * Turns on WooCommerce Stats, which Jetpack ships off, so Atomic stores keep the WooCommerce tab in Stats. |
| 51 | * |
| 52 | * Priority 5 for the same reason as the classic Stats retirement above. |
| 53 | * |
| 54 | * @param bool $enabled Whether the flag is enabled. |
| 55 | * @param string $name Feature flag name. |
| 56 | * @return bool |
| 57 | */ |
| 58 | function wpcomsh_enable_woocommerce_stats( $enabled, $name ) { |
| 59 | return 'jetpack-woocommerce-stats' === $name ? true : $enabled; |
| 60 | } |
| 61 | add_filter( 'jetpack_feature_flag_enabled', 'wpcomsh_enable_woocommerce_stats', 5, 2 ); |
| 62 | |
| 63 | /** |
| 64 | * Turns on WooCommerce Analytics proxy tracking while Stats v2 runs. |
| 65 | * |
| 66 | * Asked when the filter is read rather than at file scope, because Jetpack loads after wpcomsh. |
| 67 | * |
| 68 | * @param bool $enabled Whether proxy tracking is enabled. |
| 69 | * @return bool |
| 70 | */ |
| 71 | function wpcomsh_enable_woocommerce_analytics_proxy_tracking( $enabled ) { |
| 72 | $analytics = 'Automattic\Jetpack\PremiumAnalytics\Analytics'; |
| 73 | |
| 74 | return $enabled || ( is_callable( array( $analytics, 'is_initialized' ) ) && $analytics::is_initialized() ); |
| 75 | } |
| 76 | add_filter( 'woocommerce_analytics_experimental_proxy_tracking_enabled', 'wpcomsh_enable_woocommerce_analytics_proxy_tracking' ); |