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

Blocks: Add get_variation method #41589

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Blocks: added get_variation method
91 changes: 91 additions & 0 deletions projects/packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,95 @@ public static function get_path_to_block_metadata( $block_src_dir, $package_dist

return false === $result ? $block_src_dir : $result;
}

/**
* Determine whether a site should use the default set of blocks, or a custom set.
* Possible variations are currently beta, experimental, and production.
*
* @since $$next-version$$
*
* @return string $block_varation production|beta|experimental
*/
public static function get_variation() {
// Default to production blocks.
$block_varation = 'production';

/*
* 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 jetpack-6.9.0
* @deprecated jetpack-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 jetpack-6.9.0
* @deprecated jetpack-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
* or the deprecated jetpack_load_experimental_blocks filter.
* This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
*/
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 jetpack-8.1.0
*
* @param string $block_variation Can be beta, experimental, and production. Defaults to production.
*/
return apply_filters( 'jetpack_blocks_variation', $block_varation );
}
}
124 changes: 124 additions & 0 deletions projects/packages/blocks/tests/php/test-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,128 @@ public function test_get_block_name_from_path_convention() {
$this->assertEquals( $expected, $result, "Failed for path: $path" );
}
}

/**
* Test getting block variation with various constants.
*
* @since $$next-version$$
*
* @dataProvider get_variation_constants
*
* @param string $expected Expected variation value.
* @param string|null $constant_name Name of the constant to set, if any.
* @param mixed|null $constant_val Value of the constant to set, if any.
*
* @covers Automattic\Jetpack\Blocks::get_variation
*/
public function test_get_variation_with_constants( $expected, $constant_name, $constant_val ) {
if ( $constant_name ) {
Jetpack_Constants::set_constant( $constant_name, $constant_val );
}

try {
$this->assertEquals( $expected, Blocks::get_variation() );
} finally {
if ( $constant_name ) {
Jetpack_Constants::clear_constants();
}
}
}

/**
* Data provider for testing block variations with constants.
*
* @since $$next-version$$
*
* @return array[] Test parameters
*/
public function get_variation_constants() {
return array(
'default' => array(
'expected' => 'production',
'constant_name' => null,
'constant_val' => null,
),
'valid constant' => array(
'expected' => 'beta',
'constant_name' => 'JETPACK_BLOCKS_VARIATION',
'constant_val' => 'beta',
),
'invalid constant' => array(
'expected' => 'production',
'constant_name' => 'JETPACK_BLOCKS_VARIATION',
'constant_val' => 'invalid',
),
'old beta blocks constant' => array(
'expected' => 'beta',
'constant_name' => 'JETPACK_BETA_BLOCKS',
'constant_val' => true,
),
'old experimental blocks constant' => array(
'expected' => 'experimental',
'constant_name' => 'JETPACK_EXPERIMENTAL_BLOCKS',
'constant_val' => true,
),
);
}

/**
* Test getting block variation with various filters.
*
* @since $$next-version$$
*
* @dataProvider get_variation_deprecated_filters
*
* @param string $expected Expected variation value.
* @param string $filter_name Name of the filter to add.
*
* @covers Automattic\Jetpack\Blocks::get_variation
*/
public function test_get_variation_with_filters( $expected, $filter_name ) {
add_filter( $filter_name, '__return_true' );
try {
$this->assertEquals( $expected, Blocks::get_variation() );
} finally {
remove_filter( $filter_name, '__return_true' );
}
}

/**
* Data provider for testing block variations with filters.
*
* @since $$next-version$$
*
* @return array[] Test parameters
*/
public function get_variation_deprecated_filters() {
return array(
'deprecated beta filter' => array(
'expected' => 'beta',
'filter_name' => 'jetpack_load_beta_blocks',
),
'deprecated experimental filter' => array(
'expected' => 'experimental',
'filter_name' => 'jetpack_load_experimental_blocks',
),
);
}

/**
* Test getting block variation with jetpack_blocks_variation filter.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_variation
*/
public function test_get_variation_with_new_filter() {
$filter = function () {
return 'beta';
};
add_filter( 'jetpack_blocks_variation', $filter );
try {
$this->assertEquals( 'beta', Blocks::get_variation() );
} finally {
remove_filter( 'jetpack_blocks_variation', $filter );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Blocks: added get_variation method
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ public static function register_child_blocks() {
)
);

$blocks_variation = apply_filters( 'jetpack_blocks_variation', \Automattic\Jetpack\Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' ) );
if ( 'beta' === $blocks_variation ) {
if ( 'beta' === Blocks::get_variation() ) {
self::register_beta_blocks();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: deprecated Jetpack_Gutenberg::blocks_variation() method for Blocks::get_variation.


90 changes: 6 additions & 84 deletions projects/plugins/jetpack/class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static function get_preset( $deprecated = null ) {
*/
public static function get_jetpack_gutenberg_extensions_allowed_list() {
$preset_extensions_manifest = ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) ? array() : self::get_preset();
$blocks_variation = self::blocks_variation();
$blocks_variation = Blocks::get_variation();

return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation );
}
Expand Down Expand Up @@ -672,7 +672,7 @@ public static function enqueue_block_editor_assets() {
}

$blocks_dir = self::get_blocks_directory();
$blocks_variation = self::blocks_variation();
$blocks_variation = Blocks::get_variation();

if ( 'production' !== $blocks_variation ) {
$blocks_env = '-' . esc_attr( $blocks_variation );
Expand Down Expand Up @@ -886,91 +886,13 @@ public static function load_block_editor_extensions() {
*
* @since 8.1.0
*
* @deprecated $$next-version$$ Use Automattic\Jetpack\Blocks::get_variation instead. Requires the blocks pacakge.
*
* @return string $block_varation production|beta|experimental
*/
public static function blocks_variation() {
// Default to production blocks.
$block_varation = 'production';

/*
* 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
* or the deprecated jetpack_load_experimental_blocks filter.
* This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
*/
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
*
* @param string $block_variation Can be beta, experimental, and production. Defaults to production.
*/
return apply_filters( 'jetpack_blocks_variation', $block_varation );
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Blocks::get_variation' );
return Blocks::get_variation();
enejb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Loading