| 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 |
// This must exactly match the _fields list in packages/core-data/src/entities.js, |
| 92 |
// same fields in the same order, or the preload is never consumed. |
| 93 |
$preload_paths = array( |
| 94 |
'/?_fields=description,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front', |
| 95 |
array( '/wp/v2/settings', 'OPTIONS' ), |
| 96 |
); |
| 97 |
|
| 98 |
// Use rest_preload_api_request to gather the preloaded data |
| 99 |
$preload_data = array_reduce( |
| 100 |
$preload_paths, |
| 101 |
'rest_preload_api_request', |
| 102 |
array() |
| 103 |
); |
| 104 |
|
| 105 |
// Register the preloading middleware with wp-api-fetch |
| 106 |
wp_add_inline_script( |
| 107 |
'wp-api-fetch', |
| 108 |
sprintf( |
| 109 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 110 |
wp_json_encode( $preload_data ) |
| 111 |
), |
| 112 |
'after' |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Render the dashboard page. |
| 118 |
* Call this function from add_menu_page or add_submenu_page. |
| 119 |
*/ |
| 120 |
function gutenberg_dashboard_render_page() { |
| 121 |
// Load build constants |
| 122 |
$build_constants = require __DIR__ . '/../../constants.php'; |
| 123 |
|
| 124 |
// Set current screen |
| 125 |
set_current_screen(); |
| 126 |
|
| 127 |
// Remove unwanted deprecated handler |
| 128 |
remove_action( 'admin_head', 'wp_admin_bar_header' ); |
| 129 |
|
| 130 |
// Remove unwanted scripts and styles that were enqueued during `admin_init` |
| 131 |
foreach ( wp_scripts()->queue as $script ) { |
| 132 |
wp_dequeue_script( $script ); |
| 133 |
} |
| 134 |
foreach ( wp_styles()->queue as $style ) { |
| 135 |
wp_dequeue_style( $style ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Fires when the dashboard page is initialized so extensions can register routes and menu items. |
| 140 |
*/ |
| 141 |
do_action( 'dashboard_init' ); |
| 142 |
|
| 143 |
// Enqueue command palette assets for boot-based pages |
| 144 |
if ( function_exists( 'wp_enqueue_command_palette_assets' ) ) { |
| 145 |
wp_enqueue_command_palette_assets(); |
| 146 |
} |
| 147 |
|
| 148 |
// Preload REST API data |
| 149 |
gutenberg_dashboard_preload_data(); |
| 150 |
|
| 151 |
// Get all registered routes and menu items |
| 152 |
$menu_items = gutenberg_get_dashboard_menu_items(); |
| 153 |
$routes = gutenberg_get_dashboard_routes(); |
| 154 |
|
| 155 |
// Get boot module asset file for dependencies |
| 156 |
$asset_file = __DIR__ . '/../../modules/boot/index.min.asset.php'; |
| 157 |
if ( file_exists( $asset_file ) ) { |
| 158 |
$asset = require $asset_file; |
| 159 |
|
| 160 |
// This script serves two purposes: |
| 161 |
// 1. It ensures all the globals that are made available to the modules are loaded. |
| 162 |
// 2. It initializes the boot module as an inline script. |
| 163 |
wp_register_script( 'dashboard-prerequisites', '', $asset['dependencies'], $asset['version'], true ); |
| 164 |
|
| 165 |
// Add inline script to initialize the app |
| 166 |
$init_modules = ["@wordpress/dashboard-init"]; |
| 167 |
wp_add_inline_script( |
| 168 |
'dashboard-prerequisites', |
| 169 |
sprintf( |
| 170 |
'import("@wordpress/boot").then(mod => mod.init({mountId: "%s", menuItems: %s, routes: %s, initModules: %s, dashboardLink: "%s"}));', |
| 171 |
'dashboard-app', |
| 172 |
wp_json_encode( $menu_items, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), |
| 173 |
wp_json_encode( $routes, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), |
| 174 |
wp_json_encode( $init_modules, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), |
| 175 |
esc_url( admin_url( '/' ) ) |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
// Register prerequisites style by filtering script dependencies to find registered styles |
| 180 |
$style_dependencies = array_filter( |
| 181 |
$asset['dependencies'], |
| 182 |
function ( $handle ) { |
| 183 |
return wp_style_is( $handle, 'registered' ); |
| 184 |
} |
| 185 |
); |
| 186 |
wp_register_style( 'dashboard-prerequisites', false, $style_dependencies, $asset['version'] ); |
| 187 |
|
| 188 |
// Build dependencies for dashboard module |
| 189 |
$boot_dependencies = array( |
| 190 |
array( |
| 191 |
'import' => 'static', |
| 192 |
'id' => '@wordpress/boot', |
| 193 |
), |
| 194 |
); |
| 195 |
|
| 196 |
// Add init modules as static dependencies |
| 197 |
$boot_dependencies[] = array( 'import' => 'static', 'id' => '@wordpress/dashboard-init' ); |
| 198 |
|
| 199 |
// Add all registered routes as dependencies |
| 200 |
foreach ( $routes as $route ) { |
| 201 |
if ( isset( $route['route_module'] ) ) { |
| 202 |
$boot_dependencies[] = array( |
| 203 |
'import' => 'static', |
| 204 |
'id' => $route['route_module'], |
| 205 |
); |
| 206 |
} |
| 207 |
if ( isset( $route['content_module'] ) ) { |
| 208 |
$boot_dependencies[] = array( |
| 209 |
'import' => 'dynamic', |
| 210 |
'id' => $route['content_module'], |
| 211 |
); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Filters the boot script-module dependencies for the |
| 217 |
* dashboard page. |
| 218 |
* |
| 219 |
* Surfaces extending this page can append entries to the boot |
| 220 |
* dependency list. Each entry is an array with 'import' (string |
| 221 |
* 'static' or 'dynamic') and 'id' (script-module handle) keys. |
| 222 |
* |
| 223 |
* @param array $boot_dependencies Boot dependencies for the page. |
| 224 |
*/ |
| 225 |
$boot_dependencies = apply_filters( |
| 226 |
'dashboard_boot_dependencies', |
| 227 |
$boot_dependencies |
| 228 |
); |
| 229 |
|
| 230 |
// Dummy script module to ensure dependencies are loaded |
| 231 |
wp_register_script_module( |
| 232 |
'dashboard', |
| 233 |
$build_constants['build_url'] . 'pages/dashboard/loader.js', |
| 234 |
$boot_dependencies |
| 235 |
); |
| 236 |
|
| 237 |
// Enqueue the boot scripts and styles |
| 238 |
wp_enqueue_script( 'dashboard-prerequisites' ); |
| 239 |
wp_enqueue_script_module( 'dashboard' ); |
| 240 |
wp_enqueue_style( 'dashboard-prerequisites' ); |
| 241 |
} |
| 242 |
|
| 243 |
// Output the HTML |
| 244 |
?> |
| 245 |
<!DOCTYPE html> |
| 246 |
<html <?php language_attributes(); ?>> |
| 247 |
<head> |
| 248 |
<meta charset="<?php bloginfo( 'charset' ); ?>"> |
| 249 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 250 |
<title><?php echo esc_html( get_admin_page_title() ); ?></title> |
| 251 |
<style> |
| 252 |
html { |
| 253 |
background: #f1f1f1; |
| 254 |
color: #444; |
| 255 |
font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; |
| 256 |
font-size: 13px; |
| 257 |
line-height: 1.4em; |
| 258 |
} |
| 259 |
body { |
| 260 |
margin: 0; |
| 261 |
} |
| 262 |
#wpadminbar { display: none; } |
| 263 |
</style> |
| 264 |
<?php |
| 265 |
global $hook_suffix; |
| 266 |
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 267 |
$hook_suffix = 'dashboard'; |
| 268 |
|
| 269 |
// BEGIN see wp-admin/admin-header.php |
| 270 |
print_admin_styles(); |
| 271 |
print_head_scripts(); |
| 272 |
|
| 273 |
/** This action is documented in wp-admin/admin-header.php */ |
| 274 |
do_action( "admin_head-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 275 |
|
| 276 |
/** This action is documented in wp-admin/admin-header.php */ |
| 277 |
do_action( 'admin_head' ); |
| 278 |
// END see wp-admin/admin-header.php |
| 279 |
?> |
| 280 |
</head> |
| 281 |
<body class="dashboard"> |
| 282 |
<div id="dashboard-app" style="height: 100vh; box-sizing: border-box;"></div> |
| 283 |
<?php |
| 284 |
// BEGIN see wp-admin/admin-footer.php |
| 285 |
|
| 286 |
/** This action is documented in wp-admin/admin-footer.php */ |
| 287 |
do_action( 'admin_footer', $hook_suffix ); |
| 288 |
|
| 289 |
// Print import map first so it's available for inline scripts |
| 290 |
wp_script_modules()->print_import_map(); |
| 291 |
print_footer_scripts(); |
| 292 |
wp_script_modules()->print_enqueued_script_modules(); |
| 293 |
wp_script_modules()->print_script_module_preloads(); |
| 294 |
wp_script_modules()->print_script_module_data(); |
| 295 |
|
| 296 |
/** This action is documented in wp-admin/admin-footer.php */ |
| 297 |
do_action( "admin_footer-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 298 |
// END see wp-admin/admin-footer.php |
| 299 |
?> |
| 300 |
</body> |
| 301 |
</html> |
| 302 |
<?php |
| 303 |
exit; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Intercept admin_init to render the page early. |
| 308 |
* This bypasses the default WordPress admin template. |
| 309 |
*/ |
| 310 |
function gutenberg_dashboard_intercept_render() { |
| 311 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 312 |
if ( isset( $_GET['page'] ) && 'dashboard' === $_GET['page'] ) { |
| 313 |
gutenberg_dashboard_render_page(); |
| 314 |
exit; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
// Hook the interceptor to admin_init |
| 319 |
add_action( 'admin_init', 'gutenberg_dashboard_intercept_render' ); |
| 320 |
|