Shiny Updates: Replace $.post() calls with wp.ajax.post(), and clean up a bunch of the now unnecessary code.

See #29820


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


git-svn-id: http://core.svn.wordpress.org/trunk@31390 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2015-02-11 05:06:28 +00:00
parent 97698eb127
commit daf711a338
4 changed files with 31 additions and 81 deletions

View File

@ -1,6 +1,6 @@
window.wp = window.wp || {}; window.wp = window.wp || {};
(function( $, wp, pagenow, ajaxurl ) { (function( $, wp, pagenow ) {
wp.updates = {}; wp.updates = {};
/** /**
@ -122,45 +122,15 @@ window.wp = window.wp || {};
wp.updates.updateLock = true; wp.updates.updateLock = true;
var data = { var data = {
'action': 'update-plugin',
'_ajax_nonce': wp.updates.ajaxNonce, '_ajax_nonce': wp.updates.ajaxNonce,
'plugin': plugin, 'plugin': plugin,
'slug': slug 'slug': slug
}; };
$.ajax( { wp.ajax.post( 'update-plugin', data )
type: 'post', .done( wp.updates.updateSuccess )
url: ajaxurl, .fail( wp.updates.updateError )
data: data, .always( wp.updates.updateAlways );
complete: wp.updates.updateRequestComplete
} );
};
/**
* After an update attempt has completed, deal with the response.
*
* @since 4.2.0
*
* @param {jqXHR} jqxhr The jQuery XMLHttpRequest for the request.
*/
wp.updates.updateRequestComplete = function( jqxhr ) {
wp.updates.updateLock = false;
if ( jqxhr.responseJSON && jqxhr.responseJSON.success ) {
wp.updates.updateSuccess( jqxhr.responseJSON );
} else {
var alertText = wp.updates.l10n.updateFailed;
if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.error ) {
alertText += ': ' + jqxhr.responseJSON.data.error;
}
window.alert( alertText );
if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.slug ) {
wp.updates.updateError( jqxhr.responseJSON );
}
}
/**
* Check the queue.
*/
wp.updates.queueChecker();
}; };
/** /**
@ -173,11 +143,11 @@ window.wp = window.wp || {};
wp.updates.updateSuccess = function( response ) { wp.updates.updateSuccess = function( response ) {
var $message; var $message;
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
$message = $( '#' + response.data.slug ).next().find( '.update-message' ); $message = $( '#' + response.slug ).next().find( '.update-message' );
$( '#' + response.data.slug ).addClass( 'updated' ).removeClass( 'update' ); $( '#' + response.slug ).addClass( 'updated' ).removeClass( 'update' );
$( '#' + response.data.slug + '-update' ).addClass( 'updated' ).removeClass( 'update' ); $( '#' + response.slug + '-update' ).addClass( 'updated' ).removeClass( 'update' );
} else if ( 'plugin-install' === pagenow ) { } else if ( 'plugin-install' === pagenow ) {
$message = $( '.plugin-card-' + response.data.slug ).find( '.update-now' ); $message = $( '.plugin-card-' + response.slug ).find( '.update-now' );
$message.addClass( 'button-disabled' ); $message.addClass( 'button-disabled' );
} }
@ -197,14 +167,25 @@ window.wp = window.wp || {};
wp.updates.updateError = function( response ) { wp.updates.updateError = function( response ) {
var $message; var $message;
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
$message = $( '#' + response.data.slug ).next().find( '.update-message' ); $message = $( '#' + response.slug ).next().find( '.update-message' );
} else if ( 'plugin-install' === pagenow ) { } else if ( 'plugin-install' === pagenow ) {
$message = $( '.plugin-card-' + response.data.slug ).find( '.update-now' ); $message = $( '.plugin-card-' + response.slug ).find( '.update-now' );
} }
$message.removeClass( 'updating-message' ); $message.removeClass( 'updating-message' );
$message.text( wp.updates.l10n.updateFailed ); $message.text( wp.updates.l10n.updateFailed );
}; };
/**
* After an update attempt has completed, check the queue.
*
* @since 4.2.0
*/
wp.updates.updateAlways = function() {
wp.updates.updateLock = false;
wp.updates.queueChecker();
};
/** /**
* Send an Ajax request to the server to install a plugin. * Send an Ajax request to the server to install a plugin.
* *
@ -231,45 +212,14 @@ window.wp = window.wp || {};
wp.updates.updateLock = true; wp.updates.updateLock = true;
var data = { var data = {
'action': 'install-plugin',
'_ajax_nonce': wp.updates.ajaxNonce, '_ajax_nonce': wp.updates.ajaxNonce,
'slug': slug 'slug': slug
}; };
$.ajax( { wp.ajax.post( 'install-plugin', data )
type: 'post', .done( wp.updates.installSuccess )
url: ajaxurl, .fail( wp.updates.installError )
data: data, .always( wp.updates.updateAlways );
complete: wp.updates.installRequestComplete
} );
};
/**
* After an installation attempt has completed, deal with the response.
*
* @since 4.2.0
*
* @param {jqXHR} jqxhr The jQuery XMLHttpRequest for the request.
*/
wp.updates.installRequestComplete = function( jqxhr ) {
wp.updates.updateLock = false;
if ( jqxhr.responseJSON && jqxhr.responseJSON.success ) {
wp.updates.installSuccess( jqxhr.responseJSON );
} else {
var alertText = wp.updates.l10n.installFailed;
if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.error ) {
alertText += ': ' + jqxhr.responseJSON.data.error;
}
window.alert( alertText );
if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.slug ) {
wp.updates.installError( jqxhr.responseJSON );
}
}
/**
* Check the queue.
*/
wp.updates.queueChecker();
}; };
/** /**
@ -280,7 +230,7 @@ window.wp = window.wp || {};
* @param {object} response * @param {object} response
*/ */
wp.updates.installSuccess = function( response ) { wp.updates.installSuccess = function( response ) {
var $message = $( '.plugin-card-' + response.data.slug ).find( '.install-now' ); var $message = $( '.plugin-card-' + response.slug ).find( '.install-now' );
$message.removeClass( 'updating-message' ).addClass( 'updated-message button-disabled' ); $message.removeClass( 'updating-message' ).addClass( 'updated-message button-disabled' );
$message.text( wp.updates.l10n.installed ); $message.text( wp.updates.l10n.installed );
@ -294,7 +244,7 @@ window.wp = window.wp || {};
* @param {object} response * @param {object} response
*/ */
wp.updates.installError = function( response ) { wp.updates.installError = function( response ) {
var $message = $( '.plugin-card-' + response.data.slug ).find( '.install-now' ); var $message = $( '.plugin-card-' + response.slug ).find( '.install-now' );
$message.removeClass( 'updating-message' ); $message.removeClass( 'updating-message' );
$message.text( wp.updates.l10n.installNow ); $message.text( wp.updates.l10n.installNow );

File diff suppressed because one or more lines are too long

View File

@ -500,7 +500,7 @@ function wp_default_scripts( &$scripts ) {
'ays' => __('Are you sure you want to install this plugin?') 'ays' => __('Are you sure you want to install this plugin?')
) ); ) );
$scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery' ) ); $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery', 'wp-util' ) );
did_action( 'init' ) && $scripts->localize( 'updates', '_wpUpdatesSettings', array( did_action( 'init' ) && $scripts->localize( 'updates', '_wpUpdatesSettings', array(
'ajax_nonce' => wp_create_nonce( 'updates' ), 'ajax_nonce' => wp_create_nonce( 'updates' ),
'l10n' => array( 'l10n' => array(

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.2-alpha-31408'; $wp_version = '4.2-alpha-31409';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.