PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
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-premium-analytics/src/class-jetpack-stats-tracker.php +97 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,97 @@
1 +<?php
2 +/**
3 + * Jetpack Stats front-end tracking configuration.
4 + *
5 + * @package automattic/jetpack-premium-analytics
6 + */
7 +
8 +namespace Automattic\Jetpack\PremiumAnalytics;
9 +
10 +use Automattic\Jetpack\Config;
11 +use Automattic\Jetpack\Connection\Manager as Connection_Manager;
12 +use Automattic\Jetpack\Modules;
13 +use Automattic\Jetpack\Stats\Main as Stats_Main;
14 +
15 +/**
16 + * Configures the existing Jetpack Stats front-end tracking pipeline.
17 + */
18 +class Jetpack_Stats_Tracker {
19 +
20 + /**
21 + * Whether the tracker has been configured.
22 + *
23 + * @var bool
24 + */
25 + private static $configured = false;
26 +
27 + /**
28 + * Initialize Stats tracking and its standalone module lifecycle.
29 + *
30 + * @return void
31 + */
32 + public static function configure() {
33 + if ( self::$configured ) {
34 + return;
35 + }
36 +
37 + self::$configured = true;
38 +
39 + add_filter( 'jetpack_get_available_standalone_modules', array( static::class, 'add_stats_module' ) );
40 + add_action( 'jetpack_site_registered', array( static::class, 'activate_stats_module' ) );
41 +
42 + if ( did_action( 'plugins_loaded' ) ) {
43 + Stats_Main::init();
44 + self::activate_stats_module_if_connected();
45 + } else {
46 + $config = new Config();
47 + $config->ensure( 'stats' );
48 + add_action( 'plugins_loaded', array( static::class, 'activate_stats_module_if_connected' ) );
49 + }
50 + }
51 +
52 + /**
53 + * Make Stats available to the module controller when Jetpack is not installed.
54 + *
55 + * @param array $modules Available standalone module slugs.
56 + * @return array
57 + */
58 + public static function add_stats_module( $modules ) {
59 + $modules[] = 'stats';
60 +
61 + return array_values( array_unique( $modules ) );
62 + }
63 +
64 + /**
65 + * Activate Stats for a connected standalone Premium Analytics site.
66 + *
67 + * @return void
68 + */
69 + public static function activate_stats_module_if_connected() {
70 + if ( ( new Connection_Manager() )->is_connected() ) {
71 + self::activate_stats_module();
72 + }
73 + }
74 +
75 + /**
76 + * Activate Stats in standalone mode without redirecting or ending the request.
77 + *
78 + * @return void
79 + */
80 + public static function activate_stats_module() {
81 + // When Jetpack is active, respect its Stats module setting.
82 + if ( static::is_jetpack_plugin_active() ) {
83 + return;
84 + }
85 +
86 + ( new Modules() )->activate( 'stats', false, false );
87 + }
88 +
89 + /**
90 + * Whether the Jetpack plugin is active and owns the Stats module setting.
91 + *
92 + * @return bool
93 + */
94 + protected static function is_jetpack_plugin_active() {
95 + return class_exists( 'Jetpack' );
96 + }
97 +}