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


git-svn-id: http://core.svn.wordpress.org/trunk@44368 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
desrosj 2019-01-10 02:37:51 +00:00
parent 8441700842
commit 6fbf4ceaea
5 changed files with 92 additions and 69 deletions

View File

@ -99,6 +99,70 @@ function wp_ajax_fetch_list() {
wp_die( 0 ); 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. * Ajax handler for compression testing.
* *

View File

@ -1514,61 +1514,3 @@ function options_permalink_add_js() {
</script> </script>
<?php <?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

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

View File

@ -1 +1 @@
!function(a){function b(a){return a.split(new RegExp(d+"\\s*"))}function c(a){return b(a).pop()}if("undefined"!=typeof window.tagsSuggestL10n&&"undefined"!=typeof window.uiAutocompleteL10n){var d=window.tagsSuggestL10n.tagDelimiter||",";a.fn.wpTagsSuggest=function(e){var f,g,h=a(this);e=e||{};var i=e.taxonomy||h.attr("data-wp-taxonomy")||"post_tag";return delete e.taxonomy,e=a.extend({source:function(b,d){var e;return g===b.term?void d(f):(e=c(b.term),a.get(window.tagsSuggestL10n.restURL,{_fields:["id","name"],taxonomy:i,search:e}).always(function(){h.removeClass("ui-autocomplete-loading")}).done(function(a){f=a,d(a)}),void(g=b.term))},focus:function(a,b){h.attr("aria-activedescendant","wp-tags-autocomplete-"+b.item.id),a.preventDefault()},select:function(c,e){var f=b(h.val());return f.pop(),f.push(e.item.name,""),h.val(f.join(d+" ")),a.ui.keyCode.TAB===c.keyCode?(window.wp.a11y.speak(window.tagsSuggestL10n.termSelected,"assertive"),c.preventDefault()):a.ui.keyCode.ENTER===c.keyCode&&(c.preventDefault(),c.stopPropagation()),!1},open:function(){h.attr("aria-expanded","true")},close:function(){h.attr("aria-expanded","false")},minLength:window.tagsSuggestL10n.minChars,position:{my:"left top+2",at:"left bottom",collision:"none"},messages:{noResults:window.uiAutocompleteL10n.noResults,results:function(a){return a>1?window.uiAutocompleteL10n.manyResults.replace("%d",a):window.uiAutocompleteL10n.oneResult}}},e),h.on("keydown",function(){h.removeAttr("aria-activedescendant")}).autocomplete(e).autocomplete("instance")._renderItem=function(b,c){return a('<li role="option" id="wp-tags-autocomplete-'+c.id+'">').text(c.name).appendTo(b)},h.attr({role:"combobox","aria-autocomplete":"list","aria-expanded":"false","aria-owns":h.autocomplete("widget").attr("id")}).on("focus",function(){var a=b(h.val()).pop();a&&h.autocomplete("search")}).autocomplete("widget").addClass("wp-tags-autocomplete").attr("role","listbox").removeAttr("tabindex").on("menufocus",function(a,b){b.item.attr("aria-selected","true")}).on("menublur",function(){a(this).find('[aria-selected="true"]').removeAttr("aria-selected")}),this}}}(jQuery); !function(a){function b(a){return a.split(new RegExp(e+"\\s*"))}function c(a){return b(a).pop()}if("undefined"!=typeof window.tagsSuggestL10n&&"undefined"!=typeof window.uiAutocompleteL10n){var d=0,e=window.tagsSuggestL10n.tagDelimiter||",";a.fn.wpTagsSuggest=function(f){var g,h,i=a(this);f=f||{};var j=f.taxonomy||i.attr("data-wp-taxonomy")||"post_tag";return delete f.taxonomy,f=a.extend({source:function(b,e){var f;return h===b.term?void e(g):(f=c(b.term),a.get(window.ajaxurl,{action:"ajax-tag-search",tax:j,q:f}).always(function(){i.removeClass("ui-autocomplete-loading")}).done(function(a){var b,c=[];if(a){a=a.split("\n");for(b in a){var f=++d;c.push({id:f,name:a[b]})}g=c,e(c)}else e(c)}),void(h=b.term))},focus:function(a,b){i.attr("aria-activedescendant","wp-tags-autocomplete-"+b.item.id),a.preventDefault()},select:function(c,d){var f=b(i.val());return f.pop(),f.push(d.item.name,""),i.val(f.join(e+" ")),a.ui.keyCode.TAB===c.keyCode?(window.wp.a11y.speak(window.tagsSuggestL10n.termSelected,"assertive"),c.preventDefault()):a.ui.keyCode.ENTER===c.keyCode&&(c.preventDefault(),c.stopPropagation()),!1},open:function(){i.attr("aria-expanded","true")},close:function(){i.attr("aria-expanded","false")},minLength:2,position:{my:"left top+2",at:"left bottom",collision:"none"},messages:{noResults:window.uiAutocompleteL10n.noResults,results:function(a){return a>1?window.uiAutocompleteL10n.manyResults.replace("%d",a):window.uiAutocompleteL10n.oneResult}}},f),i.on("keydown",function(){i.removeAttr("aria-activedescendant")}).autocomplete(f).autocomplete("instance")._renderItem=function(b,c){return a('<li role="option" id="wp-tags-autocomplete-'+c.id+'">').text(c.name).appendTo(b)},i.attr({role:"combobox","aria-autocomplete":"list","aria-expanded":"false","aria-owns":i.autocomplete("widget").attr("id")}).on("focus",function(){var a=b(i.val()).pop();a&&i.autocomplete("search")}).autocomplete("widget").addClass("wp-tags-autocomplete").attr("role","listbox").removeAttr("tabindex").on("menufocus",function(a,b){b.item.attr("aria-selected","true")}).on("menublur",function(){a(this).find('[aria-selected="true"]').removeAttr("aria-selected")}),this}}}(jQuery);

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.1-alpha-44536'; $wp_version = '5.1-alpha-44537';
/** /**
* 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.