lib/experimental/dashboard-widgets
class-wp-rest-widget-modules-controller.php
7.6 KB
3 months ago
class-wp-widget-type-registry.php
5.1 KB
3 months ago
class-wp-widget-type.php
2.2 KB
3 months ago
dashboard-layout.php
5.2 KB
3 months ago
default-layout-seed.php
1.6 KB
3 months ago
load.php
821 B
3 months ago
widget-types.php
3.6 KB
3 months ago
widget-types.php in Gutenberg 23.2.0, at lib/experimental/dashboard-widgets/widget-types.php
| 1 | <?php |
| 2 | /** |
| 3 | * Widget Types: server-side registry and REST exposure. |
| 4 | * |
| 5 | * Hydrates `WP_Widget_Type_Registry` from the build manifest at `init`, |
| 6 | * and exposes the registry to the client through the |
| 7 | * `/wp/v2/widget-modules` REST endpoint. The JS layer reads the endpoint |
| 8 | * via core-data and dynamically imports each widget's render module on |
| 9 | * the consumer side. |
| 10 | * |
| 11 | * @package gutenberg |
| 12 | */ |
| 13 | |
| 14 | require_once __DIR__ . '/class-wp-widget-type.php'; |
| 15 | require_once __DIR__ . '/class-wp-widget-type-registry.php'; |
| 16 | require_once __DIR__ . '/class-wp-rest-widget-modules-controller.php'; |
| 17 | |
| 18 | /** |
| 19 | * Hydrates the widget type registry from the build manifest. |
| 20 | * |
| 21 | * Iterates the widgets discovered by the build pipeline (via |
| 22 | * `gutenberg_get_registered_widget_modules()`) and registers each one in |
| 23 | * `WP_Widget_Type_Registry`. The manifest is the single source of widget |
| 24 | * authorship in this codebase; this loop is a deterministic copy of it |
| 25 | * into the in-memory registry, with no filters in between. |
| 26 | */ |
| 27 | function gutenberg_register_widget_types() { |
| 28 | if ( ! function_exists( 'gutenberg_get_registered_widget_modules' ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | $registry = WP_Widget_Type_Registry::get_instance(); |
| 33 | |
| 34 | foreach ( gutenberg_get_registered_widget_modules() as $widget ) { |
| 35 | if ( empty( $widget['name'] ) || $registry->is_registered( $widget['name'] ) ) { |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | $registry->register( |
| 40 | $widget['name'], |
| 41 | array( |
| 42 | 'render_module' => $widget['render_module'] ?? null, |
| 43 | 'widget_module' => $widget['widget_module'] ?? null, |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if ( did_action( 'init' ) ) { |
| 50 | gutenberg_register_widget_types(); |
| 51 | } else { |
| 52 | add_action( 'init', 'gutenberg_register_widget_types' ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns all widget types registered in the widget type registry. |
| 57 | * |
| 58 | * Convenience accessor around `WP_Widget_Type_Registry::get_all_registered()` |
| 59 | * for callers that prefer a function-based API. |
| 60 | * |
| 61 | * @return WP_Widget_Type[] Associative array of `$name => $widget_type` |
| 62 | * pairs. |
| 63 | */ |
| 64 | function gutenberg_get_registered_widget_types() { |
| 65 | return WP_Widget_Type_Registry::get_instance()->get_all_registered(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Registers the REST controller that exposes the widget type registry. |
| 70 | */ |
| 71 | function gutenberg_register_widget_modules_rest_controller() { |
| 72 | $controller = new WP_REST_Widget_Modules_Controller(); |
| 73 | $controller->register_routes(); |
| 74 | } |
| 75 | add_action( 'rest_api_init', 'gutenberg_register_widget_modules_rest_controller' ); |
| 76 | |
| 77 | /** |
| 78 | * Adds the registered widget modules to the dashboard page's boot |
| 79 | * dependencies. |
| 80 | * |
| 81 | * The wp-build page templates expose a generic |
| 82 | * `{page-id}-wp-admin_boot_dependencies` filter. The dashboard surface |
| 83 | * hooks it to make every registered widget render and metadata module |
| 84 | * available in the page's import map for dynamic `import()` calls. |
| 85 | * |
| 86 | * Both the render module and the metadata module are added as |
| 87 | * 'dynamic' dependencies so they are reachable from the import map but |
| 88 | * not eagerly executed. |
| 89 | * |
| 90 | * @param array $boot_dependencies Boot dependencies for the page. |
| 91 | * @return array Updated boot dependencies. |
| 92 | */ |
| 93 | function gutenberg_add_widget_modules_to_dashboard_boot_deps( $boot_dependencies ) { |
| 94 | foreach ( gutenberg_get_registered_widget_types() as $widget_type ) { |
| 95 | if ( $widget_type->render_module ) { |
| 96 | $boot_dependencies[] = array( |
| 97 | 'import' => 'dynamic', |
| 98 | 'id' => $widget_type->render_module, |
| 99 | ); |
| 100 | } |
| 101 | if ( $widget_type->widget_module ) { |
| 102 | $boot_dependencies[] = array( |
| 103 | 'import' => 'dynamic', |
| 104 | 'id' => $widget_type->widget_module, |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $boot_dependencies; |
| 110 | } |
| 111 | add_filter( 'dashboard-wp-admin_boot_dependencies', 'gutenberg_add_widget_modules_to_dashboard_boot_deps' ); |
| 112 |