From 8a8788d9651f89b8e26436949d55ddd4e9938839 Mon Sep 17 00:00:00 2001 From: Yahnis Elsts Date: Mon, 27 Sep 2021 20:25:50 +0300 Subject: [PATCH] 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. --- Puc/v4p11/Vcs/PluginUpdateChecker.php | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Puc/v4p11/Vcs/PluginUpdateChecker.php b/Puc/v4p11/Vcs/PluginUpdateChecker.php index bc6d94a..e76e901 100644 --- a/Puc/v4p11/Vcs/PluginUpdateChecker.php +++ b/Puc/v4p11/Vcs/PluginUpdateChecker.php @@ -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;