Fixed a potential bug with multiple plugins using GitHub release assets.

To allow WP to download a release asset from GitHub, the update checker needs to add an "Accept: application/octet-stream" header to the HTTP request. We use the "http_request_args" filter for that. 

Previously, we used a static variable to ensure that the filter callback is added only once. However, in PHP, static variables defined in a method are shared by all instances of the class that the method belongs to. This means that if one plugin enables release assets, adds a "http_request_args" filter, and sets ``$filterAdded` to `true`, then the next plugin that enables release assets won't add its own filter because the variable will already be `true`.

Fixed by using an instance variable (`$downloadFilterAdded`) instead of a static variable.
This commit is contained in:
Yahnis Elsts 2019-10-01 14:35:16 +03:00
parent 969f8adf21
commit a3ff7ada08
1 changed files with 7 additions and 3 deletions

View File

@ -37,6 +37,11 @@ if ( !class_exists('Puc_v4p8_Vcs_GitHubApi', false) ):
*/
protected $assetApiBaseUrl = null;
/**
* @var bool
*/
private $downloadFilterAdded = false;
public function __construct($repositoryUrl, $accessToken = null) {
$path = parse_url($repositoryUrl, PHP_URL_PATH);
if ( preg_match('@^/?(?P<username>[^/]+?)/(?P<repository>[^/#?&]+?)/?$@', $path, $matches) ) {
@ -382,10 +387,9 @@ if ( !class_exists('Puc_v4p8_Vcs_GitHubApi', false) ):
* @return bool
*/
public function addHttpRequestFilter($result) {
static $filterAdded = false;
if ( $this->releaseAssetsEnabled && !$filterAdded && $this->isAuthenticationEnabled() ) {
if ( $this->releaseAssetsEnabled && !$this->downloadFilterAdded && $this->isAuthenticationEnabled() ) {
add_filter('http_request_args', array($this, 'setReleaseDownloadHeader'), 10, 2);
$filterAdded = true;
$this->downloadFilterAdded = true;
}
return $result;
}