Skip to content

Commit 09eeb8c

Browse files
authored
[Private APIs] Make the re-registration safeguard opt-in rather than opt-out via IS_WORDPRESS_CORE (#48352)
Gutenberg introduced a system of sharing private APIs in #46131. One of the safeguards is a check preventing the same module from opting-in twice so that contributors cannot easily gain access by pretending to be a core module. That safeguard is only meant for WordPress core and not for the released @WordPress packages. However, right now it is opt-out and must be explicitly disabled by developers wanting to install the @WordPress packages. Let's make it opt-out instead. In other words: * If we’re in WP core – prevent opting-in to private API with the same package name twice * If we’re not in WP core – don’t prevent it Or: * Before this commit, double opt-in safeguard is enabled by default * After this commit, double opt-in safeguard is disabled by default AND WordPress core [explicitly enables it](WordPress/wordpress-develop#4121) The corresponding PR in `wordpress-develop` repo makes WordPress explicitly set the `IS_WORDPRESS_CORE` to true: WordPress/wordpress-develop#4121
1 parent 2a52f30 commit 09eeb8c

File tree

7 files changed

+17
-29
lines changed

7 files changed

+17
-29
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"npm": ">=6.9.0 <7"
2020
},
2121
"config": {
22-
"IS_GUTENBERG_PLUGIN": true,
23-
"ALLOW_EXPERIMENT_REREGISTRATION": true
22+
"IS_GUTENBERG_PLUGIN": true
2423
},
2524
"dependencies": {
2625
"@wordpress/a11y": "file:packages/a11y",

packages/private-apis/src/implementation.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ const requiredConsent =
4949

5050
/** @type {boolean} */
5151
let allowReRegistration;
52-
// Use try/catch to force "false" if the environment variable is not explicitly
53-
// set to true (e.g. when building WordPress core).
52+
// The safety measure is meant for WordPress core where IS_WORDPRESS_CORE
53+
// is set to true.
54+
// For the general use-case, the re-registration should be allowed by default
55+
// Let's default to true, then. Try/catch will fall back to "true" even if the
56+
// environment variable is not explicitly defined.
5457
try {
55-
allowReRegistration = process.env.ALLOW_EXPERIMENT_REREGISTRATION ?? false;
58+
allowReRegistration = process.env.IS_WORDPRESS_CORE ? false : true;
5659
} catch ( error ) {
57-
allowReRegistration = false;
60+
allowReRegistration = true;
5861
}
5962

6063
/**

storybook/main.js

-6
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,4 @@ module.exports = {
2828
emotionAlias: false,
2929
storyStoreV7: true,
3030
},
31-
env: ( config ) => ( {
32-
...config,
33-
// Inject the `ALLOW_EXPERIMENT_REREGISTRATION` global, used by
34-
// @wordpress/private-apis.
35-
ALLOW_EXPERIMENT_REREGISTRATION: true,
36-
} ),
3731
};

test/native/setup.js

-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ import { Image, NativeModules as RNNativeModules } from 'react-native';
88
// testing environment: https://github.com/facebook/react-native/blob/6c19dc3266b84f47a076b647a1c93b3c3b69d2c5/Libraries/Core/setUpNavigator.js#L17
99
global.navigator = global.navigator ?? {};
1010

11-
/**
12-
* Whether to allow the same experiment to be registered multiple times.
13-
* This is useful for development purposes, but should be set to false
14-
* during the unit tests to ensure the Gutenberg plugin can be cleanly
15-
* merged into WordPress core where this is false.
16-
*/
17-
global.process.env.ALLOW_EXPERIMENT_REREGISTRATION = true;
18-
1911
// Set up the app runtime globals for the test environment, which includes
2012
// modifying the above `global.navigator`
2113
require( '../../packages/react-native-editor/src/globals' );

test/unit/config/gutenberg-env.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ global.process.env = {
2323
IS_GUTENBERG_PLUGIN:
2424
String( process.env.npm_package_config_IS_GUTENBERG_PLUGIN ) === 'true',
2525
/**
26-
* Whether to allow the same experiment to be registered multiple times.
27-
* This is useful for development purposes, but should be set to false
28-
* during the unit tests to ensure the Gutenberg plugin can be cleanly
29-
* merged into WordPress core where this is false.
26+
* Feature flag guarding features specific to WordPress core.
27+
* It's important to set it to "true" in the test environment
28+
* to ensure the Gutenberg plugin can be cleanly merged into
29+
* WordPress core.
3030
*/
31-
ALLOW_EXPERIMENT_REREGISTRATION: false,
31+
IS_WORDPRESS_CORE: true,
3232
};

tools/webpack/shared.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ const plugins = [
6666
// Inject the `IS_GUTENBERG_PLUGIN` global, used for feature flagging.
6767
'process.env.IS_GUTENBERG_PLUGIN':
6868
process.env.npm_package_config_IS_GUTENBERG_PLUGIN,
69-
// Inject the `ALLOW_EXPERIMENT_REREGISTRATION` global, used by @wordpress/private-apis.
70-
'process.env.ALLOW_EXPERIMENT_REREGISTRATION':
71-
process.env.npm_package_config_ALLOW_EXPERIMENT_REREGISTRATION,
69+
// Inject the `IS_WORDPRESS_CORE` global, used for feature flagging.
70+
'process.env.IS_WORDPRESS_CORE':
71+
process.env.npm_package_config_IS_WORDPRESS_CORE,
7272
} ),
7373
mode === 'production' && new ReadableJsAssetsWebpackPlugin(),
7474
];

typings/gutenberg-env/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interface Environment {
22
NODE_ENV: unknown;
33
IS_GUTENBERG_PLUGIN?: boolean;
4-
ALLOW_EXPERIMENT_REREGISTRATION?: boolean;
4+
IS_WORDPRESS_CORE?: boolean;
55
}
66
interface Process {
77
env: Environment;

0 commit comments

Comments
 (0)