Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Main | |
0.00% |
0 / 30 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| __construct | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| register_transient_cleanup_prefix | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| update_new_stats_status | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| 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\Tracking; |
| 13 | |
| 14 | /** |
| 15 | * Stats Main class. |
| 16 | * |
| 17 | * Entrypoint for Stats. |
| 18 | * |
| 19 | * @since 0.1.0 |
| 20 | */ |
| 21 | class Main { |
| 22 | /** |
| 23 | * Stats version. |
| 24 | */ |
| 25 | const VERSION = '0.31.1'; |
| 26 | |
| 27 | /** |
| 28 | * Singleton Main instance. |
| 29 | * |
| 30 | * @var Main |
| 31 | **/ |
| 32 | private static $instance = null; |
| 33 | |
| 34 | /** |
| 35 | * Initializer. |
| 36 | * Used to configure the stats package, eg when called via the Config package. |
| 37 | * |
| 38 | * @return object |
| 39 | */ |
| 40 | public static function init() { |
| 41 | if ( null === self::$instance ) { |
| 42 | self::$instance = new Main(); |
| 43 | } |
| 44 | |
| 45 | return self::$instance; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Class constructor. |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | private function __construct() { |
| 54 | add_action( 'rest_api_init', array( new REST_Controller(), 'register_rest_routes' ) ); |
| 55 | // Disable JITM assets on the Stats page. |
| 56 | // JITM is handled separately by Stats: https://github.com/Automattic/wp-calypso/pull/95273. |
| 57 | add_filter( |
| 58 | 'jetpack_display_jitms_on_screen', |
| 59 | function ( $show, $screen_id ) { |
| 60 | if ( 'jetpack_page_stats' === $screen_id ) { |
| 61 | return false; |
| 62 | } |
| 63 | return $show; |
| 64 | }, |
| 65 | 10, |
| 66 | 2 |
| 67 | ); |
| 68 | |
| 69 | // Register stats-admin transient prefix for cleanup by the stats package. |
| 70 | add_filter( 'jetpack_stats_transient_cleanup_prefixes', array( $this, 'register_transient_cleanup_prefix' ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Register the stats-admin transient prefix for cleanup. |
| 75 | * |
| 76 | * @param array $prefixes List of transient prefixes to clean up. |
| 77 | * @return array Modified list of prefixes. |
| 78 | */ |
| 79 | public function register_transient_cleanup_prefix( $prefixes ) { |
| 80 | $prefixes[] = WPCOM_Client::CACHE_TRANSIENT_PREFIX; |
| 81 | return $prefixes; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Update New Stats status. |
| 86 | * |
| 87 | * @param bool $status true to enable or false to disable. |
| 88 | * @return bool |
| 89 | */ |
| 90 | public static function update_new_stats_status( $status ) { |
| 91 | $status = (bool) $status; |
| 92 | |
| 93 | $stats_options = array( |
| 94 | 'enable_odyssey_stats' => $status, |
| 95 | 'odyssey_stats_changed_at' => time(), |
| 96 | ); |
| 97 | $updated = Stats_Options::set_options( $stats_options ); |
| 98 | |
| 99 | // Track the event. |
| 100 | $event_name = 'calypso_stats_disabled'; |
| 101 | if ( $status ) { |
| 102 | $event_name = 'calypso_stats_enabled'; |
| 103 | } |
| 104 | $connection_manager = new Manager( 'jetpack' ); |
| 105 | $tracking = new Tracking( 'jetpack', $connection_manager ); |
| 106 | $tracking->record_user_event( $event_name, array_merge( $stats_options, array( 'updated' => $updated ) ) ); |
| 107 | |
| 108 | return $updated; |
| 109 | } |
| 110 | } |