Make WordPress Core


Ignore:
Timestamp:
08/11/2021 09:06:31 AM (5 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/l10n.php

    r51298 r51599  
    17131713    return $wp_locale_switcher->is_switched();
    17141714}
     1715
     1716/**
     1717 * Translates the provided settings value using its i18n schema.
     1718 *
     1719 * @since 5.9.0
     1720 * @access private
     1721 *
     1722 * @param string|string[]|array[]|object $i18n_schema I18n schema for the setting.
     1723 * @param string|string[]|array[]        $settings    Value for the settings.
     1724 * @param string                         $textdomain  Textdomain to use with translations.
     1725 *
     1726 * @return string|string[]|array[] Translated settings.
     1727 */
     1728function translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdomain ) {
     1729    if ( empty( $i18n_schema ) || empty( $settings ) || empty( $textdomain ) ) {
     1730        return $settings;
     1731    }
     1732
     1733    if ( is_string( $i18n_schema ) && is_string( $settings ) ) {
     1734        return translate_with_gettext_context( $settings, $i18n_schema, $textdomain );
     1735    }
     1736    if ( is_array( $i18n_schema ) && is_array( $settings ) ) {
     1737        $translated_settings = array();
     1738        foreach ( $settings as $value ) {
     1739            $translated_settings[] = translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain );
     1740        }
     1741        return $translated_settings;
     1742    }
     1743    if ( is_object( $i18n_schema ) && is_array( $settings ) ) {
     1744        $group_key           = '*';
     1745        $translated_settings = array();
     1746        foreach ( $settings as $key => $value ) {
     1747            if ( isset( $i18n_schema->$key ) ) {
     1748                $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain );
     1749            } elseif ( isset( $i18n_schema->$group_key ) ) {
     1750                $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain );
     1751            } else {
     1752                $translated_settings[ $key ] = $value;
     1753            }
     1754        }
     1755        return $translated_settings;
     1756    }
     1757    return $settings;
     1758}
Note: See TracChangeset for help on using the changeset viewer.