Skip to content
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

Plugin: Try API for making experiments limited to WordPress codebase #41278

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/interface": "file:../interface",
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/keycodes": "file:../keycodes",
Expand Down
14 changes: 14 additions & 0 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { createExperiments } from '@wordpress/interface';

/**
* Internal dependencies
*/
Expand All @@ -17,3 +22,12 @@ export * from './elements';
export * from './utils';
export { storeConfig, store } from './store';
export { SETTINGS_DEFAULTS } from './store/defaults';

const __experiments = createExperiments( {
test( name ) {
// eslint-disable-next-line no-console
console.log( `Executed with "${ name }".` );
},
} );

export { __experiments };
6 changes: 6 additions & 0 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
} from '@wordpress/block-library';
import { __experiments } from '@wordpress/block-editor';
import { secretKey } from '@wordpress/interface';
import { render, unmountComponentAtNode } from '@wordpress/element';
import { dispatch, select } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
Expand Down Expand Up @@ -80,6 +82,10 @@ export function initializeEditor(
settings,
initialEdits
) {
const test = __experiments( 'test', secretKey );

test( 'hello world' );

// Prevent adding template part in the post editor.
// Only add the filter when the post editor is initialized, not imported.
addFilter(
Expand Down
11 changes: 11 additions & 0 deletions packages/interface/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
export * from './components';
export { store } from './store';

export const secretKey = process.env.WP_SECRET_KEY;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible, probable even, I am totally missing something but wouldn't this lead to the problem of plugins being able to import the secret key?

I ask this:

  • knowing this is a really early POC so maybe I should wait
  • acknowledging Adam's earlier comment about the overhead.

It's not that I don't trust plugins not to import the secret key if it's available, but it kind of is.

Copy link
Contributor

@draganescu draganescu May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterwilsoncc the way I understand this is that it solves the problem of experiments not being exposed in wp.* JavaScript globals. Plus the key even if you get it, it changes with every release, therefore you'll never be able to rely on it for more than a few months. This is a great leap compared to how __experimental* is within a simple wp.* call AND sometimes it remains for years unchanged, therefore "reliable".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like @draganescu answered the question. This is the related part I included in the description:

What matters is that the secretKey is not available to everyone through wp global. The value assigned to the secretKey gets generated on every build. In practice, it means it will change with every new public release for WordPress so developers won't be able to hardcode this version. We can think about an even more elaborate approach if a stable key for a single WP release version is still a concern.

The trick is that secretKey is bundled with webpack into every entry point that consumes the experimental/internal/private APIs. So in practice, secretKey would never be available from WordPress core (and Gutenberg) in the browser.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks both. I was confused between internal exports for the package to use, vs external exports to the wp global.

I think I have it now, thanks for your patience explaining.


export function createExperiments( experiments ) {
return function ( name, key ) {
if ( secretKey !== key ) {
throw new Error( 'Experiments are not supported.' );
}
return experiments[ name ];
};
}
1 change: 1 addition & 0 deletions tools/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const plugins = [
// Inject the `IS_GUTENBERG_PLUGIN` global, used for feature flagging.
'process.env.IS_GUTENBERG_PLUGIN':
process.env.npm_package_config_IS_GUTENBERG_PLUGIN,
'process.env.WP_SECRET_KEY': JSON.stringify( new Date() ),
} ),
mode === 'production' && new ReadableJsAssetsWebpackPlugin(),
];
Expand Down