Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.46% covered (danger)
38.46%
5 / 13
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AI_Traffic
38.46% covered (danger)
38.46%
5 / 13
66.67% covered (warning)
66.67%
2 / 3
10.83
0.00% covered (danger)
0.00%
0 / 1
 register_feature_flag
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 site_has_edge_data
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_available
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Whether a site gets the AI and bot traffic surfaces of Stats.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10use Automattic\Jetpack\Feature_Flags\Feature_Flags;
11use Automattic\Jetpack\Status\Host;
12
13/**
14 * Owns the AI tab's flag and the one check every AI and bot traffic surface reads.
15 *
16 * @since 0.19.0
17 */
18final class AI_Traffic {
19
20    const FLAG = 'jetpack-stats-ai-tab';
21
22    /**
23     * Filter through which the availability of the AI and bot traffic surfaces is resolved.
24     */
25    const AVAILABLE_FILTER = 'jetpack_premium_analytics_ai_traffic_available';
26
27    /**
28     * Register the flag, so feature-flag tooling lists it.
29     *
30     * @return void
31     */
32    public static function register_feature_flag() {
33        Feature_Flags::register(
34            self::FLAG,
35            array(
36                'default'     => false,
37                'description' => 'Show the AI tab in Stats, with the AI and bot traffic widgets, on sites that have edge request data.',
38                'owner'       => 'jetpack-stats',
39            )
40        );
41    }
42
43    /**
44     * Whether the site has edge request data, which only VIP sites do so far.
45     *
46     * @return bool
47     */
48    private static function site_has_edge_data() {
49        return ( new Host() )->is_vip_site();
50    }
51
52    /**
53     * Whether the reader gets the AI and bot traffic surfaces.
54     *
55     * @return bool
56     */
57    public static function is_available() {
58        // Outside the filter, which answers only whether the site has the data.
59        if ( ! Capabilities::current_user_can_view_stats() || ! Feature_Flags::is_enabled( self::FLAG ) ) {
60            return false;
61        }
62
63        $is_available = self::site_has_edge_data();
64
65        /**
66         * Filters whether the AI and bot traffic surfaces of Stats are available.
67         *
68         * @since 0.19.0
69         *
70         * @param bool $is_available Whether the site has edge request data.
71         */
72        return (bool) apply_filters( self::AVAILABLE_FILTER, $is_available );
73    }
74}