-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from newfold-labs/add/site-classification-helper
Add site classification helper
- Loading branch information
Showing
6 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace NewfoldLabs\WP\Module\Data; | ||
|
||
/** | ||
* Class HiiveWorker | ||
* | ||
* Base class for Hiive Worker related actions. | ||
* | ||
* @package NewfoldLabs\WP\Module\Data | ||
*/ | ||
class HiiveWorker { | ||
|
||
/** | ||
* The URL of the worker. | ||
* | ||
* @var string | ||
*/ | ||
protected $url; | ||
|
||
/** | ||
* The endpoint of the worker URL. | ||
* | ||
* @var string | ||
*/ | ||
protected $endpoint; | ||
|
||
/** | ||
* HiiveWorker constructor. | ||
* | ||
* @param string $endpoint The endpoint of the worker URL. | ||
*/ | ||
public function __construct( $endpoint ) { | ||
|
||
if ( ! defined( 'NFD_HIIVE_BASE_URL' ) ) { | ||
define( 'NFD_HIIVE_BASE_URL', 'https://hiive.cloud' ); | ||
} | ||
|
||
$this->endpoint = $endpoint; | ||
$this->url = NFD_HIIVE_BASE_URL . '/workers/' . $endpoint; | ||
} | ||
|
||
/** | ||
* Places an HTTP request to the Hiive CF worker. | ||
* | ||
* @param string $method The HTTP request method (GET, POST, ....). | ||
* @param array $args The HTTP request arguments (headers, body, ...) | ||
* @return array | ||
*/ | ||
public function request( $method, $args ) { | ||
$args['method'] = $method; | ||
|
||
return \wp_remote_request( | ||
$this->url, | ||
$args | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
Oops, something went wrong.