Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Dashboard_Support_Routes
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 boot_routes
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
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        require_once __DIR__ . '/widget-modules.php';
50        require_once __DIR__ . '/dashboard-layout.php';
51        require_once __DIR__ . '/dashboard-sections.php';
52
53        register_widget_modules_rest_route();
54        register_dashboard_default_layout_route();
55        register_dashboard_sections_rest_routes();
56    }
57}