Skip to content

Commit

Permalink
Add the ability to show a plugin icon on the "Dashboard -> Updates" p…
Browse files Browse the repository at this point in the history
…age.

Usage: Add the following entry to your JSON file:

"icons" : {
	"1x" : "http://example.com/assets/icon-128x128.png",
	"2x" : "http://example.com/assets/icon-256x256.png",
	"svg": "http://example.com/assets/icon.svg"
} 

All of the keys are optional. They should point to different versions of the same icon:
- "1x": a 128x128 px image.
- "2x": a 256x256 px image for HiDPI screens.
- "svg": a vector version of the icon.

See WordPress.org for more information on supported icon sizes and image formats:  
https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons

Suggested in #153
  • Loading branch information
YahnisElsts committed Nov 21, 2017
1 parent 150d3d8 commit 585f60d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
7 changes: 5 additions & 2 deletions Puc/v4p3/Plugin/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class Puc_v4p3_Plugin_Info extends Puc_v4p3_Metadata {
public $version;
public $homepage;
public $sections = array();
public $download_url;

public $banners;
public $icons = array();
public $translations = array();
public $download_url;

public $author;
public $author_homepage;
Expand Down Expand Up @@ -51,8 +53,9 @@ public static function fromJson($json){
return null;
}

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

return $instance;
}
Expand Down
25 changes: 22 additions & 3 deletions Puc/v4p3/Plugin/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class Puc_v4p3_Plugin_Update extends Puc_v4p3_Update {
public $homepage;
public $upgrade_notice;
public $tested;
public $icons = array();
public $filename; //Plugin filename relative to the plugins directory.

protected static $extraFields = array(
'id', 'homepage', 'tested', 'upgrade_notice', 'filename',
'id', 'homepage', 'tested', 'upgrade_notice', 'icons', 'filename',
);

/**
Expand Down Expand Up @@ -72,18 +73,36 @@ protected function getFieldNames() {
*
* @return object
*/
public function toWpFormat(){
public function toWpFormat() {
$update = parent::toWpFormat();

$update->id = $this->id;
$update->url = $this->homepage;
$update->tested = $this->tested;
$update->plugin = $this->filename;

if ( !empty($this->upgrade_notice) ){
if ( !empty($this->upgrade_notice) ) {
$update->upgrade_notice = $this->upgrade_notice;
}

if ( !empty($this->icons) && is_array($this->icons) ) {
//This should be an array with up to 4 keys: 'svg', '1x', '2x' and 'default'.
//Docs: https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
$icons = array_intersect_key(
$this->icons,
array('svg' => true, '1x' => true, '2x' => true, 'default' => true)
);
if ( !empty($icons) ) {
$update->icons = $icons;

//It appears that the 'default' icon isn't used anywhere in WordPress 4.9,
//but lets set it just in case a future release needs it.
if ( !isset($update->icons['default']) ) {
$update->icons['default'] = current($update->icons);
}
}
}

return $update;
}
}
Expand Down
5 changes: 5 additions & 0 deletions examples/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"changelog": "(Recommended) Changelog. <p>This section will be displayed by default when the user clicks 'View version x.y.z details'.</p>",
"custom_section": "This is a custom section labeled 'Custom Section'."
},

"icons" : {
"1x" : "http://w-shadow.com/files/external-update-example/assets/icon-128x128.png",
"2x" : "http://w-shadow.com/files/external-update-example/assets/icon-256x256.png"
},

"banners": {
"low": "http://w-shadow.com/files/external-update-example/assets/banner-772x250.png",
Expand Down

0 comments on commit 585f60d

Please sign in to comment.