autoship_get_default_client()
(src/api.php) Used to retrieve the QPilot client by scope (merchant vs customer). The default scope may be overridden using the ' autoship_default_client_token_scope' filter. It takes an empty $scope as a parameter and returns the new QPilot client.
Function
/**
* Retrieves a new instance of the QPilot Client class
* using the current Autoship settings
* @param string $scope Optional. A scope override for the token
* @param string $source Optional. The owner of the Client ( 'Customer' or 'Merchant' )
* @return QPilotClient
*/
function autoship_get_default_client( $scope = '' ) {
if ( empty( $scope ) )
$scope = apply_filters( 'autoship_default_client_token_scope', 'merchant' );
// Check token expiration & regenerate if needed.
$token_created_at = autoship_get_token_created_at();
$token_expires_in = autoship_get_token_expires_in();
$one_week = 604800;
if ( ( $token_created_at + $token_expires_in - $one_week ) < time() ) {
try {
// Generate a fresh access token
autoship_refresh_token_auth();
} catch (Exception $e) {
// Ignore and move on
}
}
$customer_id = get_current_user_id();
// Create a new Client instance.
$client = new QPilotClient();
// Now we check for the token scope
$admin = autoship_rights_checker( 'autoship_client_token_scope', array( 'edit_pages' ) );
// Set source to Customer or Merchant based on caps
if ( !$admin )
$client->set_source( 'Customer' );
// If not an admin but customer then let's set customer scope
if ( !$admin && ( 'merchant' != $scope ) && $customer_id ){
try {
$token = $client->generate_customer_access_token( $customer_id, autoship_get_client_secret() );
$client->set_token_auth( $token->tokenBearerAuth );
} catch ( Exception $e ) {
autoship_log_entry( __( 'Autoship Orders', 'autoship' ), sprintf( '%d Generate customer access token failed for customer %d. Additional Details: %s', $e->getCode(), $customer_id, $e->getMessage() ) );
}
}
return $client;
}