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 Entitlement API Proxy #2

Merged
merged 1 commit into from
Oct 2, 2024
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
110 changes: 110 additions & 0 deletions includes/EntitlementsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace NewfoldLabs\WP\Module\WPSolutions;

use NewfoldLabs\WP\Module\Data\HiiveConnection;
use WP_Error;
use WP_HTTP_Response;
use WP_REST_Controller;
use WP_REST_Response;
use WP_REST_Server;

/**
* Class EntitlementsApi
*/
class EntitlementsApi {

/**
* Transient name where data is stored.
*/
const TRANSIENT = 'newfold_solutions';

/**
* Hiive API endpoint for fetching site entitlements.
*/
const HIIVE_API_ENTITLEMENTS_ENDPOINT = 'sites/v1/entitlements';

/**
* Instance of the HiiveConnection class.
*
* @var HiiveConnection
*/
private $hiive;

/**
* EntitilementsApi constructor.
*
* @param HiiveConnection $hiive Instance of the HiiveConnection class.
*/
public function __construct( HiiveConnection $hiive ) {
$this->hiive = $hiive;
$this->namespace = 'newfold-solutions/v1';
$this->rest_base = '/entitlements';
}

/**
* Register Entitlement routes.
*/
public function register_routes() {

// Add route for fetching entitlements
register_rest_route(
$this->namespace,
$this->rest_base,
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => function () {
return current_user_can( 'read' );
},
)
);
}

/**
* Set the transient where entitlements are stored (6 Hours).
*
* @param array $data
* @param float|int $expiration Transient expiration.
*/
public function setTransient( $data, $expiration = 21600 ) {
set_transient( self::TRANSIENT, $data, $expiration );
}

/**
* Get entitlements of a site.
*
*
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items() {

$entitlements = get_transient( self::TRANSIENT );

if ( false === $entitlements ) {

$args = array(
'method' => 'GET'
);

$hiive_response = $this->hiive->hiive_request( self::HIIVE_API_ENTITLEMENTS_ENDPOINT, array(), $args );

if ( is_wp_error( $hiive_response ) ) {
return new \WP_REST_Response( $hiive_response->get_error_message(), 401 );
}

$status_code = wp_remote_retrieve_response_code( $hiive_response );

if ( 200 !== $status_code ) {
return new \WP_REST_Response( wp_remote_retrieve_response_message( $hiive_response ), $status_code );
}

$entitlements = json_decode( wp_remote_retrieve_body( $hiive_response ) );
$this->setTransient( $entitlements );

}

return new \WP_REST_Response( $entitlements, 201 );
}

}
12 changes: 12 additions & 0 deletions includes/WPSolutions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use NewfoldLabs\WP\ModuleLoader\Container;
use NewfoldLabs\WP\Module\WPSolutions\I18nService;
use NewfoldLabs\WP\Module\Data\HiiveConnection;

/**
* Manages all the functionalities for the module.
Expand All @@ -26,6 +27,7 @@ public function __construct( Container $container ) {
$this->container = $container;

do_action( 'qm/debug', 'Hello from the Solutions module!' );
add_action( 'rest_api_init', array( $this, 'init_entitilements_apis' ) );
}

/**
Expand Down Expand Up @@ -72,4 +74,14 @@ public function register_assets() {
}
}

/**
* Initialize the Entitilement API Controller.
*/
public function init_entitilements_apis(): void {
$hiive = new HiiveConnection();

$entitlements_api = new EntitlementsApi( $hiive );
$entitlements_api->register_routes();
}

}