Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
18 / 36
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Main
50.00% covered (danger)
50.00%
18 / 36
50.00% covered (danger)
50.00%
3 / 6
30.00
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%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 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.39.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        if ( self::is_site_connected() ) {
100            return false;
101        }
102
103        /**
104         * Filters whether Stats registers its own dashboard while the site is not connected.
105         *
106         * A host that offers its own Stats page before connection returns false, so the site
107         * does not show two Stats menus. Read when the package initializes, on `plugins_loaded`.
108         *
109         * @since $$next-version$$
110         *
111         * @param bool $register Whether to register the pre-connection dashboard. Default true.
112         */
113        return (bool) apply_filters( 'jetpack_stats_admin_pre_connection_dashboard', true );
114    }
115
116    /**
117     * Register the stats-admin transient prefix for cleanup.
118     *
119     * @param array $prefixes List of transient prefixes to clean up.
120     * @return array Modified list of prefixes.
121     */
122    public function register_transient_cleanup_prefix( $prefixes ) {
123        $prefixes[] = WPCOM_Client::CACHE_TRANSIENT_PREFIX;
124        return $prefixes;
125    }
126
127    /**
128     * Update New Stats status.
129     *
130     * @param bool $status true to enable or false to disable.
131     * @return bool
132     */
133    public static function update_new_stats_status( $status ) {
134        $status = (bool) $status;
135
136        $stats_options = array(
137            'enable_odyssey_stats'     => $status,
138            'odyssey_stats_changed_at' => time(),
139        );
140        $updated       = Stats_Options::set_options( $stats_options );
141
142        // Track the event.
143        $event_name = 'calypso_stats_disabled';
144        if ( $status ) {
145            $event_name = 'calypso_stats_enabled';
146        }
147        $connection_manager = new Manager( 'jetpack' );
148        $tracking           = new Tracking( 'jetpack', $connection_manager );
149        $tracking->record_user_event( $event_name, array_merge( $stats_options, array( 'updated' => $updated ) ) );
150
151        return $updated;
152    }
153}