Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.06% covered (danger)
47.06%
16 / 34
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Main
47.06% covered (danger)
47.06%
16 / 34
50.00% covered (danger)
50.00%
3 / 6
28.95
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __construct
78.57% covered (warning)
78.57%
11 / 14
0.00% covered (danger)
0.00%
0 / 1
3.09
 is_site_connected
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 needs_own_dashboard
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register_transient_cleanup_prefix
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 update_new_stats_status
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Stats Main
4 *
5 * @package automattic/jetpack-stats-admin
6 */
7
8namespace Automattic\Jetpack\Stats_Admin;
9
10use Automattic\Jetpack\Connection\Manager;
11use Automattic\Jetpack\Stats\Options as Stats_Options;
12use Automattic\Jetpack\Status\Host;
13use Automattic\Jetpack\Tracking;
14
15/**
16 * Stats Main class.
17 *
18 * Entrypoint for Stats.
19 *
20 * @since 0.1.0
21 */
22class Main {
23    /**
24     * Stats version.
25     */
26    const VERSION = '0.35.1';
27
28    /**
29     * Singleton Main instance.
30     *
31     * @var Main
32     **/
33    private static $instance = null;
34
35    /**
36     * Initializer.
37     * Used to configure the stats package, eg when called via the Config package.
38     *
39     * @return object
40     */
41    public static function init() {
42        if ( null === self::$instance ) {
43            self::$instance = new Main();
44        }
45
46        return self::$instance;
47    }
48
49    /**
50     * Class constructor.
51     *
52     * @return void
53     */
54    private function __construct() {
55        add_action( 'rest_api_init', array( REST_Controller::class, 'register' ) );
56        // Disable JITM assets on the Stats page.
57        // JITM is handled separately by Stats: https://github.com/Automattic/wp-calypso/pull/95273.
58        add_filter(
59            'jetpack_display_jitms_on_screen',
60            function ( $show, $screen_id ) {
61                if ( 'jetpack_page_stats' === $screen_id ) {
62                    return false;
63                }
64                return $show;
65            },
66            10,
67            2
68        );
69
70        // Register stats-admin transient prefix for cleanup by the stats package.
71        add_filter( 'jetpack_stats_transient_cleanup_prefixes', array( $this, 'register_transient_cleanup_prefix' ) );
72
73        if ( self::needs_own_dashboard() ) {
74            Dashboard::init();
75        }
76    }
77
78    /**
79     * Whether the site can talk to WordPress.com.
80     *
81     * Simple sites always can, and hold no Jetpack blog token to check.
82     *
83     * @return bool
84     */
85    public static function is_site_connected() {
86        return ( new Host() )->is_wpcom_simple() || ( new Manager( 'jetpack' ) )->is_connected();
87    }
88
89    /**
90     * Whether Stats has to register its own dashboard because nothing else will.
91     *
92     * The Jetpack plugin registers the Stats menu from its stats module, and modules are not
93     * loaded until the site is connected. Registering the dashboard here keeps Stats reachable
94     * before that happens, where the app offers a plan and drives the connection itself.
95     *
96     * @return bool
97     */
98    private static function needs_own_dashboard() {
99        return ! self::is_site_connected();
100    }
101
102    /**
103     * Register the stats-admin transient prefix for cleanup.
104     *
105     * @param array $prefixes List of transient prefixes to clean up.
106     * @return array Modified list of prefixes.
107     */
108    public function register_transient_cleanup_prefix( $prefixes ) {
109        $prefixes[] = WPCOM_Client::CACHE_TRANSIENT_PREFIX;
110        return $prefixes;
111    }
112
113    /**
114     * Update New Stats status.
115     *
116     * @param bool $status true to enable or false to disable.
117     * @return bool
118     */
119    public static function update_new_stats_status( $status ) {
120        $status = (bool) $status;
121
122        $stats_options = array(
123            'enable_odyssey_stats'     => $status,
124            'odyssey_stats_changed_at' => time(),
125        );
126        $updated       = Stats_Options::set_options( $stats_options );
127
128        // Track the event.
129        $event_name = 'calypso_stats_disabled';
130        if ( $status ) {
131            $event_name = 'calypso_stats_enabled';
132        }
133        $connection_manager = new Manager( 'jetpack' );
134        $tracking           = new Tracking( 'jetpack', $connection_manager );
135        $tracking->record_user_event( $event_name, array_merge( $stats_options, array( 'updated' => $updated ) ) );
136
137        return $updated;
138    }
139}