Make WordPress Core

Changeset 53801


Ignore:
Timestamp:
07/30/2022 03:14:34 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use more meaningful variables names in some Ajax functions.

This renames $tax to $taxonomy_object and $s to $search for clarity. The latter is only renamed when used as an internal variable and not referring to the $s global.

The list of affected functions:

  • wp_ajax_ajax_tag_search()
  • wp_ajax_add_link_category
  • wp_ajax_add_tag()
  • wp_ajax_get_tagcloud()
  • wp_ajax_inline_save_tax()
  • wp_ajax_find_posts()

Follow-up to [6542], [8901], [10222], [12833], [16771], [16992], [22723], [38698].

Props azouamauriac.
Fixes #55098.

File:
1 edited

Legend:

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

    r53781 r53801  
    109109    }
    110110
    111     $taxonomy = sanitize_key( $_GET['tax'] );
    112     $tax      = get_taxonomy( $taxonomy );
    113 
    114     if ( ! $tax ) {
     111    $taxonomy        = sanitize_key( $_GET['tax'] );
     112    $taxonomy_object = get_taxonomy( $taxonomy );
     113
     114    if ( ! $taxonomy_object ) {
    115115        wp_die( 0 );
    116116    }
    117117
    118     if ( ! current_user_can( $tax->cap->assign_terms ) ) {
     118    if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {
    119119        wp_die( -1 );
    120120    }
    121121
    122     $s = wp_unslash( $_GET['q'] );
     122    $search = wp_unslash( $_GET['q'] );
    123123
    124124    $comma = _x( ',', 'tag delimiter' );
    125125    if ( ',' !== $comma ) {
    126         $s = str_replace( $comma, ',', $s );
    127     }
    128 
    129     if ( false !== strpos( $s, ',' ) ) {
    130         $s = explode( ',', $s );
    131         $s = $s[ count( $s ) - 1 ];
    132     }
    133 
    134     $s = trim( $s );
     126        $search = str_replace( $comma, ',', $search );
     127    }
     128
     129    if ( false !== strpos( $search, ',' ) ) {
     130        $search = explode( ',', $search );
     131        $search = $search[ count( $search ) - 1 ];
     132    }
     133
     134    $search = trim( $search );
    135135
    136136    /**
     
    139139     * @since 4.0.0
    140140     *
    141      * @param int         $characters The minimum number of characters required. Default 2.
    142      * @param WP_Taxonomy $tax        The taxonomy object.
    143      * @param string      $s          The search term.
     141     * @param int         $characters      The minimum number of characters required. Default 2.
     142     * @param WP_Taxonomy $taxonomy_object The taxonomy object.
     143     * @param string      $search          The search term.
    144144     */
    145     $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
     145    $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $taxonomy_object, $search );
    146146
    147147    /*
     
    149149     * ensure it's a non-negative, non-zero integer.
    150150     */
    151     if ( ( 0 == $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) {
     151    if ( ( 0 == $term_search_min_chars ) || ( strlen( $search ) < $term_search_min_chars ) ) {
    152152        wp_die();
    153153    }
     
    156156        array(
    157157            'taxonomy'   => $taxonomy,
    158             'name__like' => $s,
     158            'name__like' => $search,
    159159            'fields'     => 'names',
    160160            'hide_empty' => false,
     
    168168     * @since 6.1.0
    169169     *
    170      * @param string[]    $results Array of term names.
    171      * @param WP_Taxonomy $tax    The taxonomy object.
    172      * @param string      $s       The search term.
     170     * @param string[]    $results         Array of term names.
     171     * @param WP_Taxonomy $taxonomy_object The taxonomy object.
     172     * @param string      $search          The search term.
    173173     */
    174     $results = apply_filters( 'ajax_term_search_results', $results, $tax, $s );
     174    $results = apply_filters( 'ajax_term_search_results', $results, $taxonomy_object, $search );
    175175
    176176    echo implode( "\n", $results );
     
    10221022
    10231023    check_ajax_referer( $action );
    1024     $tax = get_taxonomy( 'link_category' );
    1025 
    1026     if ( ! current_user_can( $tax->cap->manage_terms ) ) {
     1024
     1025    $taxonomy_object = get_taxonomy( 'link_category' );
     1026
     1027    if ( ! current_user_can( $taxonomy_object->cap->manage_terms ) ) {
    10271028        wp_die( -1 );
    10281029    }
     
    10681069function wp_ajax_add_tag() {
    10691070    check_ajax_referer( 'add-tag', '_wpnonce_add-tag' );
    1070     $taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
    1071     $tax      = get_taxonomy( $taxonomy );
    1072 
    1073     if ( ! current_user_can( $tax->cap->edit_terms ) ) {
     1071
     1072    $taxonomy        = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
     1073    $taxonomy_object = get_taxonomy( $taxonomy );
     1074
     1075    if ( ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
    10741076        wp_die( -1 );
    10751077    }
     
    11231125
    11241126    $message = '';
    1125     if ( isset( $messages[ $tax->name ][1] ) ) {
    1126         $message = $messages[ $tax->name ][1];
     1127    if ( isset( $messages[ $taxonomy_object->name ][1] ) ) {
     1128        $message = $messages[ $taxonomy_object->name ][1];
    11271129    } elseif ( isset( $messages['_item'][1] ) ) {
    11281130        $message = $messages['_item'][1];
     
    11621164    }
    11631165
    1164     $taxonomy = sanitize_key( $_POST['tax'] );
    1165     $tax      = get_taxonomy( $taxonomy );
    1166 
    1167     if ( ! $tax ) {
     1166    $taxonomy        = sanitize_key( $_POST['tax'] );
     1167    $taxonomy_object = get_taxonomy( $taxonomy );
     1168
     1169    if ( ! $taxonomy_object ) {
    11681170        wp_die( 0 );
    11691171    }
    11701172
    1171     if ( ! current_user_can( $tax->cap->assign_terms ) ) {
     1173    if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {
    11721174        wp_die( -1 );
    11731175    }
     
    11831185
    11841186    if ( empty( $tags ) ) {
    1185         wp_die( $tax->labels->not_found );
     1187        wp_die( $taxonomy_object->labels->not_found );
    11861188    }
    11871189
     
    21442146    check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
    21452147
    2146     $taxonomy = sanitize_key( $_POST['taxonomy'] );
    2147     $tax      = get_taxonomy( $taxonomy );
    2148 
    2149     if ( ! $tax ) {
     2148    $taxonomy        = sanitize_key( $_POST['taxonomy'] );
     2149    $taxonomy_object = get_taxonomy( $taxonomy );
     2150
     2151    if ( ! $taxonomy_object ) {
    21502152        wp_die( 0 );
    21512153    }
     
    22092211    unset( $post_types['attachment'] );
    22102212
    2211     $s    = wp_unslash( $_POST['ps'] );
    22122213    $args = array(
    22132214        'post_type'      => array_keys( $post_types ),
     
    22162217    );
    22172218
    2218     if ( '' !== $s ) {
    2219         $args['s'] = $s;
     2219    $search = wp_unslash( $_POST['ps'] );
     2220
     2221    if ( '' !== $search ) {
     2222        $args['s'] = $search;
    22202223    }
    22212224
Note: See TracChangeset for help on using the changeset viewer.