build/pages/dashboard
loader.js
45 B
4 months ago
page-wp-admin.php
8.5 KB
3 months ago
page.php
9.5 KB
3 months ago
| 1 | <?php |
| 2 | /** |
| 3 | * Page: dashboard |
| 4 | * Auto-generated by build process. |
| 5 | * Do not edit this file manually. |
| 6 | * |
| 7 | * @package gutenberg |
| 8 | */ |
| 9 | |
| 10 | // Global storage for dashboard routes and menu items |
| 11 | global $gutenberg_dashboard_routes, $gutenberg_dashboard_menu_items; |
| 12 | $gutenberg_dashboard_routes = array(); |
| 13 | $gutenberg_dashboard_menu_items = array(); |
| 14 | |
| 15 | /** |
| 16 | * Register a route for the dashboard page. |
| 17 | * |
| 18 | * @param string $path Route path (e.g., '/types/$type/edit/$id'). |
| 19 | * @param string|null $content_module Script module ID for content (stage/inspector). |
| 20 | * @param string|null $route_module Script module ID for route lifecycle hooks. |
| 21 | */ |
| 22 | function gutenberg_register_dashboard_route( $path, $content_module = null, $route_module = null ) { |
| 23 | global $gutenberg_dashboard_routes; |
| 24 | |
| 25 | $route = array( 'path' => $path ); |
| 26 | if ( ! empty( $content_module ) ) { |
| 27 | $route['content_module'] = $content_module; |
| 28 | } |
| 29 | if ( ! empty( $route_module ) ) { |
| 30 | $route['route_module'] = $route_module; |
| 31 | } |
| 32 | |
| 33 | $gutenberg_dashboard_routes[] = $route; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Register a menu item for the dashboard page. |
| 38 | * |
| 39 | * @param string $id Menu item ID. |
| 40 | * @param string $label Display label. |
| 41 | * @param string $to Route path to navigate to. |
| 42 | * @param string $parent_id Optional. Parent menu item ID. |
| 43 | * @param string $parent_type Optional. Parent type: 'drilldown' or 'dropdown'. |
| 44 | */ |
| 45 | function gutenberg_register_dashboard_menu_item( $id, $label, $to, $parent_id = '', $parent_type = '' ) { |
| 46 | global $gutenberg_dashboard_menu_items; |
| 47 | |
| 48 | $menu_item = array( |
| 49 | 'id' => $id, |
| 50 | 'label' => $label, |
| 51 | 'to' => $to, |
| 52 | ); |
| 53 | |
| 54 | if ( ! empty( $parent_id ) ) { |
| 55 | $menu_item['parent'] = $parent_id; |
| 56 | } |
| 57 | |
| 58 | if ( ! empty( $parent_type ) && in_array( $parent_type, array( 'drilldown', 'dropdown' ), true ) ) { |
| 59 | $menu_item['parent_type'] = $parent_type; |
| 60 | } |
| 61 | |
| 62 | $gutenberg_dashboard_menu_items[] = $menu_item; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get all registered routes for the dashboard page. |
| 67 | * |
| 68 | * @return array Array of route objects. |
| 69 | */ |
| 70 | function gutenberg_get_dashboard_routes() { |
| 71 | global $gutenberg_dashboard_routes; |
| 72 | return $gutenberg_dashboard_routes ?? array(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get all registered menu items for the dashboard page. |
| 77 | * |
| 78 | * @return array Array of menu item objects. |
| 79 | */ |
| 80 | function gutenberg_get_dashboard_menu_items() { |
| 81 | global $gutenberg_dashboard_menu_items; |
| 82 | return $gutenberg_dashboard_menu_items ?? array(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Preload REST API data for the dashboard page. |
| 87 | * Automatically called during page rendering. |
| 88 | */ |
| 89 | function gutenberg_dashboard_preload_data() { |
| 90 | // Define paths to preload - same for all pages |
| 91 | // Please also change packages/core-data/src/entities.js when changing this. |
| 92 | $preload_paths = array( |
| 93 | '/?_fields=description,gmt_offset,home,image_sizes,image_size_threshold,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front', |
| 94 | array( '/wp/v2/settings', 'OPTIONS' ), |
| 95 | ); |
| 96 | |
| 97 | // Use rest_preload_api_request to gather the preloaded data |
| 98 | $preload_data = array_reduce( |
| 99 | $preload_paths, |
| 100 | 'rest_preload_api_request', |
| 101 | array() |
| 102 | ); |
| 103 | |
| 104 | // Register the preloading middleware with wp-api-fetch |
| 105 | wp_add_inline_script( |
| 106 | 'wp-api-fetch', |
| 107 | sprintf( |
| 108 | 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 109 | wp_json_encode( $preload_data ) |
| 110 | ), |
| 111 | 'after' |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Render the dashboard page. |
| 117 | * Call this function from add_menu_page or add_submenu_page. |
| 118 | */ |
| 119 | function gutenberg_dashboard_render_page() { |
| 120 | // Load build constants |
| 121 | $build_constants = require __DIR__ . '/../../constants.php'; |
| 122 | |
| 123 | // Set current screen |
| 124 | set_current_screen(); |
| 125 | |
| 126 | // Remove unwanted deprecated handler |
| 127 | remove_action( 'admin_head', 'wp_admin_bar_header' ); |
| 128 | |
| 129 | // Remove unwanted scripts and styles that were enqueued during `admin_init` |
| 130 | foreach ( wp_scripts()->queue as $script ) { |
| 131 | wp_dequeue_script( $script ); |
| 132 | } |
| 133 | foreach ( wp_styles()->queue as $style ) { |
| 134 | wp_dequeue_style( $style ); |
| 135 | } |
| 136 | |
| 137 | // Fire init action for extensions to register routes and menu items |
| 138 | do_action( 'dashboard_init' ); |
| 139 | |
| 140 | // Enqueue command palette assets for boot-based pages |
| 141 | if ( function_exists( 'wp_enqueue_command_palette_assets' ) ) { |
| 142 | wp_enqueue_command_palette_assets(); |
| 143 | } |
| 144 | |
| 145 | // Preload REST API data |
| 146 | gutenberg_dashboard_preload_data(); |
| 147 | |
| 148 | // Get all registered routes and menu items |
| 149 | $menu_items = gutenberg_get_dashboard_menu_items(); |
| 150 | $routes = gutenberg_get_dashboard_routes(); |
| 151 | |
| 152 | // Get boot module asset file for dependencies |
| 153 | $asset_file = __DIR__ . '/../../modules/boot/index.min.asset.php'; |
| 154 | if ( file_exists( $asset_file ) ) { |
| 155 | $asset = require $asset_file; |
| 156 | |
| 157 | // This script serves two purposes: |
| 158 | // 1. It ensures all the globals that are made available to the modules are loaded. |
| 159 | // 2. It initializes the boot module as an inline script. |
| 160 | wp_register_script( 'dashboard-prerequisites', '', $asset['dependencies'], $asset['version'], true ); |
| 161 | |
| 162 | // Add inline script to initialize the app |
| 163 | $init_modules = []; |
| 164 | wp_add_inline_script( |
| 165 | 'dashboard-prerequisites', |
| 166 | sprintf( |
| 167 | 'import("@wordpress/boot").then(mod => mod.init({mountId: "%s", menuItems: %s, routes: %s, initModules: %s, dashboardLink: "%s"}));', |
| 168 | 'dashboard-app', |
| 169 | wp_json_encode( $menu_items, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), |
| 170 | wp_json_encode( $routes, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), |
| 171 | wp_json_encode( $init_modules, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), |
| 172 | esc_url( admin_url( '/' ) ) |
| 173 | ) |
| 174 | ); |
| 175 | |
| 176 | // Register prerequisites style by filtering script dependencies to find registered styles |
| 177 | $style_dependencies = array_filter( |
| 178 | $asset['dependencies'], |
| 179 | function ( $handle ) { |
| 180 | return wp_style_is( $handle, 'registered' ); |
| 181 | } |
| 182 | ); |
| 183 | wp_register_style( 'dashboard-prerequisites', false, $style_dependencies, $asset['version'] ); |
| 184 | |
| 185 | // Build dependencies for dashboard module |
| 186 | $boot_dependencies = array( |
| 187 | array( |
| 188 | 'import' => 'static', |
| 189 | 'id' => '@wordpress/boot', |
| 190 | ), |
| 191 | ); |
| 192 | |
| 193 | // Add init modules as static dependencies |
| 194 | // No init modules configured |
| 195 | |
| 196 | // Add all registered routes as dependencies |
| 197 | foreach ( $routes as $route ) { |
| 198 | if ( isset( $route['route_module'] ) ) { |
| 199 | $boot_dependencies[] = array( |
| 200 | 'import' => 'static', |
| 201 | 'id' => $route['route_module'], |
| 202 | ); |
| 203 | } |
| 204 | if ( isset( $route['content_module'] ) ) { |
| 205 | $boot_dependencies[] = array( |
| 206 | 'import' => 'dynamic', |
| 207 | 'id' => $route['content_module'], |
| 208 | ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Filters the boot script-module dependencies for the |
| 214 | * dashboard page. |
| 215 | * |
| 216 | * Surfaces extending this page can append entries to the boot |
| 217 | * dependency list. Each entry is an array with 'import' (string |
| 218 | * 'static' or 'dynamic') and 'id' (script-module handle) keys. |
| 219 | * |
| 220 | * @param array $boot_dependencies Boot dependencies for the page. |
| 221 | */ |
| 222 | $boot_dependencies = apply_filters( |
| 223 | 'dashboard_boot_dependencies', |
| 224 | $boot_dependencies |
| 225 | ); |
| 226 | |
| 227 | // Dummy script module to ensure dependencies are loaded |
| 228 | wp_register_script_module( |
| 229 | 'dashboard', |
| 230 | $build_constants['build_url'] . 'pages/dashboard/loader.js', |
| 231 | $boot_dependencies |
| 232 | ); |
| 233 | |
| 234 | // Enqueue the boot scripts and styles |
| 235 | wp_enqueue_script( 'dashboard-prerequisites' ); |
| 236 | wp_enqueue_script_module( 'dashboard' ); |
| 237 | wp_enqueue_style( 'dashboard-prerequisites' ); |
| 238 | } |
| 239 | |
| 240 | // Output the HTML |
| 241 | ?> |
| 242 | <!DOCTYPE html> |
| 243 | <html <?php language_attributes(); ?>> |
| 244 | <head> |
| 245 | <meta charset="<?php bloginfo( 'charset' ); ?>"> |
| 246 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 247 | <title><?php echo esc_html( get_admin_page_title() ); ?></title> |
| 248 | <style> |
| 249 | html { |
| 250 | background: #f1f1f1; |
| 251 | color: #444; |
| 252 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; |
| 253 | font-size: 13px; |
| 254 | line-height: 1.4em; |
| 255 | } |
| 256 | body { |
| 257 | margin: 0; |
| 258 | } |
| 259 | #wpadminbar { display: none; } |
| 260 | </style> |
| 261 | <?php |
| 262 | global $hook_suffix; |
| 263 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 264 | $hook_suffix = 'dashboard'; |
| 265 | |
| 266 | // BEGIN see wp-admin/admin-header.php |
| 267 | print_admin_styles(); |
| 268 | print_head_scripts(); |
| 269 | |
| 270 | /** |
| 271 | * Fires in head section for a specific admin page. |
| 272 | * |
| 273 | * @since 2.1.0 |
| 274 | */ |
| 275 | do_action( "admin_head-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 276 | |
| 277 | /** |
| 278 | * Fires in head section for all admin pages. |
| 279 | * |
| 280 | * @since 2.1.0 |
| 281 | */ |
| 282 | do_action( 'admin_head' ); |
| 283 | // END see wp-admin/admin-header.php |
| 284 | ?> |
| 285 | </head> |
| 286 | <body class="dashboard"> |
| 287 | <div id="dashboard-app" style="height: 100vh; box-sizing: border-box;"></div> |
| 288 | <?php |
| 289 | // BEGIN see wp-admin/admin-footer.php |
| 290 | |
| 291 | /** |
| 292 | * Prints scripts or data before the default footer scripts. |
| 293 | * |
| 294 | * @since 1.2.0 |
| 295 | */ |
| 296 | do_action( 'admin_footer', '' ); |
| 297 | |
| 298 | // Print import map first so it's available for inline scripts |
| 299 | wp_script_modules()->print_import_map(); |
| 300 | print_footer_scripts(); |
| 301 | wp_script_modules()->print_enqueued_script_modules(); |
| 302 | wp_script_modules()->print_script_module_preloads(); |
| 303 | wp_script_modules()->print_script_module_data(); |
| 304 | |
| 305 | /** |
| 306 | * Prints scripts or data after the default footer scripts. |
| 307 | * |
| 308 | * @since 2.8.0 |
| 309 | */ |
| 310 | do_action( "admin_footer-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 311 | // END see wp-admin/admin-footer.php |
| 312 | ?> |
| 313 | </body> |
| 314 | </html> |
| 315 | <?php |
| 316 | exit; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Intercept admin_init to render the page early. |
| 321 | * This bypasses the default WordPress admin template. |
| 322 | */ |
| 323 | function gutenberg_dashboard_intercept_render() { |
| 324 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 325 | if ( isset( $_GET['page'] ) && 'dashboard' === $_GET['page'] ) { |
| 326 | gutenberg_dashboard_render_page(); |
| 327 | exit; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // Hook the interceptor to admin_init |
| 332 | add_action( 'admin_init', 'gutenberg_dashboard_intercept_render' ); |
| 333 |