Skip to content

Commit

Permalink
add data passing for script modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Apr 23, 2024
1 parent 8f28b29 commit 529b670
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 39 deletions.
9 changes: 9 additions & 0 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
}
);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

/**
Expand Down Expand Up @@ -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,
)
);
}
}
}
}
39 changes: 0 additions & 39 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<plaintext>';
// 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.
*
Expand Down Expand Up @@ -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' );
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/wp-includes/script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
12 changes: 12 additions & 0 deletions src/wp-includes/theme-previews.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ),
)
);
}
);
}

/**
Expand Down

0 comments on commit 529b670

Please sign in to comment.