Blocks: Add support for variations in block.json` file

We integrated variations with block types and the corresponding REST API endpoint in #52688. It's a follow-up patch to add missing support to the `block.json` metadata file when using `register_block_type`.

Some fields for variations are translatable.Therefore, i18n schema was copied over from Gutenberg: https://github.com/WordPress/gutenberg/blob/trunk/packages/blocks/src/api/i18n-block.json. The accompanying implementation was adapted as `translate_settings_using_i18n_schema`.

Props: gwwar, swissspidy, schlessera, jorgefilipecosta.
Fixes #53238.



git-svn-id: https://develop.svn.wordpress.org/trunk@51599 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Greg Ziółkowski
2021-08-11 09:06:31 +00:00
parent 9b565f7907
commit d9599addd1
11 changed files with 326 additions and 244 deletions

View File

@@ -69,6 +69,15 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
parent::tear_down();
}
/**
* Returns Polish locale string.
*
* @return string
*/
function filter_set_locale_to_polish() {
return 'pl_PL';
}
/**
* @ticket 45109
*/
@@ -370,6 +379,17 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
),
$result->styles
);
$this->assertSame(
array(
array(
'name' => 'error',
'title' => 'Error',
'description' => 'Shows error.',
'keywords' => array( 'failure' ),
),
),
$result->variations
);
$this->assertSame(
array(
'attributes' => array(
@@ -407,10 +427,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
* @ticket 52301
*/
function test_block_registers_with_metadata_i18n_support() {
function filter_set_locale_to_polish() {
return 'pl_PL';
}
add_filter( 'locale', 'filter_set_locale_to_polish' );
add_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) );
load_textdomain( 'notice', WP_LANG_DIR . '/plugins/notice-pl_PL.mo' );
$result = register_block_type_from_metadata(
@@ -418,7 +435,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
);
unload_textdomain( 'notice' );
remove_filter( 'locale', 'filter_set_locale_to_polish' );
remove_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) );
$this->assertInstanceOf( 'WP_Block_Type', $result );
$this->assertSame( 'tests/notice', $result->name );
@@ -439,6 +456,17 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
),
$result->styles
);
$this->assertSame(
array(
array(
'name' => 'error',
'title' => 'Błąd',
'description' => 'Wyświetla błąd.',
'keywords' => array( 'niepowodzenie' ),
),
),
$result->variations
);
}
/**

View File

@@ -1037,6 +1037,31 @@ class Tests_Functions extends WP_UnitTestCase {
$this->assertFalse( $json );
}
/**
* @ticket 53238
*/
function test_wp_json_file_decode() {
$result = wp_json_file_decode(
DIR_TESTDATA . '/blocks/notice/block.json'
);
$this->assertIsObject( $result );
$this->assertSame( 'tests/notice', $result->name );
}
/**
* @ticket 53238
*/
function test_wp_json_file_decode_associative_array() {
$result = wp_json_file_decode(
DIR_TESTDATA . '/blocks/notice/block.json',
array( 'associative' => true )
);
$this->assertIsArray( $result );
$this->assertSame( 'tests/notice', $result['name'] );
}
/**
* @ticket 36054
* @dataProvider datetime_provider

View File

@@ -0,0 +1,99 @@
<?php
/**
* @group l10n
* @group i18n
*/
class Tests_L10n_TranslateSettingsUsingI18nSchema extends WP_UnitTestCase {
/**
* Returns Polish locale string.
*
* @return string
*/
function filter_set_locale_to_polish() {
return 'pl_PL';
}
/**
* @ticket 53238
*/
function test_translate_settings_using_i18n_schema() {
$textdomain = 'notice';
add_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) );
load_textdomain( $textdomain, WP_LANG_DIR . '/plugins/notice-pl_PL.mo' );
$i18n_schema = (object) array(
'title' => 'block title',
'keywords' => array( 'block keyword' ),
'styles' => array(
(object) array( 'label' => 'block style label' ),
),
'context' => (object) array(
'*' => (object) array(
'variations' => array(
(object) array(
'title' => 'block variation title',
'description' => 'block variation description',
'keywords' => array( 'block variation keyword' ),
),
),
),
),
);
$settings = array(
'title' => 'Notice',
'keywords' => array(
'alert',
'message',
),
'styles' => array(
array( 'label' => 'Default' ),
array( 'label' => 'Other' ),
),
'context' => array(
'namespace' => array(
'variations' => array(
array(
'title' => 'Error',
'description' => 'Shows error.',
'keywords' => array( 'failure' ),
),
),
),
),
);
$result = translate_settings_using_i18n_schema(
$i18n_schema,
$settings,
$textdomain
);
unload_textdomain( $textdomain );
remove_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) );
$this->assertSame( 'Powiadomienie', $result['title'] );
$this->assertSameSets( array( 'ostrzeżenie', 'wiadomość' ), $result['keywords'] );
$this->assertSame(
array(
array(
'label' => 'Domyślny',
),
array(
'label' => 'Inny',
),
),
$result['styles']
);
$this->assertSame(
array(
array(
'title' => 'Błąd',
'description' => 'Wyświetla błąd.',
'keywords' => array( 'niepowodzenie' ),
),
),
$result['context']['namespace']['variations']
);
}
}

View File

@@ -44,53 +44,6 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
return 'pl_PL';
}
/**
* @ticket 52991
*/
public function test_fields_are_extracted() {
$actual = WP_Theme_JSON_Resolver::get_fields_to_translate();
$expected = array(
array(
'path' => array( 'settings', 'typography', 'fontSizes' ),
'key' => 'name',
'context' => 'Font size name',
),
array(
'path' => array( 'settings', 'color', 'palette' ),
'key' => 'name',
'context' => 'Color name',
),
array(
'path' => array( 'settings', 'color', 'gradients' ),
'key' => 'name',
'context' => 'Gradient name',
),
array(
'path' => array( 'settings', 'color', 'duotone' ),
'key' => 'name',
'context' => 'Duotone name',
),
array(
'path' => array( 'settings', 'blocks', '*', 'typography', 'fontSizes' ),
'key' => 'name',
'context' => 'Font size name',
),
array(
'path' => array( 'settings', 'blocks', '*', 'color', 'palette' ),
'key' => 'name',
'context' => 'Color name',
),
array(
'path' => array( 'settings', 'blocks', '*', 'color', 'gradients' ),
'key' => 'name',
'context' => 'Gradient name',
),
);
$this->assertSame( $expected, $actual );
}
/**
* @ticket 52991
*/