Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add site classification helper #16

Merged
merged 5 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Data/Static/site-classification.json

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions src/HiiveWorker.php
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
);
}
}
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