Skip to content

Commit

Permalink
improve the site classification logic
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed May 9, 2023
1 parent c3839b7 commit 8820b8d
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/Data/Static/site-classification.json

Large diffs are not rendered by default.

68 changes: 0 additions & 68 deletions src/SiteClassification.php

This file was deleted.

72 changes: 72 additions & 0 deletions src/SiteClassification/PrimaryType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace NewfoldLabs\WP\Module\Data\SiteClassification;

/**
* Class PrimaryType
*
* Class that manages the primary site classification type.
*
* @package NewfoldLabs\WP\Module\Data
*/
final class PrimaryType extends Types {
/**
* Name of the site classification primary option.
*
* @var string
*/
public static $primary_option_name = 'nfd_data_site_classification_primary';

/**
* Constructor for PrimaryType.
*
* @param string $refers Indicates what the value refers to, slug(from default slugs)|custom(from a custom input field).
* @param string $value The actual value of the site classification type.
*/
public function __construct( $refers, $value ) {
parent::__construct( self::$primary_option_name, $refers, $value );
}

/**
* Validates the data.
*
* @return boolean
*/
public function validate() {
// If the primary type refers to a custom value (from a user input field) we cannot validate the value.
if ( 'custom' === $this->refers ) {
return true;
}

// Retrieve the data to validate.
$classification = SiteClassification::get();
// Checks if the value is a valid primary type slug.
if ( ! isset( $classification['types'] ) || ! isset( $classification['types'][ $this->value ] ) ) {
return false;
}

return true;
}

/**
* Instantiates a class object from the data stored in the option.
*
* @return PrimaryType|boolean
*/
public static function instantiate_from_option() {
$data = get_option( self::$primary_option_name, false );

if ( ! $data || ! is_array( $data ) || ! isset( $data['refers'] ) || ! isset( $data['value'] ) ) {
delete_option( self::$primary_option_name );
return false;
}

$instance = new static( $data['refers'], $data['value'] );
if ( ! $instance->validate() ) {
delete_option( self::$primary_option_name );
return false;
}

return $instance;
}
}
79 changes: 79 additions & 0 deletions src/SiteClassification/SecondaryType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace NewfoldLabs\WP\Module\Data\SiteClassification;

/**
* Class SecondaryType
*
* Class that manages the secondary site classification type.
*
* @package NewfoldLabs\WP\Module\Data
*/
final class SecondaryType extends Types {
/**
* Name of the site classification secondary option.
*
* @var string
*/
public static $secondary_option_name = 'nfd_data_site_classification_secondary';

/**
* Constructor for the SecondaryType class.
*
* @param string $refers Indicates what the value refers to, slug(from default slugs)|custom(from a custom input field).
* @param string $value The actual value of the site classification type.
*/
public function __construct( $refers, $value ) {
parent::__construct( self::$secondary_option_name, $refers, $value );
}

/**
* Validates the data.
*
* @return boolean
*/
public function validate() {
// If the secondary type refers to a custom value (from a user input field) we cannot validate the value.
if ( 'custom' === $this->refers ) {
return true;
}

// Retrieve the selected primary type.
$primary = PrimaryType::instantiate_from_option();
// If it does not exist, then give benefit of doubt.
if ( ! ( $primary instanceof PrimaryType ) ) {
return true;
}

$classification = SiteClassification::get();
$secondary_types = $classification['types'][ $primary->value ]['secondaryTypes'];
// If secondaryTypes does not exist or the selected slug does not exist then return false.
if ( ! isset( $secondary_types ) || ! isset( $secondary_types[ $this->value ] ) ) {
return false;
}

return true;
}

/**
* Instantiates a class object from the data stored in the option.
*
* @return SecondaryType|boolean
*/
public static function instantiate_from_option() {
$data = get_option( self::$secondary_option_name, false );

if ( ! $data || ! is_array( $data ) || ! isset( $data['refers'] ) || ! isset( $data['value'] ) ) {
delete_option( self::$secondary_option_name );
return false;
}

$instance = new static( $data['refers'], $data['value'] );
if ( ! $instance->validate() ) {
delete_option( self::$secondary_option_name );
return false;
}

return $instance;
}
}
103 changes: 103 additions & 0 deletions src/SiteClassification/SiteClassification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace NewfoldLabs\WP\Module\Data\SiteClassification;

use NewfoldLabs\WP\Module\Data\HiiveWorker;

/**
* Class SiteClassification
*
* Class that handles fetching and caching of site classification data.
*
* @package NewfoldLabs\WP\Module\Data
*/
class SiteClassification {
/**
* Name of the site classification data transient.
*
* @var string
*/
public static $transient_name = 'nfd_data_site_classification';

/**
* Retrieves the site classification data.
*
* @return array
*/
public static function get() {
// Checks the transient for data.
$classification = get_transient( 'nfd_site_classification' );
if ( false !== $classification ) {
return $classification;
}

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

// Fetch data from the static JSON file.
$classification = self::fetch_from_static_file();
if ( ! empty( $classification ) ) {
$classification['static'] = true;
set_transient( self::$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 );
return array();
}

/**
* Fetch site classification data from the CF worker.
*
* @return array
*/
public static function fetch_from_worker() {
$worker = new HiiveWorker( 'site-classification' );
$response = $worker->request(
'GET',
array(
'headers' => array(
'Accept' => 'application/json',
),
)
);

if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return array();
}

$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( ! $data || ! is_array( $data ) ) {
return array();
}

return $data;
}

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

if ( ! file_exists( $filename ) ) {
return array();
}

$data = json_decode( file_get_contents( $filename ), true );
if ( ! $data ) {
return array();
}

return $data;

}
}
Loading

0 comments on commit 8820b8d

Please sign in to comment.