Use plugin icons from the local "assets" directory.

Props to @timwiel for the suggestion and the original implementation. See  #461. 

This change applies to plugins (but not themes) that are updated from a VCS hosting provider like GitHub. Previously, the plugin author would have had to use a filter to provide icons. Now they can just put them in a subdirectory named "assets" instead. Icon file names and extensions should match those described here:
https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons

Note that the update checker will use icons from the installed version, not from the VCS repository.
This commit is contained in:
Yahnis Elsts 2021-09-27 20:25:50 +03:00
parent a5dd2bb951
commit 8a8788d965
1 changed files with 44 additions and 0 deletions

View File

@ -46,6 +46,7 @@ if ( !class_exists('Puc_v4p11_Vcs_PluginUpdateChecker') ):
$info->slug = $this->slug;
$this->setInfoFromHeader($this->package->getPluginHeader(), $info);
$this->setIconsFromLocalAssets($info);
//Pick a branch or tag.
$updateSource = $api->chooseReference($this->branch);
@ -183,6 +184,49 @@ if ( !class_exists('Puc_v4p11_Vcs_PluginUpdateChecker') ):
}
}
/**
* Add icons from the currently installed version to a Plugin Info object.
*
* The icons should be in a subdirectory named "assets". Supported image formats
* and file names are described here:
* @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
*
* @param Puc_v4p11_Plugin_Info $pluginInfo
*/
protected function setIconsFromLocalAssets($pluginInfo) {
$assetDirectory = $this->package->getAbsoluteDirectoryPath() . DIRECTORY_SEPARATOR . 'assets';
if ( !is_dir($assetDirectory) ) {
return;
}
$assetBaseUrl = trailingslashit(plugins_url('', $assetDirectory . '/imaginary.file'));
$filesToKeys = array(
'icon.svg' => 'svg',
'icon-256x256.png' => '2x',
'icon-256x256.jpg' => '2x',
'icon-128x128.png' => '1x',
'icon-128x128.jpg' => '1x',
);
$icons = array();
foreach ($filesToKeys as $fileName => $key) {
$fullIconPath = $assetDirectory . DIRECTORY_SEPARATOR . $fileName;
if ( !isset($icons[$key]) && is_file($fullIconPath) ) {
$icons[$key] = $assetBaseUrl . $fileName;
}
}
if ( !empty($icons) ) {
//The "default" key seems to be used only as last-resort fallback in WP core (5.8/5.9),
//but we'll set it anyway in case some code somewhere needs it.
reset($icons);
$firstKey = key($icons);
$icons['default'] = $icons[$firstKey];
$pluginInfo->icons = $icons;
}
}
public function setBranch($branch) {
$this->branch = $branch;
return $this;