REST
2 weeks ago
Reports
1 week ago
Sync
2 weeks ago
class-analytics.php
2 days ago
class-capabilities.php
2 weeks ago
class-connection-configuration.php
2 weeks ago
class-dashboard-section-registry.php
2 weeks ago
class-dashboard-section.php
2 days ago
class-dashboard-support-routes.php
1 week ago
class-jetpack-stats-tracker.php
2 weeks ago
class-notices.php
2 weeks ago
class-widget-type-registry.php
2 weeks ago
class-widget-type.php
2 weeks ago
class-woocommerce-analytics-tracker.php
2 weeks ago
csv-exports.php
2 weeks ago
dashboard-grammar.php
2 weeks ago
dashboard-layout.php
2 days ago
dashboard-sections.php
2 days ago
rest-namespace.php
2 weeks ago
videopress-availability.php
2 days ago
widget-availability.php
2 days ago
widget-i18n.json
2 weeks ago
widget-modules.php
2 days ago
widget-type-support.php
2 days ago
widget-types.php
2 weeks ago
widget-modules.php
123 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dashboard widget modules: REST exposure + import-map wiring. |
| 4 | * |
| 5 | * Reads get_available_widget_types() (the registry filtered by |
| 6 | * widget-availability.php) and exposes it two ways: the |
| 7 | * `/wpcom/v2/widget-modules` REST list, and the page import map, where each |
| 8 | * widget's render and metadata modules are registered for dynamic `import()`. |
| 9 | * |
| 10 | * @package automattic/jetpack-premium-analytics |
| 11 | */ |
| 12 | |
| 13 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 14 | |
| 15 | require_once __DIR__ . '/rest-namespace.php'; |
| 16 | |
| 17 | /** |
| 18 | * Register the `/wpcom/v2/widget-modules` REST route. |
| 19 | * |
| 20 | * @return void |
| 21 | */ |
| 22 | function register_widget_modules_rest_route() { |
| 23 | register_rest_route( |
| 24 | DASHBOARD_REST_NAMESPACE, |
| 25 | '/widget-modules', |
| 26 | array( |
| 27 | 'methods' => \WP_REST_Server::READABLE, |
| 28 | 'callback' => __NAMESPACE__ . '\\get_widget_modules_response', |
| 29 | 'permission_callback' => array( Capabilities::class, 'current_user_can_view_analytics' ), |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Load and hydrate the widget type registry, once. |
| 36 | * |
| 37 | * Deferred to here (the registry's only two readers) instead of running |
| 38 | * unconditionally at boot: this package boots on every request to a site |
| 39 | * that has it active, and on WPCOM Simple this file's registration runs on |
| 40 | * every public-api request across all of WPCOM, not just ones that touch |
| 41 | * Premium Analytics. Most such requests never read the registry at all, so |
| 42 | * parsing the widget manifest and hydrating it eagerly was wasted work. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | function ensure_widget_registry_ready() { |
| 47 | static $ready = false; |
| 48 | if ( $ready ) { |
| 49 | return; |
| 50 | } |
| 51 | $ready = true; |
| 52 | |
| 53 | require_once __DIR__ . '/widget-types.php'; |
| 54 | require_once __DIR__ . '/widget-availability.php'; |
| 55 | |
| 56 | // REST does not load the admin build, so this require is the manifest's only route |
| 57 | // in (WOOA7S-1804). Drop it and every widget renders "Widget is no longer |
| 58 | // available" — the #49961 bug. |
| 59 | $widgets_manifest = Analytics::widget_manifest_path(); |
| 60 | if ( file_exists( $widgets_manifest ) ) { |
| 61 | require_once $widgets_manifest; |
| 62 | } |
| 63 | |
| 64 | bootstrap_widget_types(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Build the REST response: one record per available widget type. |
| 69 | * |
| 70 | * @return \WP_REST_Response |
| 71 | */ |
| 72 | function get_widget_modules_response() { |
| 73 | ensure_widget_registry_ready(); |
| 74 | |
| 75 | $records = array(); |
| 76 | |
| 77 | foreach ( get_available_widget_types() as $widget_type ) { |
| 78 | $records[] = array( |
| 79 | 'name' => $widget_type->name, |
| 80 | 'render_module' => $widget_type->render_module, |
| 81 | 'widget_module' => $widget_type->widget_module, |
| 82 | 'presentation' => $widget_type->presentation, |
| 83 | 'category' => $widget_type->category, |
| 84 | 'title' => $widget_type->title, |
| 85 | 'description' => $widget_type->description, |
| 86 | 'help' => $widget_type->help, |
| 87 | 'keywords' => $widget_type->keywords, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return rest_ensure_response( $records ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Add available widget modules to the page import map as dynamic dependencies, |
| 96 | * so the client can `import()` them on demand. |
| 97 | * |
| 98 | * @param array $boot_dependencies Boot dependencies for the page. |
| 99 | * @return array Updated boot dependencies. |
| 100 | */ |
| 101 | function add_widget_modules_to_boot_deps( $boot_dependencies ) { |
| 102 | ensure_widget_registry_ready(); |
| 103 | |
| 104 | foreach ( get_available_widget_types() as $widget_type ) { |
| 105 | if ( ! empty( $widget_type->render_module ) ) { |
| 106 | $boot_dependencies[] = array( |
| 107 | 'import' => 'dynamic', |
| 108 | 'id' => $widget_type->render_module, |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | if ( ! empty( $widget_type->widget_module ) ) { |
| 113 | $boot_dependencies[] = array( |
| 114 | 'import' => 'dynamic', |
| 115 | 'id' => $widget_type->widget_module, |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return $boot_dependencies; |
| 121 | } |
| 122 | add_filter( 'jetpack-premium-analytics-wp-admin_boot_dependencies', __NAMESPACE__ . '\\add_widget_modules_to_boot_deps' ); |
| 123 |