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.
Built from https://develop.svn.wordpress.org/trunk@58957


git-svn-id: http://core.svn.wordpress.org/trunk@58353 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
joedolson 2024-08-30 22:17:16 +00:00
parent 42c2e3578e
commit 8d6f540982
3 changed files with 41 additions and 4 deletions

View File

@ -141,6 +141,15 @@
*/ */
wp.updates.searchTerm = ''; wp.updates.searchTerm = '';
/**
* Minimum number of characters before an ajax search is fired.
*
* @since 6.7.0
*
* @type {number}
*/
wp.updates.searchMinCharacters = 2;
/** /**
* Whether filesystem credentials need to be requested from the user. * Whether filesystem credentials need to be requested from the user.
* *
@ -2977,6 +2986,15 @@
$pluginInstallSearch.attr( 'aria-describedby', 'live-search-desc' ); $pluginInstallSearch.attr( 'aria-describedby', 'live-search-desc' );
} }
// Track the previous search string length.
var previousSearchStringLength = 0;
wp.updates.shouldSearch = function( searchStringLength ) {
var shouldSearch = searchStringLength >= wp.updates.searchMinCharacters ||
previousSearchStringLength > wp.updates.searchMinCharacters;
previousSearchStringLength = searchStringLength;
return shouldSearch;
};
/** /**
* Handles changes to the plugin search box on the new-plugin page, * Handles changes to the plugin search box on the new-plugin page,
* searching the repository dynamically. * searching the repository dynamically.
@ -2984,7 +3002,8 @@
* @since 4.6.0 * @since 4.6.0
*/ */
$pluginInstallSearch.on( 'keyup input', _.debounce( function( event, eventtype ) { $pluginInstallSearch.on( 'keyup input', _.debounce( function( event, eventtype ) {
var $searchTab = $( '.plugin-install-search' ), data, searchLocation; var $searchTab = $( '.plugin-install-search' ), data, searchLocation,
searchStringLength = $pluginInstallSearch.val().length;
data = { data = {
_ajax_nonce: wp.updates.ajaxNonce, _ajax_nonce: wp.updates.ajaxNonce,
@ -2995,6 +3014,14 @@
}; };
searchLocation = location.href.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) ); searchLocation = location.href.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) );
// Set the autocomplete attribute, turning off autocomplete 1 character before ajax search kicks in.
if ( wp.updates.shouldSearch( searchStringLength ) ) {
$pluginInstallSearch.attr( 'autocomplete', 'off' );
} else {
$pluginInstallSearch.attr( 'autocomplete', 'on' );
return;
}
// Clear on escape. // Clear on escape.
if ( 'keyup' === event.type && 27 === event.which ) { if ( 'keyup' === event.type && 27 === event.which ) {
event.target.value = ''; event.target.value = '';
@ -3054,6 +3081,7 @@
if ( $pluginSearch.length ) { if ( $pluginSearch.length ) {
$pluginSearch.attr( 'aria-describedby', 'live-search-desc' ); $pluginSearch.attr( 'aria-describedby', 'live-search-desc' );
} }
/** /**
@ -3069,7 +3097,16 @@
pagenow: pagenow, pagenow: pagenow,
plugin_status: 'all' plugin_status: 'all'
}, },
queryArgs; queryArgs,
searchStringLength = $pluginSearch.val().length;
// Set the autocomplete attribute, turning off autocomplete 1 character before ajax search kicks in.
if ( wp.updates.shouldSearch( searchStringLength ) ) {
$pluginSearch.attr( 'autocomplete', 'off' );
} else {
$pluginSearch.attr( 'autocomplete', 'on' );
return;
}
// Clear on escape. // Clear on escape.
if ( 'keyup' === event.type && 27 === event.which ) { if ( 'keyup' === event.type && 27 === event.which ) {

File diff suppressed because one or more lines are too long

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-alpha-58956'; $wp_version = '6.7-alpha-58957';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.