Make WordPress Core

Changeset 38827


Ignore:
Timestamp:
10/19/2016 10:26:31 AM (10 years ago)
Author:
swissspidy
Message:

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.

Location:
trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-ms-themes-list-table.php

    r38307 r38827  
    151151
    152152                wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
    153                         'totals' => $totals,
     153                        'themes' => $totals,
     154                        'totals' => wp_get_update_data(),
    154155                ) );
    155156
  • trunk/src/wp-admin/includes/class-wp-plugins-list-table.php

    r38672 r38827  
    254254                wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
    255255                        'plugins' => $js_plugins,
     256                        'totals'  => wp_get_update_data(),
    256257                ) );
    257258
  • trunk/src/wp-admin/js/updates.js

    r38813 r38827  
    2222 * @param {Array}   settings.plugins.upgrade            Base names of plugins with updates available.
    2323 * @param {Array}   settings.plugins.recently_activated Base names of recently activated plugins.
    24  * @param {object=} settings.totals                     Plugin/theme status information or null.
    25  * @param {number}  settings.totals.all                 Amount of all plugins or themes.
    26  * @param {number}  settings.totals.upgrade             Amount of plugins or themes with updates available.
    27  * @param {number}  settings.totals.disabled            Amount of disabled themes.
     24 * @param {object=} settings.themes                     Plugin/theme status information or null.
     25 * @param {number}  settings.themes.all                 Amount of all themes.
     26 * @param {number}  settings.themes.upgrade             Amount of themes with updates available.
     27 * @param {number}  settings.themes.disabled            Amount of disabled themes.
     28 * @param {object=} settings.totals                     Combined information for available update counts.
     29 * @param {number}  settings.totals.count               Holds the amount of available updates.
    2830 */
    2931(function( $, wp, settings ) {
    … …  
    262264
    263265        /**
     266         * Refreshes update counts everywhere on the screen.
     267         *
     268         * @since 4.7.0
     269         */
     270        wp.updates.refreshCount = function() {
     271                var $adminBarUpdates              = $( '#wp-admin-bar-updates' ),
     272                        $dashboardNavMenuUpdateCount  = $( 'a[href="update-core.php"] .update-plugins' ),
     273                        $pluginsNavMenuUpdateCount    = $( 'a[href="plugins.php"] .update-plugins' ),
     274                        $appearanceNavMenuUpdateCount = $( 'a[href="themes.php"] .update-plugins' ),
     275                        itemCount;
     276
     277                $adminBarUpdates.find( '.ab-item' ).removeAttr( 'title' );
     278                $adminBarUpdates.find( '.ab-label' ).text( settings.totals.counts.total );
     279
     280                // Remove the update count from the toolbar if it's zero.
     281                if ( 0 === settings.totals.counts.total ) {
     282                        $adminBarUpdates.find( '.ab-label' ).parents( 'li' ).remove();
     283                }
     284
     285                // Update the "Updates" menu item.
     286                $dashboardNavMenuUpdateCount.each( function( index, element ) {
     287                        element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.total );
     288                } );
     289                if ( settings.totals.counts.total > 0 ) {
     290                        $dashboardNavMenuUpdateCount.find( '.update-count' ).text( settings.totals.counts.total );
     291                } else {
     292                        $dashboardNavMenuUpdateCount.remove();
     293                }
     294
     295                // Update the "Plugins" menu item.
     296                $pluginsNavMenuUpdateCount.each( function( index, element ) {
     297                        element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.plugins );
     298                } );
     299                if ( settings.totals.counts.total > 0 ) {
     300                        $pluginsNavMenuUpdateCount.find( '.plugin-count' ).text( settings.totals.counts.plugins );
     301                } else {
     302                        $pluginsNavMenuUpdateCount.remove();
     303                }
     304
     305                // Update the "Appearance" menu item.
     306                $appearanceNavMenuUpdateCount.each( function( index, element ) {
     307                        element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.themes );
     308                } );
     309                if ( settings.totals.counts.total > 0 ) {
     310                        $appearanceNavMenuUpdateCount.find( '.theme-count' ).text( settings.totals.counts.themes );
     311                } else {
     312                        $appearanceNavMenuUpdateCount.remove();
     313                }
     314
     315                // Update list table filter navigation.
     316                if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
     317                        itemCount = settings.totals.counts.plugins;
     318                } else if ( 'themes' === pagenow || 'themes-network' === pagenow ) {
     319                        itemCount = settings.totals.counts.themes;
     320                }
     321
     322                if ( itemCount > 0 ) {
     323                        $( '.subsubsub .upgrade .count' ).text( '(' + itemCount + ')' );
     324                } else {
     325                        $( '.subsubsub .upgrade' ).remove();
     326                }
     327        };
     328
     329        /**
    264330         * Decrements the update counts throughout the various menus.
    265331         *
    … …  
    273339         */
    274340        wp.updates.decrementCount = function( type ) {
    275                 var $adminBarUpdates             = $( '#wp-admin-bar-updates' ),
    276                         $dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ),
    277                         count                        = $adminBarUpdates.find( '.ab-label' ).text(),
    278                         $menuItem, $itemCount, itemCount;
    279 
    280                 count = parseInt( count, 10 ) - 1;
    281 
    282                 if ( count < 0 || isNaN( count ) ) {
    283                         return;
    284                 }
    285 
    286                 $adminBarUpdates.find( '.ab-item' ).removeAttr( 'title' );
    287                 $adminBarUpdates.find( '.ab-label' ).text( count );
    288 
    289                 // Remove the update count from the toolbar if it's zero.
    290                 if ( ! count ) {
    291                         $adminBarUpdates.find( '.ab-label' ).parents( 'li' ).remove();
    292                 }
    293 
    294                 // Update the "Updates" menu item.
    295                 $dashboardNavMenuUpdateCount.each( function( index, element ) {
    296                         element.className = element.className.replace( /count-\d+/, 'count-' + count );
    297                 } );
    298 
    299                 $dashboardNavMenuUpdateCount.removeAttr( 'title' );
    300                 $dashboardNavMenuUpdateCount.find( '.update-count' ).text( count );
     341                settings.totals.counts.total = Math.max( --settings.totals.counts.total, 0 );
    301342
    302343                if ( 'plugin' === type ) {
    303                         $menuItem  = $( '#menu-plugins' );
    304                         $itemCount = $menuItem.find( '.plugin-count' );
     344                        settings.totals.counts.plugins = Math.max( --settings.totals.counts.plugins, 0 );
    305345                } else if ( 'theme' === type ) {
    306                         $menuItem  = $( '#menu-appearance' );
    307                         $itemCount = $menuItem.find( '.theme-count' );
    308                 }
    309 
    310                 // Decrement the counter of the other menu items.
    311                 if ( $itemCount ) {
    312                         itemCount = $itemCount.eq( 0 ).text();
    313                         itemCount = parseInt( itemCount, 10 ) - 1;
    314                 }
    315 
    316                 if ( itemCount < 0 || isNaN( itemCount ) ) {
    317                         return;
    318                 }
    319 
    320                 if ( itemCount > 0 ) {
    321                         $( '.subsubsub .upgrade .count' ).text( '(' + itemCount + ')' );
    322 
    323                         $itemCount.text( itemCount );
    324                         $menuItem.find( '.update-plugins' ).each( function( index, element ) {
    325                                 element.className = element.className.replace( /count-\d+/, 'count-' + itemCount );
    326                         } );
    327                 } else {
    328                         $( '.subsubsub .upgrade' ).remove();
    329                         $menuItem.find( '.update-plugins' ).remove();
    330                 }
     346                        settings.totals.counts.themes = Math.max( --settings.totals.counts.themes, 0 );
     347                }
     348
     349                wp.updates.refreshCount( type );
    331350        };
    332351
    … …  
    12521271                                var $views     = $( '.subsubsub' ),
    12531272                                        $themeRow  = $( this ),
    1254                                         totals     = settings.totals,
     1273                                        totals     = settings.themes,
    12551274                                        deletedRow = wp.template( 'item-deleted-row' );
    12561275
    … …  
    16891708                        $pluginSearch        = $( '.plugins-php .wp-filter-search' ),
    16901709                        $pluginInstallSearch = $( '.plugin-install-php .wp-filter-search' );
     1710
     1711                settings = _.extend( settings, window._wpUpdatesItemCounts || {} );
     1712
     1713                if ( settings.totals ) {
     1714                        wp.updates.refreshCount();
     1715                }
    16911716
    16921717                /*
    … …  
    24132438                $( window ).on( 'beforeunload', wp.updates.beforeunload );
    24142439        } );
    2415 })( jQuery, window.wp, _.extend( window._wpUpdatesSettings, window._wpUpdatesItemCounts || {} ) );
     2440})( jQuery, window.wp, window._wpUpdatesSettings );
  • trunk/src/wp-admin/menu.php

    r37431 r38827  
    3939        else
    4040                $cap = 'update_themes';
    41         $submenu[ 'index.php' ][10] = array( sprintf( __('Updates %s'), "<span class='update-plugins count-{$update_data['counts']['total']}' title='{$update_data['title']}'><span class='update-count'>" . number_format_i18n($update_data['counts']['total']) . "</span></span>" ), $cap, 'update-core.php');
     41        $submenu[ 'index.php' ][10] = array( sprintf( __('Updates %s'), "<span class='update-plugins count-{$update_data['counts']['total']}'><span class='update-count'>" . number_format_i18n($update_data['counts']['total']) . "</span></span>" ), $cap, 'update-core.php');
    4242        unset( $cap );
    4343}
  • trunk/src/wp-admin/network/menu.php

    r37365 r38827  
    1515$update_data = wp_get_update_data();
    1616if ( $update_data['counts']['total'] ) {
    17         $submenu['index.php'][10] = array( sprintf( __( 'Updates %s' ), "<span class='update-plugins count-{$update_data['counts']['total']}' title='{$update_data['title']}'><span class='update-count'>" . number_format_i18n( $update_data['counts']['total'] ) . "</span></span>" ), 'update_core', 'update-core.php' );
     17        $submenu['index.php'][10] = array( sprintf( __( 'Updates %s' ), "<span class='update-plugins count-{$update_data['counts']['total']}'><span class='update-count'>" . number_format_i18n( $update_data['counts']['total'] ) . "</span></span>" ), 'update_core', 'update-core.php' );
    1818} else {
    1919        $submenu['index.php'][10] = array( __( 'Updates' ), 'update_core', 'update-core.php' );
  • trunk/src/wp-admin/themes.php

    r38788 r38827  
    491491wp_print_update_row_templates();
    492492
     493wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
     494        'totals'  => wp_get_update_data(),
     495) );
     496
    493497require( ABSPATH . 'wp-admin/admin-footer.php' );
  • trunk/src/wp-admin/update-core.php

    r38743 r38827  
    619619        do_action( 'core_upgrade_preamble' );
    620620        echo '</div>';
     621
     622        wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
     623                'totals'  => wp_get_update_data(),
     624        ) );
     625
    621626        include(ABSPATH . 'wp-admin/admin-footer.php');
    622627
    … …  
    642647        if ( isset( $_POST['upgrade'] ) )
    643648                do_core_upgrade($reinstall);
     649
     650        wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
     651                'totals'  => wp_get_update_data(),
     652        ) );
    644653
    645654        include(ABSPATH . 'wp-admin/admin-footer.php');
    … …  
    671680        echo '<iframe src="', $url, '" style="width: 100%; height: 100%; min-height: 750px;" frameborder="0" title="' . esc_attr__( 'Update progress' ) . '"></iframe>';
    672681        echo '</div>';
     682
     683        wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
     684                'totals'  => wp_get_update_data(),
     685        ) );
     686
    673687        include(ABSPATH . 'wp-admin/admin-footer.php');
    674688
    … …  
    701715        </div>
    702716        <?php
     717
     718        wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
     719                'totals'  => wp_get_update_data(),
     720        ) );
     721
    703722        include(ABSPATH . 'wp-admin/admin-footer.php');
    704723
    … …  
    720739        $upgrader = new Language_Pack_Upgrader( new Language_Pack_Upgrader_Skin( compact( 'url', 'nonce', 'title', 'context' ) ) );
    721740        $result = $upgrader->bulk_upgrade();
     741
     742        wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
     743                'totals'  => wp_get_update_data(),
     744        ) );
    722745
    723746        require_once( ABSPATH . 'wp-admin/admin-footer.php' );
  • trunk/src/wp-includes/script-loader.php

    r38810 r38827  
    612612                ) );
    613613
    614                 $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery', 'wp-util', 'wp-a11y' ) );
     614                $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery', 'wp-util', 'wp-a11y' ), false, 1 );
    615615                did_action( 'init' ) && $scripts->localize( 'updates', '_wpUpdatesSettings', array(
    616616                        'ajax_nonce' => wp_create_nonce( 'updates' ),
Note: See TracChangeset for help on using the changeset viewer.