diff --git a/src/wp-admin/includes/class-wp-ms-themes-list-table.php b/src/wp-admin/includes/class-wp-ms-themes-list-table.php index 82eafee2ad..0f3865a985 100644 --- a/src/wp-admin/includes/class-wp-ms-themes-list-table.php +++ b/src/wp-admin/includes/class-wp-ms-themes-list-table.php @@ -150,7 +150,8 @@ class WP_MS_Themes_List_Table extends WP_List_Table { $total_this_page = $totals[ $status ]; wp_localize_script( 'updates', '_wpUpdatesItemCounts', array( - 'totals' => $totals, + 'themes' => $totals, + 'totals' => wp_get_update_data(), ) ); if ( $orderby ) { diff --git a/src/wp-admin/includes/class-wp-plugins-list-table.php b/src/wp-admin/includes/class-wp-plugins-list-table.php index 7e2a3d339b..6aa8788aec 100644 --- a/src/wp-admin/includes/class-wp-plugins-list-table.php +++ b/src/wp-admin/includes/class-wp-plugins-list-table.php @@ -253,6 +253,7 @@ class WP_Plugins_List_Table extends WP_List_Table { wp_localize_script( 'updates', '_wpUpdatesItemCounts', array( 'plugins' => $js_plugins, + 'totals' => wp_get_update_data(), ) ); if ( ! $orderby ) { diff --git a/src/wp-admin/js/updates.js b/src/wp-admin/js/updates.js index f321eae91c..a28bbb50f5 100644 --- a/src/wp-admin/js/updates.js +++ b/src/wp-admin/js/updates.js @@ -21,10 +21,12 @@ * @param {Array} settings.plugins.inactive Base names of inactive plugins. * @param {Array} settings.plugins.upgrade Base names of plugins with updates available. * @param {Array} settings.plugins.recently_activated Base names of recently activated plugins. - * @param {object=} settings.totals Plugin/theme status information or null. - * @param {number} settings.totals.all Amount of all plugins or themes. - * @param {number} settings.totals.upgrade Amount of plugins or themes with updates available. - * @param {number} settings.totals.disabled Amount of disabled themes. + * @param {object=} settings.themes Plugin/theme status information or null. + * @param {number} settings.themes.all Amount of all themes. + * @param {number} settings.themes.upgrade Amount of themes with updates available. + * @param {number} settings.themes.disabled Amount of disabled themes. + * @param {object=} settings.totals Combined information for available update counts. + * @param {number} settings.totals.count Holds the amount of available updates. */ (function( $, wp, settings ) { var $document = $( document ); @@ -260,6 +262,70 @@ } }; + /** + * Refreshes update counts everywhere on the screen. + * + * @since 4.7.0 + */ + wp.updates.refreshCount = function() { + var $adminBarUpdates = $( '#wp-admin-bar-updates' ), + $dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ), + $pluginsNavMenuUpdateCount = $( 'a[href="plugins.php"] .update-plugins' ), + $appearanceNavMenuUpdateCount = $( 'a[href="themes.php"] .update-plugins' ), + itemCount; + + $adminBarUpdates.find( '.ab-item' ).removeAttr( 'title' ); + $adminBarUpdates.find( '.ab-label' ).text( settings.totals.counts.total ); + + // Remove the update count from the toolbar if it's zero. + if ( 0 === settings.totals.counts.total ) { + $adminBarUpdates.find( '.ab-label' ).parents( 'li' ).remove(); + } + + // Update the "Updates" menu item. + $dashboardNavMenuUpdateCount.each( function( index, element ) { + element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.total ); + } ); + if ( settings.totals.counts.total > 0 ) { + $dashboardNavMenuUpdateCount.find( '.update-count' ).text( settings.totals.counts.total ); + } else { + $dashboardNavMenuUpdateCount.remove(); + } + + // Update the "Plugins" menu item. + $pluginsNavMenuUpdateCount.each( function( index, element ) { + element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.plugins ); + } ); + if ( settings.totals.counts.total > 0 ) { + $pluginsNavMenuUpdateCount.find( '.plugin-count' ).text( settings.totals.counts.plugins ); + } else { + $pluginsNavMenuUpdateCount.remove(); + } + + // Update the "Appearance" menu item. + $appearanceNavMenuUpdateCount.each( function( index, element ) { + element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.themes ); + } ); + if ( settings.totals.counts.total > 0 ) { + $appearanceNavMenuUpdateCount.find( '.theme-count' ).text( settings.totals.counts.themes ); + } else { + $appearanceNavMenuUpdateCount.remove(); + } + + // Update list table filter navigation. + if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { + itemCount = settings.totals.counts.plugins; + } else if ( 'themes' === pagenow || 'themes-network' === pagenow ) { + itemCount = settings.totals.counts.themes; + } + + if ( itemCount > 0 ) { + $( '.subsubsub .upgrade .count' ).text( '(' + itemCount + ')' ); + } else { + $( '.subsubsub .upgrade' ).remove(); + } + }; + /** * Decrements the update counts throughout the various menus. * @@ -272,62 +338,15 @@ * Can be 'plugin', 'theme'. */ wp.updates.decrementCount = function( type ) { - var $adminBarUpdates = $( '#wp-admin-bar-updates' ), - $dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ), - count = $adminBarUpdates.find( '.ab-label' ).text(), - $menuItem, $itemCount, itemCount; - - count = parseInt( count, 10 ) - 1; - - if ( count < 0 || isNaN( count ) ) { - return; - } - - $adminBarUpdates.find( '.ab-item' ).removeAttr( 'title' ); - $adminBarUpdates.find( '.ab-label' ).text( count ); - - // Remove the update count from the toolbar if it's zero. - if ( ! count ) { - $adminBarUpdates.find( '.ab-label' ).parents( 'li' ).remove(); - } - - // Update the "Updates" menu item. - $dashboardNavMenuUpdateCount.each( function( index, element ) { - element.className = element.className.replace( /count-\d+/, 'count-' + count ); - } ); - - $dashboardNavMenuUpdateCount.removeAttr( 'title' ); - $dashboardNavMenuUpdateCount.find( '.update-count' ).text( count ); + settings.totals.counts.total = Math.max( --settings.totals.counts.total, 0 ); if ( 'plugin' === type ) { - $menuItem = $( '#menu-plugins' ); - $itemCount = $menuItem.find( '.plugin-count' ); + settings.totals.counts.plugins = Math.max( --settings.totals.counts.plugins, 0 ); } else if ( 'theme' === type ) { - $menuItem = $( '#menu-appearance' ); - $itemCount = $menuItem.find( '.theme-count' ); + settings.totals.counts.themes = Math.max( --settings.totals.counts.themes, 0 ); } - // Decrement the counter of the other menu items. - if ( $itemCount ) { - itemCount = $itemCount.eq( 0 ).text(); - itemCount = parseInt( itemCount, 10 ) - 1; - } - - if ( itemCount < 0 || isNaN( itemCount ) ) { - return; - } - - if ( itemCount > 0 ) { - $( '.subsubsub .upgrade .count' ).text( '(' + itemCount + ')' ); - - $itemCount.text( itemCount ); - $menuItem.find( '.update-plugins' ).each( function( index, element ) { - element.className = element.className.replace( /count-\d+/, 'count-' + itemCount ); - } ); - } else { - $( '.subsubsub .upgrade' ).remove(); - $menuItem.find( '.update-plugins' ).remove(); - } + wp.updates.refreshCount( type ); }; /** @@ -1251,7 +1270,7 @@ $themeRows.css( { backgroundColor: '#faafaa' } ).fadeOut( 350, function() { var $views = $( '.subsubsub' ), $themeRow = $( this ), - totals = settings.totals, + totals = settings.themes, deletedRow = wp.template( 'item-deleted-row' ); if ( ! $themeRow.hasClass( 'plugin-update-tr' ) ) { @@ -1689,6 +1708,12 @@ $pluginSearch = $( '.plugins-php .wp-filter-search' ), $pluginInstallSearch = $( '.plugin-install-php .wp-filter-search' ); + settings = _.extend( settings, window._wpUpdatesItemCounts || {} ); + + if ( settings.totals ) { + wp.updates.refreshCount(); + } + /* * Whether a user needs to submit filesystem credentials. * @@ -2412,4 +2437,4 @@ */ $( window ).on( 'beforeunload', wp.updates.beforeunload ); } ); -})( jQuery, window.wp, _.extend( window._wpUpdatesSettings, window._wpUpdatesItemCounts || {} ) ); +})( jQuery, window.wp, window._wpUpdatesSettings ); diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index 6c0b38f723..2fd146bd1a 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -38,7 +38,7 @@ if ( ! is_multisite() ) { $cap = 'update_plugins'; else $cap = 'update_themes'; - $submenu[ 'index.php' ][10] = array( sprintf( __('Updates %s'), "" . number_format_i18n($update_data['counts']['total']) . "" ), $cap, 'update-core.php'); + $submenu[ 'index.php' ][10] = array( sprintf( __('Updates %s'), "" . number_format_i18n($update_data['counts']['total']) . "" ), $cap, 'update-core.php'); unset( $cap ); } diff --git a/src/wp-admin/network/menu.php b/src/wp-admin/network/menu.php index 8b5f00269b..1cb22f2223 100644 --- a/src/wp-admin/network/menu.php +++ b/src/wp-admin/network/menu.php @@ -14,7 +14,7 @@ $submenu['index.php'][0] = array( __( 'Home' ), 'read', 'index.php' ); $update_data = wp_get_update_data(); if ( $update_data['counts']['total'] ) { - $submenu['index.php'][10] = array( sprintf( __( 'Updates %s' ), "" . number_format_i18n( $update_data['counts']['total'] ) . "" ), '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' ); } else { $submenu['index.php'][10] = array( __( 'Updates' ), 'update_core', 'update-core.php' ); } diff --git a/src/wp-admin/themes.php b/src/wp-admin/themes.php index 3be8fba9af..e2c0372efb 100644 --- a/src/wp-admin/themes.php +++ b/src/wp-admin/themes.php @@ -490,4 +490,8 @@ wp_print_request_filesystem_credentials_modal(); wp_print_admin_notice_templates(); wp_print_update_row_templates(); +wp_localize_script( 'updates', '_wpUpdatesItemCounts', array( + 'totals' => wp_get_update_data(), +) ); + require( ABSPATH . 'wp-admin/admin-footer.php' ); diff --git a/src/wp-admin/update-core.php b/src/wp-admin/update-core.php index 2c1149181d..c66c29cefd 100644 --- a/src/wp-admin/update-core.php +++ b/src/wp-admin/update-core.php @@ -618,6 +618,11 @@ if ( 'upgrade-core' == $action ) { */ do_action( 'core_upgrade_preamble' ); echo ''; + + wp_localize_script( 'updates', '_wpUpdatesItemCounts', array( + 'totals' => wp_get_update_data(), + ) ); + include(ABSPATH . 'wp-admin/admin-footer.php'); } elseif ( 'do-core-upgrade' == $action || 'do-core-reinstall' == $action ) { @@ -642,6 +647,10 @@ if ( 'upgrade-core' == $action ) { if ( isset( $_POST['upgrade'] ) ) do_core_upgrade($reinstall); + wp_localize_script( 'updates', '_wpUpdatesItemCounts', array( + 'totals' => wp_get_update_data(), + ) ); + include(ABSPATH . 'wp-admin/admin-footer.php'); } elseif ( 'do-plugin-upgrade' == $action ) { @@ -670,6 +679,11 @@ if ( 'upgrade-core' == $action ) { echo '