PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-premium-analytics / src / widget-modules.php

widget-modules.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at jetpack_vendor/automattic/jetpack-premium-analytics/src/widget-modules.php

118 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dashboard widget modules: REST exposure + import-map wiring.
4 *
5 * Reads get_available_widget_types() and exposes it two ways: the `/wpcom/v2/widget-modules`
6 * REST list, and the page import map, for dynamic `import()`.
7 *
8 * @package automattic/jetpack-premium-analytics
9 */
10
11 namespace Automattic\Jetpack\PremiumAnalytics;
12
13 require_once __DIR__ . '/rest-namespace.php';
14
15 /**
16 * Register the `/wpcom/v2/widget-modules` REST route.
17 *
18 * @return void
19 */
20 function register_widget_modules_rest_route() {
21 register_rest_route(
22 DASHBOARD_REST_NAMESPACE,
23 '/widget-modules',
24 array(
25 'methods' => \WP_REST_Server::READABLE,
26 'callback' => __NAMESPACE__ . '\\get_widget_modules_response',
27 'permission_callback' => array( Capabilities::class, 'current_user_can_view_analytics' ),
28 )
29 );
30 }
31
32 /**
33 * Load and hydrate the widget type registry, once.
34 *
35 * Deferred to the registry's only two readers rather than run at boot: on Simple this file's
36 * registration runs on every WPCOM public-api request, and most never read the registry.
37 *
38 * @return void
39 */
40 function ensure_widget_registry_ready() {
41 static $ready = false;
42 if ( $ready ) {
43 return;
44 }
45 $ready = true;
46
47 require_once __DIR__ . '/widget-types.php';
48 require_once __DIR__ . '/widget-availability.php';
49
50 // REST does not load the admin build, so this require is the manifest's only route in
51 // (WOOA7S-1804). Drop it and every widget renders "Widget is no longer available" (#49961).
52 $widgets_manifest = Analytics::widget_manifest_path();
53 if ( file_exists( $widgets_manifest ) ) {
54 require_once $widgets_manifest;
55 }
56
57 bootstrap_widget_types();
58 }
59
60 /**
61 * Build the REST response: one record per available widget type.
62 *
63 * @return \WP_REST_Response
64 */
65 function get_widget_modules_response() {
66 ensure_widget_registry_ready();
67
68 $records = array();
69
70 foreach ( get_available_widget_types() as $widget_type ) {
71 $records[] = array(
72 'name' => $widget_type->name,
73 'render_module' => $widget_type->render_module,
74 'widget_module' => $widget_type->widget_module,
75 'presentation' => $widget_type->presentation,
76 'category' => $widget_type->category,
77 'title' => $widget_type->title,
78 'description' => $widget_type->description,
79 'help' => $widget_type->help,
80 'icon' => $widget_type->icon,
81 'actions' => $widget_type->actions,
82 'keywords' => $widget_type->keywords,
83 );
84 }
85
86 return rest_ensure_response( $records );
87 }
88
89 /**
90 * Add available widget modules to the page import map as dynamic dependencies,
91 * so the client can `import()` them on demand.
92 *
93 * @param array $boot_dependencies Boot dependencies for the page.
94 * @return array Updated boot dependencies.
95 */
96 function add_widget_modules_to_boot_deps( $boot_dependencies ) {
97 ensure_widget_registry_ready();
98
99 foreach ( get_available_widget_types() as $widget_type ) {
100 if ( ! empty( $widget_type->render_module ) ) {
101 $boot_dependencies[] = array(
102 'import' => 'dynamic',
103 'id' => $widget_type->render_module,
104 );
105 }
106
107 if ( ! empty( $widget_type->widget_module ) ) {
108 $boot_dependencies[] = array(
109 'import' => 'dynamic',
110 'id' => $widget_type->widget_module,
111 );
112 }
113 }
114
115 return $boot_dependencies;
116 }
117 add_filter( 'jetpack-premium-analytics-wp-admin_boot_dependencies', __NAMESPACE__ . '\\add_widget_modules_to_boot_deps' );
118