How can I update Wordpress plugin with transient by add authorize_token to header

Viewed 136

I am trying to update my WordPress plugins automatically using transient. I have one that works but it adds authorize_token to query params like below code.

        $new_files = $this->github_response['zipball_url']; // Get the ZIP
        ***$package = add_query_arg(array("access_token" => $this->authorize_token), $new_files);***
        $slug = current( explode('/', $this->basename ) ); // Create valid slug

        $plugin = array( // setup our plugin info
          'url' => $this->plugin["PluginURI"],
          'slug' => $slug,
          'package' => $package,
          'new_version' => $this->github_response['tag_name']
        );

        $transient->response[$this->basename] = (object) $plugin;

but i have received email from github that says i should add authorize_token to header.

I'm trying to find solution by google, but i can't find it. How can I fix it?

1 Answers

I figured that you will have to add a filter to the http_request. So when wordpress will try any http request it will go through your filter. Then inside that filter you can check the url and if it is your plugin source url, you can then add your accessToken to the header. this method was in my updater class, and I had setup the class property accessToken and username when initializing the updater.

add_filter( "http_request_args", array( $this, "addHeaders") , 10, 3);

public function addHeaders($parsed_args, $url)
{
    if(empty($parsed_args['headers']))
    {
        $parsed_args['headers'] = [];
    }

    if(strpos($url, "https://api.github.com/repos/{$this->username}/{$this->repo}") !== FALSE)
    {
        $parsed_args['headers']['Authorization'] = "token $this->accessToken";
        error_log("Adding token for : $url");
    }
    else
    {
        error_log("Not adding token for $url");
    }
    return $parsed_args;
}
Related