# jetpack/16.3-beta/jetpack_vendor/automattic/jetpack-premium-analytics/src/widget-modules.php

Jetpack – WP Security, Backup, Speed, &amp; Growth, version 16.3-beta. 120 lines.

- Page: https://pluginprobe.com/plugins/jetpack/16.3-beta/code/jetpack_vendor/automattic/jetpack-premium-analytics/src/widget-modules.php
- Raw: https://pluginprobe.com/plugins/jetpack/16.3-beta/raw/jetpack_vendor/automattic/jetpack-premium-analytics/src/widget-modules.php
- Modified: 2026-10-05T16:03:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/jetpack/16.3-beta/code/jetpack_vendor/automattic/jetpack-premium-analytics/src/widget-modules.php#L10-L20`.

```php
<?php
/**
 * Dashboard widget modules: REST exposure + import-map wiring.
 *
 * Reads get_available_widget_types() and exposes it two ways: the `/wpcom/v2/widget-modules`
 * REST list, and the page import map, for dynamic `import()`.
 *
 * @package automattic/jetpack-premium-analytics
 */

namespace Automattic\Jetpack\PremiumAnalytics;

require_once __DIR__ . '/rest-namespace.php';

/**
 * Register the `/wpcom/v2/widget-modules` REST route.
 *
 * @return void
 */
function register_widget_modules_rest_route() {
	register_rest_route(
		DASHBOARD_REST_NAMESPACE,
		'/widget-modules',
		array(
			'methods'             => \WP_REST_Server::READABLE,
			'callback'            => __NAMESPACE__ . '\\get_widget_modules_response',
			'permission_callback' => array( Capabilities::class, 'current_user_can_view_analytics' ),
		)
	);
}

/**
 * Load the widget type API and the build manifest, once, ahead of the registry's first read.
 *
 * Deferred to the registry's only two readers rather than run at boot: on Simple this file's
 * registration runs on every WPCOM public-api request, and most never read the registry. The
 * registry hydrates itself on that read, from the registration action.
 *
 * @return void
 */
function ensure_widget_registry_ready() {
	static $ready = false;
	if ( $ready ) {
		return;
	}
	$ready = true;

	require_once __DIR__ . '/widget-types.php';
	require_once __DIR__ . '/widget-availability.php';

	// REST does not load the admin build, so this require is the manifest's only route in
	// (WOOA7S-1804). Drop it and every widget renders "Widget is no longer available" (#49961).
	$widgets_manifest = Analytics::widget_manifest_path();
	if ( file_exists( $widgets_manifest ) ) {
		require_once $widgets_manifest;
	}
}

/**
 * Build the REST response: one record per available widget type.
 *
 * @return \WP_REST_Response
 */
function get_widget_modules_response() {
	ensure_widget_registry_ready();

	$records = array();

	foreach ( get_available_widget_types() as $widget_type ) {
		$records[] = array(
			'name'          => $widget_type->name,
			'render_module' => $widget_type->render_module,
			'widget_module' => $widget_type->widget_module,
			'presentation'  => $widget_type->presentation,
			'category'      => $widget_type->category,
			'title'         => $widget_type->title,
			'description'   => $widget_type->description,
			'help'          => $widget_type->help,
			'icon'          => $widget_type->icon,
			'actions'       => $widget_type->actions,
			'keywords'      => $widget_type->keywords,
			'textdomain'    => $widget_type->textdomain,
			'i18n_manifest' => $widget_type->i18n_manifest,
			'former_names'  => $widget_type->former_names,
		);
	}

	return rest_ensure_response( $records );
}

/**
 * Add available widget modules to the page import map as dynamic dependencies,
 * so the client can `import()` them on demand.
 *
 * @param array $boot_dependencies Boot dependencies for the page.
 * @return array Updated boot dependencies.
 */
function add_widget_modules_to_boot_deps( $boot_dependencies ) {
	ensure_widget_registry_ready();

	foreach ( get_available_widget_types() as $widget_type ) {
		if ( ! empty( $widget_type->render_module ) ) {
			$boot_dependencies[] = array(
				'import' => 'dynamic',
				'id'     => $widget_type->render_module,
			);
		}

		if ( ! empty( $widget_type->widget_module ) ) {
			$boot_dependencies[] = array(
				'import' => 'dynamic',
				'id'     => $widget_type->widget_module,
			);
		}
	}

	return $boot_dependencies;
}
add_filter( 'jetpack-premium-analytics-wp-admin_boot_dependencies', __NAMESPACE__ . '\\add_widget_modules_to_boot_deps' );

```
