Envato_Theme_Setup_Wizard::envato_market_http_request_args PHP Метод

envato_market_http_request_args() публичный Метод

public envato_market_http_request_args ( $args, $url ) : mixed
$args
$url
Результат mixed Filter the WordPress HTTP call args. We do this to find any queries that are using an expired token from an oAuth bounce login. Since these oAuth tokens only last 1 hour we have to hit up our server again for a refresh of that token before using it on the Envato API. Hacky, but only way to do it.
        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;
        }