Make WordPress Core

Changeset 42614


Ignore:
Timestamp:
01/30/2018 12:16:37 AM (6 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.

Location:
trunk/src
Files:
4 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
  • trunk/src/wp-admin/includes/deprecated.php

    r41598 r42614  
    15151515    <?php
    15161516}
     1517
     1518/**
     1519 * Ajax handler for tag search.
     1520 *
     1521 * @since 3.1.0
     1522 * @deprecated 4.9.0 Use the REST API tags endpoint instead.
     1523 */
     1524function wp_ajax_ajax_tag_search() {
     1525    _deprecated_function( __FUNCTION__, '4.8', '/wp-json/wp/v2/tags' );
     1526
     1527    if ( ! isset( $_GET['tax'] ) ) {
     1528        wp_die( 0 );
     1529    }
     1530
     1531    $taxonomy = sanitize_key( $_GET['tax'] );
     1532    $tax      = get_taxonomy( $taxonomy );
     1533    if ( ! $tax ) {
     1534        wp_die( 0 );
     1535    }
     1536
     1537    if ( ! current_user_can( $tax->cap->assign_terms ) ) {
     1538        wp_die( -1 );
     1539    }
     1540
     1541    $s = wp_unslash( $_GET['q'] );
     1542
     1543    $comma = _x( ',', 'tag delimiter' );
     1544    if ( ',' !== $comma ) {
     1545        $s = str_replace( $comma, ',', $s );
     1546    }
     1547    if ( false !== strpos( $s, ',' ) ) {
     1548        $s = explode( ',', $s );
     1549        $s = $s[ count( $s ) - 1 ];
     1550    }
     1551    $s = trim( $s );
     1552
     1553    /** This filter is documented in wp-includes/script-loader.php */
     1554    $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
     1555
     1556    /*
     1557     * Require $term_search_min_chars chars for matching (default: 2)
     1558     * ensure it's a non-negative, non-zero integer.
     1559     */
     1560    if ( ( $term_search_min_chars == 0 ) || ( strlen( $s ) < $term_search_min_chars ) ) {
     1561        wp_die();
     1562    }
     1563
     1564    $results = get_terms(
     1565        $taxonomy, array(
     1566            'name__like' => $s,
     1567            'fields'     => 'names',
     1568            'hide_empty' => false,
     1569        )
     1570    );
     1571
     1572    echo join( $results, "\n" );
     1573    wp_die();
     1574}
  • trunk/src/wp-admin/js/tags-suggest.js

    r40357 r42614  
    5454                term = getLast( request.term );
    5555
    56                 $.get( window.ajaxurl, {
    57                     action: 'ajax-tag-search',
    58                     tax: taxonomy,
    59                     q: term
     56                $.get( window.tagsSuggestL10n.restURL, {
     57                    _fields: [ 'id', 'name' ],
     58                    taxonomy: taxonomy,
     59                    search: term
    6060                } ).always( function() {
    6161                    $element.removeClass( 'ui-autocomplete-loading' ); // UI fails to remove this sometimes?
    6262                } ).done( function( data ) {
    63                     var tagName;
    64                     var tags = [];
    65 
    66                     if ( data ) {
    67                         data = data.split( '\n' );
    68 
    69                         for ( tagName in data ) {
    70                             var id = ++tempID;
    71 
    72                             tags.push({
    73                                 id: id,
    74                                 name: data[tagName]
    75                             });
    76                         }
    77 
    78                         cache = tags;
    79                         response( tags );
    80                     } else {
    81                         response( tags );
    82                     }
     63                    cache = data;
     64                    response( data );
    8365                } );
    8466
     
    119101                $element.attr( 'aria-expanded', 'false' );
    120102            },
    121             minLength: 2,
     103            minLength: window.tagsSuggestL10n.minChars,
    122104            position: {
    123105                my: 'left top+2',
  • trunk/src/wp-includes/script-loader.php

    r42547 r42614  
    727727                'termAdded'    => __( 'Term added.' ),
    728728                'termRemoved'  => __( 'Term removed.' ),
     729                'restURL'      => rest_url( '/wp/v2/tags' ),
     730
     731                /**
     732                 * Filters the minimum number of characters required to fire a tag search via Ajax.
     733                 *
     734                 * Previous to 4.8.0, this filter passed taxonomy and search context parameters.
     735                 * @since 4.0.0
     736                 *
     737                 * @param int         $characters The minimum number of characters required. Default 2.
     738                 */
     739                'minChars'     => (int) apply_filters( 'term_search_min_chars', 2 ),
    729740            )
    730741        );
Note: See TracChangeset for help on using the changeset viewer.