WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/30/2018 12:16:37 AM (3 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/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}
Note: See TracChangeset for help on using the changeset viewer.