Skip to content

Commit

Permalink
Merge pull request #29 from newfold-labs/add/site-classification-locale
Browse files Browse the repository at this point in the history
Add locales to site classification
  • Loading branch information
circlecube authored Sep 18, 2023
2 parents 1c29bcb + c2768f2 commit 2fc2a4c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
exit;
}

define( 'NFD_DATA_MODULE_VERSION', '2.4.4' );
define( 'NFD_DATA_MODULE_VERSION', '2.4.5' );

if ( function_exists( 'is_admin' ) && is_admin() ) {
$upgrade_handler = new UpgradeHandler(
Expand Down
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: 28 additions & 11 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,24 @@ 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' );
public static function fetch_from_static_file( $locale = 'en-US' ) {
$filename = realpath( __DIR__ . "/../Data/Static/site-classification-{$locale}.json" );

if ( ! file_exists( $filename ) ) {
return array();
if ( 'en-US' === $locale ) {
return array();
}

// If the file does not exist and the locale is not en-US, then default to the en-US file.
$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 2fc2a4c

Please sign in to comment.