Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Indentation and PHP 7 typing improvements
Co-authored-by: Weston Ruter <[email protected]>
  • Loading branch information
luisherranz and westonruter authored Jan 18, 2024
commit dfcdc6f141aa651cb2d6f7aa1def984af059ded9
68 changes: 37 additions & 31 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class WP_Script_Modules {
* Holds the script module identifiers that were enqueued before registered.
*
* @since 6.5.0
* @var array
* @var array<string, true>
*/
private $enqueued_before_registered = array();

Expand Down Expand Up @@ -58,7 +58,7 @@ class WP_Script_Modules {
* is set to false, the version number is the currently installed WordPress version.
* If $version is set to null, no version is added.
*/
public function register( $id, $src, $deps = array(), $version = false ) {
public function register( string $id, string $src, array $deps = array(), $version = false ) {
if ( ! isset( $this->registered[ $id ] ) ) {
$dependencies = array();
foreach ( $deps as $dependency ) {
Expand Down Expand Up @@ -103,28 +103,33 @@ public function register( $id, $src, $deps = array(), $version = false ) {
*
* @param string $id The identifier of the script module. Should be unique. It will be used in the
* final import map.
* @param string $src Optinal. Full URL of the script module, or path of the script module relative to
* the WordPress root directory. If it is provided and the script module has not
* been registered yet, it will be registered.
* @param array $deps {
* Optional. List of dependencies.
* @type string|array $0... {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. 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.
* @param string $src Optional. Full URL of the script module, or path of the script module relative
* to the WordPress root directory. If it is provided and the script module has
* not been registered yet, it will be registered.
* @param array<string|array{
* id: string,
* import?: 'static'|'dynamic'
* }> $deps {
* Optional. List of dependencies.
*
* @type string|array $0... {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
*
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. 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.
*/
public function enqueue( $id, $src = '', $deps = array(), $version = false ) {
public function enqueue( string $id, string $src = '', array $deps = array(), $version = false ) {
if ( isset( $this->registered[ $id ] ) ) {
$this->registered[ $id ]['enqueue'] = true;
} elseif ( $src ) {
Expand All @@ -142,7 +147,7 @@ public function enqueue( $id, $src = '', $deps = array(), $version = false ) {
*
* @param string $id The identifier of the script module.
*/
public function dequeue( $id ) {
public function dequeue( string $id ) {
if ( isset( $this->registered[ $id ] ) ) {
$this->registered[ $id ]['enqueue'] = false;
}
Expand Down Expand Up @@ -256,7 +261,7 @@ public function print_import_map() {
* @return array Array with an `imports` key mapping to an array of script module identifiers and their respective
* URLs, including the version query.
*/
private function get_import_map() {
private function get_import_map(): array {
$imports = array();
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) {
$imports[ $id ] = $this->get_versioned_src( $script_module );
Expand All @@ -271,7 +276,7 @@ private function get_import_map() {
*
* @return array Script modules marked for enqueue, keyed by script module identifier.
*/
private function get_marked_for_enqueue() {
private function get_marked_for_enqueue(): array {
$enqueued = array();
foreach ( $this->registered as $id => $script_module ) {
if ( true === $script_module['enqueue'] ) {
Expand All @@ -291,12 +296,13 @@ private function get_marked_for_enqueue() {
*
* @since 6.5.0
*
* @param array $ids The identifiers of the script modules for which to gather dependencies.
* @param array $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
* Default is both.

* @param string[] $ids The identifiers of the script modules for which to gather dependencies.
* @param array $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
* Default is both.
* @return array List of dependencies, keyed by script module identifier.
*/
private function get_dependencies( $ids, $import_types = array( 'static', 'dynamic' ) ) {
private function get_dependencies( array $ids, array $import_types = array( 'static', 'dynamic' ) ) {
return array_reduce(
$ids,
function ( $dependency_script_modules, $id ) use ( $import_types ) {
Expand Down Expand Up @@ -328,7 +334,7 @@ function ( $dependency_script_modules, $id ) use ( $import_types ) {
* @param array $script_module The script module.
* @return string The script module src with a version if relevant.
*/
private function get_versioned_src( array $script_module ) {
private function get_versioned_src( array $script_module ): string {
$args = array();
if ( false === $script_module['version'] ) {
$args['ver'] = get_bloginfo( 'version' );
Expand Down
97 changes: 54 additions & 43 deletions src/wp-includes/script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @return WP_Script_Modules The main WP_Script_Modules instance.
*/
function wp_script_modules() {
function wp_script_modules(): WP_Script_Modules {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new WP_Script_Modules();
Expand All @@ -35,27 +35,33 @@ function wp_script_modules() {
*
* @param string $id The identifier of the script module. Should be unique. It will be used in the
* final import map.
* @param string $src Full URL of the script module, or path of the script module relative to the
* WordPress root directory.
* @param array $deps {
* Optional. List of dependencies.
* @type string|array $0... {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. 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.
* @param string $src Optional. Full URL of the script module, or path of the script module relative
* to the WordPress root directory. If it is provided and the script module has
* not been registered yet, it will be registered.
* @param array<string|array{
* id: string,
* import?: 'static'|'dynamic'
* }> $deps {
* Optional. List of dependencies.
*
* @type string|array $0... {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
*
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. 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_script_module( $id, $src, $deps = array(), $version = false ) {
function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) {
wp_script_modules()->register( $id, $src, $deps, $version );
}

Expand All @@ -69,28 +75,33 @@ function wp_register_script_module( $id, $src, $deps = array(), $version = false
*
* @param string $id The identifier of the script module. Should be unique. It will be used in the
* final import map.
* @param string $src Optinal. Full URL of the script module, or path of the script module relative to
* the WordPress root directory. If it is provided and the script module has not
* been registered yet, it will be registered.
* @param array $deps {
* Optional. List of dependencies.
* @type string|array $0... {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. 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.
* @param string $src Optional. Full URL of the script module, or path of the script module relative
* to the WordPress root directory. If it is provided and the script module has
* not been registered yet, it will be registered.
* @param array<string|array{
* id: string,
* import?: 'static'|'dynamic'
* }> $deps {
* Optional. List of dependencies.
*
* @type string|array $0... {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
*
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. 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_enqueue_script_module( $id, $src = '', $deps = array(), $version = false ) {
function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) {
wp_script_modules()->enqueue( $id, $src, $deps, $version );
}

Expand All @@ -101,6 +112,6 @@ function wp_enqueue_script_module( $id, $src = '', $deps = array(), $version = f
*
* @param string $id The identifier of the script module.
*/
function wp_dequeue_script_module( $id ) {
function wp_dequeue_script_module( string $id ) {
wp_script_modules()->dequeue( $id );
}