Skip to content

Instantly share code, notes, and snippets.

@jontey
Last active February 7, 2018 19:44
Show Gist options
  • Save jontey/5492a588a16162dc63ce340344f6c959 to your computer and use it in GitHub Desktop.
Save jontey/5492a588a16162dc63ce340344f6c959 to your computer and use it in GitHub Desktop.
WordPress plugin to show module/theme/core update notifications in menu even when DISALLOW_FILE_MODS is TRUE.
<?php
/*
Plugin Name: Update Notifications Menu
Plugin URI: https://gist.github.com/jontey/5492a588a16162dc63ce340344f6c959
Description: WordPress plugin to show module/theme/core update notifications in menu even when DISALLOW_FILE_MODS is TRUE.
Version: 1.2
Author: Jonathan Tey
Author URI: https://gist.github.com/jontey
*/
/* Copyright 2018 Jonathan Tey
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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
*/
/**
* Register dashboard widget and page
*/
add_action('wp_dashboard_setup', 'wp_update_notifications_dashboard');
add_action('admin_menu', 'wp_update_notifications_menu');
function wordpress_core_has_update() {
$wp_version = get_bloginfo( 'version' );
$cur_wp_version = preg_replace( '/-.*$/', '', $wp_version );
$core_updates = get_core_updates();
if ( !isset($core_updates[0]->response) || 'latest' == $core_updates[0]->response || 'development' == $core_updates[0]->response || version_compare( $core_updates[0]->current, $cur_wp_version, '=') )
return 0;
return 1;
}
function get_updates_count() {
return wordpress_core_has_update() + count(get_theme_updates()) + count(get_plugin_updates());
}
/**
* Adds updates page to menu
*/
function wp_update_notifications_menu() {
$count = get_updates_count();
$notification = '';
if ($count) {
$notification = '<span class="update-plugins"><span class="plugin-count">'.$count.'</span></span>';
}
add_dashboard_page( 'Updates', 'Updates '.$notification, 'activate_plugins', 'updates', 'unm_display_updates', '');
}
/**
* Adds updates widget to dashboard
*/
function wp_update_notifications_dashboard(){
$count = get_updates_count();
if ($count) { // Show only if updates are available
wp_add_dashboard_widget('unm_dashboard_widget', 'Updates', 'unm_display_dashboard');
}
}
/**
* Function to display updates page
* Prints html page
*/
function unm_display_updates() {
unm_list_plugin_updates();
unm_list_theme_updates();
}
function unm_display_dashboard(){
$count = get_updates_count();
echo '<p style="color:#721c24; background-color:#f8d7da; border-color:#f5c6cb;">' . sprintf(__('You have %d updates <a href="?page=updates">pending.</a>'), $count) . '</p>';
}
/**
* Modified version of list_plugin_updates() from wp-admin/update-core.php
*/
function unm_list_plugin_updates() {
$wp_version = get_bloginfo( 'version' );
$cur_wp_version = preg_replace( '/-.*$/', '', $wp_version );
$plugins = get_plugin_updates();
if ( empty( $plugins ) ) {
echo '<h2>' . __( 'Plugins' ) . '</h2>';
echo '<p>' . __( 'Your plugins are all up to date.' ) . '</p>';
return;
}
$core_updates = get_core_updates();
if ( !isset($core_updates[0]->response) || 'latest' == $core_updates[0]->response || 'development' == $core_updates[0]->response || version_compare( $core_updates[0]->current, $cur_wp_version, '=') )
$core_update_version = false;
else
$core_update_version = $core_updates[0]->current;
?>
<h2><?php _e( 'Plugins' ); ?></h2>
<p><?php _e( 'The following plugins have new versions available. To update please contact site administrator.' ); ?></p>
<table class="widefat updates-table" id="update-plugins-table">
<thead>
<tr>
<td class="manage-column check-column"></td>
<td class="manage-column"><label for="plugins-select-all"><?php _e( 'Plugins' ); ?></label></td>
</tr>
</thead>
<tbody class="plugins">
<?php
foreach ( (array) $plugins as $plugin_file => $plugin_data ) {
$plugin_data = (object) _get_plugin_data_markup_translate( $plugin_file, (array) $plugin_data, false, true );
$icon = '<span class="dashicons dashicons-admin-plugins"></span>';
$preferred_icons = array( 'svg', '1x', '2x', 'default' );
foreach ( $preferred_icons as $preferred_icon ) {
if ( ! empty( $plugin_data->update->icons[ $preferred_icon ] ) ) {
$icon = '<img src="' . esc_url( $plugin_data->update->icons[ $preferred_icon ] ) . '" alt="" />';
break;
}
}
// Get plugin compat for running version of WordPress.
if ( isset($plugin_data->update->tested) && version_compare($plugin_data->update->tested, $cur_wp_version, '>=') ) {
$compat = '<br />' . sprintf(__('Compatibility with WordPress %1$s: 100%% (according to its author)'), $cur_wp_version);
} elseif ( isset($plugin_data->update->compatibility->{$cur_wp_version}) ) {
$compat = $plugin_data->update->compatibility->{$cur_wp_version};
$compat = '<br />' . sprintf(__('Compatibility with WordPress %1$s: %2$d%% (%3$d "works" votes out of %4$d total)'), $cur_wp_version, $compat->percent, $compat->votes, $compat->total_votes);
} else {
$compat = '<br />' . sprintf(__('Compatibility with WordPress %1$s: Unknown'), $cur_wp_version);
}
// Get plugin compat for updated version of WordPress.
if ( $core_update_version ) {
if ( isset( $plugin_data->update->tested ) && version_compare( $plugin_data->update->tested, $core_update_version, '>=' ) ) {
$compat .= '<br />' . sprintf( __( 'Compatibility with WordPress %1$s: 100%% (according to its author)' ), $core_update_version );
} elseif ( isset( $plugin_data->update->compatibility->{$core_update_version} ) ) {
$update_compat = $plugin_data->update->compatibility->{$core_update_version};
$compat .= '<br />' . sprintf(__('Compatibility with WordPress %1$s: %2$d%% (%3$d "works" votes out of %4$d total)'), $core_update_version, $update_compat->percent, $update_compat->votes, $update_compat->total_votes);
} else {
$compat .= '<br />' . sprintf(__('Compatibility with WordPress %1$s: Unknown'), $core_update_version);
}
}
// Get the upgrade notice for the new plugin version.
if ( isset($plugin_data->update->upgrade_notice) ) {
$upgrade_notice = '<br />' . strip_tags($plugin_data->update->upgrade_notice);
} else {
$upgrade_notice = '';
}
if ( isset($plugin_data->update->package) ){
$details_url = $plugin_data->update->package;
$details = sprintf(
'<a href="%1$s" class="thickbox open-plugin-details-modal" aria-label="%2$s">%3$s</a>',
esc_url( $details_url ),
/* translators: 1: plugin name, 2: version number */
esc_attr( sprintf( __( 'Download %1$s version %2$s' ), $plugin_data->Name, $plugin_data->update->new_version ) ),
/* translators: %s: plugin version */
sprintf( __( 'Download version %s.' ), $plugin_data->update->new_version )
);
} else {
$details_url = $plugin_data->update->url;
$details = sprintf(
'<a href="%1$s" class="thickbox open-plugin-details-modal" aria-label="%2$s">%3$s</a>',
esc_url( $details_url ),
/* translators: 1: plugin name, 2: version number */
esc_attr( sprintf( __( 'View %1$s homepage' ), $plugin_data->Name, $plugin_data->update->new_version ) ),
/* translators: %s: plugin version */
__( 'Update file not available.' )
);
}
$checkbox_id = "checkbox_" . md5( $plugin_data->Name );
?>
<tr style="<?php is_plugin_inactive($plugin_file)?_e('background-color:#ffd9d9;'):'' ?>">
<td class="check-column">
</td>
<td class="plugin-title"><p>
<?php echo $icon; ?>
<strong><?php echo $plugin_data->Name; ?> <?php is_plugin_inactive($plugin_file)?_e( '(Inactive)' ):'' ?></strong>
<?php
/* translators: 1: plugin version, 2: new version */
printf( __( 'You have version %1$s installed. Update to %2$s.' ),
$plugin_data->Version,
$plugin_data->update->new_version
);
echo ' ' . $details . $compat . $upgrade_notice;
?>
</p></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
/**
* Modified version of list_theme_updates() from wp-admin/update-core.php
*/
function unm_list_theme_updates() {
$themes = get_theme_updates();
if ( empty( $themes ) ) {
echo '<h2>' . __( 'Themes' ) . '</h2>';
echo '<p>' . __( 'Your themes are all up to date.' ) . '</p>';
return;
}
?>
<h2><?php _e( 'Themes' ); ?></h2>
<p><?php _e( 'The following themes have new versions available. To update please contact site administrator.' ); ?></p>
<p><?php printf( __( '<strong>Please Note:</strong> Any customizations you have made to theme files will be lost. Please consider using <a href="%s">child themes</a> for modifications.' ), __( 'https://codex.wordpress.org/Child_Themes' ) ); ?></p>
<table class="widefat updates-table" id="update-themes-table">
<thead>
<tr>
<td class="manage-column check-column"></td>
<td class="manage-column"><label for="themes-select-all"><?php _e( 'Select All' ); ?></label></td>
</tr>
</thead>
<tbody class="plugins">
<?php
foreach ( $themes as $stylesheet => $theme ) {
?>
<tr>
<td class="check-column">
</td>
<td class="plugin-title"><p>
<img src="<?php echo esc_url( $theme->get_screenshot() ); ?>" width="85" height="64" class="updates-table-screenshot" alt="" />
<strong><?php echo $theme->display( 'Name' ); ?></strong>
<?php
/* translators: 1: theme version, 2: new version */
printf( __( 'You have version %1$s installed. Update to %2$s. ' ),
$theme->display( 'Version' ),
$theme->update['new_version']
);
if ( isset($theme->update['package']) ){
$details_url = $theme->update['package'];
printf(
'<a href="%1$s" class="thickbox open-plugin-details-modal" aria-label="%2$s">%3$s</a>',
esc_url( $details_url ),
/* translators: 1: plugin name, 2: version number */
esc_attr( sprintf( __( 'Download %1$s version %2$s' ), $theme->Name, $theme->update['new_version'] ) ),
/* translators: %s: plugin version */
sprintf( __( 'Download version %s.' ), $theme->update['new_version'] )
);
} else {
$details_url = $theme->update['url'];
printf(
'<a href="%1$s" class="thickbox open-plugin-details-modal" aria-label="%2$s">%3$s</a>',
esc_url( $details_url ),
/* translators: 1: plugin name, 2: version number */
esc_attr( sprintf( __( 'View %1$s homepage' ), $theme->Name, $theme->update['new_version'] ) ),
/* translators: %s: plugin version */
__( 'Update file not available.' )
);
}
?>
</p></td>
</tr>
<?php
}
?>
</tbody>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment