diff --git a/wp-admin/includes/class-wp-upgrader-skins.php b/wp-admin/includes/class-wp-upgrader-skins.php index 71a1293e8b..ed6c5e6447 100644 --- a/wp-admin/includes/class-wp-upgrader-skins.php +++ b/wp-admin/includes/class-wp-upgrader-skins.php @@ -92,6 +92,23 @@ class WP_Upgrader_Skin { function before() {} function after() {} + /** + * Output JavaScript that calls function to decrement the update counts. + * + * @since 3.9.0 + */ + protected function decrement_update_count( $type ) { + if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) { + return; + } + echo ''; + } } /** @@ -124,6 +141,8 @@ class Plugin_Upgrader_Skin extends WP_Upgrader_Skin { echo ''; } + $this->decrement_update_count( 'plugin' ); + $update_actions = array( 'activate_plugin' => '' . __('Activate Plugin') . '', 'plugins_page' => '' . __('Return to Plugins page') . '' @@ -252,6 +271,22 @@ class Bulk_Upgrader_Skin extends WP_Upgrader_Skin { wp_ob_end_flush_all(); flush(); } + + /** + * Output JavaScript that sends message to parent window to decrement the update counts. + * + * @since 3.9.0 + */ + protected function decrement_update_count( $type ) { + if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) { + return; + } + echo ''; + } } class Bulk_Plugin_Upgrader_Skin extends Bulk_Upgrader_Skin { @@ -272,6 +307,7 @@ class Bulk_Plugin_Upgrader_Skin extends Bulk_Upgrader_Skin { function after($title = '') { parent::after($this->plugin_info['Title']); + $this->decrement_update_count( 'plugin' ); } function bulk_footer() { parent::bulk_footer(); @@ -306,6 +342,7 @@ class Bulk_Theme_Upgrader_Skin extends Bulk_Upgrader_Skin { function after($title = '') { parent::after( $this->theme_info->display('Name') ); + $this->decrement_update_count( 'theme' ); } function bulk_footer() { @@ -479,6 +516,7 @@ class Theme_Upgrader_Skin extends WP_Upgrader_Skin { } function after() { + $this->decrement_update_count( 'theme' ); $update_actions = array(); if ( ! empty( $this->upgrader->result['destination_name'] ) && $theme_info = $this->upgrader->theme_info() ) { @@ -561,6 +599,7 @@ class Language_Pack_Upgrader_Skin extends WP_Upgrader_Skin { } function bulk_footer() { + $this->decrement_update_count( 'translation' ); $update_actions = array(); $update_actions['updates_page'] = '' . __( 'Return to WordPress Updates' ) . ''; $update_actions = apply_filters( 'update_translations_complete_actions', $update_actions ); diff --git a/wp-admin/includes/class-wp-upgrader.php b/wp-admin/includes/class-wp-upgrader.php index 664595a28d..4e1e0c256b 100644 --- a/wp-admin/includes/class-wp-upgrader.php +++ b/wp-admin/includes/class-wp-upgrader.php @@ -560,7 +560,7 @@ class Plugin_Upgrader extends WP_Upgrader { $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true); if ( !isset( $current->response[ $plugin ] ) ) { - $this->skin->set_result(true); + $this->skin->set_result('up_to_date'); $this->skin->before(); $this->skin->feedback('up_to_date'); $this->skin->after(); @@ -863,7 +863,7 @@ class Theme_Upgrader extends WP_Upgrader { if ( !isset( $current->response[ $theme ] ) ) { $this->skin->before(); $this->skin->set_result(false); - $this->skin->error('up_to_date'); + $this->skin->error( 'up_to_date' ); $this->skin->after(); return false; } @@ -948,7 +948,7 @@ class Theme_Upgrader extends WP_Upgrader { if ( !isset( $current->response[ $theme ] ) ) { $this->skin->set_result(true); $this->skin->before(); - $this->skin->feedback('up_to_date'); + $this->skin->feedback( 'up_to_date' ); $this->skin->after(); $results[$theme] = true; continue; diff --git a/wp-admin/js/updates.js b/wp-admin/js/updates.js new file mode 100644 index 0000000000..64569df8d3 --- /dev/null +++ b/wp-admin/js/updates.js @@ -0,0 +1,63 @@ +window.wp = window.wp || {}; + +(function( $, wp ) { + + wp.updates = {}; + + /** + * Decrement update counts throughout the various menus + * + * @param {string} updateType + */ + wp.updates.decrementCount = function( upgradeType ) { + var count, pluginCount, $elem; + + $elem = $( '#wp-admin-bar-updates .ab-label' ); + count = $elem.text(); + count = parseInt( count, 10 ) - 1; + if ( count < 0 ) { + return; + } + $elem.text( count ); + + $elem = $( 'a[href="update-core.php"] .update-plugins' ); + $elem.each( function( index, elem ) { + elem.className = elem.className.replace( /count-\d+/, 'count-' + count ); + } ); + $elem.find( '.update-count' ).text( count ); + + if ( 'plugin' === upgradeType ) { + $elem = $( '#menu-plugins' ); + pluginCount = $elem.find( '.plugin-count' ).eq(0).text(); + pluginCount = parseInt( pluginCount, 10 ) - 1; + if ( count < 0 ) { + return; + } + $elem.find( '.plugin-count' ).text( pluginCount ); + $elem.find( '.update-plugins' ).each( function( index, elem ) { + elem.className = elem.className.replace( /count-\d+/, 'count-' + pluginCount ); + } ); + } + }; + + $( window ).on( 'message', function( e ) { + var event = e.originalEvent, + message, + loc = document.location, + expectedOrigin = loc.protocol + '//' + loc.hostname; + + if ( event.origin !== expectedOrigin ) { + return; + } + + message = $.parseJSON( event.data ); + + if ( typeof message.action === 'undefined' || message.action !== 'decrementUpdateCount' ) { + return; + } + + wp.updates.decrementCount( message.upgradeType ); + + } ); + +})( jQuery, window.wp ); diff --git a/wp-admin/js/updates.min.js b/wp-admin/js/updates.min.js new file mode 100644 index 0000000000..5e6709cbd9 --- /dev/null +++ b/wp-admin/js/updates.min.js @@ -0,0 +1 @@ +window.wp=window.wp||{},function(a,b){b.updates={},b.updates.decrementCount=function(b){var c,d,e;if(e=a("#wp-admin-bar-updates .ab-label"),c=e.text(),c=parseInt(c,10)-1,!(0>c)&&(e.text(c),e=a('a[href="update-core.php"] .update-plugins'),e.each(function(a,b){b.className=b.className.replace(/count-\d+/,"count-"+c)}),e.find(".update-count").text(c),"plugin"===b)){if(e=a("#menu-plugins"),d=e.find(".plugin-count").eq(0).text(),d=parseInt(d,10)-1,0>c)return;e.find(".plugin-count").text(d),e.find(".update-plugins").each(function(a,b){b.className=b.className.replace(/count-\d+/,"count-"+d)})}},a(window).on("message",function(c){var d,e=c.originalEvent,f=document.location,g=f.protocol+"//"+f.hostname;e.origin===g&&(d=a.parseJSON(e.data),"undefined"!=typeof d.action&&"decrementUpdateCount"===d.action&&b.updates.decrementCount(d.upgradeType))})}(jQuery,window.wp); \ No newline at end of file diff --git a/wp-admin/plugins.php b/wp-admin/plugins.php index 61efcf5c80..261f0f24fd 100644 --- a/wp-admin/plugins.php +++ b/wp-admin/plugins.php @@ -113,6 +113,7 @@ if ( $action ) { $title = __( 'Update Plugins' ); $parent_file = 'plugins.php'; + wp_enqueue_script( 'updates' ); require_once(ABSPATH . 'wp-admin/admin-header.php'); echo '
'; diff --git a/wp-admin/update-core.php b/wp-admin/update-core.php index 5b84ec43b1..aa2d19bdf2 100644 --- a/wp-admin/update-core.php +++ b/wp-admin/update-core.php @@ -11,6 +11,7 @@ require_once( dirname( __FILE__ ) . '/admin.php' ); wp_enqueue_style( 'plugin-install' ); wp_enqueue_script( 'plugin-install' ); +wp_enqueue_script( 'updates' ); add_thickbox(); if ( is_multisite() && ! is_network_admin() ) { diff --git a/wp-admin/update.php b/wp-admin/update.php index e70d9c2221..d698e26fa5 100644 --- a/wp-admin/update.php +++ b/wp-admin/update.php @@ -37,7 +37,7 @@ if ( isset($_GET['action']) ) { $url = 'update.php?action=update-selected&plugins=' . urlencode(implode(',', $plugins)); $nonce = 'bulk-update-plugins'; - wp_enqueue_script('jquery'); + wp_enqueue_script( 'updates' ); iframe_header(); $upgrader = new Plugin_Upgrader( new Bulk_Plugin_Upgrader_Skin( compact( 'nonce', 'url' ) ) ); @@ -54,6 +54,8 @@ if ( isset($_GET['action']) ) { $title = __('Update Plugin'); $parent_file = 'plugins.php'; $submenu_file = 'plugins.php'; + + wp_enqueue_script( 'updates' ); require_once(ABSPATH . 'wp-admin/admin-header.php'); $nonce = 'upgrade-plugin_' . $plugin; @@ -154,6 +156,7 @@ if ( isset($_GET['action']) ) { check_admin_referer('upgrade-theme_' . $theme); wp_enqueue_script( 'customize-loader' ); + wp_enqueue_script( 'updates' ); $title = __('Update Theme'); $parent_file = 'themes.php'; @@ -185,7 +188,7 @@ if ( isset($_GET['action']) ) { $url = 'update.php?action=update-selected-themes&themes=' . urlencode(implode(',', $themes)); $nonce = 'bulk-update-themes'; - wp_enqueue_script('jquery'); + wp_enqueue_script( 'updates' ); iframe_header(); $upgrader = new Theme_Upgrader( new Bulk_Theme_Upgrader_Skin( compact( 'nonce', 'url' ) ) ); diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index d696b13483..a098b199ac 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -481,6 +481,8 @@ 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' ) ); + $scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array('jquery'), '1.2' ); $scripts->add( 'iris', '/wp-admin/js/iris.min.js', array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), false, 1 );