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

Add a fallback to check Hiive when missing a customer ID from cdata #15

Merged
merged 5 commits into from
Nov 18, 2022
Merged
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
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