Skip to content
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

Add @wordpress/a11y script module #7405

Draft
wants to merge 26 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d6de7d0
Prepare build for more script modules
sirreal Sep 17, 2024
54476a9
Redo script modules build
sirreal Sep 17, 2024
17e7dbc
Register scripts from asset file
sirreal Sep 17, 2024
bdd5123
Work on centralized script module registration
sirreal Sep 18, 2024
a3011f4
Remove redundant case
sirreal Sep 18, 2024
b207015
Fix interactivity-api deprecation note
sirreal Sep 19, 2024
b2547a2
Rename "modules" webpack to "script-modules"
sirreal Sep 19, 2024
502f146
Update script module loading code
sirreal Sep 20, 2024
1f65609
Use buildTarget in assets path
sirreal Sep 20, 2024
96a28a8
Update script modules assets
sirreal Sep 20, 2024
d2317ab
Prettier webpack config file
sirreal Sep 20, 2024
fb04163
Test with block-library assets from Gutenberg
sirreal Sep 20, 2024
47524d4
Revert "Test with block-library assets from Gutenberg"
sirreal Sep 20, 2024
02deb22
Remove a11y script module
sirreal Sep 20, 2024
7047c60
Prevent deletion of script-modules-packages.min.php by grunt
sirreal Sep 20, 2024
92aa6f9
Fix function name in deprecation notices
sirreal Sep 20, 2024
d864449
Remove debug code
sirreal Sep 20, 2024
7836320
Test deprecation
sirreal Sep 20, 2024
42250d8
Merge branch 'build/prepare-more-script-modules' into add/a11y-script…
sirreal Sep 20, 2024
5b812dc
Add a11y to dual script + modules list
sirreal Sep 20, 2024
e21b13b
Update script-modules-packages file
sirreal Sep 20, 2024
f236ae5
Add a11y HTML to page
sirreal Sep 20, 2024
2d89203
Add note
sirreal Sep 20, 2024
dce1f07
Remove iAPI-specific screen reader text
sirreal Sep 20, 2024
6e814fb
Revert "Remove iAPI-specific screen reader text"
sirreal Sep 20, 2024
8f38434
Update assets file
sirreal Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ wp-tests-config.php
/src/wp-admin/js
/src/wp-includes/assets/*
!/src/wp-includes/assets/script-loader-packages.min.php
!/src/wp-includes/assets/script-modules-packages.min.php
/src/wp-includes/js
/src/wp-includes/css/dist
/src/wp-includes/css/*.min.css
Expand Down
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = function(grunt) {
'wp-includes/css/dist',
'wp-includes/blocks/**/*.css',
'!wp-includes/assets/script-loader-packages.min.php',
'!wp-includes/assets/script-modules-packages.min.php',
],

// Prepend `dir` to `file`, and keep `!` in place.
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/assets/script-modules-packages.min.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('interactivity/index.min.js' => array('dependencies' => array(), 'version' => '2d6d1fdbcb3fda39c768', 'type' => 'module'), 'interactivity/debug.min.js' => array('dependencies' => array(), 'version' => '1ccc67b05c275e51a8f8', 'type' => 'module'), 'interactivity-router/index.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '64645ef3cd2d32860d7d', 'type' => 'module'), 'a11y/index.min.js' => array('dependencies' => array(), 'version' => 'b7d06936b8bc23cff2ad', 'type' => 'module'), 'block-library/file/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => 'fdc2f6842e015af83140', 'type' => 'module'), 'block-library/image/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => 'acfec7b3c0be4a859b31', 'type' => 'module'), 'block-library/navigation/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '8ff192874fc8910a284c', 'type' => 'module'), 'block-library/query/view.min.js' => array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'f4c91c89fa5271f3dad9', 'type' => 'module'), 'block-library/search/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '2a73400a693958f604de', 'type' => 'module'));
35 changes: 35 additions & 0 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ class WP_Script_Modules {
*/
private $enqueued_before_registered = array();

/**
* Tracks wehther the @wordpress/a11y script module is available.
*
* Some additional HTML is required on the page for the module to work. Track
* whether it's available to print at the appropriate time.
*
* @since 6.7.0
* @var bool
*/
private $a11y_available = false;

/**
* Registers the script module if no script module with that script module
* identifier has already been registered.
Expand Down Expand Up @@ -185,6 +196,8 @@ public function add_hooks() {

add_action( 'wp_footer', array( $this, 'print_script_module_data' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_data' ) );
add_action( 'wp_footer', array( $this, 'print_a11y_script_module_html' ), 20 );
add_action( 'admin_print_footer_scripts', array( $this, 'print_a11y_script_module_html' ), 20 );
}

/**
Expand Down Expand Up @@ -367,9 +380,15 @@ private function get_src( string $id ): string {
public function print_script_module_data(): void {
$modules = array();
foreach ( array_keys( $this->get_marked_for_enqueue() ) as $id ) {
if ( '@wordpress/a11y' === $id ) {
$this->a11y_available = true;
}
$modules[ $id ] = true;
}
foreach ( array_keys( $this->get_import_map()['imports'] ) as $id ) {
if ( '@wordpress/a11y' === $id ) {
$this->a11y_available = true;
}
$modules[ $id ] = true;
}

Expand Down Expand Up @@ -465,4 +484,20 @@ public function print_script_module_data(): void {
}
}
}

/**
* @access private This is only intended to be called by the registered actions.
*
* @since 6.7.0
*/
public function print_a11y_script_module_html() {
if ( ! $this->a11y_available ) {
return;
}
echo '<div style="position:absolute;margin:-1px;padding:0;height:1px;width:1px;overflow:hidden;clip-path:inset(50%);border:0;word-wrap:normal !important;">'
. '<p id="a11y-speak-intro-text" class="a11y-speak-intro-text" hidden>' . esc_html__( 'Notifications' ) . '</p>'
. '<div id="a11y-speak-assertive" class="a11y-speak-region" aria-live="assertive" aria-relevant="additions text" aria-atomic="true"></div>'
. '<div id="a11y-speak-polite" class="a11y-speak-region" aria-live="polite" aria-relevant="additions text" aria-atomic="true"></div>'
. '</div>';
}
}
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@
// Script Loader.
add_action( 'wp_default_scripts', 'wp_default_scripts' );
add_action( 'wp_default_scripts', 'wp_default_packages' );
add_action( 'wp_default_scripts', 'wp_default_script_modules' );

add_action( 'wp_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
add_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
Expand Down
19 changes: 3 additions & 16 deletions src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,33 +281,20 @@ public function get_context( ?string $store_namespace = null ): array {
/**
* Registers the `@wordpress/interactivity` script modules.
*
* @deprecated 6.7.0 Script Modules registration is handled by {@see wp_default_script_modules()}.
*
* @since 6.5.0
*/
public function register_script_modules() {
$suffix = wp_scripts_get_suffix();

wp_register_script_module(
'@wordpress/interactivity',
includes_url( "js/dist/interactivity$suffix.js" )
);

wp_register_script_module(
'@wordpress/interactivity-router',
includes_url( "js/dist/interactivity-router$suffix.js" ),
array( '@wordpress/interactivity' )
);
_deprecated_function( __METHOD__, '6.7.0', 'wp_default_script_modules' );
}

/**
* Adds the necessary hooks for the Interactivity API.
*
* @since 6.5.0
* @since 6.7.0 Use the {@see "script_module_data_{$module_id}"} filter to pass client-side data.
*/
public function add_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'register_script_modules' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_script_modules' ) );

add_filter( 'script_module_data_@wordpress/interactivity', array( $this, 'filter_script_module_interactivity_data' ) );
}

Expand Down
50 changes: 50 additions & 0 deletions src/wp-includes/script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,53 @@ function wp_dequeue_script_module( string $id ) {
function wp_deregister_script_module( string $id ) {
wp_script_modules()->deregister( $id );
}

/**
* Registers all the default WordPress Script Modules.
*
* @since 6.7.0
*/
function wp_default_script_modules() {
$suffix = defined( 'WP_RUN_CORE_TESTS' ) ? '.min' : wp_scripts_get_suffix();

/*
* Expects multidimensional array like:
*
* 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
* 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'),
* 'interactivity-router/index.min.js' => …
*/
$assets = include ABSPATH . WPINC . "/assets/script-modules-packages{$suffix}.php";

foreach ( $assets as $file_name => $script_module_data ) {
/*
* Build the WordPress Script Module ID from the file name.
* Prepend `@wordpress/` and remove extensions and `/index` if present:
* - interactivity/index.min.js => @wordpress/interactivity
* - interactivity/debug.min.js => @wordpress/interactivity/debug
* - block-library/query/view.js => @wordpress/block-library/query/view
*/
$script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?(?:\.min)?\.js$~D', '', $file_name, 1 );

switch ( $script_module_id ) {
/*
* Interactivity exposes two entrypoints, "/index" and "/debug".
* "/debug" should replalce "/index" in devlopment.
*/
case '@wordpress/interactivity/debug':
if ( ! SCRIPT_DEBUG ) {
continue 2;
}
$script_module_id = '@wordpress/interactivity';
break;
case '@wordpress/interactivity':
if ( SCRIPT_DEBUG ) {
continue 2;
}
break;
}

$path = "/wp-includes/js/dist/script-modules/{$file_name}";
wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
}
}
12 changes: 11 additions & 1 deletion tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ public function test_config_not_printed_when_empty() {
$this->expectOutputString( '' );
}

/**
* Test that the deprecated register_script_modules method is deprecated but does not throw.
*
* @ticket 60647
*
* @expectedDeprecated WP_Interactivity_API::register_script_modules
*/
public function test_register_script_modules_deprecated() {
$this->interactivity->register_script_modules();
}

/**
* Sets up an activity, runs an optional callback, and returns a MockAction for inspection.
*
Expand All @@ -221,7 +232,6 @@ public function test_config_not_printed_when_empty() {
*/
private function get_script_data_filter_result( ?Closure $callback = null ): MockAction {
$this->interactivity->add_hooks();
$this->interactivity->register_script_modules();
wp_enqueue_script_module( '@wordpress/interactivity' );
$filter = new MockAction();
add_filter( 'script_module_data_@wordpress/interactivity', array( $filter, 'filter' ) );
Expand Down
76 changes: 0 additions & 76 deletions tools/webpack/modules.js

This file was deleted.

Loading
Loading