Make WordPress Core

Ticket #35501: 35501.diff

File 35501.diff, 5.4 KB (added by adamsilverstein, 6 years ago)
  • src/wp-admin/js/edit-comments.js

    diff --git src/wp-admin/js/edit-comments.js src/wp-admin/js/edit-comments.js
    index 0b0d0846bc..ff75b4b3e6 100644
    setCommentsList = function() { 
    288288                        $('.avatar', el).first().clone().prependTo('#undo-' + id + ' .' + action + '-undo-inside');
    289289
    290290                        a.click(function( e ){
     291
     292                                // Abort undo if there is an unfinished AJAX request involving the same comment.
     293                                if ( wpList.xhrs.inProgress( 'comment-' + id ) ) {
     294                                        return false;
     295                                }
    291296                                e.preventDefault();
    292297                                e.stopPropagation(); // ticket #35904
    293298                                list.wpList.del(this);
  • src/wp-includes/js/wp-lists.js

    diff --git src/wp-includes/js/wp-lists.js src/wp-includes/js/wp-lists.js
    index c73471dbcb..9877c5baf6 100644
    wpList = { 
    187187                dimAfter: null
    188188        },
    189189
     190        /**
     191         * Track XHR connections.
     192         * @type {Object}
     193         */
     194        xhrs: wp.xhrs(),
     195
    190196        /**
    191197         * Finds a nonce.
    192198         *
    wpList = { 
    384390                                        parsed: parsedResponse
    385391                                }, settings ) );
    386392                        }
     393                        wpList.xhrs.clear();
    387394                };
    388395
    389                 $.ajax( settings );
     396                wpList.xhrs.setXhrs( settings.element, $.ajax( settings ) );
    390397
    391398                return false;
    392399        },
    wpList = { 
    410417                settings.element  = data[2] || settings.element || null;
    411418                settings.delColor = data[3] ? '#' + data[3] : settings.delColor;
    412419
     420                // Return if there is already an AJAX request in progress involving the same element.
     421                if ( wpList.xhrs.inProgress( settings.element ) ) {
     422                        return false;
     423                }
     424
    413425                if ( ! settings || ! settings.element ) {
    414426                        return false;
    415427                }
    wpList = { 
    473485                        }
    474486                };
    475487
    476                 $.ajax( settings );
     488                wpList.xhrs.setXhrs( settings.element, $.ajax( settings ) );
    477489
    478490                return false;
    479491        },
    wpList = { 
    504516                settings.dimAddColor = data[4] ? '#' + data[4] : settings.dimAddColor;
    505517                settings.dimDelColor = data[5] ? '#' + data[5] : settings.dimDelColor;
    506518
     519                // Return if there is already an AJAX request in progress involving the same element.
     520                if ( wpList.xhrs.inProgress( settings.element ) ) {
     521                        return false;
     522                }
     523
    507524                if ( ! settings || ! settings.element || ! settings.dimClass ) {
    508525                        return true;
    509526                }
    wpList = { 
    587604                };
    588605
    589606                settings.complete = function( jqXHR, status ) {
     607
     608                        // Clear all finished AJAX requests from xhrs object.
     609                        wpList.xhrs.clear();
     610
    590611                        if ( $.isFunction( settings.dimAfter ) ) {
    591612                                $eventTarget.queue( function() {
    592613                                        settings.dimAfter( returnedResponse, $.extend( {
    wpList = { 
    598619                        }
    599620                };
    600621
    601                 $.ajax( settings );
     622                wpList.xhrs.setXhrs( settings.element, $.ajax( settings ) );
    602623
    603624                return false;
    604625        },
    wpList = { 
    836857$.fn.wpList = function( settings ) {
    837858        this.each( function( index, list ) {
    838859                list.wpList = {
    839                         settings: $.extend( {}, wpList.settings, { what: wpList.parseData( list, 'list' )[1] || '' }, settings )
     860                        settings: $.extend( {}, wpList.settings, { what: wpList.parseData( list, 'list' )[1] || '' }, { xhrs: wpList.xhrs }, settings )
    840861                };
    841862
    842863                $.each( functions, function( func, callback ) {
  • new file src/wp-includes/js/xhrs.js

    diff --git src/wp-includes/js/xhrs.js src/wp-includes/js/xhrs.js
    new file mode 100644
    index 0000000000..744b4484b3
    - +  
     1( function() {
     2        var wp = window.wp || {};
     3
     4        /**
     5         * Track XHR requests in progress.
     6         */
     7        wp.xhrs = function() {
     8                return {
     9                        xhrs: {},
     10
     11                        // Clear all tracked xhrs.
     12                        clear: function() {
     13
     14                                // Clear all finished AJAX requests from xhrs object.
     15                                if ( 'undefined' !== typeof this.xhrs ) {
     16                                        for ( var key in this.xhrs ) {
     17                                                if ( 4 === this.xhrs[ key ].readyState ) {
     18                                                        delete this.xhrs[ key ];
     19                                                }
     20                                        }
     21                                }
     22
     23                        },
     24
     25                        // Does the element in question have a request in progress?
     26                        inProgress: function( element ) {
     27                                return (
     28                                        this.xhrs &&
     29                                        'undefined' !== typeof this.xhrs[ element ] &&
     30                                        4 !== this.xhrs[ element ].readyState
     31                                );
     32                        },
     33
     34                        // Set up xhr record for an element.
     35                        setXhrs: function( element, xhr ) {
     36                                this.xhrs[ element ] = xhr;
     37                        }
     38                }
     39        }();
     40} ) ();
  • src/wp-includes/script-loader.php

    diff --git src/wp-includes/script-loader.php src/wp-includes/script-loader.php
    index 45cb46348d..8b866a94d3 100644
    function wp_default_scripts( &$scripts ) { 
    190190                )
    191191        );
    192192
    193         $scripts->add( 'wp-lists', "/wp-includes/js/wp-lists$suffix.js", array( 'wp-ajax-response', 'jquery-color' ), false, 1 );
     193        $scripts->add( 'xhrs', "/wp-includes/js/xhrs$suffix.js", array(), false, 1 );
     194        $scripts->add( 'wp-lists', "/wp-includes/js/wp-lists$suffix.js", array( 'wp-ajax-response', 'jquery-color', 'xhrs' ), false, 1 );
    194195
    195196        // WordPress no longer uses or bundles Prototype or script.aculo.us. These are now pulled from an external source.
    196197        $scripts->add( 'prototype', 'https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js', array(), '1.7.1' );
    function wp_default_scripts( &$scripts ) { 
    693694                        )
    694695                );
    695696
    696                 $scripts->add( 'admin-comments', "/wp-admin/js/edit-comments$suffix.js", array( 'wp-lists', 'quicktags', 'jquery-query' ), false, 1 );
     697                $scripts->add( 'admin-comments', "/wp-admin/js/edit-comments$suffix.js", array( 'wp-lists', 'quicktags', 'jquery-query', 'xhrs' ), false, 1 );
    697698                did_action( 'init' ) && $scripts->localize(
    698699                        'admin-comments', 'adminCommentsL10n', array(
    699700                                'hotkeys_highlight_first' => isset( $_GET['hotkeys_highlight_first'] ),