plugin-update-checker/Puc/v4/BitBucket/PluginUpdateChecker.php

197 lines
6.3 KiB
PHP

<?php
if ( !class_exists('Puc_v4_BitBucket_PluginUpdateChecker') ):
class Puc_v4_BitBucket_PluginUpdateChecker extends Puc_v4_Plugin_UpdateChecker {
/**
* @var string
*/
protected $branch = 'master';
/**
* @var Puc_v4_BitBucket_Api
*/
protected $api;
protected $credentials = array();
public function requestInfo($queryArgs = array()) {
$api = $this->api = new Puc_v4_BitBucket_Api($this->metadataUrl, $this->credentials);
$info = new Puc_v4_Plugin_Info();
$info->filename = $this->pluginFile;
$info->slug = $this->slug;
$this->setInfoFromHeader($this->getPluginHeader(), $info);
//Figure out which reference (tag or branch) we'll use to get the latest version of the plugin.
$ref = $this->branch;
$foundVersion = false;
//Check if there's a "Stable tag: v1.2.3" header that points to a valid tag.
$remoteReadme = $api->getRemoteReadme($this->branch);
if ( !empty($remoteReadme['stable_tag']) ) {
$tag = $api->getTag($remoteReadme['stable_tag']);
if ( ($tag !== null) && isset($tag->name) ) {
$ref = $tag->name;
$info->version = ltrim($tag->name, 'v');
$info->last_updated = $tag->target->date;
$foundVersion = true;
}
}
//Look for version-like tags.
if ( ($this->branch === 'master') && !$foundVersion ) {
$tag = $api->getLatestTag();
if ( ($tag !== null) && isset($tag->name) ) {
$ref = $tag->name;
$info->version = ltrim($tag->name, 'v');
$info->last_updated = $tag->target->date;
$foundVersion = true;
}
}
//If all else fails, use the specified branch itself.
if ( !$foundVersion ) {
$ref = $this->branch;
}
$info->download_url = trailingslashit($this->metadataUrl) . 'get/' . $ref . '.zip';
//Get headers from the main plugin file in this branch/tag. Its "Version" header and other metadata
//are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
$mainPluginFile = basename($this->pluginFile);
$remotePlugin = $api->getRemoteFile($mainPluginFile, $ref);
if ( !empty($remotePlugin) ) {
$remoteHeader = $this->getFileHeader($remotePlugin);
$this->setInfoFromHeader($remoteHeader, $info);
}
//Try parsing readme.txt. If it's formatted according to WordPress.org standards, it will contain
//a lot of useful information like the required/tested WP version, changelog, and so on.
if ( $this->readmeTxtExistsLocally() || !empty($remoteReadme) ) {
$this->setInfoFromRemoteReadme($ref, $info);
}
//The changelog might be in a separate file.
if ( empty($info->sections['changelog']) ) {
$info->sections['changelog'] = $api->getRemoteChangelog($ref, dirname($this->getAbsolutePath()));
if ( empty($info->sections['changelog']) ) {
$info->sections['changelog'] = __('There is no changelog available.', 'plugin-update-checker');
}
}
if ( empty($info->last_updated) ) {
//Fetch the latest commit that changed the tag or branch and use it as the "last_updated" date.
$latestCommitTime = $api->getLatestCommitTime($ref);
if ( $latestCommitTime !== null ) {
$info->last_updated = $latestCommitTime;
}
}
$info = apply_filters($this->getUniqueName('request_info_result'), $info, null);
return $info;
}
public function setAuthentication($credentials) {
$this->credentials = array_merge(
array(
'consumer_key' => '',
'consumer_secret' => '',
),
$credentials
);
}
/**
* Check if the currently installed version has a readme.txt file.
*
* @return bool
*/
protected function readmeTxtExistsLocally() {
$pluginDirectory = dirname($this->pluginAbsolutePath);
if ( empty($this->pluginAbsolutePath) || !is_dir($pluginDirectory) || ($pluginDirectory === '.') ) {
return false;
}
return is_file($pluginDirectory . '/readme.txt');
}
/**
* Copy plugin metadata from a file header to a Plugin Info object.
*
* @param array $fileHeader
* @param Puc_v4_Plugin_Info $pluginInfo
*/
protected function setInfoFromHeader($fileHeader, $pluginInfo) {
$headerToPropertyMap = array(
'Version' => 'version',
'Name' => 'name',
'PluginURI' => 'homepage',
'Author' => 'author',
'AuthorName' => 'author',
'AuthorURI' => 'author_homepage',
'Requires WP' => 'requires',
'Tested WP' => 'tested',
'Requires at least' => 'requires',
'Tested up to' => 'tested',
);
foreach ($headerToPropertyMap as $headerName => $property) {
if ( isset($fileHeader[$headerName]) && !empty($fileHeader[$headerName]) ) {
$pluginInfo->$property = $fileHeader[$headerName];
}
}
if ( !empty($fileHeader['Description']) ) {
$pluginInfo->sections['description'] = $fileHeader['Description'];
}
}
/**
* Copy plugin metadata from the remote readme.txt file.
*
* @param string $ref GitHub tag or branch where to look for the readme.
* @param Puc_v4_Plugin_Info $pluginInfo
*/
protected function setInfoFromRemoteReadme($ref, $pluginInfo) {
$readme = $this->api->getRemoteReadme($ref);
if ( empty($readme) ) {
return;
}
if ( isset($readme['sections']) ) {
$pluginInfo->sections = array_merge($pluginInfo->sections, $readme['sections']);
}
if ( !empty($readme['tested_up_to']) ) {
$pluginInfo->tested = $readme['tested_up_to'];
}
if ( !empty($readme['requires_at_least']) ) {
$pluginInfo->requires = $readme['requires_at_least'];
}
if ( isset($readme['upgrade_notice'], $readme['upgrade_notice'][$pluginInfo->version]) ) {
$pluginInfo->upgrade_notice = $readme['upgrade_notice'][$pluginInfo->version];
}
}
public function getUpdate() {
$update = parent::getUpdate();
//Add authentication data to download URLs. Since OAuth signatures incorporate
//timestamps, we have to do this immediately before inserting the update. Otherwise
//authentication could fail due to a stale timestamp.
if ( isset($update, $update->download_url) && !empty($update->download_url) && !empty($this->credentials) ) {
if ( !empty($this->credentials['consumer_key']) ) {
$oauth = new Puc_v4_OAuthSignature(
$this->credentials['consumer_key'],
$this->credentials['consumer_secret']
);
$update->download_url = $oauth->sign($update->download_url);
}
}
return $update;
}
}
endif;