Skip to content

Commit

Permalink
Block bundling: introduce single variation constant
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeherve committed Jan 16, 2023
1 parent 1bb4df9 commit ce7a1ae
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 42 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 constant
47 changes: 39 additions & 8 deletions projects/packages/videopress/src/class-block-editor-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 @@ -34,14 +41,38 @@ 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.
*/
if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) {
Constants::set_constant( 'JETPACK_BETA_BLOCKS', true );
$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 @@ -70,7 +101,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 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 @@ -869,19 +847,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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Automattic\Jetpack\Extensions\BloggingPrompts\Settings;

use Automattic\Jetpack\Constants;
use Jetpack_Gutenberg;

/**
* Renders the settings field for enabling/disabling blogging prompts in the editor.
Expand All @@ -30,12 +30,17 @@ function enabled_field_callback() {
*/
function init() {
// If editor extensions are not loaded, don't show the settings.
if ( ! \Jetpack_Gutenberg::should_load() ) {
if ( ! Jetpack_Gutenberg::should_load() ) {
return;
}

// Blogging prompts is an expermental extension: if expermental blocks are not enabled, don't show the settings.
if ( ! Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' ) && ! Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
/*
* Blogging prompts is an experimental extension:
* Settings should only be shown in an environment
* where beta or experimental extension are loaded.
*/
$blocks_variation = Jetpack_Gutenberg::blocks_variation();
if ( ! in_array( $blocks_variation, array( 'beta', 'experimental' ), true ) ) {
return;
}

Expand Down

0 comments on commit ce7a1ae

Please sign in to comment.