public function envato_market_http_request_args($args, $url)
{
if (strpos($url, 'api.envato.com') && function_exists('envato_market')) {
// we have an API request.
// check if it's using an expired token.
if (!empty($args['headers']['Authorization'])) {
$token = str_replace('Bearer ', '', $args['headers']['Authorization']);
if ($token) {
// check our options for a list of active oauth tokens and see if one matches, for this envato username.
$option = envato_market()->get_options();
if ($option && !empty($option['oauth'][$this->envato_username]) && $option['oauth'][$this->envato_username]['access_token'] == $token && $option['oauth'][$this->envato_username]['expires'] < time() + 120) {
// we've found an expired token for this oauth user!
// time to hit up our bounce server for a refresh of this token and update associated data.
$this->_manage_oauth_token($option['oauth'][$this->envato_username]);
$updated_option = envato_market()->get_options();
if ($updated_option && !empty($updated_option['oauth'][$this->envato_username]['access_token'])) {
// hopefully this means we have an updated access token to deal with.
$args['headers']['Authorization'] = 'Bearer ' . $updated_option['oauth'][$this->envato_username]['access_token'];
}
}
}
}
}
return $args;
}