Make WordPress Core

Changeset 31409


Ignore:
Timestamp:
02/11/2015 05:05:13 AM (12 years ago)
Author:
pento
Message:

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

See #29820

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/updates.js

    r31341 r31409  
    11window.wp = window.wp || {};
    22
    3 (function( $, wp, pagenow, ajaxurl ) {
     3(function( $, wp, pagenow ) {
    44        wp.updates = {};
    55
    … …  
    123123
    124124                var data = {
    125                         'action':      'update-plugin',
    126125                        '_ajax_nonce': wp.updates.ajaxNonce,
    127126                        'plugin':      plugin,
    … …  
    129128                };
    130129
    131                 $.ajax( {
    132                         type:      'post',
    133                         url:       ajaxurl,
    134                         data:      data,
    135                         complete:  wp.updates.updateRequestComplete
    136                 } );
    137         };
    138 
    139         /**
    140          * After an update attempt has completed, deal with the response.
    141          *
    142          * @since 4.2.0
    143          *
    144          * @param  {jqXHR} jqxhr The jQuery XMLHttpRequest for the request.
    145          */
    146         wp.updates.updateRequestComplete = function( jqxhr ) {
    147                 wp.updates.updateLock = false;
    148                 if ( jqxhr.responseJSON && jqxhr.responseJSON.success ) {
    149                         wp.updates.updateSuccess( jqxhr.responseJSON );
    150                 } else {
    151                         var alertText = wp.updates.l10n.updateFailed;
    152                         if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.error ) {
    153                                  alertText += ': ' + jqxhr.responseJSON.data.error;
    154                         }
    155                         window.alert( alertText );
    156                         if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.slug ) {
    157                                 wp.updates.updateError( jqxhr.responseJSON );
    158                         }
    159                 }
    160                 /**
    161                  * Check the queue.
    162                  */
    163                 wp.updates.queueChecker();
     130                wp.ajax.post( 'update-plugin', data )
     131                        .done( wp.updates.updateSuccess )
     132                        .fail( wp.updates.updateError )
     133                        .always( wp.updates.updateAlways );
    164134        };
    165135
    … …  
    174144                var $message;
    175145                if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
    176                         $message = $( '#' + response.data.slug ).next().find( '.update-message' );
    177                         $( '#' + response.data.slug ).addClass( 'updated' ).removeClass( 'update' );
    178                         $( '#' + response.data.slug + '-update' ).addClass( 'updated' ).removeClass( 'update' );
     146                        $message = $( '#' + response.slug ).next().find( '.update-message' );
     147                        $( '#' + response.slug ).addClass( 'updated' ).removeClass( 'update' );
     148                        $( '#' + response.slug + '-update' ).addClass( 'updated' ).removeClass( 'update' );
    179149                } else if ( 'plugin-install' === pagenow ) {
    180                         $message = $( '.plugin-card-' + response.data.slug ).find( '.update-now' );
     150                        $message = $( '.plugin-card-' + response.slug ).find( '.update-now' );
    181151                        $message.addClass( 'button-disabled' );
    182152                }
    … …  
    198168                var $message;
    199169                if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
    200                         $message = $( '#' + response.data.slug ).next().find( '.update-message' );
     170                        $message = $( '#' + response.slug ).next().find( '.update-message' );
    201171                } else if ( 'plugin-install' === pagenow ) {
    202                         $message = $( '.plugin-card-' + response.data.slug ).find( '.update-now' );
     172                        $message = $( '.plugin-card-' + response.slug ).find( '.update-now' );
    203173                }
    204174                $message.removeClass( 'updating-message' );
    205175                $message.text( wp.updates.l10n.updateFailed );
    206176        };
     177
     178        /**
     179         * After an update attempt has completed, check the queue.
     180         *
     181         * @since 4.2.0
     182         */
     183        wp.updates.updateAlways = function() {
     184                wp.updates.updateLock = false;
     185                wp.updates.queueChecker();
     186        };
     187
    207188
    208189        /**
    … …  
    232213
    233214                var data = {
    234                         'action':      'install-plugin',
    235215                        '_ajax_nonce': wp.updates.ajaxNonce,
    236216                        'slug':        slug
    237217                };
    238218
    239                 $.ajax( {
    240                         type:     'post',
    241                         url:      ajaxurl,
    242                         data:     data,
    243                         complete: wp.updates.installRequestComplete
    244                 } );
    245         };
    246 
    247 
    248         /**
    249          * After an installation attempt has completed, deal with the response.
    250          *
    251          * @since 4.2.0
    252          *
    253          * @param {jqXHR} jqxhr The jQuery XMLHttpRequest for the request.
    254          */
    255         wp.updates.installRequestComplete = function( jqxhr ) {
    256                 wp.updates.updateLock = false;
    257                 if ( jqxhr.responseJSON && jqxhr.responseJSON.success ) {
    258                         wp.updates.installSuccess( jqxhr.responseJSON );
    259                 } else {
    260                         var alertText = wp.updates.l10n.installFailed;
    261                         if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.error ) {
    262                                  alertText += ': ' + jqxhr.responseJSON.data.error;
    263                         }
    264                         window.alert( alertText );
    265                         if ( jqxhr.responseJSON && jqxhr.responseJSON.data && jqxhr.responseJSON.data.slug ) {
    266                                 wp.updates.installError( jqxhr.responseJSON );
    267                         }
    268                 }
    269                 /**
    270                  * Check the queue.
    271                  */
    272                 wp.updates.queueChecker();
     219                wp.ajax.post( 'install-plugin', data )
     220                        .done( wp.updates.installSuccess )
     221                        .fail( wp.updates.installError )
     222                        .always( wp.updates.updateAlways );
    273223        };
    274224
    … …  
    281231         */
    282232        wp.updates.installSuccess = function( response ) {
    283                 var $message = $( '.plugin-card-' + response.data.slug ).find( '.install-now' );
     233                var $message = $( '.plugin-card-' + response.slug ).find( '.install-now' );
    284234
    285235                $message.removeClass( 'updating-message' ).addClass( 'updated-message button-disabled' );
    … …  
    295245         */
    296246        wp.updates.installError = function( response ) {
    297                 var $message = $( '.plugin-card-' + response.data.slug ).find( '.install-now' );
     247                var $message = $( '.plugin-card-' + response.slug ).find( '.install-now' );
    298248
    299249                $message.removeClass( 'updating-message' );
  • trunk/src/wp-includes/script-loader.php

    r31373 r31409  
    501501                ) );
    502502
    503                 $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery' ) );
     503                $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery', 'wp-util' ) );
    504504                did_action( 'init' ) && $scripts->localize( 'updates', '_wpUpdatesSettings', array(
    505505                        'ajax_nonce' => wp_create_nonce( 'updates' ),
Note: See TracChangeset for help on using the changeset viewer.