Make WordPress Core


Ignore:
Timestamp:
01/10/2019 02:36:40 AM (8 years ago)
Author:
desrosj
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r44335 r44537  
    9898
    9999        wp_die( 0 );
     100}
     101
     102/**
     103 * Ajax handler for tag search.
     104 *
     105 * @since 3.1.0
     106 */
     107function wp_ajax_ajax_tag_search() {
     108        if ( ! isset( $_GET['tax'] ) ) {
     109                wp_die( 0 );
     110        }
     111
     112        $taxonomy = sanitize_key( $_GET['tax'] );
     113        $tax      = get_taxonomy( $taxonomy );
     114        if ( ! $tax ) {
     115                wp_die( 0 );
     116        }
     117
     118        if ( ! current_user_can( $tax->cap->assign_terms ) ) {
     119                wp_die( -1 );
     120        }
     121
     122        $s = wp_unslash( $_GET['q'] );
     123
     124        $comma = _x( ',', 'tag delimiter' );
     125        if ( ',' !== $comma ) {
     126                $s = str_replace( $comma, ',', $s );
     127        }
     128        if ( false !== strpos( $s, ',' ) ) {
     129                $s = explode( ',', $s );
     130                $s = $s[ count( $s ) - 1 ];
     131        }
     132        $s = trim( $s );
     133
     134        /**
     135         * Filters the minimum number of characters required to fire a tag search via Ajax.
     136         *
     137         * @since 4.0.0
     138         *
     139         * @param int         $characters The minimum number of characters required. Default 2.
     140         * @param WP_Taxonomy $tax        The taxonomy object.
     141         * @param string      $s          The search term.
     142         */
     143        $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
     144
     145        /*
     146         * Require $term_search_min_chars chars for matching (default: 2)
     147         * ensure it's a non-negative, non-zero integer.
     148         */
     149        if ( ( $term_search_min_chars == 0 ) || ( strlen( $s ) < $term_search_min_chars ) ) {
     150                wp_die();
     151        }
     152
     153        $results = get_terms(
     154                $taxonomy,
     155                array(
     156                        'name__like' => $s,
     157                        'fields'     => 'names',
     158                        'hide_empty' => false,
     159                )
     160        );
     161
     162        echo join( $results, "\n" );
     163        wp_die();
    100164}
    101165
Note: See TracChangeset for help on using the changeset viewer.