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.
Built from https://develop.svn.wordpress.org/trunk@53801


git-svn-id: http://core.svn.wordpress.org/trunk@53360 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-07-30 15:16:08 +00:00
parent 75ff07ccbf
commit f53c815b6e
2 changed files with 42 additions and 39 deletions

View File

@ -108,54 +108,54 @@ function wp_ajax_ajax_tag_search() {
wp_die( 0 );
}
$taxonomy = sanitize_key( $_GET['tax'] );
$tax = get_taxonomy( $taxonomy );
$taxonomy = sanitize_key( $_GET['tax'] );
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $tax ) {
if ( ! $taxonomy_object ) {
wp_die( 0 );
}
if ( ! current_user_can( $tax->cap->assign_terms ) ) {
if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {
wp_die( -1 );
}
$s = wp_unslash( $_GET['q'] );
$search = wp_unslash( $_GET['q'] );
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma ) {
$s = str_replace( $comma, ',', $s );
$search = str_replace( $comma, ',', $search );
}
if ( false !== strpos( $s, ',' ) ) {
$s = explode( ',', $s );
$s = $s[ count( $s ) - 1 ];
if ( false !== strpos( $search, ',' ) ) {
$search = explode( ',', $search );
$search = $search[ count( $search ) - 1 ];
}
$s = trim( $s );
$search = trim( $search );
/**
* Filters the minimum number of characters required to fire a tag search via Ajax.
*
* @since 4.0.0
*
* @param int $characters The minimum number of characters required. Default 2.
* @param WP_Taxonomy $tax The taxonomy object.
* @param string $s The search term.
* @param int $characters The minimum number of characters required. Default 2.
* @param WP_Taxonomy $taxonomy_object The taxonomy object.
* @param string $search The search term.
*/
$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $taxonomy_object, $search );
/*
* Require $term_search_min_chars chars for matching (default: 2)
* ensure it's a non-negative, non-zero integer.
*/
if ( ( 0 == $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) {
if ( ( 0 == $term_search_min_chars ) || ( strlen( $search ) < $term_search_min_chars ) ) {
wp_die();
}
$results = get_terms(
array(
'taxonomy' => $taxonomy,
'name__like' => $s,
'name__like' => $search,
'fields' => 'names',
'hide_empty' => false,
'number' => isset( $_GET['number'] ) ? (int) $_GET['number'] : 0,
@ -167,11 +167,11 @@ function wp_ajax_ajax_tag_search() {
*
* @since 6.1.0
*
* @param string[] $results Array of term names.
* @param WP_Taxonomy $tax The taxonomy object.
* @param string $s The search term.
* @param string[] $results Array of term names.
* @param WP_Taxonomy $taxonomy_object The taxonomy object.
* @param string $search The search term.
*/
$results = apply_filters( 'ajax_term_search_results', $results, $tax, $s );
$results = apply_filters( 'ajax_term_search_results', $results, $taxonomy_object, $search );
echo implode( "\n", $results );
wp_die();
@ -1021,9 +1021,10 @@ function wp_ajax_add_link_category( $action ) {
}
check_ajax_referer( $action );
$tax = get_taxonomy( 'link_category' );
if ( ! current_user_can( $tax->cap->manage_terms ) ) {
$taxonomy_object = get_taxonomy( 'link_category' );
if ( ! current_user_can( $taxonomy_object->cap->manage_terms ) ) {
wp_die( -1 );
}
@ -1067,10 +1068,11 @@ function wp_ajax_add_link_category( $action ) {
*/
function wp_ajax_add_tag() {
check_ajax_referer( 'add-tag', '_wpnonce_add-tag' );
$taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
$tax = get_taxonomy( $taxonomy );
if ( ! current_user_can( $tax->cap->edit_terms ) ) {
$taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
wp_die( -1 );
}
@ -1122,8 +1124,8 @@ function wp_ajax_add_tag() {
require ABSPATH . 'wp-admin/includes/edit-tag-messages.php';
$message = '';
if ( isset( $messages[ $tax->name ][1] ) ) {
$message = $messages[ $tax->name ][1];
if ( isset( $messages[ $taxonomy_object->name ][1] ) ) {
$message = $messages[ $taxonomy_object->name ][1];
} elseif ( isset( $messages['_item'][1] ) ) {
$message = $messages['_item'][1];
}
@ -1161,14 +1163,14 @@ function wp_ajax_get_tagcloud() {
wp_die( 0 );
}
$taxonomy = sanitize_key( $_POST['tax'] );
$tax = get_taxonomy( $taxonomy );
$taxonomy = sanitize_key( $_POST['tax'] );
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $tax ) {
if ( ! $taxonomy_object ) {
wp_die( 0 );
}
if ( ! current_user_can( $tax->cap->assign_terms ) ) {
if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {
wp_die( -1 );
}
@ -1182,7 +1184,7 @@ function wp_ajax_get_tagcloud() {
);
if ( empty( $tags ) ) {
wp_die( $tax->labels->not_found );
wp_die( $taxonomy_object->labels->not_found );
}
if ( is_wp_error( $tags ) ) {
@ -2143,10 +2145,10 @@ function wp_ajax_inline_save() {
function wp_ajax_inline_save_tax() {
check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
$taxonomy = sanitize_key( $_POST['taxonomy'] );
$tax = get_taxonomy( $taxonomy );
$taxonomy = sanitize_key( $_POST['taxonomy'] );
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $tax ) {
if ( ! $taxonomy_object ) {
wp_die( 0 );
}
@ -2208,15 +2210,16 @@ function wp_ajax_find_posts() {
$post_types = get_post_types( array( 'public' => true ), 'objects' );
unset( $post_types['attachment'] );
$s = wp_unslash( $_POST['ps'] );
$args = array(
'post_type' => array_keys( $post_types ),
'post_status' => 'any',
'posts_per_page' => 50,
);
if ( '' !== $s ) {
$args['s'] = $s;
$search = wp_unslash( $_POST['ps'] );
if ( '' !== $search ) {
$args['s'] = $search;
}
$posts = get_posts( $args );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-53800';
$wp_version = '6.1-alpha-53801';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.