-
Notifications
You must be signed in to change notification settings - Fork 890
My Jetpack: register wp-build-polyfills so the app (and Boost) loads without Gutenberg on WP < 7.0 #50291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My Jetpack: register wp-build-polyfills so the app (and Boost) loads without Gutenberg on WP < 7.0 #50291
Changes from 4 commits
32e49ee
196002b
7fb8bd8
253d5dc
df02d7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: fixed | ||
|
|
||
| Fix the My Jetpack app failing to load on WordPress installs without the Gutenberg plugin active, where the wp-theme script handle it depends on is otherwise unregistered. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |||||||||||||||||||||||||||
| use Automattic\Jetpack\Sync\Functions as Sync_Functions; | ||||||||||||||||||||||||||||
| use Automattic\Jetpack\Terms_Of_Service; | ||||||||||||||||||||||||||||
| use Automattic\Jetpack\Tracking; | ||||||||||||||||||||||||||||
| use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; | ||||||||||||||||||||||||||||
| use Jetpack; | ||||||||||||||||||||||||||||
| use WP_Error; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -247,12 +248,42 @@ public static function can_use_analytics() { | |||||||||||||||||||||||||||
| return $tracking->should_enable_tracking( new Terms_Of_Service(), $status ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Register polyfills for the wp-notices / wp-private-apis / wp-theme handles the | ||||||||||||||||||||||||||||
| * My Jetpack app bundle depends on but WP < 7.0 does not ship (or ships with an | ||||||||||||||||||||||||||||
| * incomplete allowlist) when the Gutenberg plugin is not active. | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * Without this, `my_jetpack_main_app` is enqueued with an unregistered `wp-theme` | ||||||||||||||||||||||||||||
| * dependency, so WP silently drops the script (no console error) and the My Jetpack | ||||||||||||||||||||||||||||
| * app — plus any consumer that hard-depends on it, such as Jetpack Boost — renders | ||||||||||||||||||||||||||||
| * a blank page. Only the handles the bundle actually uses are requested. | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
|
Comment on lines
+252
to
+255
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like AI over-explanation to me.
Suggested change
|
||||||||||||||||||||||||||||
| * @return void | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| public static function register_wp_build_polyfills() { | ||||||||||||||||||||||||||||
| if ( ! class_exists( WP_Build_Polyfills::class ) ) { | ||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| WP_Build_Polyfills::register( | ||||||||||||||||||||||||||||
| 'my-jetpack', | ||||||||||||||||||||||||||||
| array( 'wp-notices', 'wp-private-apis', 'wp-theme' ) | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Enqueue admin page assets. | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * @return void | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| public static function enqueue_scripts() { | ||||||||||||||||||||||||||||
| // Register the wp-build-polyfills shim before the extension hook below or | ||||||||||||||||||||||||||||
| // the app script can enqueue against wp-theme / wp-private-apis / wp-notices. | ||||||||||||||||||||||||||||
| // WP_Build_Polyfills registers synchronously on its first caller, so calling | ||||||||||||||||||||||||||||
| // it after a hook consumer would leave our handles recorded but unregistered | ||||||||||||||||||||||||||||
| // for this request. | ||||||||||||||||||||||||||||
| self::register_wp_build_polyfills(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Fires after the My Jetpack page is initialized. | ||||||||||||||||||||||||||||
| * Allows for enqueuing additional scripts only on the My Jetpack page. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
| /** | ||
| * Tests for Initializer::register_wp_build_polyfills(). | ||
| * | ||
| * @package automattic/my-jetpack | ||
| */ | ||
|
|
||
| namespace Automattic\Jetpack\My_Jetpack; | ||
|
|
||
| use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ReflectionProperty; | ||
|
|
||
| /** | ||
| * Verifies My Jetpack requests the wp-build-polyfills shim for the script handles | ||
| * its app bundle depends on but older WordPress (< 7.0, no Gutenberg) does not ship. | ||
| * | ||
| * @see \Automattic\Jetpack\My_Jetpack\Initializer::register_wp_build_polyfills | ||
| */ | ||
| class Register_Wp_Build_Polyfills_Test extends TestCase { | ||
|
|
||
| /** | ||
| * The app bundle's polyfill-provided handles that must be requested. | ||
| */ | ||
| const EXPECTED_HANDLES = array( 'wp-notices', 'wp-private-apis', 'wp-theme' ); | ||
|
|
||
| /** | ||
| * Reset the WP_Build_Polyfills static registrar so tests do not inherit state. | ||
| */ | ||
| public function tearDown(): void { | ||
| foreach ( array( | ||
| 'requested' => array(), | ||
| 'hooked' => false, | ||
| 'wp_version_threshold' => '7.0', | ||
| ) as $name => $value ) { | ||
| $prop = new ReflectionProperty( WP_Build_Polyfills::class, $name ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $prop->setAccessible( true ); | ||
| } | ||
| $prop->setValue( null, $value ); | ||
| } | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * Registering requests each required handle for the `my-jetpack` consumer. | ||
| */ | ||
| public function test_registers_required_polyfill_handles() { | ||
| Initializer::register_wp_build_polyfills(); | ||
|
|
||
| $consumers = WP_Build_Polyfills::get_consumers(); | ||
|
|
||
| foreach ( self::EXPECTED_HANDLES as $handle ) { | ||
| $this->assertArrayHasKey( $handle, $consumers, "$handle should be requested" ); | ||
| $this->assertContains( 'my-jetpack', $consumers[ $handle ], "$handle should list the my-jetpack consumer" ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Guards against bundle drift: if a future `@wordpress/*` bump makes the app | ||
| * depend on another polyfill-covered handle (e.g. `wp-views`) that we do not | ||
| * request, that page would blank out with no console error. Fail here instead. | ||
| * | ||
| * Skips when the package has not been built (no asset file to inspect). | ||
| */ | ||
| public function test_requested_handles_cover_polyfilled_bundle_dependencies() { | ||
| $asset_file = dirname( __DIR__, 2 ) . '/build/index.asset.php'; | ||
| if ( ! is_readable( $asset_file ) ) { | ||
| $this->markTestSkipped( 'my_jetpack_main_app build asset not found; run the package build first.' ); | ||
| } | ||
|
|
||
| $asset = require $asset_file; | ||
| $deps = $asset['dependencies'] ?? array(); | ||
| $polyfilled = array_values( array_intersect( $deps, WP_Build_Polyfills::SCRIPT_HANDLES ) ); | ||
| $missing = array_values( array_diff( $polyfilled, self::EXPECTED_HANDLES ) ); | ||
|
|
||
| $this->assertSame( | ||
| array(), | ||
| $missing, | ||
| 'The app bundle depends on polyfill-covered handle(s) not requested by register_wp_build_polyfills(): ' . implode( ', ', $missing ) | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Significance: patch | ||
| Type: changed | ||
| Comment: Update composer.lock. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,4 @@ | ||||||
| Significance: patch | ||||||
| Type: fixed | ||||||
|
|
||||||
| Fix a blank Boost admin page on WordPress installs without the Gutenberg plugin active, where the wp-theme script handle the embedded My Jetpack app depends on was otherwise unregistered. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Significance: patch | ||
| Type: other | ||
| Comment: Update composer.lock. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Significance: patch | ||
| Type: changed | ||
| Comment: Update composer.lock. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Significance: patch | ||
| Type: changed | ||
| Comment: Update composer.lock. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.