Skip to content

Instantly share code, notes, and snippets.

@webaware
Last active October 24, 2024 03:45
Show Gist options
  • Save webaware/4a4f2dca44b1c10fb9a8fe85a5f90f40 to your computer and use it in GitHub Desktop.
Save webaware/4a4f2dca44b1c10fb9a8fe85a5f90f40 to your computer and use it in GitHub Desktop.
Add download links to WordPress plugins with updates available
<?php
/*
Plugin Name: Plugin Download
Plugin URI: https://gist.github.com/webaware/4a4f2dca44b1c10fb9a8fe85a5f90f40
Update URI: plugin-download
Description: add download links to plugins with updates available
Version: 0.0.2
Author: WebAware
Author URI: https://shop.webaware.com.au/
*/
/*
copyright (c) 2024 WebAware Pty Ltd (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace webawareau\plugin_download;
if (!defined('ABSPATH')) {
exit;
}
/**
* maybe add a download link to plugin row meta on the Plugins admin page
*/
add_filter('plugin_row_meta', function(array $row_meta, string $plugin_file, array $plugin_data) : array {
if (empty($plugin_data['new_version']) || empty($plugin_data['Version']) || empty($plugin_data['package'])) {
return $row_meta;
}
if (version_compare($plugin_data['new_version'], $plugin_data['Version'], '>')) {
$row_meta[] = sprintf('<a href="%s">Download update package</a>', esc_url($plugin_data['package']));
}
return $row_meta;
}, 10, 3);
/**
* add menu item for showing theme updates
*/
add_action('admin_menu', function() : void {
add_theme_page('Updates', 'Updates', 'update_themes', 'theme-packages', __NAMESPACE__ . '\\list_theme_updates');
});
/**
* list the available theme updates
*/
function list_theme_updates() : void {
$themes = [];
$updates_transient = get_site_transient('update_themes');
if (isset($updates_transient->response)) {
$themes = $updates_transient->response;
}
echo '<h1>Theme update packages</h1>';
if ($themes) {
echo '<table class="theme-updates wp-list-table widefat">';
echo '<thead><tr><th>Theme</th><th>Version</th><th>Update package</th></tr></thead>';
echo '<tbody>';
foreach ($themes as $theme) {
printf('<tr><td>%s</td><td>%s</td><td>', esc_html($theme['theme']), esc_html($theme['new_version']));
if (empty($theme['package'])) {
echo 'no package';
}
else {
printf('<a href="%s">download package</a>', esc_url($theme['package']));
}
echo '</td></tr>';
}
echo '</tbody>';
echo '</table>';
}
else {
echo '<p>No updates.</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment