diff --git a/projects/packages/wp-build-polyfills/README.md b/projects/packages/wp-build-polyfills/README.md index 9674088a9d8a..6f101fb17878 100644 --- a/projects/packages/wp-build-polyfills/README.md +++ b/projects/packages/wp-build-polyfills/README.md @@ -34,14 +34,14 @@ This package provides those missing or updated packages so that plugins using `@ | `@wordpress/a11y` | `@wordpress/a11y` | | `@wordpress/widget-primitives` | `@wordpress/widget-primitives` | -Script modules use "first-wins" semantics — if Core or Gutenberg already registered the module, the polyfill is silently ignored. The exception is `@wordpress/widget-primitives`, which replaces the copy an active Gutenberg older than 24.4.0 registers. +Script modules use "first-wins" semantics — if Core or Gutenberg already registered the module, the polyfill is silently ignored. The exception is `@wordpress/widget-primitives`: the bundled copy replaces both Core's registration and the one an active Gutenberg older than 24.4.0 makes. Core ships its copy from WordPress 7.2, built from a Gutenberg branch behind the bundled one, so it lacks exports the dashboards import. A Gutenberg 24.4.0 or later copy stays. ## How it works 1. `WP_Build_Polyfills::register()` hooks into `wp_default_scripts` at **priority 20**, after Core and Gutenberg (both at the default priority 10) have registered their scripts. 2. For each polyfill, it checks whether a built asset file exists (`build/scripts/*/index.asset.php` or `build/modules/*/index.asset.php`). 3. For classic scripts, it checks whether the handle is already registered. Scripts marked for force replacement are deregistered and re-registered with the polyfill version when the WordPress version is below the script's threshold and active Gutenberg is not known to provide a compatible implementation. Non-force scripts are skipped if already registered. -4. For script modules, it calls `wp_register_script_module()`, which ignores duplicates, except that `@wordpress/widget-primitives` first deregisters the copy an active Gutenberg older than 24.4.0 registers. +4. For script modules, it calls `wp_register_script_module()`, which ignores duplicates, except that `@wordpress/widget-primitives` first deregisters three copies: Core's, identified by a `src` under `wp-includes`; an active Gutenberg's below 24.4.0; and an older copy from another plugin that bundles this package. A copy from anywhere else keeps first-wins, because nothing exposes the package version behind a registration. `wp-private-apis` has an additional Gutenberg-version guard because the dashboard packages require a private-apis allowlist that includes `@wordpress/widget-dashboard`. Gutenberg 23.4.0 and older do not include that allowlist entry; Gutenberg 23.5.0 is expected to be the first active-Gutenberg version that matches the current `@next` package build used here. diff --git a/projects/packages/wp-build-polyfills/changelog/fix-stats-620-widget-primitives-core-copy b/projects/packages/wp-build-polyfills/changelog/fix-stats-620-widget-primitives-core-copy new file mode 100644 index 000000000000..be5d355478f9 --- /dev/null +++ b/projects/packages/wp-build-polyfills/changelog/fix-stats-620-widget-primitives-core-copy @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Fix dashboards failing to load on WordPress 7.2, where Core's own, older widget-primitives module took precedence over the bundled one and lacked an export they import. diff --git a/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php b/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php index c6ebb2a64848..db73e1d57cab 100644 --- a/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php +++ b/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php @@ -2,8 +2,9 @@ /** * Polyfill registration for Core packages not available or incomplete in older WordPress versions. * - * On WordPress 7.0 at the default threshold, only wp-private-apis and wp-rich-text replace Core's - * copies; every other polyfill fills in what Core lacks, or replaces a Gutenberg copy too old to use. + * On WordPress 7.0 at the default threshold, wp-private-apis and wp-rich-text replace Core's copies, + * and @wordpress/widget-primitives replaces Core's wherever Core ships one, from 7.2; every other + * polyfill fills in what Core lacks, or replaces a Gutenberg copy too old to use. * * @package automattic/jetpack-wp-build-polyfills */ @@ -342,8 +343,9 @@ private static function is_gutenberg_version_safe( $minimum_version, $gutenberg_ * * Calls to wp_register_script_module() silently ignore duplicate registrations (first wins), so an * already registered module is left alone unless it is known to be too old for this package's - * current build: Gutenberg's copy below the minimum version, or another plugin's copy of this - * package bundling an older package version. Those are replaced. + * current build: Gutenberg's copy below the minimum version, another plugin's copy of this + * package bundling an older package version, or Core's own copy of a module the bundled build + * must serve. Those are replaced. * * @param string $build_dir Absolute path to the build directory. * @param string $base_file File path for plugins_url() computation. @@ -370,7 +372,8 @@ private static function register_modules( $build_dir, $base_file, $module_ids = // older ones lack exports the dashboard imports at module scope. The // replacement only adds exports, so Gutenberg's own consumers keep working. 'gutenberg_min_version' => self::GUTENBERG_WIDGET_PRIMITIVES_MIN_VERSION, - // Core never ships it, so outside Gutenberg the newest bundled copy wins. + // Outside a supported Gutenberg the newest bundled copy wins, over Core's + // registration as well as over another plugin's older copy. 'newest_copy_wins' => true, ), ); @@ -404,7 +407,13 @@ private static function register_modules( $build_dir, $base_file, $module_ids = $record = false; if ( ! empty( $data['newest_copy_wins'] ) ) { - if ( array_key_exists( $module_id, $registered_versions ) ) { + // From WP 7.2 Core ships its own copy, built from Gutenberg's wp/X branch and so + // behind the bundled one: 7.2 carries 0.10.0, which has no `useWidgetIdentity`. + // Only Core's is displaced, by its src; nothing exposes the version behind another. + $registered = wp_script_modules()->get_registered( $module_id ); + $is_core = null !== $registered && str_starts_with( $registered['src'], includes_url() ); + + if ( ! $is_core && array_key_exists( $module_id, $registered_versions ) ) { $current = $registered_versions[ $module_id ]; if ( null === $version || ( null !== $current && version_compare( $version, $current, '<=' ) ) ) { continue; @@ -412,8 +421,10 @@ private static function register_modules( $build_dir, $base_file, $module_ids = $replace = true; $record = true; } else { - // With a supported Gutenberg its copy stays; otherwise nobody else ships it. - $record = null === $gutenberg_version || $gutenberg_too_old; + // A copy released before this check records its version even where Core had + // already won the ID, so a recorded version cannot stand against Core's copy. + $record = $gutenberg_too_old || ( null === $gutenberg_version && ( null === $registered || $is_core ) ); + $replace = $record; } } diff --git a/projects/packages/wp-build-polyfills/tests/php/WP_Build_Polyfills_Test.php b/projects/packages/wp-build-polyfills/tests/php/WP_Build_Polyfills_Test.php index 5bdd4a827e5d..fef2559c6c57 100644 --- a/projects/packages/wp-build-polyfills/tests/php/WP_Build_Polyfills_Test.php +++ b/projects/packages/wp-build-polyfills/tests/php/WP_Build_Polyfills_Test.php @@ -905,6 +905,46 @@ public function test_register_modules_does_not_replace_widget_primitives_with_su $this->assertSame( '1.0.0-gutenberg', $module['version'] ); } + /** + * Core registers widget-primitives from WP 7.2, from a Gutenberg branch older than the bundled + * copy, so with no Gutenberg active the bundled copy has to displace it (STATS-620). + */ + public function test_register_modules_replaces_core_widget_primitives_without_gutenberg() { + $GLOBALS['wp_script_modules'] = new \WP_Script_Modules(); + wp_register_script_module( '@wordpress/widget-primitives', includes_url( 'js/dist/script-modules/widget-primitives/index.js' ), array(), '1.0.0-core' ); + + $this->invoke_register_modules( $this->make_widget_primitives_build( '0.11.0', 'bundled' ) ); + + $this->assert_widget_primitives_from( 'bundled' ); + } + + /** + * A copy released before the Core check records its version even where Core won the ID, so + * the recorded version says the module is served when Core's copy is what the page gets. + */ + public function test_register_modules_replaces_core_widget_primitives_despite_a_recorded_version() { + $GLOBALS['wp_script_modules'] = new \WP_Script_Modules(); + wp_register_script_module( '@wordpress/widget-primitives', includes_url( 'js/dist/script-modules/widget-primitives/index.js' ), array(), '1.0.0-core' ); + $GLOBALS[ WP_Build_Polyfills::MODULE_VERSIONS_GLOBAL ] = array( '@wordpress/widget-primitives' => '0.11.0' ); + + $this->invoke_register_modules( $this->make_widget_primitives_build( '0.11.0', 'bundled' ) ); + + $this->assert_widget_primitives_from( 'bundled' ); + } + + /** + * Nothing exposes the package version behind a registration, so a copy from outside + * wp-includes could be newer than the bundled one and keeps first-wins. + */ + public function test_register_modules_keeps_third_party_widget_primitives_without_gutenberg() { + $GLOBALS['wp_script_modules'] = new \WP_Script_Modules(); + wp_register_script_module( '@wordpress/widget-primitives', 'https://example.com/other-plugin-widget-primitives.js', array(), '1.0.0-third-party' ); + + $this->invoke_register_modules( $this->make_widget_primitives_build( '0.11.0', 'bundled' ) ); + + $this->assert_widget_primitives_from( '1.0.0-third-party' ); + } + /** * A newer bundled widget-primitives replaces the copy another plugin registered first. */ diff --git a/projects/plugins/jetpack/changelog/fix-stats-620-widget-primitives-core-copy b/projects/plugins/jetpack/changelog/fix-stats-620-widget-primitives-core-copy new file mode 100644 index 000000000000..881c09ca97e7 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-stats-620-widget-primitives-core-copy @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +Premium Analytics: Fix the Stats dashboard failing to load on WordPress 7.2. diff --git a/projects/plugins/premium-analytics/changelog/fix-stats-620-widget-primitives-core-copy b/projects/plugins/premium-analytics/changelog/fix-stats-620-widget-primitives-core-copy new file mode 100644 index 000000000000..66f91e8d33a8 --- /dev/null +++ b/projects/plugins/premium-analytics/changelog/fix-stats-620-widget-primitives-core-copy @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Fix the dashboard failing to load on WordPress 7.2. diff --git a/projects/plugins/stats/changelog/fix-stats-620-widget-primitives-core-copy b/projects/plugins/stats/changelog/fix-stats-620-widget-primitives-core-copy new file mode 100644 index 000000000000..aab31b5fc26f --- /dev/null +++ b/projects/plugins/stats/changelog/fix-stats-620-widget-primitives-core-copy @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Fix the Stats dashboard failing to load on WordPress 7.2.