Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
8 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Dashboard_Support_Routes
72.73% covered (warning)
72.73%
8 / 11
50.00% covered (danger)
50.00%
1 / 2
5.51
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
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
4.43
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, default
13 * layout, sections) 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. dashboard-sections.php hydrates
41     * its own section registry at file scope, which should not run before
42     * WordPress has decided this request is actually dispatching a REST route.
43     * widget-modules.php hydrates the widget type registry itself, lazily,
44     * only when its REST callback or the boot-deps filter actually runs.
45     *
46     * @return void
47     */
48    public static function boot_routes() {
49        // Load-bearing, not defensive duplication: since WOOA7S-1804 these files load
50        // only in wp-admin, so on REST this method is their only loader.
51        //
52        // Guarded on a symbol from each file: two copies of this package can be loaded
53        // in one request, and these file-scope functions are outside the autoloader's
54        // dedupe. See Analytics::load_dashboard_components() for the full rationale.
55        if ( ! function_exists( __NAMESPACE__ . '\\register_widget_modules_rest_route' ) ) {
56            require_once __DIR__ . '/widget-modules.php';
57        }
58        if ( ! function_exists( __NAMESPACE__ . '\\register_dashboard_default_layout_route' ) ) {
59            require_once __DIR__ . '/dashboard-layout.php';
60        }
61        if ( ! function_exists( __NAMESPACE__ . '\\register_dashboard_section' ) ) {
62            require_once __DIR__ . '/dashboard-sections.php';
63        }
64
65        // These routes are gated on the dashboard capability, and WPCOM Simple boots
66        // this class standalone, without going through Analytics::init().
67        Capabilities::register();
68
69        register_widget_modules_rest_route();
70        register_dashboard_default_layout_route();
71        register_dashboard_sections_rest_routes();
72    }
73}