Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\PremiumAnalytics\register_widget_modules_rest_route
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\PremiumAnalytics\get_widget_modules_response
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
Automattic\Jetpack\PremiumAnalytics\add_widget_modules_to_boot_deps
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Dashboard widget modules: REST exposure + import-map wiring.
4 *
5 * Reads the widget types from Widget_Type_Registry (hydrated from the build
6 * manifest in widget-types.php) and exposes them to the client through the
7 * `/jetpack/v4/widget-modules` REST endpoint, plus adds each widget's render
8 * and metadata modules to the dashboard page's import map so the client can
9 * dynamically `import()` them on demand. The host feeds the REST records to
10 * `useWidgetTypes()` in @wordpress/widget-primitives.
11 *
12 * @package automattic/jetpack-premium-analytics
13 */
14
15namespace Automattic\Jetpack\PremiumAnalytics;
16
17/**
18 * Register the `/jetpack/v4/widget-modules` REST route.
19 *
20 * @return void
21 */
22function register_widget_modules_rest_route() {
23    register_rest_route(
24        'jetpack/v4',
25        '/widget-modules',
26        array(
27            'methods'             => \WP_REST_Server::READABLE,
28            'callback'            => __NAMESPACE__ . '\\get_widget_modules_response',
29            'permission_callback' => static function () {
30                return current_user_can( 'manage_options' );
31            },
32        )
33    );
34}
35add_action( 'rest_api_init', __NAMESPACE__ . '\\register_widget_modules_rest_route' );
36
37/**
38 * Build the REST response: one record per registered widget.
39 *
40 * @return \WP_REST_Response
41 */
42function get_widget_modules_response() {
43    $records = array();
44
45    foreach ( get_registered_widget_types() as $widget_type ) {
46        $records[] = array(
47            'name'          => $widget_type->name,
48            'render_module' => $widget_type->render_module,
49            'widget_module' => $widget_type->widget_module,
50            'presentation'  => $widget_type->presentation,
51        );
52    }
53
54    return rest_ensure_response( $records );
55}
56
57/**
58 * Add registered widget modules to the dashboard page import map as dynamic
59 * dependencies, so the client can `import()` them on demand.
60 *
61 * @param array $boot_dependencies Boot dependencies for the page.
62 * @return array Updated boot dependencies.
63 */
64function add_widget_modules_to_boot_deps( $boot_dependencies ) {
65    foreach ( get_registered_widget_types() as $widget_type ) {
66        if ( ! empty( $widget_type->render_module ) ) {
67            $boot_dependencies[] = array(
68                'import' => 'dynamic',
69                'id'     => $widget_type->render_module,
70            );
71        }
72        if ( ! empty( $widget_type->widget_module ) ) {
73            $boot_dependencies[] = array(
74                'import' => 'dynamic',
75                'id'     => $widget_type->widget_module,
76            );
77        }
78    }
79
80    return $boot_dependencies;
81}
82// The full-page interceptor (page.php) renders via the `{page-id}` filter; the
83// in-admin variant (page-wp-admin.php) uses the `{page-id}-wp-admin` filter. Hook
84// both so the widget modules land in the import map regardless of which renders.
85add_filter( 'jetpack-premium-analytics_boot_dependencies', __NAMESPACE__ . '\\add_widget_modules_to_boot_deps' );
86add_filter( 'jetpack-premium-analytics-wp-admin_boot_dependencies', __NAMESPACE__ . '\\add_widget_modules_to_boot_deps' );