Make WordPress Core


Ignore:
Timestamp:
08/11/2021 09:06:31 AM (4 years ago)
Author:
gziolo
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r51501 r51599  
    189189
    190190/**
     191 * Gets i18n schema for block's metadata read from `block.json` file.
     192 *
     193 * @since 5.9.0
     194 *
     195 * @return array The schema for block's metadata.
     196 */
     197function get_block_metadata_i18n_schema() {
     198    static $i18n_block_schema;
     199
     200    if ( ! isset( $i18n_block_schema ) ) {
     201        $i18n_block_schema = wp_json_file_decode( __DIR__ . '/block-i18n.json' );
     202    }
     203
     204    return $i18n_block_schema;
     205}
     206
     207/**
    191208 * Registers a block type from the metadata stored in the `block.json` file.
    192209 *
    193210 * @since 5.5.0
    194  * @since 5.9.0 Added support for the `viewScript` field.
     211 * @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields.
     212 * @since 5.9.0 Added support for `variations` and `viewScript` fields.
    195213 *
    196214 * @param string $file_or_folder Path to the JSON file with metadata definition for
     
    210228    }
    211229
    212     $metadata = json_decode( file_get_contents( $metadata_file ), true );
     230    $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
    213231    if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
    214232        return false;
     
    239257    $settings          = array();
    240258    $property_mappings = array(
     259        'apiVersion'      => 'api_version',
    241260        'title'           => 'title',
    242261        'category'        => 'category',
     
    250269        'supports'        => 'supports',
    251270        'styles'          => 'styles',
     271        'variations'      => 'variations',
    252272        'example'         => 'example',
    253         'apiVersion'      => 'api_version',
    254     );
     273    );
     274    $textdomain        = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null;
     275    $i18n_schema       = get_block_metadata_i18n_schema();
    255276
    256277    foreach ( $property_mappings as $key => $mapped_key ) {
    257278        if ( isset( $metadata[ $key ] ) ) {
    258             $value = $metadata[ $key ];
    259             if ( empty( $metadata['textdomain'] ) ) {
    260                 $settings[ $mapped_key ] = $value;
    261                 continue;
    262             }
    263             $textdomain = $metadata['textdomain'];
    264             switch ( $key ) {
    265                 case 'title':
    266                 case 'description':
    267                     // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain
    268                     $settings[ $mapped_key ] = translate_with_gettext_context( $value, sprintf( 'block %s', $key ), $textdomain );
    269                     break;
    270                 case 'keywords':
    271                     $settings[ $mapped_key ] = array();
    272                     if ( ! is_array( $value ) ) {
    273                         continue 2;
    274                     }
    275 
    276                     foreach ( $value as $keyword ) {
    277                         // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
    278                         $settings[ $mapped_key ][] = translate_with_gettext_context( $keyword, 'block keyword', $textdomain );
    279                     }
    280 
    281                     break;
    282                 case 'styles':
    283                     $settings[ $mapped_key ] = array();
    284                     if ( ! is_array( $value ) ) {
    285                         continue 2;
    286                     }
    287 
    288                     foreach ( $value as $style ) {
    289                         if ( ! empty( $style['label'] ) ) {
    290                             // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
    291                             $style['label'] = translate_with_gettext_context( $style['label'], 'block style label', $textdomain );
    292                         }
    293                         $settings[ $mapped_key ][] = $style;
    294                     }
    295 
    296                     break;
    297                 default:
    298                     $settings[ $mapped_key ] = $value;
     279            $settings[ $mapped_key ] = $metadata[ $key ];
     280            if ( $textdomain && isset( $i18n_schema->$key ) ) {
     281                $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain );
    299282            }
    300283        }
Note: See TracChangeset for help on using the changeset viewer.