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

Script Modules API #5818

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8347e8b
Modules API: Initial version
luisherranz Dec 22, 2023
27ba624
Move actions to default-filters.php
luisherranz Dec 23, 2023
156eb58
Move module functions to its own file
luisherranz Dec 23, 2023
bbae17c
Use wp_print_inline_script_tag to print import map
luisherranz Dec 23, 2023
76e8726
Divide tests into two files and improve covers
luisherranz Dec 23, 2023
f0fa726
Switch to ReflectionProperty to setStaticPropertyValue
luisherranz Dec 23, 2023
f13a068
Refactor the tests set_up
luisherranz Dec 23, 2023
ef978d8
Test that static dependencies of dynamic dependencies are not preloaded
luisherranz Jan 3, 2024
212cdac
Add since to main class DocBlock
luisherranz Jan 3, 2024
e644da8
Change assertEquals with custom ones
luisherranz Jan 3, 2024
1aa45a6
Avoid static methods and rely on instances
luisherranz Jan 4, 2024
eaaa563
Introduce wrapper functions for printing methods
luisherranz Jan 4, 2024
e30c8e9
Remove space between @since and @var in class properties
luisherranz Jan 4, 2024
a13bc87
Test that version is propagated correctly
luisherranz Jan 4, 2024
67f3900
Cap parameter descriptions to 120 chars
luisherranz Jan 4, 2024
66987f0
Turn $enqueued_before_registered into a map
luisherranz Jan 4, 2024
fda294d
Remove SCRIPT_DEBUG support
luisherranz Jan 4, 2024
c94568d
Rename enqueued to enqueue
luisherranz Jan 8, 2024
2eaf6ea
Add failing test for multiple prints of enqueued modules
luisherranz Jan 8, 2024
4dcfc88
Fix test
luisherranz Jan 8, 2024
a4f9812
Add failing test for multiple prints of preloaded modules
luisherranz Jan 8, 2024
89a82fc
Fix the test
luisherranz Jan 8, 2024
9f08176
Print enqueued and preloaded modules in the head and footer
luisherranz Jan 8, 2024
4b815bc
Move the hooks to the class
luisherranz Jan 9, 2024
bc8c633
Rename `type` key to `import`
luisherranz Jan 9, 2024
b3554d6
Add _doing_it_wrongs
luisherranz Jan 9, 2024
35a9402
Merge remote-tracking branch 'origin/add/modules-api' into add/module…
luisherranz Jan 9, 2024
df73485
Improve @param type of $deps
luisherranz Jan 9, 2024
8fd7501
Indicate how the new print functions work
luisherranz Jan 9, 2024
506784a
Reorder methods
luisherranz Jan 9, 2024
168ae91
Test an empty import map
luisherranz Jan 9, 2024
1df672d
Replace get_version_query_string with get_versioned_src
luisherranz Jan 9, 2024
0cb7f1b
Use `esc_url` instead of `esc_attr` for link's href attribute
luisherranz Jan 9, 2024
834693d
Add suffixes to the ids and an id to the import map
luisherranz Jan 9, 2024
b2ff803
Rename class to WP_Script_Modules
luisherranz Jan 10, 2024
adb5913
Allow wp_enqueue_module to also register modules
luisherranz Jan 10, 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
247 changes: 247 additions & 0 deletions src/wp-includes/class-wp-modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php
/**
* Modules API: WP_Modules class.
*
* Native support for ES Modules and Import Maps.
*
* @package WordPress
* @subpackage Modules
*/

/**
* Core class used to register modules.
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
*
* @since 6.5.0
*/
class WP_Modules {
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
/**
* Holds the registered modules, keyed by module identifier.
*
* @since 6.5.0
*
* @var array
*/
private static $registered = array();

/**
* Holds the module identifiers that were enqueued before registered.
*
* @since 6.5.0
*
* @var array
*/
private static $enqueued_before_registered = array();
luisherranz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Registers the module if no module with that module identifier has already
* been registered.
*
* @since 6.5.0
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
* @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $dependencies Optional. An array of module identifiers of the dependencies of this module. The dependencies can be strings or arrays. If they are arrays, they need an `id` key with the module identifier, and can contain a `type` key with either `static` or `dynamic`. By default, dependencies that don't contain a type are considered static.
* @param string|false|null $version Optional. String specifying module version number. Defaults to false. It is added to the URL as a query string for cache busting purposes. If SCRIPT_DEBUG is true, the version is the current timestamp. If $version is set to false, the version number is the currently installed WordPress version. If $version is set to null, no version is added.
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function register( $module_identifier, $src, $dependencies = array(), $version = false ) {
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
if ( ! isset( self::$registered[ $module_identifier ] ) ) {
$deps = array();
foreach ( $dependencies as $dependency ) {
if ( isset( $dependency['id'] ) ) {
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
$deps[] = array(
'id' => $dependency['id'],
'type' => isset( $dependency['type'] ) && 'dynamic' === $dependency['type'] ? 'dynamic' : 'static',
);
} elseif ( is_string( $dependency ) ) {
$deps[] = array(
'id' => $dependency,
'type' => 'static',
);
}
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
}

self::$registered[ $module_identifier ] = array(
'src' => $src,
'version' => $version,
'enqueued' => in_array( $module_identifier, self::$enqueued_before_registered, true ),
'dependencies' => $deps,
);
}
}

/**
* Marks the module to be enqueued in the page.
*
* @since 6.5.0
*
* @param string $module_identifier The identifier of the module.
*/
public static function enqueue( $module_identifier ) {
if ( isset( self::$registered[ $module_identifier ] ) ) {
self::$registered[ $module_identifier ]['enqueued'] = true;
} elseif ( ! in_array( $module_identifier, self::$enqueued_before_registered, true ) ) {
self::$enqueued_before_registered[] = $module_identifier;
}
}

/**
* Unmarks the module so it is no longer enqueued in the page.
*
* @since 6.5.0
*
* @param string $module_identifier The identifier of the module.
*/
public static function dequeue( $module_identifier ) {
if ( isset( self::$registered[ $module_identifier ] ) ) {
self::$registered[ $module_identifier ]['enqueued'] = false;
}
$key = array_search( $module_identifier, self::$enqueued_before_registered, true );
if ( false !== $key ) {
array_splice( self::$enqueued_before_registered, $key, 1 );
}
}

/**
* Returns the import map array.
*
* @since 6.5.0
*
* @return array Array with an `imports` key mapping to an array of module identifiers and their respective URLs, including the version query.
*/
public static function get_import_map() {
$imports = array();
foreach ( self::get_dependencies( array_keys( self::get_enqueued() ) ) as $module_identifier => $module ) {
$imports[ $module_identifier ] = $module['src'] . self::get_version_query_string( $module['version'] );
}
return array( 'imports' => $imports );
}

/**
* Prints the import map using a script tag with a type="importmap" attribute.
*
* @since 6.5.0
*/
public static function print_import_map() {
$import_map = self::get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
wp_print_inline_script_tag(
wp_json_encode( self::get_import_map(), JSON_HEX_TAG | JSON_HEX_AMP ),
array(
'type' => 'importmap',
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
)
);
}
}

/**
* Prints all the enqueued modules using script tags with type="module" attributes.
*
* @since 6.5.0
*/
public static function print_enqueued_modules() {
foreach ( self::get_enqueued() as $module_identifier => $module ) {
wp_print_script_tag(
array(
'type' => 'module',
'src' => $module['src'] . self::get_version_query_string( $module['version'] ),
'id' => $module_identifier,
)
);
}
}

/**
* Prints the the static dependencies of the enqueued modules using link tags
* with rel="modulepreload" attributes.
*
* If a module has already been enqueued, it will not be preloaded.
*
* @since 6.5.0
*/
public static function print_module_preloads() {
foreach ( self::get_dependencies( array_keys( self::get_enqueued() ), array( 'static' ) ) as $module_identifier => $module ) {
if ( true !== $module['enqueued'] ) {
echo sprintf(
'<link rel="modulepreload" href="%s" id="%s">',
esc_attr( $module['src'] . self::get_version_query_string( $module['version'] ) ),
esc_attr( $module_identifier )
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
}

/**
* Gets the version of a module.
*
* If SCRIPT_DEBUG is true, the version is the current timestamp. If $version
* is set to false, the version number is the currently installed WordPress
* version. If $version is set to null, no version is added. Otherwise, the
* string passed in $version is used.
*
* @since 6.5.0
*
* @param string|false|null $version The version of the module.
* @return string A string with the version, prepended by `?ver=`, or an empty string if there is no version.
*/
private static function get_version_query_string( $version ) {
if ( defined( 'SCRIPT_DEBUG ' ) && SCRIPT_DEBUG ) {
return '?ver=' . time();
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
} elseif ( false === $version ) {
return '?ver=' . get_bloginfo( 'version' );
} elseif ( null !== $version ) {
return '?ver=' . $version;
}
return '';
}

/**
* Retrieves an array of enqueued modules.
*
* @since 6.5.0
*
* @return array Enqueued modules, keyed by module identifier.
*/
private static function get_enqueued() {
$enqueued = array();
foreach ( self::$registered as $module_identifier => $module ) {
if ( true === $module['enqueued'] ) {
$enqueued[ $module_identifier ] = $module;
}
}
return $enqueued;
}

/**
* Retrieves all the dependencies for the given module identifiers, filtered
* by types.
*
* It will consolidate an array containing a set of unique dependencies based
* on the requested types: 'static', 'dynamic', or both. This method is
* recursive and also retrieves dependencies of the dependencies.
*
* @since 6.5.0
*
* @param array $module_identifiers The identifiers of the modules for which to gather dependencies.
* @param array $types Optional. Types of dependencies to retrieve: 'static', 'dynamic', or both. Default is both.
* @return array List of dependencies, keyed by module identifier.
*/
private static function get_dependencies( $module_identifiers, $types = array( 'static', 'dynamic' ) ) {
return array_reduce(
$module_identifiers,
function ( $dependency_modules, $module_identifier ) use ( $types ) {
$dependencies = array();
foreach ( self::$registered[ $module_identifier ]['dependencies'] as $dependency ) {
if (
in_array( $dependency['type'], $types, true ) &&
isset( self::$registered[ $dependency['id'] ] ) &&
! isset( $dependency_modules[ $dependency['id'] ] )
) {
$dependencies[ $dependency['id'] ] = self::$registered[ $dependency['id'] ];
}
}
return array_merge( $dependency_modules, $dependencies, self::get_dependencies( array_keys( $dependencies ), $types ) );
},
array()
);
}
}
5 changes: 5 additions & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,4 +748,9 @@
// Font management.
add_action( 'wp_head', 'wp_print_font_faces', 50 );

// Modules
add_action( 'wp_head', array( 'WP_Modules', 'print_import_map' ) );
add_action( 'wp_head', array( 'WP_Modules', 'print_enqueued_modules' ) );
add_action( 'wp_head', array( 'WP_Modules', 'print_module_preloads' ) );
luisherranz marked this conversation as resolved.
Show resolved Hide resolved

unset( $filter, $action );
46 changes: 46 additions & 0 deletions src/wp-includes/modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Modules API: Module functions
*
* @since 6.5.0
*
* @package WordPress
* @subpackage Modules
*/

/**
* Registers the module if no module with that module identifier has already
* been registered.
*
* @since 6.5.0
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
* @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $dependencies Optional. An array of module identifiers of the dependencies of this module. The dependencies can be strings or arrays. If they are arrays, they need an `id` key with the module identifier, and can contain a `type` key with either `static` or `dynamic`. By default, dependencies that don't contain a type are considered static.
* @param string|false|null $version Optional. String specifying module version number. Defaults to false. It is added to the URL as a query string for cache busting purposes. If SCRIPT_DEBUG is true, the version is the current timestamp. If $version is set to false, the version number is the currently installed WordPress version. If $version is set to null, no version is added.
*/
function wp_register_module( $module_identifier, $src, $dependencies = array(), $version = false ) {
WP_Modules::register( $module_identifier, $src, $dependencies, $version );
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Marks the module to be enqueued in the page.
*
* @since 6.5.0
*
* @param string $module_identifier The identifier of the module.
*/
function wp_enqueue_module( $module_identifier ) {
WP_Modules::enqueue( $module_identifier );
}

/**
* Unmarks the module so it is no longer enqueued in the page.
*
* @since 6.5.0
*
* @param string $module_identifier The identifier of the module.
*/
function wp_dequeue_module( $module_identifier ) {
WP_Modules::dequeue( $module_identifier );
}
2 changes: 2 additions & 0 deletions src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@
require ABSPATH . WPINC . '/fonts/class-wp-font-face-resolver.php';
require ABSPATH . WPINC . '/fonts/class-wp-font-face.php';
require ABSPATH . WPINC . '/fonts.php';
require ABSPATH . WPINC . '/class-wp-modules.php';
require ABSPATH . WPINC . '/modules.php';

$GLOBALS['wp_embed'] = new WP_Embed();

Expand Down
Loading
Loading