Skip to content

Commit

Permalink
Use Utils\http_request for remote request
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Jul 19, 2021
1 parent f286473 commit b3bede0
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions src/ThemeJsonExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
use Gettext\Extractors\ExtractorInterface;
use Gettext\Translations;
use WP_CLI;
use WP_CLI\Utils;

final class ThemeJsonExtractor extends Extractor implements ExtractorInterface {
use IterableCodeExtractor;

/**
* Source URL from which to download the latest theme-i18n.json file.
*
* @var string
*/
const THEME_JSON_SOURCE = 'https://develop.svn.wordpress.org/trunk/src/wp-includes/theme-i18n.json';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -87,38 +95,33 @@ public static function fromString( $string, Translations $translations, array $o
}

/**
* Given a file path, reads it as a JSON file
* and returns an array with its contents.
*
* Returns an empty array in case of error.
* Given a remote URL, fetches it remotely and returns its content.
*
* Ported from the core class `WP_Theme_JSON_Resolver`.
* Returns an empty string in case of error.
*
* @param string $file_path Path to file.
* @param array $context A valid context resource created with stream_context_create(). Optional.
* @param string $url URL of the file to fetch.
*
* @return array Contents of the file.
* @return string Contents of the file.
*/
private static function read_json_file( $file_path, $context = null ) {
$config = [];
if ( $file_path ) {
$decoded_file = json_decode(
file_get_contents( $file_path, false, $context ),
true
);

$json_decoding_error = json_last_error();
if ( JSON_ERROR_NONE !== $json_decoding_error ) {
WP_CLI::debug( "Error when decoding {$file_path}", 'make-pot' );
private static function remote_get( $url ) {
if ( ! $url ) {
return '';
}

return $config;
}
$headers = [ 'Content-type: application/json' ];
$options = [ 'halt_on_error' => false ];
$response = Utils\http_request( 'GET', $url, null, $headers, $options );

if ( is_array( $decoded_file ) ) {
$config = $decoded_file;
}
if (
! $response->success
|| 200 > (int) $response->status_code
|| 300 <= $response->status_code
) {
WP_CLI::debug( "Failed to download from URL {$url}", 'make-pot' );
return '';
}
return $config;

return trim( $response->body );
}

/**
Expand All @@ -142,22 +145,24 @@ private static function read_json_file( $file_path, $context = null ) {
* @return array An array of theme.json fields that are translatable and the keys that are translatable.
*/
private static function get_fields_to_translate() {
$context = stream_context_create(
[
'http' => [
'method' => 'GET',
'header' => 'Content-type: application/json',
'timeout' => '3', // To make sure it resolves in a reasonable timeframe.
],
]
);
// Using the WordPress.org SVN repo resolved to a 403.
// $file_structure = self::read_json_file( 'http://develop.svn.wordpress.org/trunk/src/wp-includes/theme-i18n.json', $context );
$file_structure = self::read_json_file( 'https://raw.githubusercontent.com/WordPress/wordpress-develop/master/src/wp-includes/theme-i18n.json', $context );
if ( empty( $file_structure ) ) {
$json = self::remote_get( self::THEME_JSON_SOURCE );

if ( empty( $json ) ) {
WP_CLI::debug( 'Remote file could not be accessed, will use local file as fallback', 'make-pot' );
$file_structure = self::read_json_file( __DIR__ . '/../assets/theme-i18n.json' );
$json = file_get_contents( __DIR__ . '/../assets/theme-i18n.json' );
}

$file_structure = json_decode( $json, true );

if ( JSON_ERROR_NONE !== json_last_error() ) {
WP_CLI::debug( 'Error when decoding theme-i18n.json file', 'make-pot' );
return [];
}

if ( ! is_array( $file_structure ) ) {
return [];
}

return self::extract_paths_to_translate( $file_structure );
}

Expand Down

0 comments on commit b3bede0

Please sign in to comment.