Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
8 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Dashboard_Support_Routes
66.67% covered (warning)
66.67%
8 / 12
50.00% covered (danger)
50.00%
1 / 2
7.33
0.00% covered (danger)
0.00%
0 / 1
 register
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 boot_routes
63.64% covered (warning)
63.64%
7 / 11
0.00% covered (danger)
0.00%
0 / 1
6.20
1<?php
2/**
3 * Public entry point for hosts that serve the dashboard support routes
4 * without booting the rest of the package.
5 *
6 * @package automattic/jetpack-premium-analytics
7 */
8
9namespace Automattic\Jetpack\PremiumAnalytics;
10
11/**
12 * Registers the dashboard's REST support routes (widget modules, sections)
13 * for a host that never calls Analytics::init().
14 *
15 * WordPress.com Simple serves the dashboard from WPCOM, not the site (see
16 * Analytics::init_wpcom_simple()), so WPCOM's public-api process calls this
17 * directly — see AGENTS.md for the call site and what to update on the WPCOM
18 * side if this changes. Analytics::init() calls the same method for
19 * connected sites, so there's one implementation, not two.
20 */
21class Dashboard_Support_Routes {
22
23    /**
24     * Register the dashboard support routes.
25     *
26     * Idempotent: safe to call standalone, and safe to call more than once.
27     *
28     * @return void
29     */
30    public static function register() {
31        add_action( 'rest_api_init', array( __CLASS__, 'boot_routes' ) );
32    }
33
34    /**
35     * Load the route files and register their REST routes.
36     *
37     * Deferred here (rest_api_init), not run from register() itself: register()
38     * runs on every request that boots this package, and on WPCOM Simple that's
39     * every request across all of WPCOM's public-api process, most of which
40     * never dispatch a Premium Analytics route. Both registries hydrate lazily,
41     * on their first read: the section registry fires its registration action
42     * then, and widget-modules.php hydrates the widget type registry only when
43     * its REST callback or the boot-deps filter actually runs.
44     *
45     * @return void
46     */
47    public static function boot_routes() {
48        // Load-bearing, not defensive duplication: since WOOA7S-1804 these files load
49        // only in wp-admin, so on REST this method is their only loader.
50        //
51        // Guarded on a symbol from each file: two copies of this package can be loaded
52        // in one request, and these file-scope functions are outside the autoloader's
53        // dedupe. See Analytics::load_dashboard_components() for the full rationale.
54        if ( ! function_exists( __NAMESPACE__ . '\\register_widget_modules_rest_route' ) ) {
55            require_once __DIR__ . '/widget-modules.php';
56        }
57        if ( ! function_exists( __NAMESPACE__ . '\\get_dashboard_default_widget_instance' ) ) {
58            require_once __DIR__ . '/dashboard-layout.php';
59        }
60        if ( ! function_exists( __NAMESPACE__ . '\\register_dashboard_section' ) ) {
61            require_once __DIR__ . '/dashboard-sections.php';
62        }
63        if ( ! function_exists( __NAMESPACE__ . '\\register_default_dashboard_sections' ) ) {
64            require_once __DIR__ . '/default-dashboard-sections.php';
65        }
66
67        // These routes are gated on the dashboard capability, and WPCOM Simple boots
68        // this class standalone, without going through Analytics::init().
69        Capabilities::register();
70
71        register_widget_modules_rest_route();
72        register_dashboard_sections_rest_routes();
73    }
74}