Skip to content

Commit

Permalink
Refactor PluginInfo a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
YahnisElsts committed Jan 8, 2016
1 parent b76fb8b commit c6f4e48
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions plugin-update-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ public static function fromJson($json){
}

//Very, very basic validation.
$valid = isset($apiResponse->name) && !empty($apiResponse->name) && isset($apiResponse->version) && !empty($apiResponse->version);
$valid = isset($apiResponse->name, $apiResponse->version) && !empty($apiResponse->name) && !empty($apiResponse->version);
if ( !$valid ){
trigger_error(
"The plugin metadata file does not contain the required 'name' and/or 'version' keys.",
Expand All @@ -1040,6 +1040,9 @@ public static function fromJson($json){
foreach(get_object_vars($apiResponse) as $key => $value){
$info->$key = $value;
}

//json_decode decodes assoc. arrays as objects. We want it as an array.
$info->sections = (array)$info->sections;

return $info;
}
Expand Down Expand Up @@ -1068,20 +1071,8 @@ public function toWpFormat(){

//Other fields need to be renamed and/or transformed.
$info->download_link = $this->download_url;

if ( !empty($this->author_homepage) ){
$info->author = sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author);
} else {
$info->author = $this->author;
}

if ( is_object($this->sections) ){
$info->sections = get_object_vars($this->sections);
} elseif ( is_array($this->sections) ) {
$info->sections = $this->sections;
} else {
$info->sections = array('description' => '');
}
$info->author = $this->getFormattedAuthor();
$info->sections = array_merge(array('description' => ''), $this->sections);

if ( !empty($this->banners) ) {
//WP expects an array with two keys: "high" and "low". Both are optional.
Expand All @@ -1092,6 +1083,13 @@ public function toWpFormat(){

return $info;
}

protected function getFormattedAuthor() {
if ( !empty($this->author_homepage) ){
return sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author);
}
return $this->author;
}
}

endif;
Expand Down

0 comments on commit c6f4e48

Please sign in to comment.