From af0516ac857658fb4c63d79e34aa2a18efb6d366 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 7 Feb 2024 15:43:51 +0100 Subject: [PATCH 1/5] Add deregister module function --- src/wp-includes/class-wp-script-modules.php | 14 ++++++++++ src/wp-includes/script-modules.php | 11 ++++++++ .../tests/script-modules/wpScriptModules.php | 28 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index b2413d0f2442e..8ddd9f8ab50d4 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -151,6 +151,20 @@ public function dequeue( string $id ) { unset( $this->enqueued_before_registered[ $id ] ); } + /** + * Deregister the script module so it will no longer be registered. + * + * @since 6.5.0 + * + * @param string $id The identifier of the script module. + */ + public function deregister( string $id ) { + if ( isset( $this->registered[ $id ] ) ) { + unset( $this->registered[ $id ] ); + } + unset( $this->enqueued_before_registered[ $id ] ); + } + /** * Adds the hooks to print the import map, enqueued script modules and script * module preloads. diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php index 2aff768bb3d1a..1739f26abb64e 100644 --- a/src/wp-includes/script-modules.php +++ b/src/wp-includes/script-modules.php @@ -112,3 +112,14 @@ function wp_enqueue_script_module( string $id, string $src = '', array $deps = a function wp_dequeue_script_module( string $id ) { wp_script_modules()->dequeue( $id ); } + +/** + * Unregisters the script module. + * + * @since 6.5.0 + * + * @param string $id The identifier of the script module. + */ +function wp_deregister_script_module( string $id ) { + wp_script_modules()->deregister( $id ); +} diff --git a/tests/phpunit/tests/script-modules/wpScriptModules.php b/tests/phpunit/tests/script-modules/wpScriptModules.php index 26ca916140693..59680614dcb82 100644 --- a/tests/phpunit/tests/script-modules/wpScriptModules.php +++ b/tests/phpunit/tests/script-modules/wpScriptModules.php @@ -126,6 +126,34 @@ public function test_wp_dequeue_script_module() { $this->assertTrue( isset( $enqueued_script_modules['bar'] ) ); } + + /** + * Tests that a script module can be deregistered + * after being enqueued, and that will be removed + * from the enqueue list too. + * + * @ticket + * + * @covers ::register() + * @covers ::enqueue() + * @covers ::dequeue() + * @covers ::deregister() + * @covers ::print_enqueued_script_modules() + */ + public function test_wp_deregister_script_module() { + $this->script_modules->register( 'foo', '/foo.js' ); + $this->script_modules->register( 'bar', '/bar.js' ); + $this->script_modules->enqueue( 'foo' ); + $this->script_modules->enqueue( 'bar' ); + $this->script_modules->deregister( 'foo' ); // Dequeued. + + $enqueued_script_modules = $this->get_enqueued_script_modules(); + + $this->assertCount( 1, $enqueued_script_modules ); + $this->assertFalse( isset( $enqueued_script_modules['foo'] ) ); + $this->assertTrue( isset( $enqueued_script_modules['bar'] ) ); + } + /** * Tests that a script module can be enqueued before it is registered, and will * be handled correctly once registered. From e257287b6e39c6872658e6ef4d136f0b9d141adc Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 7 Feb 2024 15:48:50 +0100 Subject: [PATCH 2/5] Add ticket number on test --- tests/phpunit/tests/script-modules/wpScriptModules.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/script-modules/wpScriptModules.php b/tests/phpunit/tests/script-modules/wpScriptModules.php index 59680614dcb82..2533851e522b8 100644 --- a/tests/phpunit/tests/script-modules/wpScriptModules.php +++ b/tests/phpunit/tests/script-modules/wpScriptModules.php @@ -132,11 +132,10 @@ public function test_wp_dequeue_script_module() { * after being enqueued, and that will be removed * from the enqueue list too. * - * @ticket + * @ticket 60463 * * @covers ::register() * @covers ::enqueue() - * @covers ::dequeue() * @covers ::deregister() * @covers ::print_enqueued_script_modules() */ From 11d2ea7ecbec9b63f1d20fbda5007397b3153b95 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 8 Feb 2024 12:19:17 +0100 Subject: [PATCH 3/5] Update suggestions --- src/wp-includes/class-wp-script-modules.php | 6 ++---- src/wp-includes/script-modules.php | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 8ddd9f8ab50d4..6770597c1bcaa 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -152,16 +152,14 @@ public function dequeue( string $id ) { } /** - * Deregister the script module so it will no longer be registered. + * Removes a registered script module. * * @since 6.5.0 * * @param string $id The identifier of the script module. */ public function deregister( string $id ) { - if ( isset( $this->registered[ $id ] ) ) { - unset( $this->registered[ $id ] ); - } + unset( $this->registered[ $id ] ); unset( $this->enqueued_before_registered[ $id ] ); } diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php index 1739f26abb64e..f8efb9484b891 100644 --- a/src/wp-includes/script-modules.php +++ b/src/wp-includes/script-modules.php @@ -114,7 +114,7 @@ function wp_dequeue_script_module( string $id ) { } /** - * Unregisters the script module. + * Deregisters the script module. * * @since 6.5.0 * From e3a254c4f3a7d073cd44483a48d8c808059ae3a8 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 12 Feb 2024 13:40:09 +0100 Subject: [PATCH 4/5] Add more tests, indent comments --- .../tests/script-modules/wpScriptModules.php | 68 ++++++++++++++++--- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/tests/script-modules/wpScriptModules.php b/tests/phpunit/tests/script-modules/wpScriptModules.php index 2533851e522b8..5cc08adacc42f 100644 --- a/tests/phpunit/tests/script-modules/wpScriptModules.php +++ b/tests/phpunit/tests/script-modules/wpScriptModules.php @@ -128,17 +128,17 @@ public function test_wp_dequeue_script_module() { /** - * Tests that a script module can be deregistered - * after being enqueued, and that will be removed - * from the enqueue list too. - * - * @ticket 60463 - * - * @covers ::register() - * @covers ::enqueue() - * @covers ::deregister() - * @covers ::print_enqueued_script_modules() - */ + * Tests that a script module can be deregistered + * after being enqueued, and that will be removed + * from the enqueue list too. + * + * @ticket 60463 + * + * @covers ::register() + * @covers ::enqueue() + * @covers ::deregister() + * @covers ::get_enqueued_script_modules() + */ public function test_wp_deregister_script_module() { $this->script_modules->register( 'foo', '/foo.js' ); $this->script_modules->register( 'bar', '/bar.js' ); @@ -153,6 +153,52 @@ public function test_wp_deregister_script_module() { $this->assertTrue( isset( $enqueued_script_modules['bar'] ) ); } + /** + * Tests that a script module cannot be deregistered + * if it has not been registered before, causing + * no errors. + * + * @ticket 60463 + * + * @covers ::deregister() + * @covers ::get_enqueued_script_modules() + */ + public function test_wp_deregister_unexistent_script_module() { + $this->script_modules->deregister( 'unexistent' ); + $enqueued_script_modules = $this->get_enqueued_script_modules(); + + $this->assertCount( 0, $enqueued_script_modules ); + $this->assertFalse( isset( $enqueued_script_modules['unexistent'] ) ); + } + + /** + * Tests that a script module cannot be deregistered + * if it has not been registered before, causing + * no errors. + * + * @ticket 60463 + * + * @covers ::get_enqueued_script_modules() + * @covers ::register() + * @covers ::deregister() + * @covers ::enqueue() + */ + public function test_wp_deregister_already_deregistered_script_module() { + $this->script_modules->register( 'foo', '/foo.js' ); + $this->script_modules->enqueue( 'foo' ); + $this->script_modules->deregister( 'foo' ); // Dequeued. + $enqueued_script_modules = $this->get_enqueued_script_modules(); + + $this->assertCount( 0, $enqueued_script_modules ); + $this->assertFalse( isset( $enqueued_script_modules['foo'] ) ); + + $this->script_modules->deregister( 'foo' ); // Dequeued. + $enqueued_script_modules = $this->get_enqueued_script_modules(); + + $this->assertCount( 0, $enqueued_script_modules ); + $this->assertFalse( isset( $enqueued_script_modules['foo'] ) ); + } + /** * Tests that a script module can be enqueued before it is registered, and will * be handled correctly once registered. From a33f12e631df2232eb52a69f2f8409895c90bbc4 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 12 Feb 2024 13:41:43 +0100 Subject: [PATCH 5/5] Reword test descriptions --- tests/phpunit/tests/script-modules/wpScriptModules.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/script-modules/wpScriptModules.php b/tests/phpunit/tests/script-modules/wpScriptModules.php index 5cc08adacc42f..bc0a5a8559c24 100644 --- a/tests/phpunit/tests/script-modules/wpScriptModules.php +++ b/tests/phpunit/tests/script-modules/wpScriptModules.php @@ -154,7 +154,7 @@ public function test_wp_deregister_script_module() { } /** - * Tests that a script module cannot be deregistered + * Tests that a script module is not deregistered * if it has not been registered before, causing * no errors. * @@ -172,8 +172,8 @@ public function test_wp_deregister_unexistent_script_module() { } /** - * Tests that a script module cannot be deregistered - * if it has not been registered before, causing + * Tests that a script module is not deregistered + * if it has been deregistered previously, causing * no errors. * * @ticket 60463