Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Analytics | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| register_admin_menu | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| register_sidebar_items | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Analytics package main class. |
| 4 | * |
| 5 | * @package automattic/jetpack-premium-analytics |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 | |
| 10 | use Automattic\Jetpack\PremiumAnalytics\Sync\Sync_Status_Tracker; |
| 11 | |
| 12 | /** |
| 13 | * Main Analytics class. |
| 14 | * |
| 15 | * Loads the wp-build output and registers an admin page. |
| 16 | * The build interceptor handles full-page rendering via admin_init. |
| 17 | */ |
| 18 | class Analytics { |
| 19 | |
| 20 | const PACKAGE_VERSION = '0.1.0-alpha'; |
| 21 | |
| 22 | /** |
| 23 | * Whether the class has been initialized. |
| 24 | * |
| 25 | * @var bool |
| 26 | */ |
| 27 | private static $initialized = false; |
| 28 | |
| 29 | /** |
| 30 | * Menu title for the admin page. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private static $menu_title = 'Analytics'; |
| 35 | |
| 36 | /** |
| 37 | * Initialize the Analytics app. |
| 38 | * |
| 39 | * @param array $options Optional configuration options. |
| 40 | * Supported keys: |
| 41 | * - menu_title (string): Admin menu label. |
| 42 | * @return void |
| 43 | */ |
| 44 | public static function init( $options = array() ) { |
| 45 | if ( self::$initialized ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | self::$initialized = true; |
| 50 | |
| 51 | if ( ! empty( $options['menu_title'] ) ) { |
| 52 | self::$menu_title = $options['menu_title']; |
| 53 | } |
| 54 | |
| 55 | // Load wp-build output (interceptor, modules, routes, page render). |
| 56 | $build_entry = __DIR__ . '/../build/build.php'; |
| 57 | if ( file_exists( $build_entry ) ) { |
| 58 | require_once $build_entry; |
| 59 | } |
| 60 | |
| 61 | Sync_Status_Tracker::configure(); |
| 62 | |
| 63 | add_action( 'admin_menu', array( static::class, 'register_admin_menu' ) ); |
| 64 | add_action( 'jetpack-premium-analytics_init', array( static::class, 'register_sidebar_items' ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Register the admin menu page. |
| 69 | * |
| 70 | * The callback is __return_null because the wp-build interceptor |
| 71 | * renders the full-page app on admin_init and calls exit() before |
| 72 | * WordPress can invoke this callback. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public static function register_admin_menu() { |
| 77 | add_menu_page( |
| 78 | esc_html( self::$menu_title ), |
| 79 | esc_html( self::$menu_title ), |
| 80 | 'manage_options', |
| 81 | 'jetpack-premium-analytics', |
| 82 | '__return_null', |
| 83 | 'dashicons-chart-bar', |
| 84 | 30 |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Register sidebar menu items for the full-page app. |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | public static function register_sidebar_items() { |
| 94 | if ( ! function_exists( 'jpa_register_jetpack_premium_analytics_menu_item' ) ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // @phan-suppress-next-line PhanUndeclaredFunction -- Guarded by function_exists() above. |
| 99 | jpa_register_jetpack_premium_analytics_menu_item( |
| 100 | 'dashboard', |
| 101 | __( 'Dashboard', 'jetpack-premium-analytics' ), |
| 102 | '/' |
| 103 | ); |
| 104 | } |
| 105 | } |