Changeset 51599 for trunk/src/wp-includes/blocks.php
- Timestamp:
- 08/11/2021 09:06:31 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r51501 r51599 189 189 190 190 /** 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 */ 197 function 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 /** 191 208 * Registers a block type from the metadata stored in the `block.json` file. 192 209 * 193 210 * @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. 195 213 * 196 214 * @param string $file_or_folder Path to the JSON file with metadata definition for … … 210 228 } 211 229 212 $metadata = json_decode( file_get_contents( $metadata_file ), true);230 $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); 213 231 if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { 214 232 return false; … … 239 257 $settings = array(); 240 258 $property_mappings = array( 259 'apiVersion' => 'api_version', 241 260 'title' => 'title', 242 261 'category' => 'category', … … 250 269 'supports' => 'supports', 251 270 'styles' => 'styles', 271 'variations' => 'variations', 252 272 'example' => 'example', 253 'apiVersion' => 'api_version', 254 ); 273 ); 274 $textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; 275 $i18n_schema = get_block_metadata_i18n_schema(); 255 276 256 277 foreach ( $property_mappings as $key => $mapped_key ) { 257 278 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 ); 299 282 } 300 283 }
Note: See TracChangeset
for help on using the changeset viewer.