-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates
WP_Theme_JSON_Resolver
and tests to be equal to core (#36223)
- Loading branch information
Showing
13 changed files
with
280 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Function to read json files. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Reads and decodes a JSON file. | ||
* | ||
* @param string $filename Path to the JSON file. | ||
* @param array $options { | ||
* Optional. Options to be used with `json_decode()`. | ||
* | ||
* @type bool associative Optional. When `true`, JSON objects will be returned as associative arrays. | ||
* When `false`, JSON objects will be returned as objects. | ||
* } | ||
* | ||
* @return mixed Returns the value encoded in JSON in appropriate PHP type. | ||
* `null` is returned if the file is not found, or its content can't be decoded. | ||
*/ | ||
function gutenberg_json_file_decode( $filename, $options = array() ) { | ||
$result = null; | ||
$filename = wp_normalize_path( realpath( $filename ) ); | ||
if ( ! file_exists( $filename ) ) { | ||
trigger_error( | ||
sprintf( | ||
/* translators: %s: Path to the JSON file. */ | ||
__( "File %s doesn't exist!", 'gutenberg' ), | ||
$filename | ||
) | ||
); | ||
return $result; | ||
} | ||
|
||
$options = wp_parse_args( $options, array( 'associative' => false ) ); | ||
$decoded_file = json_decode( file_get_contents( $filename ), $options['associative'] ); | ||
|
||
if ( JSON_ERROR_NONE !== json_last_error() ) { | ||
trigger_error( | ||
sprintf( | ||
/* translators: 1: Path to the JSON file, 2: Error message. */ | ||
__( 'Error when decoding a JSON file at path %1$s: %2$s', 'gutenberg' ), | ||
$filename, | ||
json_last_error_msg() | ||
) | ||
); | ||
return $result; | ||
} | ||
|
||
return $decoded_file; | ||
} |
48 changes: 48 additions & 0 deletions
48
lib/compat/wordpress-5.9/translate-settings-using-i18n-schema.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Function to translate json files. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Translates the provided settings value using its i18n schema. | ||
* | ||
* @param string|string[]|array[]|object $i18n_schema I18n schema for the setting. | ||
* @param string|string[]|array[] $settings Value for the settings. | ||
* @param string $textdomain Textdomain to use with translations. | ||
* | ||
* @return string|string[]|array[] Translated settings. | ||
*/ | ||
function gutenberg_translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdomain ) { | ||
if ( empty( $i18n_schema ) || empty( $settings ) || empty( $textdomain ) ) { | ||
return $settings; | ||
} | ||
|
||
if ( is_string( $i18n_schema ) && is_string( $settings ) ) { | ||
//phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralContext, WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.LowLevelTranslationFunction | ||
return translate_with_gettext_context( $settings, $i18n_schema, $textdomain ); | ||
} | ||
if ( is_array( $i18n_schema ) && is_array( $settings ) ) { | ||
$translated_settings = array(); | ||
foreach ( $settings as $value ) { | ||
$translated_settings[] = translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain ); | ||
} | ||
return $translated_settings; | ||
} | ||
if ( is_object( $i18n_schema ) && is_array( $settings ) ) { | ||
$group_key = '*'; | ||
$translated_settings = array(); | ||
foreach ( $settings as $key => $value ) { | ||
if ( isset( $i18n_schema->$key ) ) { | ||
$translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain ); | ||
} elseif ( isset( $i18n_schema->$group_key ) ) { | ||
$translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain ); | ||
} else { | ||
$translated_settings[ $key ] = $value; | ||
} | ||
} | ||
return $translated_settings; | ||
} | ||
return $settings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.