Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15 from bluehost/add-hiive-customer-id-fallback
Browse files Browse the repository at this point in the history
Add a fallback to check Hiive when missing a customer ID from cdata
  • Loading branch information
earnjam authored Nov 18, 2022
2 parents a634c3f + 88534ba commit 67a308d
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions includes/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use Bluehost\AccessToken;
use Bluehost\SiteMeta;
use NewfoldLabs\WP\Module\Data\Helpers\Transient;
use NewfoldLabs\WP\Module\Data\HiiveConnection;
use WP_Forge\Helpers\Arr;
use WP_Forge\Helpers\Str;

/**
* Helper class for gathering and formatting customer data
Expand Down Expand Up @@ -69,21 +72,47 @@ public static function collect() {
if ( self::is_stale() ) {
self::refresh_data();
}


if ( empty( $data['customer_id'] ) && ! Str::contains(site_url(), 'temp.domains') ) {
$response = wp_remote_get(
NFD_HIIVE_URL . '/sites/v1/customer',
array(
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(),
),
)
);
if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( $data && is_array( $data ) ) {
if ( ! empty( $data ) ) {
$data['customer_id'] = Arr::get( $data, 'customer_id' );
update_option( self::CUST_DATA, $data );
} else {
delete_option( self::CUST_DATA );
self::refresh_data();
}
}
}
}

return $data; // return data
}

// If no option found, check for transient value
// Get legacy data from Transient value
// Get legacy data from Transient value
if ( empty( $data ) ) {
$data = Transient::get( self::CUST_DATA );

// check if transient data is malformed
if ( $data &&
is_array( $data ) &&
(
(
! array_key_exists( 'signup_date', $data ) ||
! array_key_exists( 'plan_subtype', $data )
! array_key_exists( 'plan_subtype', $data )
)
) {
$data = array();
Expand Down Expand Up @@ -111,7 +140,7 @@ public static function collect() {
*
*/
private static function refresh_data() {

// get account info
$guapi = self::get_account_info();

Expand Down Expand Up @@ -258,11 +287,11 @@ public static function connect( $path ) {

/**
* Checks if the expiration option has passed
*
*
* @return bool
*/
private static function is_stale() {

// check cdata expiry - return if not yet soft deleted/expired
$expiry = \get_option( self::CUST_DATA_EXP, false );

Expand Down

0 comments on commit 67a308d

Please sign in to comment.