github-checker.php 13 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
<?php

if ( !class_exists('PucGitHubChecker_3_0', false) ):

class PucGitHubChecker_3_0 extends PluginUpdateChecker_3_0 {
	/**
	 * @var string GitHub username.
	 */
	protected $userName;
	/**
	 * @var string GitHub repository name.
	 */
	protected $repositoryName;

	/**
	 * @var string Either a fully qualified repository URL, or just "user/repo-name".
	 */
	protected $repositoryUrl;

	/**
	 * @var string The branch to use as the latest version. Defaults to "master".
	 */
	protected $branch;

	/**
	 * @var string GitHub authentication token. Optional.
	 */
	protected $accessToken;

	public function __construct(
		$repositoryUrl,
		$pluginFile,
		$branch = 'master',
		$checkPeriod = 12,
		$optionName = '',
		$muPluginFile = ''
	) {

		$this->repositoryUrl = $repositoryUrl;
		$this->branch = empty($branch) ? 'master' : $branch;

		$path = @parse_url($repositoryUrl, PHP_URL_PATH);
		if ( preg_match('@^/?(?P<username>[^/]+?)/(?P<repository>[^/#?&]+?)/?$@', $path, $matches) ) {
			$this->userName = $matches['username'];
			$this->repositoryName = $matches['repository'];
		} else {
			throw new InvalidArgumentException('Invalid GitHub repository URL: "' . $repositoryUrl . '"');
		}

		parent::__construct($repositoryUrl, $pluginFile, '', $checkPeriod, $optionName, $muPluginFile);
	}

	/**
	 * Retrieve details about the latest plugin version from GitHub.
	 *
	 * @param array $unusedQueryArgs Unused.
	 * @return PluginInfo
	 */
	public function requestInfo($unusedQueryArgs = array()) {
		$info = new PluginInfo_3_0();
		$info->filename = $this->pluginFile;
		$info->slug = $this->slug;
		$info->sections = array();

		$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;
		if ( $this->branch === 'master' ) {
			//Use the latest release.
			$release = $this->getLatestRelease();
			if ( $release !== null ) {
				$ref = $release->tag_name;
				$info->version = ltrim($release->tag_name, 'v'); //Remove the "v" prefix from "v1.2.3".
				$info->last_updated = $release->created_at;
				$info->download_url = $release->zipball_url;

				if ( !empty($release->body) ) {
					$info->sections['changelog'] = $this->parseMarkdown($release->body);
				}
				if ( isset($release->assets[0]) ) {
					$info->downloaded = $release->assets[0]->download_count;
				}
			} else {
				//Failing that, use the tag with the highest version number.
				$tag = $this->getLatestTag();
				if ( $tag !== null ) {
					$ref = $tag->name;
					$info->version = $tag->name;
					$info->download_url = $tag->zipball_url;
				}
			}
		}

		if ( empty($info->download_url) ) {
			$info->download_url = $this->buildArchiveDownloadUrl($ref);
		} else if ( !empty($this->accessToken) ) {
			$info->download_url = add_query_arg('access_token', $this->accessToken, $info->download_url);
		}

		//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 = $this->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() ) {
			$this->setInfoFromRemoteReadme($ref, $info);
		}

		//The changelog might be in a separate file.
		if ( empty($info->sections['changelog']) ) {
			$info->sections['changelog'] = $this->getRemoteChangelog($ref);
			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 main plugin file and use it as the "last_updated" date.
			//It's reasonable to assume that every update will change the version number in that file.
			$latestCommit = $this->getLatestCommit($mainPluginFile, $ref);
			if ( $latestCommit !== null ) {
				$info->last_updated = $latestCommit->commit->author->date;
			}
		}

		$info = apply_filters('puc_request_info_result-' . $this->slug, $info, null);
		return $info;
	}

	/**
	 * Get the latest release from GitHub.
	 *
	 * @return StdClass|null
	 */
	protected function getLatestRelease() {
		$releases = $this->api('/repos/:user/:repo/releases');
		if ( is_wp_error($releases) || !is_array($releases) || !isset($releases[0]) ) {
			return null;
		}

		$latestRelease = $releases[0];
		return $latestRelease;
	}

	/**
	 * Get the tag that looks like the highest version number.
	 *
	 * @return StdClass|null
	 */
	protected function getLatestTag() {
		$tags = $this->api('/repos/:user/:repo/tags');

		if ( is_wp_error($tags) || empty($tags) || !is_array($tags) ) {
			return null;
		}

		usort($tags, array($this, 'compareTagNames')); //Sort from highest to lowest.
		return $tags[0];
	}

	/**
	 * Compare two GitHub tags as if they were version number.
	 *
	 * @param string $tag1
	 * @param string $tag2
	 * @return int
	 */
	protected function compareTagNames($tag1, $tag2) {
		if ( !isset($tag1->name) ) {
			return 1;
		}
		if ( !isset($tag2->name) ) {
			return -1;
		}
		return -version_compare($tag1->name, $tag2->name);
	}

	/**
	 * Get the latest commit that changed the specified file.
	 *
	 * @param string $filename
	 * @param string $ref Reference name (e.g. branch or tag).
	 * @return StdClass|null
	 */
	protected function getLatestCommit($filename, $ref = 'master') {
		$commits = $this->api(
			'/repos/:user/:repo/commits',
			array(
				'path' => $filename,
				'sha' => $ref,
			)
		);
		if ( !is_wp_error($commits) && is_array($commits) && isset($commits[0]) ) {
			return $commits[0];
		}
		return null;
	}

	protected function getRemoteChangelog($ref = '') {
		$filename = $this->getChangelogFilename();
		if ( empty($filename) ) {
			return null;
		}

		$changelog = $this->getRemoteFile($filename, $ref);
		if ( $changelog === null ) {
			return null;
		}
		return $this->parseMarkdown($changelog);
	}

	protected function getChangelogFilename() {
		$pluginDirectory = dirname($this->pluginAbsolutePath);
		if ( empty($this->pluginAbsolutePath) || !is_dir($pluginDirectory) || ($pluginDirectory === '.') ) {
			return null;
		}

		$possibleNames = array('CHANGES.md', 'CHANGELOG.md', 'changes.md', 'changelog.md');
		$files = scandir($pluginDirectory);
		$foundNames = array_intersect($possibleNames, $files);

		if ( !empty($foundNames) ) {
			return reset($foundNames);
		}
		return null;
	}

	/**
	 * Convert Markdown to HTML.
	 *
	 * @param string $markdown
	 * @return string
	 */
	protected function parseMarkdown($markdown) {
		if ( !class_exists('Parsedown', false) ) {
			require_once(dirname(__FILE__) . '/vendor/Parsedown.php');
		}

		$instance = Parsedown::instance();
		return $instance->text($markdown);
	}

	/**
	 * Perform a GitHub API request.
	 *
	 * @param string $url
	 * @param array $queryParams
	 * @return mixed|WP_Error
	 */
	protected function api($url, $queryParams = array()) {
		$variables = array(
			'user' => $this->userName,
			'repo' => $this->repositoryName,
		);
		foreach ($variables as $name => $value) {
			$url = str_replace('/:' . $name, '/' . urlencode($value), $url);
		}
		$url = 'https://api.github.com' . $url;

		if ( !empty($this->accessToken) ) {
			$queryParams['access_token'] = $this->accessToken;
		}
		if ( !empty($queryParams) ) {
			$url = add_query_arg($queryParams, $url);
		}

		$response = wp_remote_get($url, array('timeout' => 10));
		if ( is_wp_error($response) ) {
			return $response;
		}

		$code = wp_remote_retrieve_response_code($response);
		$body = wp_remote_retrieve_body($response);
		if ( $code === 200 ) {
			$document = json_decode($body);
			return $document;
		}

		return new WP_Error(
			'puc-github-http-error',
			'GitHub API error. HTTP status: ' . $code
		);
	}

	/**
	 * Set the access token that will be used to make authenticated GitHub API requests.
	 *
	 * @param string $accessToken
	 */
	public function setAccessToken($accessToken) {
		$this->accessToken = $accessToken;
	}

	/**
	 * Get the contents of a file from a specific branch or tag.
	 *
	 * @param string $path File name.
	 * @param string $ref
	 * @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error.
	 */
	protected function getRemoteFile($path, $ref = 'master') {
		$apiUrl = '/repos/:user/:repo/contents/' . $path;
		$response = $this->api($apiUrl, array('ref' => $ref));

		if ( is_wp_error($response) || !isset($response->content) || ($response->encoding !== 'base64') ) {
			return null;
		}
		return base64_decode($response->content);
	}

	/**
	 * Parse plugin metadata from the header comment.
	 * This is basically a simplified version of the get_file_data() function from /wp-includes/functions.php.
	 *
	 * @param $content
	 * @return array
	 */
	protected function getFileHeader($content) {
		$headers = array(
			'Name' => 'Plugin Name',
			'PluginURI' => 'Plugin URI',
			'Version' => 'Version',
			'Description' => 'Description',
			'Author' => 'Author',
			'AuthorURI' => 'Author URI',
			'TextDomain' => 'Text Domain',
			'DomainPath' => 'Domain Path',
			'Network' => 'Network',

			//The newest WordPress version that this plugin requires or has been tested with.
			//We support several different formats for compatibility with other libraries.
			'Tested WP' => 'Tested WP',
			'Requires WP' => 'Requires WP',
			'Tested up to' => 'Tested up to',
			'Requires at least' => 'Requires at least',
		);

		$content = str_replace("\r", "\n", $content); //Normalize line endings.
		$results = array();
		foreach ($headers as $field => $name) {
			$success = preg_match('/^[ \t\/*#@]*' . preg_quote($name, '/') . ':(.*)$/mi', $content, $matches);
			if ( ($success === 1) && $matches[1] ) {
				$results[$field] = _cleanup_header_comment($matches[1]);
			} else {
				$results[$field] = '';
			}
		}

		return $results;
	}

	/**
	 * Copy plugin metadata from a file header to a PluginInfo object.
	 *
	 * @param array $fileHeader
	 * @param PluginInfo_3_0 $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 ( !isset($pluginInfo->sections) ) {
			$pluginInfo->sections = array();
		}
		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 PluginInfo_3_0 $pluginInfo
	 */
	protected function setInfoFromRemoteReadme($ref, $pluginInfo) {
		$readmeTxt = $this->getRemoteFile('readme.txt', $ref);
		if ( empty($readmeTxt) ) {
			return;
		}

		$readme = $this->parseReadme($readmeTxt);

		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];
		}
	}

	protected function parseReadme($content) {
		if ( !class_exists('PucReadmeParser', false) ) {
			require_once(dirname(__FILE__) . '/vendor/readme-parser.php');
		}
		$parser = new PucReadmeParser();
		return $parser->parse_readme_contents($content);
	}

	/**
	 * 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');
	}

	/**
	 * Generate a URL to download a ZIP archive of the specified branch/tag/etc.
	 *
	 * @param string $ref
	 * @return string
	 */
	protected function buildArchiveDownloadUrl($ref = 'master') {
		$url = sprintf(
			'https://api.github.com/repos/%1$s/%2$s/zipball/%3$s',
			urlencode($this->userName),
			urlencode($this->repositoryName),
			urlencode($ref)
		);
		if ( !empty($this->accessToken) ) {
			$url = add_query_arg('access_token', $this->accessToken, $url);
		}
		return $url;
	}
}

endif;