Skip to content

Commit

Permalink
Merge branch 'main' into PRESS0-1724
Browse files Browse the repository at this point in the history
  • Loading branch information
ramyakrishnai committed Oct 4, 2024
2 parents 9a147b3 + 5947395 commit aba3d03
Show file tree
Hide file tree
Showing 8 changed files with 815 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,28 @@ jobs:
module-repo: ${{ github.repository }}
module-branch: ${{ needs.setup.outputs.branch }}
plugin-repo: 'bluehost/bluehost-wordpress-plugin'
plugin-branch: 'solutions'
sync-npm-package: true
secrets: inherit

hostgator:
name: HostGator Build and Test
needs: setup
uses: newfold-labs/workflows/.github/workflows/module-plugin-test.yml@main
with:
module-repo: ${{ github.repository }}
module-branch: ${{ needs.setup.outputs.branch }}
plugin-repo: 'newfold-labs/wp-plugin-hostgator'
sync-npm-package: true
secrets: inherit
# hostgator:
# name: HostGator Build and Test
# needs: setup
# uses: newfold-labs/workflows/.github/workflows/module-plugin-test.yml@main
# with:
# module-repo: ${{ github.repository }}
# module-branch: ${{ needs.setup.outputs.branch }}
# plugin-repo: 'newfold-labs/wp-plugin-hostgator'
# sync-npm-package: true
# secrets: inherit

crazydomains:
name: Crazy Domains Build and Test
needs: setup
uses: newfold-labs/workflows/.github/workflows/module-plugin-test.yml@main
with:
module-repo: ${{ github.repository }}
module-branch: ${{ needs.setup.outputs.branch }}
plugin-repo: 'newfold-labs/wp-plugin-crazy-domains'
sync-npm-package: true
secrets: inherit
# crazydomains:
# name: Crazy Domains Build and Test
# needs: setup
# uses: newfold-labs/workflows/.github/workflows/module-plugin-test.yml@main
# with:
# module-repo: ${{ github.repository }}
# module-branch: ${{ needs.setup.outputs.branch }}
# plugin-repo: 'newfold-labs/wp-plugin-crazy-domains'
# sync-npm-package: true
# secrets: inherit
File renamed without changes.
File renamed without changes.
File renamed without changes.
47 changes: 31 additions & 16 deletions components/entitlements/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './styles.scss';
import { Container, Title } from '@newfold/ui-component-library';
import { Container, Title, Button } from '@newfold/ui-component-library';

const defaults = {
eventEndpoint: '/newfold-data/v1/events/',
Expand Down Expand Up @@ -43,22 +43,30 @@ const Entitlements = ( { methods, constants, ...props } ) => {
),
} )
.then( ( response ) => {
// check response for data
const r = JSON.parse( response.data );
console.log( r );
let r = response;
// if response is not already json parse it (debug mode)
if ( typeof r === 'string' ) {
console.log( 'Sample entitlements loaded.')
r = JSON.parse( response );
}
// console.log(r);
// check response for proper data
if (
! r.hasOwnProperty( 'categories' ) ||
! r.hasOwnProperty( 'entitlements' )
r.hasOwnProperty( 'solution' ) &&
r.hasOwnProperty( 'solutions' ) &&
r.hasOwnProperty( 'entitlements' ) &&
r.hasOwnProperty( 'categories' )
) {
setIsError( true );
setIsLoading( false );
} else {
setActiveSolution(
validateSolution( r.solution, r.solutions )
);
setEntitlementsCategories(
validateCategories( r.categories, r.entitlements )
);
setIsLoading( false );
} else {
console.log( 'Invalid or malformed entitlements response.' );
setIsError( true );
setIsLoading( false );
}
} ).catch( ( response ) => {
Expand Down Expand Up @@ -142,27 +150,34 @@ const Entitlements = ( { methods, constants, ...props } ) => {
{ category.name }
</Title>
{ renderEntitlementList( category.entitlements ) }
</>
</>
}
</div>;
};

const renderCTAUrl = ( url ) => {
return url.replace('{siteUrl}', window.NewfoldRuntime.base_url);
};

const renderEntitlementList = ( entitlements ) => {
return entitlements.map( ( entitlement ) => (
return entitlements.map( ( entitlement, i ) => (
<div
className={`newfold-entitlement entitlement-${entitlement.slug} entitlement-${entitlement.type}`}
key={ entitlement.basename }
key={ entitlement.basename + i }
>
<img className="entitlement-image" src={entitlement.image.primaryImage} />
<div>
<div className="entitlement-detail">
<Title as="h3" size="3">
{ entitlement.name }
</Title>
<p>{ entitlement.description }</p>
</div>
<a
href={entitlement.cta.url}
>{ entitlement.cta.text }</a>
<Button
as="a"
className="entitlement-cta"
href={ renderCTAUrl( entitlement.cta.url ) }
variant="secondary"
>{ entitlement.cta.text }</Button>
</div>
) );
};
Expand Down
26 changes: 19 additions & 7 deletions components/entitlements/styles.scss
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
.newfold-entitlement {
align-items: center;
border: 1px solid #eee;
display:flex;
flex-direction: row;
padding: 1rem;
margin: 1rem 0;
border: 1px solid #eee;
grid-gap: 1rem;
align-items: center;
justify-content: space-between;
margin: 1rem 0;
padding: 1rem;

.entitlement-image {
height: auto;
max-width: 48px;
}

.entitlement-detail {
display: flex;
flex-grow: 2;
flex-direction: column;
grid-gap: .25rem;
}

img {
width: 48px;
height: 48px;
.entitlement-cta {
text-wrap: nowrap;
}
}
51 changes: 29 additions & 22 deletions includes/EntitlementsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ class EntitlementsApi {
/**
* Hiive API endpoint for fetching site entitlements.
*/
const HIIVE_API_ENTITLEMENTS_ENDPOINT = 'sites/v1/entitlements';
const HIIVE_API_ENTITLEMENTS_ENDPOINT = '/sites/v1/entitlements';

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

/**
* EntitilementsApi constructor.
Expand All @@ -55,7 +57,7 @@ public function register_routes() {
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => function () {
return current_user_can( 'read' );
return current_user_can( 'manage_options' );
},
)
);
Expand All @@ -79,12 +81,11 @@ public function setTransient( $data, $expiration = 21600 ) {
*/
public function get_items() {


// If there is no Hiive connection, bail.
if(! HiiveConnection::is_connected()) {
if( ! HiiveConnection::is_connected()) {
if ( defined('WP_DEBUG') && true === WP_DEBUG ) {
// for debugging - use a local json file rather than hiive entitlement endpoint response
return new WP_REST_Response( array( 'data' => json_decode( file_get_contents( NFD_SOLUTIONS_DIR . '/includes/js/debug.json') ) ), 203 );
return new WP_REST_Response( json_decode( file_get_contents( NFD_SOLUTIONS_DIR . '/includes/js/debug.json' ) ), 218 );
}
return new WP_REST_Response( array( 'message' => 'Not allowed to load entitlements from server.' ), 403 );
}
Expand All @@ -93,28 +94,34 @@ public function get_items() {

if ( false === $entitlements ) {

$args = array(
'method' => 'GET'
$response = wp_remote_get(
NFD_HIIVE_URL . self::HIIVE_API_ENTITLEMENTS_ENDPOINT,
array(
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(),
),
)
);

$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 );

if ( is_wp_error( $response ) ) {
return new WP_REST_Response( array( 'message' => 'An error occurred with the entitlements response.' ), 500 );
}

$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 );

$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if (
$data &&
is_array( $data ) &&
array_key_exists('solutions', $data) &&
array_key_exists('categories', $data)
) {
$entitlements = $data;
}
}

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

}
Loading

0 comments on commit aba3d03

Please sign in to comment.