Skip to content

Commit

Permalink
Modules API: Script Modules add deregister option. (#58830)
Browse files Browse the repository at this point in the history
* Add deregister module function

* Sync with core PR
  • Loading branch information
cbravobernal authored and getdave committed Feb 20, 2024
1 parent 5044d4c commit 1605148
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
12 changes: 12 additions & 0 deletions lib/compat/wordpress-6.5/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ public function dequeue( string $id ) {
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.
Expand Down
13 changes: 13 additions & 0 deletions lib/compat/wordpress-6.5/scripts-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,16 @@ function wp_dequeue_script_module( string $id ) {
wp_script_modules()->dequeue( $id );
}
}

if ( ! function_exists( 'wp_deregister_script_module' ) ) {
/**
* 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 );
}
}
22 changes: 0 additions & 22 deletions lib/experimental/interactivity-api.php

This file was deleted.

32 changes: 32 additions & 0 deletions lib/interactivity-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Interactivity API functions specific for the Gutenberg editor plugin.
*
* @package gutenberg
*/

/**
* Deregisters the Core Interactivity API Modules and replace them
* with the ones from the Gutenberg plugin.
*/
function gutenberg_reregister_interactivity_script_modules() {
$default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
wp_deregister_script_module( '@wordpress/interactivity' );
wp_deregister_script_module( '@wordpress/interactivity-router' );

wp_register_script_module(
'@wordpress/interactivity',
gutenberg_url( '/build/interactivity/index.min.js' ),
array(),
$default_version
);

wp_register_script_module(
'@wordpress/interactivity-router',
gutenberg_url( '/build/interactivity/router.min.js' ),
array( '@wordpress/interactivity' ),
$default_version
);
}

add_action( 'init', 'gutenberg_reregister_interactivity_script_modules' );
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/l10n.php';
require __DIR__ . '/experimental/synchronization.php';
require __DIR__ . '/experimental/script-modules.php';
require __DIR__ . '/experimental/interactivity-api.php';

if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) {
require __DIR__ . '/experimental/disable-tinymce.php';
Expand Down Expand Up @@ -192,6 +191,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/client-assets.php';
require __DIR__ . '/demo.php';
require __DIR__ . '/experiments-page.php';
require __DIR__ . '/interactivity-api.php';

// Copied package PHP files.
if ( is_dir( __DIR__ . '/../build/style-engine' ) ) {
Expand Down

0 comments on commit 1605148

Please sign in to comment.