From c2d37a3d654f76358272dbc8edf522c1f7f00ed1 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 26 Jul 2011 18:39:57 +0000 Subject: [PATCH] Consolidate update count code into wp_get_update_data(). Props mitchoyoshitaka. fixes #17694 git-svn-id: http://svn.automattic.com/wordpress/trunk@18468 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/menu.php | 45 +++++++-------------------------------- wp-admin/network/menu.php | 38 ++++++++------------------------- wp-includes/admin-bar.php | 37 ++++++-------------------------- wp-includes/update.php | 42 ++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 97 deletions(-) diff --git a/wp-admin/menu.php b/wp-admin/menu.php index a5859d99be..577077c776 100644 --- a/wp-admin/menu.php +++ b/wp-admin/menu.php @@ -30,45 +30,13 @@ if ( is_multisite() ) { $submenu[ 'index.php' ][5] = array( __('My Sites'), 'read', 'my-sites.php' ); } -if ( ! is_multisite() || is_super_admin() ) { - $plugin_update_count = $theme_update_count = $wordpress_update_count = 0; - - if ( current_user_can( 'update_plugins' ) ) { - $update_plugins = get_site_transient( 'update_plugins' ); - if ( ! empty( $update_plugins->response ) ) - $plugin_update_count = count( $update_plugins->response ); - } - - if ( current_user_can( 'update_themes' ) ) { - $update_themes = get_site_transient( 'update_themes' ); - if ( !empty($update_themes->response) ) - $theme_update_count = count( $update_themes->response ); - } - - if ( current_user_can( 'update_core' ) ) { - $update_wordpress = get_core_updates( array('dismissed' => false) ); - if ( !empty($update_wordpress) && !in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') ) - $wordpress_update_count = 1; - } - - $total_update_count = $plugin_update_count + $theme_update_count + $wordpress_update_count; - $update_title = array(); - if ( $wordpress_update_count ) - $update_title[] = sprintf(__('%d WordPress Update'), $wordpress_update_count); - if ( $plugin_update_count ) - $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $plugin_update_count), $plugin_update_count); - if ( $theme_update_count ) - $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $theme_update_count), $theme_update_count); - - $update_title = !empty($update_title) ? esc_attr(implode(', ', $update_title)) : ''; -} +if ( ! is_multisite() || is_super_admin() ) + $update_data = wp_get_update_data(); if ( ! is_multisite() ) { - $submenu[ 'index.php' ][10] = array( sprintf( __('Updates %s'), "" . number_format_i18n($total_update_count) . "" ), 'update_core', 'update-core.php'); + $submenu[ 'index.php' ][10] = array( sprintf( __('Updates %s'), "" . number_format_i18n($update_data['counts']['total']) . "" ), 'update_core', 'update-core.php'); } -unset($theme_update_count, $wordpress_update_count, $update_themes, $update_plugins, $update_wordpress); - $menu[4] = array( '', 'read', 'separator1', '', 'wp-menu-separator' ); $menu[5] = array( __('Posts'), 'edit_posts', 'edit.php', '', 'open-if-no-js menu-top menu-icon-post', 'menu-posts', 'div' ); @@ -176,9 +144,12 @@ function _add_themes_utility_last() { $menu_perms = get_site_option( 'menu_items', array() ); if ( ! is_multisite() || is_super_admin() || ! empty( $menu_perms['plugins'] ) ) { + if ( ! isset( $update_data ) ) + $update_data = wp_get_update_data(); + $count = ''; if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) - $count = "" . number_format_i18n($plugin_update_count) . ""; + $count = "" . number_format_i18n($update_data['counts']['plugins']) . ""; $menu[65] = array( sprintf( __('Plugins %s'), $count ), 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'div' ); @@ -190,7 +161,7 @@ if ( ! is_multisite() || is_super_admin() || ! empty( $menu_perms['plugins'] ) ) $submenu['plugins.php'][15] = array( _x('Editor', 'plugin editor'), 'edit_plugins', 'plugin-editor.php' ); } } -unset($menu_perms, $update_plugins, $plugin_update_count); +unset($menu_perms, $update_data); if ( current_user_can('list_users') ) $menu[70] = array( __('Users'), 'list_users', 'users.php', '', 'menu-top menu-icon-users', 'menu-users', 'div' ); diff --git a/wp-admin/network/menu.php b/wp-admin/network/menu.php index c33d47a38a..66454827c1 100644 --- a/wp-admin/network/menu.php +++ b/wp-admin/network/menu.php @@ -21,12 +21,10 @@ $menu[10] = array(__('Users'), 'manage_network_users', 'users.php', '', 'menu-to $submenu['users.php'][5] = array( __('All Users'), 'manage_network_users', 'users.php' ); $submenu['users.php'][10] = array( _x('Add New', 'user'), 'create_users', 'user-new.php' ); -if ( current_user_can( 'update_themes' ) ) { - $plugin_update_count = $theme_update_count = $wordpress_update_count = 0; - $update_themes = get_site_transient( 'update_themes' ); - if ( !empty($update_themes->response) ) - $theme_update_count = count( $update_themes->response ); - $menu[15] = array(sprintf( __( 'Themes %s' ), "" . number_format_i18n( $theme_update_count ) . "" ), 'manage_network_themes', 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'div' ); +$update_data = wp_get_update_data(); + +if ( $update_data['counts']['themes'] ) { + $menu[15] = array(sprintf( __( 'Themes %s' ), "" . number_format_i18n( $update_data['counts']['themes'] ) . "" ), 'manage_network_themes', 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'div' ); } else { $menu[15] = array( __( 'Themes' ), 'manage_network_themes', 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'div' ); } @@ -35,10 +33,7 @@ $submenu['themes.php'][10] = array( _x('Add New', 'theme'), 'install_themes', 't $submenu['themes.php'][15] = array( _x('Editor', 'theme editor'), 'edit_themes', 'theme-editor.php' ); if ( current_user_can( 'update_plugins' ) ) { - $update_plugins = get_site_transient( 'update_plugins' ); - if ( !empty($update_plugins->response) ) - $plugin_update_count = count( $update_plugins->response ); - $menu[20] = array( sprintf( __( 'Plugins %s' ), "" . number_format_i18n( $plugin_update_count ) . "" ), 'manage_network_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'div'); + $menu[20] = array( sprintf( __( 'Plugins %s' ), "" . number_format_i18n( $update_data['counts']['plugins'] ) . "" ), 'manage_network_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'div'); } else { $menu[20] = array( __('Plugins'), 'manage_network_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'div' ); } @@ -53,31 +48,16 @@ if ( defined( 'MULTISITE' ) && defined( 'WP_ALLOW_MULTISITE' ) && WP_ALLOW_MULTI $submenu['settings.php'][10] = array( __('Network Setup'), 'manage_network_options', 'setup.php' ); } -if ( current_user_can( 'update_core' ) ) { - $update_wordpress = get_core_updates( array('dismissed' => false) ); - if ( !empty($update_wordpress) && !in_array( $update_wordpress[0]->response, array('development', 'latest') ) ) - $wordpress_update_count = 1; - - $update_count = $plugin_update_count + $theme_update_count + $wordpress_update_count; - $update_title = array(); - if ( $wordpress_update_count ) - $update_title[] = sprintf(__('%d WordPress Update'), $wordpress_update_count); - if ( $plugin_update_count ) - $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $plugin_update_count), $plugin_update_count); - if ( $theme_update_count ) - $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $theme_update_count), $theme_update_count); - - $update_title = !empty($update_title) ? esc_attr(implode(', ', $update_title)) : ''; - - $menu[30] = array( sprintf( __( 'Updates %s' ), "" . number_format_i18n($update_count) . "" ), 'manage_network', 'upgrade.php', '', 'menu-top menu-icon-tools', 'menu-update', 'div' ); +if ( $update_data['counts']['total'] ) { + $menu[30] = array( sprintf( __( 'Updates %s' ), "" . number_format_i18n($update_data['counts']['total']) . "" ), 'manage_network', 'upgrade.php', '', 'menu-top menu-icon-tools', 'menu-update', 'div' ); } else { $menu[30] = array( __( 'Updates' ), 'manage_network', 'upgrade.php', '', 'menu-top menu-icon-tools', 'menu-update', 'div' ); } +unset($update_data); + $submenu[ 'upgrade.php' ][10] = array( __( 'Available Updates' ), 'update_core', 'update-core.php' ); $submenu[ 'upgrade.php' ][15] = array( __( 'Update Network' ), 'manage_network', 'upgrade.php' ); -unset($plugin_update_count, $theme_update_count, $wordpress_update_count, $update_count, $update_title, $update_themes, $update_plugins, $update_wordpress); - $menu[99] = array( '', 'read', 'separator-last', '', 'wp-menu-separator-last' ); diff --git a/wp-includes/admin-bar.php b/wp-includes/admin-bar.php index 1a460e9d15..10c81142dd 100644 --- a/wp-includes/admin-bar.php +++ b/wp-includes/admin-bar.php @@ -323,39 +323,14 @@ function wp_admin_bar_appearance_menu( $wp_admin_bar ) { * @since 3.1.0 */ function wp_admin_bar_updates_menu( $wp_admin_bar ) { - if ( !current_user_can('install_plugins') ) + + $update_data = wp_get_update_data(); + + if ( !$update_data['counts']['total'] ) return; - $plugin_update_count = $theme_update_count = $wordpress_update_count = 0; - $update_plugins = get_site_transient( 'update_plugins' ); - if ( !empty($update_plugins->response) ) - $plugin_update_count = count( $update_plugins->response ); - $update_themes = get_site_transient( 'update_themes' ); - if ( !empty($update_themes->response) ) - $theme_update_count = count( $update_themes->response ); - /* @todo get_core_updates() is only available on admin page loads - $update_wordpress = get_core_updates( array('dismissed' => false) ); - if ( !empty($update_wordpress) && !in_array( $update_wordpress[0]->response, array('development', 'latest') ) ) - $wordpress_update_count = 1; - */ - - $update_count = $plugin_update_count + $theme_update_count + $wordpress_update_count; - - if ( !$update_count ) - return; - - $update_title = array(); - if ( $wordpress_update_count ) - $update_title[] = sprintf(__('%d WordPress Update'), $wordpress_update_count); - if ( $plugin_update_count ) - $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $plugin_update_count), $plugin_update_count); - if ( $theme_update_count ) - $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $theme_update_count), $theme_update_count); - - $update_title = !empty($update_title) ? esc_attr(implode(', ', $update_title)) : ''; - - $update_title = ""; - $update_title .= sprintf( __('Updates %s'), "" . number_format_i18n($update_count) . '' ); + $update_title = ""; + $update_title .= sprintf( __('Updates %s'), "" . number_format_i18n($update_data['counts']['total']) . '' ); $update_title .= ''; $wp_admin_bar->add_menu( array( 'id' => 'updates', 'title' => $update_title, 'href' => network_admin_url( 'update-core.php' ) ) ); diff --git a/wp-includes/update.php b/wp-includes/update.php index 6cb2337f3b..9de040c301 100644 --- a/wp-includes/update.php +++ b/wp-includes/update.php @@ -280,6 +280,48 @@ function wp_update_themes() { set_site_transient( 'update_themes', $new_update ); } +/* + * Collect counts and UI strings for available updates + * + * @since 3.3.0 + * + * @return array + */ +function wp_get_update_data() { + $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 ); + + if ( current_user_can( 'update_plugins' ) ) { + $update_plugins = get_site_transient( 'update_plugins' ); + if ( ! empty( $update_plugins->response ) ) + $counts['plugins'] = count( $update_plugins->response ); + } + + if ( current_user_can( 'update_themes' ) ) { + $update_themes = get_site_transient( 'update_themes' ); + if ( ! empty( $update_themes->response ) ) + $counts['themes'] = count( $update_themes->response ); + } + + if ( function_exists( 'get_core_updates' ) && current_user_can( 'update_core' ) ) { + $update_wordpress = get_core_updates( array('dismissed' => false) ); + if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') ) + $counts['wordpress'] = 1; + } + + $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress']; + $update_title = array(); + if ( $counts['wordpress'] ) + $update_title[] = sprintf(__('%d WordPress Update'), $counts['wordpress']); + if ( $counts['plugins'] ) + $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $counts['plugins']), $counts['plugins']); + if ( $counts['themes'] ) + $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $counts['themes']), $counts['themes']); + + $update_title = ! empty( $update_title ) ? esc_attr( implode( ', ', $update_title ) ) : ''; + + return array( 'counts' => $counts, 'title' => $update_title ); +} + function _maybe_update_core() { include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version