Skip to content

Commit

Permalink
Add locales to site classification
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed Sep 18, 2023
1 parent 1c29bcb commit a0eb1e7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Data/Static/site-classification-en-US.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Data/Static/site-classification-pt-BR.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/Data/Static/site-classification.json

This file was deleted.

39 changes: 26 additions & 13 deletions src/SiteClassification/SiteClassification.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,53 @@ class SiteClassification {
* @return array
*/
public static function get() {
// Get the current locale of the site.
$locale = str_replace( '_', '-', get_locale() );

$transient_name = self::$transient_name . '-' . $locale;
// Checks the transient for data.
$classification = get_transient( self::$transient_name );
$classification = get_transient( $transient_name );
if ( false !== $classification ) {
return $classification;
}

// Fetch data from the worker.
$classification = self::fetch_from_worker();
$classification = self::fetch_from_worker( $locale );
if ( ! empty( $classification ) ) {
set_transient( self::$transient_name, $classification, DAY_IN_SECONDS );
set_transient( $transient_name, $classification, DAY_IN_SECONDS );
return $classification;
}

// Fetch data from the static JSON file.
$classification = self::fetch_from_static_file();
$classification = self::fetch_from_static_file( $locale );
if ( ! empty( $classification ) ) {
$classification['static'] = true;
set_transient( self::$transient_name, $classification, HOUR_IN_SECONDS );
set_transient( $transient_name, $classification, HOUR_IN_SECONDS );
return $classification;
}

// Cache an empty array for an hour if no data could be retrieved.
set_transient( self::$transient_name, array(), HOUR_IN_SECONDS );
set_transient( $transient_name, array(), HOUR_IN_SECONDS );
return array();
}

/**
* Fetch site classification data from the CF worker.
*
* @param string $locale The locale in kebab case.
* @return array
*/
public static function fetch_from_worker() {
public static function fetch_from_worker( $locale = 'en-US' ) {
$worker = new HiiveWorker( 'site-classification' );
$response = $worker->request(
'GET',
array(
'headers' => array(
'Accept' => 'application/json',
),
'body' => array(
'locale' => $locale,
),
)
);

Expand All @@ -81,15 +89,20 @@ public static function fetch_from_worker() {
}

/**
* Fetch site classification data from a static JSON file.
* Fetch site classification data from the static JSON file.
*
* @param string $locale The locale in kebab case.
* @return array
*/
public static function fetch_from_static_file() {
$filename = realpath( __DIR__ . '/../Data/Static/site-classification.json' );

if ( ! file_exists( $filename ) ) {
return array();
public static function fetch_from_static_file( $locale = 'en-US' ) {
$filename = realpath( __DIR__ . "/../Data/Static/site-classification-{$locale}.json" );

// If the file does not exist and the locale is not en-US then default to the en-US file.
if ( ! file_exists( $filename ) && 'en-US' !== $locale ) {
$filename = realpath( __DIR__ . '/../Data/Static/site-classification-en-US.json' );
if ( ! file_exists( $filename ) ) {
return array();
}
}

$data = json_decode( file_get_contents( $filename ), true );
Expand Down

0 comments on commit a0eb1e7

Please sign in to comment.