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 #13 from bluehost/fix/cdata-throttling-on-ajax-req…
Browse files Browse the repository at this point in the history
…uests

Fix/cdata throttling on ajax requests
  • Loading branch information
wpscholar authored Sep 22, 2022
2 parents e8d52d9 + 10f3b22 commit 4ae41c9
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions includes/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Customer {
*
* @var string
*/
private const CUST_DATA_EXP = 'bh_cdata_expiry';
private const CUST_DATA_EXP = 'bh_cdata_expiration';

/**
* Retry throttle.
Expand Down Expand Up @@ -73,9 +73,8 @@ public static function collect() {
return $data; // return data
}

// Legacy - deal with Transient

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

Expand All @@ -87,19 +86,19 @@ public static function collect() {
! array_key_exists( 'plan_subtype', $data )
)
) {
$data = false;
$data = array();
Transient::delete( self::CUST_DATA ); // delete malformed transient data
}

// valid data found as transient
if ( $data ) {
if ( ! empty( $data ) ) {
// migrate transient data to option
self::save_data( $data );
Transient::delete( self::CUST_DATA ); // delete transient when data migrated to option
}
}

// still empty - no option, and no transient
// data is still empty (not found as option, or valid transient), Fetch it
if ( empty( $data ) ) {
self::refresh_data();
}
Expand All @@ -112,8 +111,11 @@ public static function collect() {
*
*/
private static function refresh_data() {

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

// bail if no data
if ( empty( $guapi ) ) {
return;
}
Expand Down Expand Up @@ -215,10 +217,16 @@ public static function connect( $path ) {
return $provided;
}

// bail if throttled
if ( self::is_throttled() ) {
return;
}

// bail to avoid throttling customer endpoint when can not access token
if ( ! AccessToken::should_refresh_token() ) {
return;
}

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

Expand Down

0 comments on commit 4ae41c9

Please sign in to comment.