PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-stats-admin/src/class-main.php +139 -0 16.2-beta → 16.3-beta View file →
@@ -1,0 +1,139 @@
1 +<?php
2 +/**
3 + * Stats Main
4 + *
5 + * @package automattic/jetpack-stats-admin
6 + */
7 +
8 +namespace Automattic\Jetpack\Stats_Admin;
9 +
10 +use Automattic\Jetpack\Connection\Manager;
11 +use Automattic\Jetpack\Stats\Options as Stats_Options;
12 +use Automattic\Jetpack\Status\Host;
13 +use Automattic\Jetpack\Tracking;
14 +
15 +/**
16 + * Stats Main class.
17 + *
18 + * Entrypoint for Stats.
19 + *
20 + * @since 0.1.0
21 + */
22 +class Main {
23 + /**
24 + * Stats version.
25 + */
26 + const VERSION = '0.38.2';
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 +}