Skip to content

Commit

Permalink
Block bundling: introduce single variation constant (#28390)
Browse files Browse the repository at this point in the history
* Block bundling: introduce single variation constant

Fixes #28227

Instead of having 2 separate constants to manage what block bundle is loaded on a site, let's only use one, JETPACK_BLOCKS_VARIATION. It makes it easier to manage and overwrite.

I also sunset 2 existing filters in favor of the existing jetpack_blocks_variation filter which works the same way as our new constant.

* Continue to support old constant

* Update changelogs' wording

Co-authored-by: Daniel Bachhuber <daniel.bachhuber@automattic.com>

* ver bump

Co-authored-by: Daniel Bachhuber <daniel.bachhuber@automattic.com>
Co-authored-by: Karen Attfield <karenlattfield@gmail.com>
Co-authored-by: Brandon Kraft <public@brandonkraft.com>
  • Loading branch information
4 people authored Jan 22, 2023
1 parent 9dcb5bc commit d6b746c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Block bundling: sunset existing methods in favor of new `JETPACK_BLOCKS_VARIATION` constant
51 changes: 43 additions & 8 deletions projects/packages/videopress/src/class-block-editor-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class Block_Editor_Extensions {
*/
const SCRIPT_HANDLE = 'videopress-extensions';

/**
* What version of the blocks we are loading.
*
* @var string
*/
public static $blocks_variation = 'production';

/**
* Initializer
*
Expand All @@ -35,14 +42,42 @@ public static function init() {
}

/**
* Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
*
* @since 6.9.0
*
* @param boolean
* Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
*
* @since 6.9.0
* @deprecated Jetpack 11.8.0 Use jetpack_blocks_variation filter instead.
*
* @param boolean
*/
if (
apply_filters_deprecated(
'jetpack_load_beta_blocks',
array( false ),
'jetpack-11.8.0',
'jetpack_blocks_variation'
)
) {
self::$blocks_variation = 'beta';
}

/*
* Get block variation, from the new constant or the old one.
*/
if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) {
Constants::set_constant( 'JETPACK_BETA_BLOCKS', true );
if ( Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
self::$blocks_variation = 'beta';
}

$blocks_variation = Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' );
if ( ! empty( $blocks_variation ) ) {
/**
* Allow customizing the variation of blocks in use on a site.
* Overwrites any previously set values, whether by constant or filter.
*
* @since Jetpack 8.1.0
*
* @param string $block_variation Can be beta, experimental, and production. Defaults to production.
*/
self::$blocks_variation = apply_filters( 'jetpack_blocks_variation', $blocks_variation );
}

// Register the script.
Expand Down Expand Up @@ -71,7 +106,7 @@ function ( $extension ) {
return (array) array(
'name' => $extension,
'isBeta' => true,
'isEnabled' => Constants::is_true( 'JETPACK_BETA_BLOCKS' ),
'isEnabled' => 'beta' === self::$blocks_variation,
);
},
$videopress_extensions_data['beta']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Block bundling: sunset existing methods in favor of new `JETPACK_BLOCKS_VARIATION` constant
4 changes: 2 additions & 2 deletions projects/plugins/jetpack/class.jetpack-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -2097,8 +2097,8 @@ function ( $matches ) {
if ( 'beta' === $variation || 'experimental' === $variation ) {
$block_constant = sprintf(
/* translators: the placeholder is a constant name */
esc_html__( 'To load the block, add the constant %1$s as true to your wp-config.php file', 'jetpack' ),
( 'beta' === $variation ? 'JETPACK_BETA_BLOCKS' : 'JETPACK_EXPERIMENTAL_BLOCKS' )
esc_html__( 'To load the block, add the constant JETPACK_BLOCKS_VARIATION set to %1$s to your wp-config.php file', 'jetpack' ),
$variation
);
} else {
$block_constant = '';
Expand Down
88 changes: 63 additions & 25 deletions projects/plugins/jetpack/class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,6 @@ public static function init() {
return;
}

/**
* Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
*
* @since 6.9.0
*
* @param boolean
*/
if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) {
Constants::set_constant( 'JETPACK_BETA_BLOCKS', true );
}

/**
* Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks.
*
* @since 8.4.0
*
* @param boolean
*/
if ( apply_filters( 'jetpack_load_experimental_blocks', false ) ) {
Constants::set_constant( 'JETPACK_EXPERIMENTAL_BLOCKS', true );
}

/**
* Filter the list of block editor extensions that are available through Jetpack.
*
Expand Down Expand Up @@ -877,19 +855,79 @@ public static function blocks_variation() {
// Default to production blocks.
$block_varation = 'production';

if ( Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
/*
* Prefer to use this JETPACK_BLOCKS_VARIATION constant
* or the jetpack_blocks_variation filter
* to set the block variation in your code.
*/
$default = Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' );
if ( ! empty( $default ) && in_array( $default, array( 'beta', 'experimental', 'production' ), true ) ) {
$block_varation = $default;
}

/**
* Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
*
* @since 6.9.0
* @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
*
* @param boolean
*/
$is_beta = apply_filters_deprecated(
'jetpack_load_beta_blocks',
array( false ),
'jetpack-11.8.0',
'jetpack_blocks_variation'
);

/*
* Switch to beta blocks if you use the JETPACK_BETA_BLOCKS constant
* or the deprecated jetpack_load_beta_blocks filter.
* This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
*/
if (
empty( $default )
&& (
$is_beta
|| Constants::is_true( 'JETPACK_BETA_BLOCKS' )
)
) {
$block_varation = 'beta';
}

/**
* Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks.
*
* @since 6.9.0
* @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
*
* @param boolean
*/
$is_experimental = apply_filters_deprecated(
'jetpack_load_experimental_blocks',
array( false ),
'jetpack-11.8.0',
'jetpack_blocks_variation'
);

/*
* Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant.
* Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant
* or the deprecated jetpack_load_experimental_blocks filter.
* This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
*/
if ( Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' ) ) {
if (
empty( $default )
&& (
$is_experimental
|| Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' )
)
) {
$block_varation = 'experimental';
}

/**
* Allow customizing the variation of blocks in use on a site.
* Overwrites any previously set values, whether by constant or filter.
*
* @since 8.1.0
*
Expand Down
6 changes: 3 additions & 3 deletions projects/plugins/jetpack/extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ By keeping your extension in the beta array, it's safe to do small PRs and merge
Generally, all new extensions should start out as a beta.

- Before you develop, remember to add your extension's slug to the beta array in `extensions/index.json`.
- In the `wp-config.php` for your Docker environment (`docker/wordpress/wp-config.php`) or in your custom mu-plugins file (`docker/mu-plugins/yourfile.php`), enable beta extensions with the following snippet: `define( 'JETPACK_BETA_BLOCKS', true );`
- In the `wp-config.php` for your Docker environment (`docker/wordpress/wp-config.php`) or in your custom mu-plugins file (`docker/mu-plugins/yourfile.php`), enable beta extensions with the following snippet: `define( 'JETPACK_BLOCKS_VARIATION', 'beta' );`
- When you use this constant, you'll get all blocks: Beta blocks, Experimental blocks, and Production blocks.
- You can also filter to specific extensions: `add_filter( 'jetpack_blocks_variation', function () { return 'beta'; } );`.
- In the WordPress.com environment, Automatticians will be able to see beta extensions with no further configuration
- In a Jurassic Ninja site, you must go to Settings > Jetpack Constants, and enable the `JETPACK_BETA_BLOCKS` option there.
- In a Jurassic Ninja site, you must go to Settings > Jetpack Constants, and enable the Beta blocks option there.
- Once you've successfully beta tested your new extension, you can open new PR to make your extension live!
- Simply move the extension's slug out of the beta array and into the production array in `extensions/index.json`.

### Experimental Extensions

We also offer an "experimental" state for extensions. Those extensions will be made available to anyone having the `JETPACK_EXPERIMENTAL_BLOCKS` constant defined in `wp-config.php`. When you use this constant, you'll get Experimental blocks as well as Production blocks.
We also offer an "experimental" state for extensions. Those extensions will be made available to anyone having the `JETPACK_BLOCKS_VARIATION` constant set to `experimental` in `wp-config.php`. When you use this constant, you'll get Experimental blocks as well as Production blocks.

Experimental extensions are usually considered ready for production, but are served only to sites requesting them.

Expand Down

0 comments on commit d6b746c

Please sign in to comment.