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 #6 from bluehost/add/provided-customer-data
Browse files Browse the repository at this point in the history
Add options for providing customer data responses
  • Loading branch information
0aveRyan authored Aug 2, 2022
2 parents 375ed3b + 7147336 commit 9573873
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions includes/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,87 @@
*/
class Customer {

/**
* Normalized, weekly-cached.
*
* @var string
*/
private const TRANSIENT = 'bh_cdata';

/**
* Provided option.
*
* @var string
*/
private const PROVIDED_GUAPI = 'bh_cdata_guapi';

/**
* Provided option.
*
* @var string
*/
private const PROVIDED_MOLE = 'bh_cdata_mole';

/**
* Prepare customer data
*
* @return array of customer data
*/
public static function collect() {
$bh_cdata = Transient::get( 'bh_cdata' );
$bh_cdata = Transient::get( self::TRANSIENT );

if ( ! $bh_cdata ) {
$guapi = self::get_account_info();
$mole = array( 'meta' => self::get_onboarding_info() );
$bh_cdata = array_merge( $guapi, $mole );
Transient::set( 'bh_cdata', $bh_cdata, WEEK_IN_SECONDS );
Transient::set( self::TRANSIENT, $bh_cdata, WEEK_IN_SECONDS );
}

return $bh_cdata;
}

/**
* Prepopulate with provided data.
*
* @param string $path of desired API endpoint
* @return object|false of response data in json format
*/
public static function provided( $path ) {
$provided = false;
switch( $path ) {
case '/onboarding-info':
case '/hosting-account-info':
$key = self::get_cdata_key_by_path( $path );
$provided = \get_option( $key );
if (
! empty( $provided )
&& is_string( $provided )
&& is_object( $decoded = json_decode( $provided ) )
) {
$provided = $decoded;
\delete_option( $key );
}
break;
}

return $provided;
}

/**
* Map usersite path to cdata key.
*
* @param string $path
* @return string
*/
private static function get_cdata_key_by_path( $path ) {
switch( $path ) {
case '/hosting-account-info':
return self::PROVIDED_GUAPI;
case '/onboarding-info':
return self::PROVIDED_MOLE;
}
}

/**
* Connect to API with token via AccessToken Class in Bluehost Plugin
*
Expand All @@ -40,6 +103,12 @@ public static function connect( $path ) {
return;
}

$provided = self::provided( $path );

if ( false !== $provided ) {
return $provided;
}

// refresh token if needed
AccessToken::maybe_refresh_token();

Expand Down

0 comments on commit 9573873

Please sign in to comment.