diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index e04b012e7dd08..6441b59c91300 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -766,6 +766,15 @@ function block_editor_rest_api_preload( array $preload_paths, $block_editor_cont ), 'after' ); + add_filter( + 'scriptmoduleconfig_@wordpress/api-fetch', + function ( $data ) use ( $preload_data ) { + return array_merge( + $data, + array( 'preloadData' => $preload_data ) + ); + } + ); } /** diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 46945ea89523c..a340bd60ac098 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -178,6 +178,8 @@ public function add_hooks() { add_action( $position, array( $this, 'print_import_map' ) ); add_action( $position, array( $this, 'print_enqueued_script_modules' ) ); add_action( $position, array( $this, 'print_script_module_preloads' ) ); + + add_action( 'wp_footer', array( $this, 'print_script_data' ) ); } /** @@ -359,4 +361,27 @@ private function get_src( string $id ): string { return $src; } + + public function print_script_data(): void { + $modules = array(); + foreach ( array_keys( $this->get_marked_for_enqueue() ) as $id ) { + $modules[ $id ] = true; + } + foreach ( array_keys( $this->get_import_map()['imports'] ) as $id ) { + $modules[ $id ] = true; + } + + foreach ( array_keys( $modules ) as $module_id ) { + $config = apply_filters( 'scriptmoduledata_' . $module_id, array() ); + if ( ! empty( $config ) ) { + wp_print_inline_script_tag( + wp_json_encode( $config, JSON_HEX_TAG | JSON_HEX_AMP ), + array( + 'type' => 'application/json', + 'id' => 'wp-config-data_' . $module_id, + ) + ); + } + } + } } diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index b256ff72e50c0..d98b0e4d2d1c2 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -508,43 +508,6 @@ function wp_default_packages_inline_scripts( $scripts ) { ); } -/** - * Adds script configuration data WordPress JavaScript packages may read. - * - * @since 6.6.0 - */ -function wp_default_packages_print_script_configuration_data( ) { - $scripts = wp_scripts(); - - // echo ''; - // var_dump( -// $scripts->query( 'wp-api-fetch', 'enqueued' ), -// $scripts->query( 'wp-api-fetch', 'done' ), - // ); - // die(); - - if ( - $scripts->query( 'wp-api-fetch', 'enqueued' ) || - $scripts->query( 'wp-api-fetch', 'done' ) - ) { - wp_print_inline_script_tag( - wp_json_encode( - array( - 'rootURL' => sanitize_url( get_rest_url() ), - 'nonce' => wp_installing() ? '' : wp_create_nonce( 'wp_rest' ), - 'shouldRegisterMediaUploadMiddleware' => true, - 'nonceEndpoint' => admin_url( 'admin-ajax.php?action=rest-nonce' ), - ), - JSON_HEX_TAG | JSON_HEX_AMP - ), - array( - 'type' => 'application/json', - 'id' => 'wp-apifetch-config-data', - ) - ); - } -} - /** * Adds inline scripts required for the TinyMCE in the block editor. * @@ -707,8 +670,6 @@ function wp_default_packages( $scripts ) { if ( did_action( 'init' ) ) { wp_default_packages_inline_scripts( $scripts ); } - - add_action( 'wp_footer', 'wp_default_packages_print_script_configuration_data' ); } /** diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php index 7ff80df5bfdb1..bde277bf0a11f 100644 --- a/src/wp-includes/script-modules.php +++ b/src/wp-includes/script-modules.php @@ -123,3 +123,22 @@ function wp_dequeue_script_module( string $id ) { function wp_deregister_script_module( string $id ) { wp_script_modules()->deregister( $id ); } + + +function wp_register_default_script_modules(): void { + add_filter( + 'scriptmoduleconfig_@wordpress/api-fetch', + function ( $data ) { + return array_merge( + $data, + array( + 'rootURL' => sanitize_url( get_rest_url() ), + 'nonce' => wp_installing() ? '' : wp_create_nonce( 'wp_rest' ), + 'shouldRegisterMediaUploadMiddleware' => true, + 'nonceEndpoint' => admin_url( 'admin-ajax.php?action=rest-nonce' ), + ) + ); + } + ); +} +add_action( 'init', 'wp_register_default_script_modules', 0 ); diff --git a/src/wp-includes/theme-previews.php b/src/wp-includes/theme-previews.php index 7e0c085b1c102..6a5a292dd0441 100644 --- a/src/wp-includes/theme-previews.php +++ b/src/wp-includes/theme-previews.php @@ -53,6 +53,18 @@ function wp_attach_theme_preview_middleware() { ), 'after' ); + + add_filter( + 'scriptmoduleconfig_@wordpress/api-fetch', + function ( $data ) { + return array_merge( + $data, + array( + 'themePreviewPath' => sanitize_text_field( wp_unslash( $_GET['wp_theme_preview'] ) ), + ) + ); + } + ); } /**