From 2a713b0b39a6585a06eb2e89a8002289021389c4 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 19 Oct 2016 10:26:31 +0000 Subject: [PATCH] Upgrade/Install: Refresh update counts after page load. By enqueuing the updates script in the footer and passing the number of available updates to it after page load, the update bubbles will be more accurate. Props ocean90, swissspidy. Fixes #13071. git-svn-id: https://develop.svn.wordpress.org/trunk@38827 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-ms-themes-list-table.php | 3 +- .../includes/class-wp-plugins-list-table.php | 1 + src/wp-admin/js/updates.js | 139 +++++++++++------- src/wp-admin/menu.php | 2 +- src/wp-admin/network/menu.php | 2 +- src/wp-admin/themes.php | 4 + src/wp-admin/update-core.php | 23 +++ src/wp-includes/script-loader.php | 2 +- 8 files changed, 115 insertions(+), 61 deletions(-) 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 '

' . __( 'Update Plugins' ) . '

'; echo ''; echo ''; + + wp_localize_script( 'updates', '_wpUpdatesItemCounts', array( + 'totals' => wp_get_update_data(), + ) ); + include(ABSPATH . 'wp-admin/admin-footer.php'); } elseif ( 'do-theme-upgrade' == $action ) { @@ -700,6 +714,11 @@ if ( 'upgrade-core' == $action ) { wp_get_update_data(), + ) ); + include(ABSPATH . 'wp-admin/admin-footer.php'); } elseif ( 'do-translation-upgrade' == $action ) { @@ -720,6 +739,10 @@ if ( 'upgrade-core' == $action ) { $upgrader = new Language_Pack_Upgrader( new Language_Pack_Upgrader_Skin( compact( 'url', 'nonce', 'title', 'context' ) ) ); $result = $upgrader->bulk_upgrade(); + wp_localize_script( 'updates', '_wpUpdatesItemCounts', array( + 'totals' => wp_get_update_data(), + ) ); + require_once( ABSPATH . 'wp-admin/admin-footer.php' ); } else { diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index c8ec2c5938..874f5e7733 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -611,7 +611,7 @@ function wp_default_scripts( &$scripts ) { 'ays' => __('Are you sure you want to install this plugin?') ) ); - $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery', 'wp-util', 'wp-a11y' ) ); + $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery', 'wp-util', 'wp-a11y' ), false, 1 ); did_action( 'init' ) && $scripts->localize( 'updates', '_wpUpdatesSettings', array( 'ajax_nonce' => wp_create_nonce( 'updates' ), 'l10n' => array(