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

@@ -41,6 +41,14 @@
"label": "Other"
}
],
"variations": [
{
"name": "error",
"title": "Error",
"description": "Shows error.",
"keywords": [ "failure" ]
}
],
"example": {
"attributes": {
"message": "This is a notice!"

View File

@@ -2,12 +2,12 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2015-12-31 16:31+0100\n"
"PO-Revision-Date: 2021-01-14 18:26+0100\n"
"PO-Revision-Date: 2021-07-15 13:36+0200\n"
"Language: pl_PL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.4.2\n"
"X-Generator: Poedit 3.0\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;"
@@ -41,3 +41,15 @@ msgstr "Domyślny"
msgctxt "block style label"
msgid "Other"
msgstr "Inny"
msgctxt "block variation title"
msgid "Error"
msgstr "Błąd"
msgctxt "block variation description"
msgid "Shows error."
msgstr "Wyświetla błąd."
msgctxt "block variation keyword"
msgid "failure"
msgstr "niepowodzenie"

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
*/