Make WordPress Core

Changeset 58957


Ignore:
Timestamp:
08/30/2024 10:15:59 PM (6 weeks ago)
Author:
joedolson
Message:

Plugins: Delay AJAX search until after 2 characters.

Prevent the AJAX search results from firing until after at least 2 characters have been typed into the search boxes. Toggle autocomplete value once AJAX is firing. Add a changeable minimum character threshold.

Props armandsdz, adamsilverstein, afercia, mklusak, finalwebsites, joedolson.
Fixes #38211.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/wp/updates.js

    r58455 r58957  
    143143
    144144    /**
     145     * Minimum number of characters before an ajax search is fired.
     146     *
     147     * @since 6.7.0
     148     *
     149     * @type {number}
     150     */
     151    wp.updates.searchMinCharacters = 2;
     152
     153    /**
    145154     * Whether filesystem credentials need to be requested from the user.
    146155     *
     
    29782987        }
    29792988
     2989        // Track the previous search string length.
     2990        var previousSearchStringLength = 0;
     2991        wp.updates.shouldSearch = function( searchStringLength ) {
     2992            var shouldSearch = searchStringLength >= wp.updates.searchMinCharacters ||
     2993                previousSearchStringLength > wp.updates.searchMinCharacters;
     2994            previousSearchStringLength = searchStringLength;
     2995            return shouldSearch;
     2996        };
     2997
    29802998        /**
    29812999         * Handles changes to the plugin search box on the new-plugin page,
     
    29853003         */
    29863004        $pluginInstallSearch.on( 'keyup input', _.debounce( function( event, eventtype ) {
    2987             var $searchTab = $( '.plugin-install-search' ), data, searchLocation;
     3005            var $searchTab = $( '.plugin-install-search' ), data, searchLocation,
     3006                searchStringLength = $pluginInstallSearch.val().length;
    29883007
    29893008            data = {
     
    29953014            };
    29963015            searchLocation = location.href.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) );
     3016
     3017            // Set the autocomplete attribute, turning off autocomplete 1 character before ajax search kicks in.
     3018            if ( wp.updates.shouldSearch( searchStringLength ) ) {
     3019                $pluginInstallSearch.attr( 'autocomplete', 'off' );
     3020            } else {
     3021                $pluginInstallSearch.attr( 'autocomplete', 'on' );
     3022                return;
     3023            }
    29973024
    29983025            // Clear on escape.
     
    30553082        if ( $pluginSearch.length ) {
    30563083            $pluginSearch.attr( 'aria-describedby', 'live-search-desc' );
     3084
    30573085        }
    30583086
     
    30703098                plugin_status: 'all'
    30713099            },
    3072             queryArgs;
     3100            queryArgs,
     3101            searchStringLength = $pluginSearch.val().length;
     3102
     3103            // Set the autocomplete attribute, turning off autocomplete 1 character before ajax search kicks in.
     3104            if ( wp.updates.shouldSearch( searchStringLength ) ) {
     3105                $pluginSearch.attr( 'autocomplete', 'off' );
     3106            } else {
     3107                $pluginSearch.attr( 'autocomplete', 'on' );
     3108                return;
     3109            }
    30733110
    30743111            // Clear on escape.
Note: See TracChangeset for help on using the changeset viewer.