REST API: Restore use of wp_ajax_ajax_tag_search() for tag search.

This solution does not work with custom taxonomies in the current state.

Reverts [42614,42619,42737].

Props danielbachhuber.
See #38922.

git-svn-id: https://develop.svn.wordpress.org/trunk@44537 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers 2019-01-10 02:36:40 +00:00
parent 0394aab7a8
commit 2fe6670aff
4 changed files with 90 additions and 79 deletions

View File

@ -1,13 +1,12 @@
/**
* Default settings for jQuery UI Autocomplete for use with non-hierarchical taxonomies.
*
* @output wp-admin/js/tags-suggest.js
*/
( function( $ ) {
if ( typeof window.tagsSuggestL10n === 'undefined' || typeof window.uiAutocompleteL10n === 'undefined' ) {
return;
}
var tempID = 0;
var separator = window.tagsSuggestL10n.tagDelimiter || ',';
function split( val ) {
@ -54,15 +53,33 @@
term = getLast( request.term );
$.get( window.tagsSuggestL10n.restURL, {
_fields: [ 'id', 'name' ],
taxonomy: taxonomy,
search: term
$.get( window.ajaxurl, {
action: 'ajax-tag-search',
tax: taxonomy,
q: term
} ).always( function() {
$element.removeClass( 'ui-autocomplete-loading' ); // UI fails to remove this sometimes?
} ).done( function( data ) {
cache = data;
response( data );
var tagName;
var tags = [];
if ( data ) {
data = data.split( '\n' );
for ( tagName in data ) {
var id = ++tempID;
tags.push({
id: id,
name: data[tagName]
});
}
cache = tags;
response( tags );
} else {
response( tags );
}
} );
last = request.term;
@ -101,7 +118,7 @@
close: function() {
$element.attr( 'aria-expanded', 'false' );
},
minLength: window.tagsSuggestL10n.minChars,
minLength: 2,
position: {
my: 'left top+2',
at: 'left bottom',

View File

@ -99,6 +99,70 @@ function wp_ajax_fetch_list() {
wp_die( 0 );
}
/**
* Ajax handler for tag search.
*
* @since 3.1.0
*/
function wp_ajax_ajax_tag_search() {
if ( ! isset( $_GET['tax'] ) ) {
wp_die( 0 );
}
$taxonomy = sanitize_key( $_GET['tax'] );
$tax = get_taxonomy( $taxonomy );
if ( ! $tax ) {
wp_die( 0 );
}
if ( ! current_user_can( $tax->cap->assign_terms ) ) {
wp_die( -1 );
}
$s = wp_unslash( $_GET['q'] );
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma ) {
$s = str_replace( $comma, ',', $s );
}
if ( false !== strpos( $s, ',' ) ) {
$s = explode( ',', $s );
$s = $s[ count( $s ) - 1 ];
}
$s = trim( $s );
/**
* Filters the minimum number of characters required to fire a tag search via Ajax.
*
* @since 4.0.0
*
* @param int $characters The minimum number of characters required. Default 2.
* @param WP_Taxonomy $tax The taxonomy object.
* @param string $s The search term.
*/
$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
/*
* Require $term_search_min_chars chars for matching (default: 2)
* ensure it's a non-negative, non-zero integer.
*/
if ( ( $term_search_min_chars == 0 ) || ( strlen( $s ) < $term_search_min_chars ) ) {
wp_die();
}
$results = get_terms(
$taxonomy,
array(
'name__like' => $s,
'fields' => 'names',
'hide_empty' => false,
)
);
echo join( $results, "\n" );
wp_die();
}
/**
* Ajax handler for compression testing.
*

View File

@ -1514,61 +1514,3 @@ function options_permalink_add_js() {
</script>
<?php
}
/**
* Ajax handler for tag search.
*
* @since 3.1.0
* @deprecated 5.0.0 Use the REST API tags endpoint instead.
*/
function wp_ajax_ajax_tag_search() {
_deprecated_function( __FUNCTION__, '5.0.0', '/wp-json/wp/v2/tags' );
if ( ! isset( $_GET['tax'] ) ) {
wp_die( 0 );
}
$taxonomy = sanitize_key( $_GET['tax'] );
$tax = get_taxonomy( $taxonomy );
if ( ! $tax ) {
wp_die( 0 );
}
if ( ! current_user_can( $tax->cap->assign_terms ) ) {
wp_die( -1 );
}
$s = wp_unslash( $_GET['q'] );
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma ) {
$s = str_replace( $comma, ',', $s );
}
if ( false !== strpos( $s, ',' ) ) {
$s = explode( ',', $s );
$s = $s[ count( $s ) - 1 ];
}
$s = trim( $s );
/** This filter is documented in wp-includes/script-loader.php */
$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
/*
* Require $term_search_min_chars chars for matching (default: 2)
* ensure it's a non-negative, non-zero integer.
*/
if ( ( $term_search_min_chars == 0 ) || ( strlen( $s ) < $term_search_min_chars ) ) {
wp_die();
}
$results = get_terms(
$taxonomy, array(
'name__like' => $s,
'fields' => 'names',
'hide_empty' => false,
)
);
echo join( $results, "\n" );
wp_die();
}

View File

@ -39,8 +39,6 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
/**
* Test as an admin
*
* @expectedDeprecated wp_ajax_ajax_tag_search
*/
public function test_post_tag() {
@ -64,8 +62,6 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
/**
* Test with no results
*
* @expectedDeprecated wp_ajax_ajax_tag_search
*/
public function test_no_results() {
@ -84,8 +80,6 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
/**
* Test with commas
*
* @expectedDeprecated wp_ajax_ajax_tag_search
*/
public function test_with_comma() {
@ -109,8 +103,6 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
/**
* Test as a logged out user
*
* @expectedDeprecated wp_ajax_ajax_tag_search
*/
public function test_logged_out() {
@ -128,8 +120,6 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
/**
* Test with an invalid taxonomy type
*
* @expectedDeprecated wp_ajax_ajax_tag_search
*/
public function test_invalid_tax() {
@ -147,8 +137,6 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
/**
* Test as an unprivileged user
*
* @expectedDeprecated wp_ajax_ajax_tag_search
*/
public function test_unprivileged_user() {