Make WordPress Core


Ignore:
Timestamp:
01/30/2018 12:16:37 AM (7 years ago)
Author:
adamsilverstein
Message:

Taxonomy: Use REST API for ajax tag search.

Deprecate wp_ajax_ajax_tag_search and switch to using the REST API when searching tags in the tags meta box.

Props nacin, chriscct7, afercia, swissspidy, jnylen0, rmccue, ryelle.
Fixes #38922.

File:
1 edited

Legend:

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

    r42384 r42614  
    9898
    9999    wp_die( 0 );
    100 }
    101 
    102 /**
    103  * Ajax handler for tag search.
    104  *
    105  * @since 3.1.0
    106  */
    107 function 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, array(
    155             'name__like' => $s,
    156             'fields'     => 'names',
    157             'hide_empty' => false,
    158         )
    159     );
    160 
    161     echo join( $results, "\n" );
    162     wp_die();
    163100}
    164101
Note: See TracChangeset for help on using the changeset viewer.