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 4 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
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
);
}
}
68 changes: 68 additions & 0 deletions src/SiteClassification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace NewfoldLabs\WP\Module\Data;

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 {

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

// Fetch data from the worker.
$classification = $this->fetch_from_worker();

// Cache the data if it is not empty.
if ( ! empty( $classification ) ) {
set_transient( 'nfd_site_classification', $classification, DAY_IN_SECONDS );
}

return $classification;
}

/**
* Fetch site classification data from the worker.
*
* @return array
*/
public 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;
}

}