Make WordPress Core


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

    r51557 r51599  
    42694269
    42704270/**
     4271 * Reads and decodes a JSON file.
     4272 *
     4273 * @since 5.9.0
     4274 *
     4275 * @param string $filename Path to the JSON file.
     4276 * @param array  $options  {
     4277 *     Optional. Options to be used with `json_decode()`.
     4278 *
     4279 *     @type bool associative Optional. When `true`, JSON objects will be returned as associative arrays.
     4280 *                            When `false`, JSON objects will be returned as objects.
     4281 * }
     4282 *
     4283 * @return mixed Returns the value encoded in JSON in appropriate PHP type.
     4284 *               `null` is returned if the file is not found, or its content can't be decoded.
     4285 */
     4286function wp_json_file_decode( $filename, $options = array() ) {
     4287    $result   = null;
     4288    $filename = wp_normalize_path( realpath( $filename ) );
     4289    if ( ! file_exists( $filename ) ) {
     4290        trigger_error(
     4291            sprintf(
     4292                /* translators: %s: Path to the JSON file. */
     4293                __( "File %s doesn't exist!" ),
     4294                $filename
     4295            )
     4296        );
     4297        return $result;
     4298    }
     4299
     4300    $options      = wp_parse_args( $options, array( 'associative' => false ) );
     4301    $decoded_file = json_decode( file_get_contents( $filename ), $options['associative'] );
     4302
     4303    if ( JSON_ERROR_NONE !== json_last_error() ) {
     4304        trigger_error(
     4305            sprintf(
     4306                /* translators: 1: Path to the JSON file, 2: Error message. */
     4307                __( 'Error when decoding a JSON file at path %1$s: %2$s' ),
     4308                $filename,
     4309                json_last_error_msg()
     4310            )
     4311        );
     4312        return $result;
     4313    }
     4314
     4315    return $decoded_file;
     4316}
     4317
     4318/**
    42714319 * Retrieve the WordPress home page URL.
    42724320 *
Note: See TracChangeset for help on using the changeset viewer.