From 4ec275f5526b74011410fcf8f60e2f33666b8869 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Tue, 26 Jul 2022 13:21:55 +0000 Subject: [PATCH] Taxonomy: Allow filtering Ajax term search results in quick edit. This changeset introduces the `ajax_term_search_results` hook which can be used to filter the term search results returned by the AJAX term query. Props grandeljay, costdev, ironprogrammer, audrasjb, SergeyBiryukov. Fixes #55606. git-svn-id: https://develop.svn.wordpress.org/trunk@53781 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ajax-actions.php | 11 +++++++++ tests/phpunit/tests/ajax/TagSearch.php | 34 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 49276b4bd0..f69892ba7e 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -162,6 +162,17 @@ function wp_ajax_ajax_tag_search() { ) ); + /** + * Filters the Ajax term search results. + * + * @since 6.1.0 + * + * @param string[] $results Array of term names. + * @param WP_Taxonomy $tax The taxonomy object. + * @param string $s The search term. + */ + $results = apply_filters( 'ajax_term_search_results', $results, $tax, $s ); + echo implode( "\n", $results ); wp_die(); } diff --git a/tests/phpunit/tests/ajax/TagSearch.php b/tests/phpunit/tests/ajax/TagSearch.php index a70a1d6964..3374033092 100644 --- a/tests/phpunit/tests/ajax/TagSearch.php +++ b/tests/phpunit/tests/ajax/TagSearch.php @@ -158,4 +158,38 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase { $this->_handleAjax( 'ajax-tag-search' ); } + /** + * Test the ajax_term_search_results filter + * + * @ticket 55606 + */ + public function test_ajax_term_search_results_filter() { + + // Become an administrator. + $this->_setRole( 'administrator' ); + + // Set up a default request. + $_GET['tax'] = 'post_tag'; + $_GET['q'] = 'chat'; + + // Add the ajax_term_search_results filter. + add_filter( + 'ajax_term_search_results', + static function( $results, $tax, $s ) { + return array( 'ajax_term_search_results was applied' ); + }, + 10, + 3 + ); + + // Make the request. + try { + $this->_handleAjax( 'ajax-tag-search', $_GET['tax'], $_GET['q'] ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + // Ensure we found the right match. + $this->assertSame( 'ajax_term_search_results was applied', $this->_last_response ); + } }