From f9b06dbec81cb50a04495503cd3d6d7269b2c3de Mon Sep 17 00:00:00 2001 From: David Ryan Date: Fri, 29 Jul 2022 12:12:22 -0700 Subject: [PATCH 1/5] Add options for providing customer data responses --- includes/Customer.php | 71 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/includes/Customer.php b/includes/Customer.php index 8c82f06..ed0e1a9 100644 --- a/includes/Customer.php +++ b/includes/Customer.php @@ -10,24 +10,85 @@ */ class Customer { + /** + * Normalized, weekly-cached. + * + * @var string + */ + private static $transient = 'bh_cdata'; + + /** + * Provided option. + * + * @var string + */ + private static $provided_guapi = 'bh_cdata_guapi'; + + /** + * Provided option. + * + * @var string + */ + private static $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': + $provided = \get_option( self::get_cdata_key_by_path( $path ) ); + if ( + ! empty( $provided ) + && is_string( $provided ) + && is_object( $decoded = json_decode( $provided ) ) + ) { + $provided = $decoded; + } + 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-details': + return self::$provided_guapi; + case '/onboarding-info': + return self::$provided_mole; + } + } + /** * Connect to API with token via AccessToken Class in Bluehost Plugin * @@ -40,6 +101,12 @@ public static function connect( $path ) { return; } + $provided = self::provided( $path ); + + if ( false !== $provided ) { + return $provided; + } + // refresh token if needed AccessToken::maybe_refresh_token(); From ab8e324b6eba965d32f31ff8f10fdce1fa9830ae Mon Sep 17 00:00:00 2001 From: David Ryan Date: Fri, 29 Jul 2022 12:15:04 -0700 Subject: [PATCH 2/5] add delete provided option to prevent duplication --- includes/Customer.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/Customer.php b/includes/Customer.php index ed0e1a9..4bba130 100644 --- a/includes/Customer.php +++ b/includes/Customer.php @@ -60,13 +60,15 @@ public static function provided( $path ) { switch( $path ) { case '/onboarding-info': case '/hosting-account-info': - $provided = \get_option( self::get_cdata_key_by_path( $path ) ); + $key = self::get_cdata_key_by_path( $path ); + $provided = \get_option( ); if ( ! empty( $provided ) && is_string( $provided ) && is_object( $decoded = json_decode( $provided ) ) ) { $provided = $decoded; + \delete_option( $key ); } break; } From be6c8d802e64d2e09b44e610a2b0eb5587898c08 Mon Sep 17 00:00:00 2001 From: David Ryan Date: Fri, 29 Jul 2022 12:17:07 -0700 Subject: [PATCH 3/5] missing key --- includes/Customer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Customer.php b/includes/Customer.php index 4bba130..0d4c36e 100644 --- a/includes/Customer.php +++ b/includes/Customer.php @@ -61,7 +61,7 @@ public static function provided( $path ) { case '/onboarding-info': case '/hosting-account-info': $key = self::get_cdata_key_by_path( $path ); - $provided = \get_option( ); + $provided = \get_option( $key ); if ( ! empty( $provided ) && is_string( $provided ) From f490c2ff13e6a76b9c2b840fb9f02aeda78fea44 Mon Sep 17 00:00:00 2001 From: David Ryan Date: Fri, 29 Jul 2022 12:55:39 -0700 Subject: [PATCH 4/5] fix endpoint match --- includes/Customer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Customer.php b/includes/Customer.php index 0d4c36e..5749d5f 100644 --- a/includes/Customer.php +++ b/includes/Customer.php @@ -84,7 +84,7 @@ public static function provided( $path ) { */ private static function get_cdata_key_by_path( $path ) { switch( $path ) { - case '/hosting-account-details': + case '/hosting-account-info': return self::$provided_guapi; case '/onboarding-info': return self::$provided_mole; From 7147336da17e99a3e20b442202916f85f8838c6d Mon Sep 17 00:00:00 2001 From: David Ryan Date: Tue, 2 Aug 2022 08:17:13 -0700 Subject: [PATCH 5/5] change to class constants --- includes/Customer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/Customer.php b/includes/Customer.php index 5749d5f..e36806f 100644 --- a/includes/Customer.php +++ b/includes/Customer.php @@ -15,21 +15,21 @@ class Customer { * * @var string */ - private static $transient = 'bh_cdata'; + private const TRANSIENT = 'bh_cdata'; /** * Provided option. * * @var string */ - private static $provided_guapi = 'bh_cdata_guapi'; + private const PROVIDED_GUAPI = 'bh_cdata_guapi'; /** * Provided option. * * @var string */ - private static $provided_mole = 'bh_cdata_mole'; + private const PROVIDED_MOLE = 'bh_cdata_mole'; /** * Prepare customer data @@ -37,13 +37,13 @@ class Customer { * @return array of customer data */ public static function collect() { - $bh_cdata = Transient::get( self::$transient ); + $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( self::$transient, $bh_cdata, WEEK_IN_SECONDS ); + Transient::set( self::TRANSIENT, $bh_cdata, WEEK_IN_SECONDS ); } return $bh_cdata; @@ -85,9 +85,9 @@ public static function provided( $path ) { private static function get_cdata_key_by_path( $path ) { switch( $path ) { case '/hosting-account-info': - return self::$provided_guapi; + return self::PROVIDED_GUAPI; case '/onboarding-info': - return self::$provided_mole; + return self::PROVIDED_MOLE; } }