@wordpress/build 0.2.1-next.2f1c7c01b.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,311 @@
1
+ <?php
2
+ /**
3
+ * Page: {{PAGE_SLUG}}
4
+ * Auto-generated by build process.
5
+ * Do not edit this file manually.
6
+ *
7
+ * @package {{PREFIX}}
8
+ */
9
+
10
+ // Global storage for {{PAGE_SLUG}} routes and menu items
11
+ global ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes, ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_menu_items;
12
+ ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes = array();
13
+ ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_menu_items = array();
14
+
15
+ if ( ! function_exists( 'register_{{PAGE_SLUG_UNDERSCORE}}_route' ) ) {
16
+ /**
17
+ * Register a route for the {{PAGE_SLUG}} page.
18
+ *
19
+ * @param string $path Route path (e.g., '/types/$type/edit/$id').
20
+ * @param string|null $content_module Script module ID for content (stage/inspector).
21
+ * @param string|null $route_module Script module ID for route lifecycle hooks.
22
+ */
23
+ function register_{{PAGE_SLUG_UNDERSCORE}}_route( $path, $content_module = null, $route_module = null ) {
24
+ global ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes;
25
+
26
+ $route = array( 'path' => $path );
27
+ if ( ! empty( $content_module ) ) {
28
+ $route['content_module'] = $content_module;
29
+ }
30
+ if ( ! empty( $route_module ) ) {
31
+ $route['route_module'] = $route_module;
32
+ }
33
+
34
+ ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes[] = $route;
35
+ }
36
+ }
37
+
38
+ if ( ! function_exists( 'register_{{PAGE_SLUG_UNDERSCORE}}_menu_item' ) ) {
39
+ /**
40
+ * Register a menu item for the {{PAGE_SLUG}} page.
41
+ *
42
+ * @param string $id Menu item ID.
43
+ * @param string $label Display label.
44
+ * @param string $to Route path to navigate to.
45
+ * @param string $parent_id Optional. Parent menu item ID.
46
+ */
47
+ function register_{{PAGE_SLUG_UNDERSCORE}}_menu_item( $id, $label, $to, $parent_id = '' ) {
48
+ global ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_menu_items;
49
+
50
+ $menu_item = array(
51
+ 'id' => $id,
52
+ 'label' => $label,
53
+ 'to' => $to,
54
+ );
55
+
56
+ if ( ! empty( $parent_id ) ) {
57
+ $menu_item['parent'] = $parent_id;
58
+ }
59
+
60
+ ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_menu_items[] = $menu_item;
61
+ }
62
+ }
63
+
64
+ if ( ! function_exists( 'get_{{PAGE_SLUG_UNDERSCORE}}_routes' ) ) {
65
+ /**
66
+ * Get all registered routes for the {{PAGE_SLUG}} page.
67
+ *
68
+ * @return array Array of route objects.
69
+ */
70
+ function get_{{PAGE_SLUG_UNDERSCORE}}_routes() {
71
+ global ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes;
72
+ return ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes ?? array();
73
+ }
74
+ }
75
+
76
+ if ( ! function_exists( 'get_{{PAGE_SLUG_UNDERSCORE}}_menu_items' ) ) {
77
+ /**
78
+ * Get all registered menu items for the {{PAGE_SLUG}} page.
79
+ *
80
+ * @return array Array of menu item objects.
81
+ */
82
+ function get_{{PAGE_SLUG_UNDERSCORE}}_menu_items() {
83
+ global ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_menu_items;
84
+ return ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_menu_items ?? array();
85
+ }
86
+ }
87
+
88
+ if ( ! function_exists( '{{PAGE_SLUG_UNDERSCORE}}_preload_data' ) ) {
89
+ /**
90
+ * Preload REST API data for the {{PAGE_SLUG}} page.
91
+ * Automatically called during page rendering.
92
+ */
93
+ function {{PAGE_SLUG_UNDERSCORE}}_preload_data() {
94
+ // Define paths to preload - same for all pages
95
+ $preload_paths = array(
96
+ '/?_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',
97
+ array( '/wp/v2/settings', 'OPTIONS' ),
98
+ );
99
+
100
+ // Use rest_preload_api_request to gather the preloaded data
101
+ $preload_data = array_reduce(
102
+ $preload_paths,
103
+ 'rest_preload_api_request',
104
+ array()
105
+ );
106
+
107
+ // Register the preloading middleware with wp-api-fetch
108
+ wp_add_inline_script(
109
+ 'wp-api-fetch',
110
+ sprintf(
111
+ 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
112
+ wp_json_encode( $preload_data )
113
+ ),
114
+ 'after'
115
+ );
116
+ }
117
+ }
118
+
119
+ if ( ! function_exists( '{{PAGE_SLUG_UNDERSCORE}}_render_page' ) ) {
120
+ /**
121
+ * Render the {{PAGE_SLUG}} page.
122
+ * Call this function from add_menu_page or add_submenu_page.
123
+ */
124
+ function {{PAGE_SLUG_UNDERSCORE}}_render_page() {
125
+ // Set current screen
126
+ set_current_screen();
127
+
128
+ // Remove unwanted deprecated handler
129
+ remove_action( 'admin_head', 'wp_admin_bar_header' );
130
+
131
+ // Remove unwanted scripts and styles that were enqueued during `admin_init`
132
+ foreach ( wp_scripts()->queue as $script ) {
133
+ wp_dequeue_script( $script );
134
+ }
135
+ foreach ( wp_styles()->queue as $style ) {
136
+ wp_dequeue_style( $style );
137
+ }
138
+
139
+ // Fire init action for extensions to register routes and menu items
140
+ do_action( '{{PAGE_SLUG}}_init' );
141
+
142
+ // Preload REST API data
143
+ {{PAGE_SLUG_UNDERSCORE}}_preload_data();
144
+
145
+ // Get all registered routes and menu items
146
+ $menu_items = get_{{PAGE_SLUG_UNDERSCORE}}_menu_items();
147
+ $routes = get_{{PAGE_SLUG_UNDERSCORE}}_routes();
148
+
149
+ // Get boot module asset file for dependencies
150
+ $asset_file = plugin_dir_path( __FILE__ ) . '../../modules/boot/index.min.asset.php';
151
+ if ( file_exists( $asset_file ) ) {
152
+ $asset = require $asset_file;
153
+
154
+ // This script serves two purposes:
155
+ // 1. It ensures all the globals that are made available to the modules are loaded.
156
+ // 2. It initializes the boot module as an inline script.
157
+ wp_register_script( '{{PAGE_SLUG}}-prerequisites', '', $asset['dependencies'], $asset['version'], true );
158
+
159
+ // Add inline script to initialize the app
160
+ wp_add_inline_script(
161
+ '{{PAGE_SLUG}}-prerequisites',
162
+ sprintf(
163
+ 'import("@wordpress/boot").then(mod => mod.init({mountId: "%s", menuItems: %s, routes: %s}));',
164
+ '{{PAGE_SLUG}}-app',
165
+ wp_json_encode( $menu_items, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
166
+ wp_json_encode( $routes, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
167
+ )
168
+ );
169
+
170
+ // Register prerequisites style by filtering script dependencies to find registered styles
171
+ $style_dependencies = array_filter(
172
+ $asset['dependencies'],
173
+ function ( $handle ) {
174
+ return wp_style_is( $handle, 'registered' );
175
+ }
176
+ );
177
+ wp_register_style( '{{PAGE_SLUG}}-prerequisites', false, $style_dependencies, $asset['version'] );
178
+
179
+ // Build dependencies for {{PAGE_SLUG}} module
180
+ $boot_dependencies = array(
181
+ array(
182
+ 'import' => 'static',
183
+ 'id' => '@wordpress/boot',
184
+ ),
185
+ );
186
+
187
+ // Add all registered routes as dependencies
188
+ foreach ( $routes as $route ) {
189
+ if ( isset( $route['route_module'] ) ) {
190
+ $boot_dependencies[] = array(
191
+ 'import' => 'static',
192
+ 'id' => $route['route_module'],
193
+ );
194
+ }
195
+ if ( isset( $route['content_module'] ) ) {
196
+ $boot_dependencies[] = array(
197
+ 'import' => 'dynamic',
198
+ 'id' => $route['content_module'],
199
+ );
200
+ }
201
+ }
202
+
203
+ // Dummy script module to ensure dependencies are loaded
204
+ wp_register_script_module(
205
+ '{{PAGE_SLUG}}',
206
+ plugin_dir_url( __FILE__ ) . 'loader.js',
207
+ $boot_dependencies
208
+ );
209
+
210
+ // Enqueue the boot scripts and styles
211
+ wp_enqueue_script( '{{PAGE_SLUG}}-prerequisites' );
212
+ wp_enqueue_script_module( '{{PAGE_SLUG}}' );
213
+ wp_enqueue_style( '{{PAGE_SLUG}}-prerequisites' );
214
+ }
215
+
216
+ // Output the HTML
217
+ ?>
218
+ <!DOCTYPE html>
219
+ <html <?php language_attributes(); ?>>
220
+ <head>
221
+ <meta charset="<?php bloginfo( 'charset' ); ?>">
222
+ <meta name="viewport" content="width=device-width, initial-scale=1">
223
+ <title><?php echo esc_html( get_admin_page_title() ); ?></title>
224
+ <style>
225
+ html {
226
+ background: #f1f1f1;
227
+ color: #444;
228
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
229
+ font-size: 13px;
230
+ line-height: 1.4em;
231
+ }
232
+ body {
233
+ margin: 0;
234
+ }
235
+ #wpadminbar { display: none; }
236
+ </style>
237
+ <?php
238
+ global $hook_suffix;
239
+ // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
240
+ $hook_suffix = '{{PAGE_SLUG}}';
241
+
242
+ // BEGIN see wp-admin/admin-header.php
243
+ print_admin_styles();
244
+ print_head_scripts();
245
+
246
+ /**
247
+ * Fires in head section for a specific admin page.
248
+ *
249
+ * @since 2.1.0
250
+ */
251
+ do_action( "admin_head-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
252
+
253
+ /**
254
+ * Fires in head section for all admin pages.
255
+ *
256
+ * @since 2.1.0
257
+ */
258
+ do_action( 'admin_head' );
259
+ // END see wp-admin/admin-header.php
260
+ ?>
261
+ </head>
262
+ <body class="{{PAGE_SLUG}}">
263
+ <div id="{{PAGE_SLUG}}-app" style="height: 100vh; box-sizing: border-box;"></div>
264
+ <?php
265
+ // BEGIN see wp-admin/admin-footer.php
266
+
267
+ /**
268
+ * Prints scripts or data before the default footer scripts.
269
+ *
270
+ * @since 1.2.0
271
+ */
272
+ do_action( 'admin_footer', '' );
273
+
274
+ // Print import map first so it's available for inline scripts
275
+ wp_script_modules()->print_import_map();
276
+ print_footer_scripts();
277
+ wp_script_modules()->print_enqueued_script_modules();
278
+ wp_script_modules()->print_script_module_preloads();
279
+ wp_script_modules()->print_script_module_data();
280
+
281
+ /**
282
+ * Prints scripts or data after the default footer scripts.
283
+ *
284
+ * @since 2.8.0
285
+ */
286
+ do_action( "admin_footer-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
287
+ // END see wp-admin/admin-footer.php
288
+ ?>
289
+ </body>
290
+ </html>
291
+ <?php
292
+ exit;
293
+ }
294
+ }
295
+
296
+ if ( ! function_exists( '{{PAGE_SLUG_UNDERSCORE}}_intercept_render' ) ) {
297
+ /**
298
+ * Intercept admin_init to render the page early.
299
+ * This bypasses the default WordPress admin template.
300
+ */
301
+ function {{PAGE_SLUG_UNDERSCORE}}_intercept_render() {
302
+ // phpcs:ignore WordPress.Security.NonceVerification.Recommended
303
+ if ( isset( $_GET['page'] ) && '{{PAGE_SLUG}}' === $_GET['page'] ) {
304
+ {{PAGE_SLUG_UNDERSCORE}}_render_page();
305
+ exit;
306
+ }
307
+ }
308
+ }
309
+
310
+ // Hook the interceptor to admin_init
311
+ add_action( 'admin_init', '{{PAGE_SLUG_UNDERSCORE}}_intercept_render' );
@@ -0,0 +1,9 @@
1
+ <?php
2
+ /**
3
+ * Pages loader - Auto-generated by build process.
4
+ * Do not edit this file manually.
5
+ *
6
+ * @package {{PREFIX}}
7
+ */
8
+
9
+ {{PAGE_INCLUDES}}
@@ -0,0 +1,90 @@
1
+ <?php
2
+ /**
3
+ * Routes registration - Auto-generated by build process.
4
+ * Registers all routes on their respective page init hooks.
5
+ * Do not edit this file manually.
6
+ *
7
+ * @package {{PREFIX}}
8
+ */
9
+
10
+ // Load routes registry
11
+ $routes_file = __DIR__ . '/routes/index.php';
12
+ if ( ! file_exists( $routes_file ) ) {
13
+ return;
14
+ }
15
+
16
+ $routes = require $routes_file;
17
+
18
+ // Group routes by page
19
+ $routes_by_page = array();
20
+ foreach ( $routes as $route ) {
21
+ $page_slug = $route['page'];
22
+ if ( ! isset( $routes_by_page[ $page_slug ] ) ) {
23
+ $routes_by_page[ $page_slug ] = array();
24
+ }
25
+ $routes_by_page[ $page_slug ][] = $route;
26
+ }
27
+
28
+ // Helper function to register routes for a page
29
+ $register_routes_callback = function ( $page_routes, $page_slug_underscore, $register_function_name ) {
30
+ return function () use ( $page_routes, $page_slug_underscore, $register_function_name ) {
31
+ foreach ( $page_routes as $route ) {
32
+ $content_handle = null;
33
+ $route_handle = null;
34
+
35
+ // Register content module if exists
36
+ if ( $route['has_content'] ) {
37
+ $content_asset_path = __DIR__ . "/routes/{$route['name']}/content.min.asset.php";
38
+ if ( file_exists( $content_asset_path ) ) {
39
+ $content_asset = require $content_asset_path;
40
+ $content_handle = '{{HANDLE_PREFIX}}/routes/' . $route['name'] . '/content';
41
+ $extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';
42
+ wp_register_script_module(
43
+ $content_handle,
44
+ plugins_url( 'build/routes/' . $route['name'] . '/content' . $extension, dirname( __FILE__ ) ),
45
+ $content_asset['module_dependencies'] ?? array(),
46
+ $content_asset['version'] ?? false
47
+ );
48
+ }
49
+ }
50
+
51
+ // Register route module if exists
52
+ if ( $route['has_route'] ) {
53
+ $route_asset_path = __DIR__ . "/routes/{$route['name']}/route.min.asset.php";
54
+ if ( file_exists( $route_asset_path ) ) {
55
+ $route_asset = require $route_asset_path;
56
+ $route_handle = '{{HANDLE_PREFIX}}/routes/' . $route['name'] . '/route';
57
+ $extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';
58
+ wp_register_script_module(
59
+ $route_handle,
60
+ plugins_url( 'build/routes/' . $route['name'] . '/route' . $extension, dirname( __FILE__ ) ),
61
+ $route_asset['module_dependencies'] ?? array(),
62
+ $route_asset['version'] ?? false
63
+ );
64
+ }
65
+ }
66
+
67
+ // Register route with page
68
+ if ( function_exists( $register_function_name ) ) {
69
+ call_user_func( $register_function_name, $route['path'], $content_handle, $route_handle );
70
+ }
71
+ }
72
+ };
73
+ };
74
+
75
+ // Register hooks for both full-page and wp-admin modes
76
+ foreach ( $routes_by_page as $page_slug => $page_routes ) {
77
+ $page_slug_underscore = str_replace( '-', '_', $page_slug );
78
+
79
+ // Register all routes for full-page mode (page.php)
80
+ add_action(
81
+ "{$page_slug}_init",
82
+ $register_routes_callback( $page_routes, $page_slug_underscore, "register_{$page_slug_underscore}_route" )
83
+ );
84
+
85
+ // Register all routes for wp-admin mode (page-wp-admin.php)
86
+ add_action(
87
+ "{$page_slug}-wp-admin_init",
88
+ $register_routes_callback( $page_routes, $page_slug_underscore, "register_{$page_slug_underscore}_wp_admin_route" )
89
+ );
90
+ }
@@ -1,66 +0,0 @@
1
- <?php
2
- /**
3
- * Routes registration - Auto-generated by build process.
4
- * Do not edit this file manually.
5
- *
6
- * @package {{PREFIX}}
7
- */
8
-
9
- if ( ! function_exists( '{{NAMESPACE}}_register_routes' ) ) {
10
- /**
11
- * Register all routes with WordPress.
12
- */
13
- function {{NAMESPACE}}_register_routes() {
14
- $routes_dir = __DIR__ . '/routes';
15
- $routes_file = $routes_dir . '/index.php';
16
-
17
- if ( ! file_exists( $routes_file ) ) {
18
- return;
19
- }
20
-
21
- $routes = require $routes_file;
22
- $base_url = plugins_url( 'build/routes/', dirname( __FILE__ ) );
23
- $extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';
24
-
25
- foreach ( $routes as $route ) {
26
- // Register content module
27
- $content_asset_path = $routes_dir . '/' . $route['name'] . '/content.min.asset.php';
28
- $content_asset = file_exists( $content_asset_path ) ? require $content_asset_path : array();
29
- $content_handle = '{{HANDLE_PREFIX}}/routes/' . $route['name'] . '/content';
30
-
31
- wp_register_script_module(
32
- $content_handle,
33
- $base_url . $route['name'] . '/content' . $extension,
34
- $content_asset['module_dependencies'] ?? array(),
35
- $content_asset['version'] ?? false
36
- );
37
-
38
- $route_handle = null;
39
-
40
- // Register route module if it exists
41
- if ( isset( $route['has_route'] ) && $route['has_route'] ) {
42
- $route_asset_path = $routes_dir . '/' . $route['name'] . '/route.min.asset.php';
43
- $route_asset = file_exists( $route_asset_path ) ? require $route_asset_path : array();
44
- $route_handle = '{{HANDLE_PREFIX}}/routes/' . $route['name'] . '/route';
45
-
46
- wp_register_script_module(
47
- $route_handle,
48
- $base_url . $route['name'] . '/route' . $extension,
49
- $route_asset['module_dependencies'] ?? array(),
50
- $route_asset['version'] ?? false
51
- );
52
- }
53
-
54
- // Register the route with boot
55
- if ( function_exists( 'gutenberg_register_boot_route' ) ) {
56
- gutenberg_register_boot_route(
57
- $route['path'],
58
- $content_handle,
59
- $route_handle
60
- );
61
- }
62
- }
63
- }
64
-
65
- add_action( 'gutenberg_boot_init', '{{NAMESPACE}}_register_routes', 10 );
66
- }