Simplify VCS-based theme checker.
This commit is contained in:
parent
870901b1f2
commit
e9b377e999
|
|
@ -722,10 +722,12 @@ if ( !class_exists('Puc_v4_UpdateChecker', false) ):
|
|||
* This is basically a simplified version of the get_file_data() function from /wp-includes/functions.php.
|
||||
* It's intended as a utility for subclasses that detect updates by parsing files in a VCS.
|
||||
*
|
||||
* @param string $content File contents.
|
||||
* @param string|null $content File contents.
|
||||
* @return string[]
|
||||
*/
|
||||
public function getFileHeader($content) {
|
||||
$content = (string) $content;
|
||||
|
||||
//WordPress only looks at the first 8 KiB of the file, so we do the same.
|
||||
$content = substr($content, 0, 8192);
|
||||
//Normalize line endings.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
if ( !class_exists('Puc_v4_Utils', false) ):
|
||||
|
||||
class Puc_v4_Utils {
|
||||
/**
|
||||
* Get a value from a nested array or object based on a path.
|
||||
*
|
||||
* @param array|object $array Get an entry from this array.
|
||||
* @param array|string $path A list of array keys in hierarchy order, or a string path like "foo.bar.baz".
|
||||
* @param mixed $default The value to return if the specified path is not found.
|
||||
* @param string $separator Path element separator. Only applies to string paths.
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($array, $path, $default = null, $separator = '.') {
|
||||
if ( is_string($path) ) {
|
||||
$path = explode($separator, $path);
|
||||
}
|
||||
if ( empty($path) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
//Follow the $path into $input as far as possible.
|
||||
$currentValue = $array;
|
||||
$pathExists = true;
|
||||
foreach ($path as $node) {
|
||||
if ( is_array($currentValue) && array_key_exists($node, $currentValue) ) {
|
||||
$currentValue = $currentValue[$node];
|
||||
} else if ( is_object($currentValue) && property_exists($currentValue, $node) ) {
|
||||
$currentValue = $currentValue->$node;
|
||||
} else {
|
||||
$pathExists = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $pathExists ) {
|
||||
return $currentValue;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first array element that is not empty.
|
||||
*
|
||||
* @param array $values
|
||||
* @param mixed|null $default Returns this value if there are no non-empty elements.
|
||||
* @return mixed|null
|
||||
*/
|
||||
public static function findNotEmpty($values, $default = null) {
|
||||
if ( empty($values) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
foreach ($values as $value) {
|
||||
if ( !empty($value) ) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
||||
|
|
@ -39,7 +39,6 @@ if ( !class_exists('Puc_v4_Vcs_ThemeUpdateChecker', false) ):
|
|||
$updateSource = $api->chooseReference($this->branch);
|
||||
if ( $updateSource ) {
|
||||
$ref = $updateSource->name;
|
||||
$update->version = $updateSource->version;
|
||||
$update->download_url = $updateSource->downloadUrl;
|
||||
} else {
|
||||
$ref = $this->branch;
|
||||
|
|
@ -47,24 +46,18 @@ if ( !class_exists('Puc_v4_Vcs_ThemeUpdateChecker', false) ):
|
|||
|
||||
//Get headers from the main stylesheet 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.
|
||||
$remoteStylesheet = $api->getRemoteFile('style.css', $ref);
|
||||
if ( !empty($remoteStylesheet) ) {
|
||||
$remoteHeader = $this->getFileHeader($remoteStylesheet);
|
||||
if ( !empty($remoteHeader['Version']) ) {
|
||||
$update->version = $remoteHeader['Version'];
|
||||
}
|
||||
if ( !empty($remoteHeader['ThemeURI']) ) {
|
||||
$update->details_url = $remoteHeader['ThemeURI'];
|
||||
}
|
||||
}
|
||||
$remoteHeader = $this->getFileHeader($api->getRemoteFile('style.css', $ref));
|
||||
$update->version = Puc_v4_Utils::findNotEmpty(array(
|
||||
$remoteHeader['Version'],
|
||||
Puc_v4_Utils::get($updateSource, 'version'),
|
||||
));
|
||||
|
||||
//The details URL defaults to the Theme URI header or the repository URL.
|
||||
if ( empty($update->details_url) ) {
|
||||
$update->details_url = $this->theme->get('ThemeURI');
|
||||
}
|
||||
if ( empty($update->details_url) ) {
|
||||
$update->details_url = $this->metadataUrl;
|
||||
}
|
||||
$update->details_url = Puc_v4_Utils::findNotEmpty(array(
|
||||
$remoteHeader['ThemeURI'],
|
||||
$this->theme->get('ThemeURI'),
|
||||
$this->metadataUrl,
|
||||
));
|
||||
|
||||
if ( empty($update->version) ) {
|
||||
//It looks like we didn't find a valid update after all.
|
||||
|
|
|
|||
Loading…
Reference in New Issue