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 || {};
(function( $, wp, pagenow, ajaxurl ) {
(function( $, wp, pagenow ) {
wp.updates = {};
/**
@ -122,45 +122,15 @@ window.wp = window.wp || {};
wp.updates.updateLock = true;
var data = {
'action': 'update-plugin',
'_ajax_nonce': wp.updates.ajaxNonce,
'plugin': plugin,
'slug': slug
};
$.ajax( {
type: 'post',
url: ajaxurl,
data: data,
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();
wp.ajax.post( 'update-plugin', data )
.done( wp.updates.updateSuccess )
.fail( wp.updates.updateError )
.always( wp.updates.updateAlways );
};
/**
@ -173,11 +143,11 @@ window.wp = window.wp || {};
wp.updates.updateSuccess = function( response ) {
var $message;
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
$message = $( '#' + response.data.slug ).next().find( '.update-message' );
$( '#' + response.data.slug ).addClass( 'updated' ).removeClass( 'update' );
$( '#' + response.data.slug + '-update' ).addClass( 'updated' ).removeClass( 'update' );
$message = $( '#' + response.slug ).next().find( '.update-message' );
$( '#' + response.slug ).addClass( 'updated' ).removeClass( 'update' );
$( '#' + response.slug + '-update' ).addClass( 'updated' ).removeClass( 'update' );
} 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' );
}
@ -197,14 +167,25 @@ window.wp = window.wp || {};
wp.updates.updateError = function( response ) {
var $message;
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 ) {
$message = $( '.plugin-card-' + response.data.slug ).find( '.update-now' );
$message = $( '.plugin-card-' + response.slug ).find( '.update-now' );
}
$message.removeClass( 'updating-message' );
$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.
*
@ -231,45 +212,14 @@ window.wp = window.wp || {};
wp.updates.updateLock = true;
var data = {
'action': 'install-plugin',
'_ajax_nonce': wp.updates.ajaxNonce,
'slug': slug
};
$.ajax( {
type: 'post',
url: ajaxurl,
data: data,
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();
wp.ajax.post( 'install-plugin', data )
.done( wp.updates.installSuccess )
.fail( wp.updates.installError )
.always( wp.updates.updateAlways );
};
/**
@ -280,7 +230,7 @@ window.wp = window.wp || {};
* @param {object} 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.text( wp.updates.l10n.installed );
@ -294,7 +244,7 @@ window.wp = window.wp || {};
* @param {object} 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.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?')
) );
$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(
'ajax_nonce' => wp_create_nonce( 'updates' ),
'l10n' => array(

View File

@ -4,7 +4,7 @@
*
* @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.