mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Script Modules API: Add deregister module function
It was impossible to deregister a script module. It is changing to avoid problems for extenders that want to override any Core script module. Fixes #60463. Props cbravobernal, gziolo, mukesh27, youknowriad. git-svn-id: https://develop.svn.wordpress.org/trunk@57593 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d7c132b829
commit
13a042f8b7
@ -151,6 +151,18 @@ class WP_Script_Modules {
|
||||
unset( $this->enqueued_before_registered[ $id ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a registered script module.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*
|
||||
* @param string $id The identifier of the script module.
|
||||
*/
|
||||
public function deregister( string $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.
|
||||
|
||||
@ -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 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deregisters 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 );
|
||||
}
|
||||
|
||||
@ -126,6 +126,79 @@ class Tests_Script_Modules_WpScriptModules extends WP_UnitTestCase {
|
||||
$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 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' );
|
||||
$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 is not 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 is not deregistered
|
||||
* if it has been deregistered previously, 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.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user