Skip to content

Commit 0244d18

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@58957 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c6ab7b2 commit 0244d18

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/js/_enqueues/wp/updates.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@
141141
*/
142142
wp.updates.searchTerm = '';
143143

144+
/**
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+
144153
/**
145154
* Whether filesystem credentials need to be requested from the user.
146155
*
@@ -2977,14 +2986,24 @@
29772986
$pluginInstallSearch.attr( 'aria-describedby', 'live-search-desc' );
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,
29823000
* searching the repository dynamically.
29833001
*
29843002
* @since 4.6.0
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 = {
29903009
_ajax_nonce: wp.updates.ajaxNonce,
@@ -2995,6 +3014,14 @@
29953014
};
29963015
searchLocation = location.href.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) );
29973016

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+
}
3024+
29983025
// Clear on escape.
29993026
if ( 'keyup' === event.type && 27 === event.which ) {
30003027
event.target.value = '';
@@ -3054,6 +3081,7 @@
30543081

30553082
if ( $pluginSearch.length ) {
30563083
$pluginSearch.attr( 'aria-describedby', 'live-search-desc' );
3084+
30573085
}
30583086

30593087
/**
@@ -3069,7 +3097,16 @@
30693097
pagenow: pagenow,
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.
30753112
if ( 'keyup' === event.type && 27 === event.which ) {

0 commit comments

Comments
 (0)