Skip to content

Commit

Permalink
Rename gutenberg_ to wp_ for some functions that land in WordPres…
Browse files Browse the repository at this point in the history
…s 5.9 (#36913)

* Rename gutenberg_json_file_decode to wp_json_file_decode

* Rename gutenberg_translate_settings_using_i18n_schema to wp_translate_settings_using_i18n_schema
  • Loading branch information
oandregal authored and noisysocks committed Nov 29, 2021
1 parent ef7edf3 commit 671ae63
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 78 deletions.
6 changes: 3 additions & 3 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Expand Up @@ -67,7 +67,7 @@ class WP_Theme_JSON_Resolver_Gutenberg {
private static function read_json_file( $file_path ) {
$config = array();
if ( $file_path ) {
$decoded_file = gutenberg_json_file_decode( $file_path, array( 'associative' => true ) );
$decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) );
if ( is_array( $decoded_file ) ) {
$config = $decoded_file;
}
Expand Down Expand Up @@ -98,11 +98,11 @@ public static function get_fields_to_translate() {
*/
private static function translate( $theme_json, $domain = 'default' ) {
if ( null === self::$i18n_schema ) {
$i18n_schema = gutenberg_json_file_decode( __DIR__ . '/theme-i18n.json' );
$i18n_schema = wp_json_file_decode( __DIR__ . '/theme-i18n.json' );
self::$i18n_schema = null === $i18n_schema ? array() : $i18n_schema;
}

return gutenberg_translate_settings_using_i18n_schema( self::$i18n_schema, $theme_json, $domain );
return wp_translate_settings_using_i18n_schema( self::$i18n_schema, $theme_json, $domain );
}

/**
Expand Down
80 changes: 41 additions & 39 deletions lib/compat/wordpress-5.9/json-file-decode.php
Expand Up @@ -5,48 +5,50 @@
* @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(
if ( ! function_exists( 'wp_json_file_decode' ) ) {
/**
* 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 wp_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;
}
__( "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'] );
$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(
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;
}
__( 'Error when decoding a JSON file at path %1$s: %2$s', 'gutenberg' ),
$filename,
json_last_error_msg()
)
);
return $result;
}

return $decoded_file;
return $decoded_file;
}
}
72 changes: 37 additions & 35 deletions lib/compat/wordpress-5.9/translate-settings-using-i18n-schema.php
Expand Up @@ -5,44 +5,46 @@
* @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 ( ! function_exists( 'wp_translate_settings_using_i18n_schema' ) ) {
/**
* 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 wp_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[] = gutenberg_translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain );
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 );
}
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 ] = gutenberg_translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain );
} elseif ( isset( $i18n_schema->$group_key ) ) {
$translated_settings[ $key ] = gutenberg_translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain );
} else {
$translated_settings[ $key ] = $value;
if ( is_array( $i18n_schema ) && is_array( $settings ) ) {
$translated_settings = array();
foreach ( $settings as $value ) {
$translated_settings[] = wp_translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain );
}
return $translated_settings;
}
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 ] = wp_translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain );
} elseif ( isset( $i18n_schema->$group_key ) ) {
$translated_settings[ $key ] = wp_translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain );
} else {
$translated_settings[ $key ] = $value;
}
}
return $translated_settings;
}
return $settings;
}
return $settings;
}
2 changes: 1 addition & 1 deletion phpunit/translate-settings-using-i18n-schema.php
Expand Up @@ -61,7 +61,7 @@ function test_gutenberg_translate_settings_using_i18n_schema() {
),
),
);
$result = gutenberg_translate_settings_using_i18n_schema(
$result = wp_translate_settings_using_i18n_schema(
$i18n_schema,
$settings,
$textdomain
Expand Down

0 comments on commit 671ae63

Please sign in to comment.