Commit 50786a7a authored by Andrew Nacin's avatar Andrew Nacin
Browse files

Decrement update count bubbles as plugins/themes are updated, live.

props gcorne, mitchoyoshitaka.
fixes #17703.

Built from https://develop.svn.wordpress.org/trunk@27280


git-svn-id: https://core.svn.wordpress.org/trunk@27136 1a063a9b-81f0-0310-95a4-ce76da25c4cd
parent 4a95f775
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -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 '<script type="text/javascript">
				(function( wp ) {
					if ( wp && wp.updates.decrementCount ) {
						wp.updates.decrementCount( "' . $type . '" );
					}
				})( window.wp );
			</script>';
	}
}

/**
@@ -124,6 +141,8 @@ class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
			echo '<iframe style="border:0;overflow:hidden" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&networkwide=' . $this->plugin_network_active . '&plugin=' . urlencode( $this->plugin ), 'activate-plugin_' . $this->plugin) .'"></iframe>';
		}

		$this->decrement_update_count( 'plugin' );

		$update_actions =  array(
			'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . urlencode( $this->plugin ), 'activate-plugin_' . $this->plugin) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
			'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
@@ -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 '<script type="text/javascript">
				if ( window.postMessage && JSON ) {
					window.parent.postMessage( JSON.stringify( { action: "decrementUpdateCount", upgradeType: "' . $type . '" } ), window.location.protocol + "//" + window.location.hostname );
				}
			</script>';
	}
}

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'] = '<a href="' . self_admin_url( 'update-core.php' ) . '" title="' . esc_attr__( 'Go to WordPress Updates page' ) . '" target="_parent">' . __( 'Return to WordPress Updates' ) . '</a>';
		$update_actions = apply_filters( 'update_translations_complete_actions', $update_actions );
+3 −3
Original line number Diff line number Diff line
@@ -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();

wp-admin/js/updates.js

0 → 100644
+63 −0
Original line number Diff line number Diff line
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 );
+1 −0
Original line number Diff line number Diff line
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
+1 −0
Original line number Diff line number Diff line
@@ -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 '<div class="wrap">';
Loading