diff --git a/includes/Customer.php b/includes/Customer.php index 8c82f06..e36806f 100644 --- a/includes/Customer.php +++ b/includes/Customer.php @@ -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 * @@ -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();