Repository navigation
Routing: Add boot package #72809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Routing: Add boot package #72809
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
| /** | ||
| * Boot package menu item registration API. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Registered menu items storage. | ||
| * | ||
| * @var array | ||
| */ | ||
| global $gutenberg_boot_menu_items; | ||
| $gutenberg_boot_menu_items = array(); | ||
|
|
||
| /** | ||
| * Register a boot menu item. | ||
| * | ||
| * @param string $id Unique menu item ID. | ||
| * @param string $label Menu item label. | ||
| * @param string $to Route path the menu item links to. | ||
| * @param string $parent_id Optional parent menu item ID. | ||
| */ | ||
| function gutenberg_register_boot_menu_item( $id, $label, $to, $parent_id = '' ) { | ||
| global $gutenberg_boot_menu_items; | ||
|
|
||
| $menu_item = array( | ||
| 'id' => $id, | ||
| 'label' => $label, | ||
| 'to' => $to, | ||
| ); | ||
|
|
||
| // Only include parent if it's not empty (matches next-admin approach). | ||
| if ( ! empty( $parent_id ) ) { | ||
| $menu_item['parent'] = $parent_id; | ||
| } | ||
|
|
||
| $gutenberg_boot_menu_items[] = $menu_item; | ||
| } | ||
|
|
||
| /** | ||
| * Get all registered boot menu items. | ||
| * | ||
| * @return array Array of registered menu items. | ||
| */ | ||
| function gutenberg_get_boot_menu_items() { | ||
| global $gutenberg_boot_menu_items; | ||
| return $gutenberg_boot_menu_items; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| <?php | ||
| /** | ||
| * Boot package - Minimal admin page with router and sidebar. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| // Include route and menu APIs. | ||
| require_once __DIR__ . '/boot-menu-items.php'; | ||
| require_once __DIR__ . '/preload.php'; | ||
|
|
||
| /** | ||
| * Initialize boot admin page. | ||
| */ | ||
| function gutenberg_boot_admin_page() { | ||
| // Set current screen. | ||
| set_current_screen(); | ||
|
|
||
| // Remove unwanted deprecated handler. | ||
| remove_action( 'admin_head', 'wp_admin_bar_header' ); | ||
|
|
||
| // Remove unwanted scripts and styles that were enqueued during `admin_init`. | ||
| foreach ( wp_scripts()->queue as $script ) { | ||
| wp_dequeue_script( $script ); | ||
| } | ||
| foreach ( wp_styles()->queue as $style ) { | ||
| wp_dequeue_style( $style ); | ||
| } | ||
|
|
||
| // Fire init action for extensions to register routes and menu items. | ||
| do_action( 'gutenberg_boot_init' ); | ||
|
|
||
| // Register some default menu items for demonstration. | ||
| gutenberg_register_boot_menu_item( 'home', __( 'Home', 'gutenberg' ), '/', '' ); | ||
| gutenberg_register_boot_menu_item( 'about', __( 'About', 'gutenberg' ), '/', '' ); | ||
| gutenberg_register_boot_menu_item( 'settings', __( 'Settings', 'gutenberg' ), '/', '' ); | ||
|
|
||
| // Get routes and menu items. | ||
| $menu_items = gutenberg_get_boot_menu_items(); | ||
|
|
||
| // Get boot module asset file for dependencies. | ||
| $asset_file = gutenberg_dir_path() . 'build/modules/boot/index.min.asset.php'; | ||
| if ( file_exists( $asset_file ) ) { | ||
| $asset = require $asset_file; | ||
|
|
||
| // This script serves two purposes: | ||
| // 1. It ensures all the globals that are made available to the modules are loaded. | ||
| // 2. It initializes the boot module as an inline script. | ||
| wp_register_script( 'gutenberg-boot-prerequisites', '', $asset['dependencies'], $asset['version'], true ); | ||
|
|
||
| // Add inline script to initialize the app. | ||
| wp_add_inline_script( | ||
| 'gutenberg-boot-prerequisites', | ||
| sprintf( | ||
| 'import("@wordpress/boot").then(mod => mod.init(%s));', | ||
| wp_json_encode( $menu_items, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) | ||
| ) | ||
| ); | ||
|
|
||
| // Register prerequisites style by filtering script dependencies to find registered styles. | ||
| $style_dependencies = array_filter( | ||
| $asset['dependencies'], | ||
| function ( $handle ) { | ||
| return wp_style_is( $handle, 'registered' ); | ||
| } | ||
| ); | ||
| wp_register_style( 'gutenberg-boot-prerequisites', false, $style_dependencies, $asset['version'] ); | ||
|
|
||
| // Dummy script module to ensure dependencies are loaded. | ||
| wp_register_script_module( | ||
| 'gutenberg-boot', | ||
| gutenberg_url( 'lib/experimental/boot/loader.js' ), | ||
| array( | ||
| 'import' => 'static', | ||
| 'id' => '@wordpress/boot', | ||
| ) | ||
| ); | ||
|
|
||
| // Enqueue the boot scripts a,d styles. | ||
| wp_enqueue_script( 'gutenberg-boot-prerequisites' ); | ||
| wp_enqueue_script_module( 'gutenberg-boot' ); | ||
| wp_enqueue_style( 'gutenberg-boot-prerequisites' ); | ||
| } | ||
|
|
||
| // Output the HTML. | ||
| ?> | ||
| <!DOCTYPE html> | ||
| <html <?php language_attributes(); ?>> | ||
| <head> | ||
| <meta charset="<?php bloginfo( 'charset' ); ?>"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| <title><?php esc_html_e( 'Boot Demo', 'gutenberg' ); ?></title> | ||
| <style> | ||
| html { | ||
| background: #f1f1f1; | ||
| color: #444; | ||
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; | ||
| font-size: 13px; | ||
| line-height: 1.4em; | ||
| } | ||
| body { | ||
| margin: 0; | ||
| } | ||
| #wpadminbar { display: none; } | ||
| </style> | ||
| <?php | ||
| global $hook_suffix; | ||
| // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | ||
| $hook_suffix = 'gutenberg-boot'; | ||
|
|
||
| // BEGIN see wp-admin/admin-header.php. | ||
| print_admin_styles(); | ||
| print_head_scripts(); | ||
|
|
||
| /** | ||
| * Fires in head section for a specific admin page. | ||
| * | ||
| * @since 2.1.0 | ||
| */ | ||
| do_action( "admin_head-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores | ||
|
|
||
| /** | ||
| * Fires in head section for all admin pages. | ||
| * | ||
| * @since 2.1.0 | ||
| */ | ||
| do_action( 'admin_head' ); | ||
| // END see wp-admin/admin-header.php. | ||
| ?> | ||
| </head> | ||
| <body class="gutenberg-boot"> | ||
| <div id="gutenberg-boot-app" style="height: 100vh; box-sizing: border-box;"></div> | ||
| <?php | ||
| // BEGIN see wp-admin/admin-footer.php. | ||
|
|
||
| /** | ||
| * Prints scripts or data before the default footer scripts. | ||
| * | ||
| * @since 1.2.0 | ||
| */ | ||
| do_action( 'admin_footer', '' ); | ||
|
|
||
| // Print import map first so it's available for inline scripts. | ||
| wp_script_modules()->print_import_map(); | ||
| print_footer_scripts(); | ||
| wp_script_modules()->print_enqueued_script_modules(); | ||
| wp_script_modules()->print_script_module_preloads(); | ||
| wp_script_modules()->print_script_module_data(); | ||
|
|
||
| /** | ||
| * Prints scripts or data after the default footer scripts. | ||
| * | ||
| * @since 2.8.0 | ||
| */ | ||
| do_action( "admin_footer-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores | ||
| // END see wp-admin/admin-footer.php. | ||
| ?> | ||
| </body> | ||
| </html> | ||
| <?php | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * Register boot admin page in WordPress admin menu. | ||
| */ | ||
| function gutenberg_register_boot_admin_page() { | ||
| add_submenu_page( | ||
| 'nothing', | ||
| __( 'Boot Demo', 'gutenberg' ), | ||
| __( 'Boot Demo', 'gutenberg' ), | ||
| 'manage_options', | ||
| 'gutenberg-boot', | ||
| '' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Render the boot admin page. | ||
| * | ||
| * This function checks if the current request is for the boot admin page | ||
| * and renders it, bypassing the default WordPress admin template. | ||
| */ | ||
| function gutenberg_boot_render_page() { | ||
| // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
| if ( isset( $_GET['page'] ) && 'gutenberg-boot' === $_GET['page'] ) { | ||
| gutenberg_boot_admin_page(); | ||
| exit; | ||
| } | ||
| } | ||
|
|
||
| add_action( 'admin_menu', 'gutenberg_register_boot_admin_page' ); | ||
| add_action( 'admin_init', 'gutenberg_boot_render_page' ); |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
| /** | ||
| * Boot package REST API preloading. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Preload REST API data for boot admin page. | ||
| * | ||
| * Manually implements preloading without block editor dependencies. | ||
| */ | ||
| function gutenberg_boot_preload_site_data() { | ||
| // Define paths to preload - must match exact fields from entities.js | ||
| $preload_paths = array( | ||
| '/?_fields=description,gmt_offset,home,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front', | ||
| array( '/wp/v2/settings', 'OPTIONS' ), | ||
| ); | ||
|
|
||
| // Use rest_preload_api_request to gather the preloaded data | ||
| $preload_data = array_reduce( | ||
| $preload_paths, | ||
| 'rest_preload_api_request', | ||
| array() | ||
| ); | ||
|
|
||
| // Register the preloading middleware with wp-api-fetch | ||
| wp_add_inline_script( | ||
| 'wp-api-fetch', | ||
| sprintf( | ||
| 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', | ||
| wp_json_encode( $preload_data ) | ||
| ), | ||
| 'after' | ||
| ); | ||
| } | ||
| add_action( 'gutenberg_boot_init', 'gutenberg_boot_preload_site_data', 5 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make this change? Isn't any other way of importing types from a package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now no, third-party devs have been doing this forever. The other way is to export all types from the "index" of components which is being explored in its own PR. #72831