Make WordPress Core

Changeset 44537


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.

Location:
trunk
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/admin/tags-suggest.js

    r43347 r44537  
    11/**
    22 * Default settings for jQuery UI Autocomplete for use with non-hierarchical taxonomies.
    3  *
    4  * @output wp-admin/js/tags-suggest.js
    53 */
    64( function( $ ) {
     
    97        }
    108
     9        var tempID = 0;
    1110        var separator = window.tagsSuggestL10n.tagDelimiter || ',';
    1211
     
    5554                                term = getLast( request.term );
    5655
    57                                 $.get( window.tagsSuggestL10n.restURL, {
    58                                         _fields: [ 'id', 'name' ],
    59                                         taxonomy: taxonomy,
    60                                         search: term
     56                                $.get( window.ajaxurl, {
     57                                        action: 'ajax-tag-search',
     58                                        tax: taxonomy,
     59                                        q: term
    6160                                } ).always( function() {
    6261                                        $element.removeClass( 'ui-autocomplete-loading' ); // UI fails to remove this sometimes?
    6362                                } ).done( function( data ) {
    64                                         cache = data;
    65                                         response( 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                                        }
    6683                                } );
    6784
     
    102119                                $element.attr( 'aria-expanded', 'false' );
    103120                        },
    104                         minLength: window.tagsSuggestL10n.minChars,
     121                        minLength: 2,
    105122                        position: {
    106123                                my: 'left top+2',
  • 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
  • trunk/src/wp-admin/includes/deprecated.php

    r42737 r44537  
    15151515        <?php
    15161516}
    1517 
    1518 /**
    1519  * Ajax handler for tag search.
    1520  *
    1521  * @since 3.1.0
    1522  * @deprecated 5.0.0 Use the REST API tags endpoint instead.
    1523  */
    1524 function wp_ajax_ajax_tag_search() {
    1525         _deprecated_function( __FUNCTION__, '5.0.0', '/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.