Coding Standards: Simplify some long conditions in `wp-includes/class-wp-term-query.php`.

This aims to improve readability and make the logic easier to parse at a glance.

Follow-up to [40293], [52970].

See #55352, #54728.
Built from https://develop.svn.wordpress.org/trunk@52972


git-svn-id: http://core.svn.wordpress.org/trunk@52561 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-03-21 11:56:04 +00:00
parent 7fda1f8e69
commit 83c4cdf92b
2 changed files with 42 additions and 9 deletions

View File

@ -453,7 +453,8 @@ class WP_Term_Query {
$order = $this->parse_order( $this->query_vars['order'] );
if ( $taxonomies ) {
$this->sql_clauses['where']['taxonomy'] = "tt.taxonomy IN ('" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')";
$this->sql_clauses['where']['taxonomy'] =
"tt.taxonomy IN ('" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')";
}
$exclude = $args['exclude'];
@ -526,7 +527,12 @@ class WP_Term_Query {
$this->sql_clauses['where']['exclusions'] = preg_replace( '/^\s*AND\s*/', '', $exclusions );
}
$args['name'] = is_string( $args['name'] ) && 0 === strlen( $args['name'] ) ? array() : (array) $args['name'];
if ( '' === $args['name'] ) {
$args['name'] = array();
} else {
$args['name'] = (array) $args['name'];
}
if ( ! empty( $args['name'] ) ) {
$names = $args['name'];
foreach ( $names as &$_name ) {
@ -537,14 +543,24 @@ class WP_Term_Query {
$this->sql_clauses['where']['name'] = "t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')";
}
$args['slug'] = is_string( $args['slug'] ) && 0 === strlen( $args['slug'] ) ? array() : array_map( 'sanitize_title', (array) $args['slug'] );
if ( '' === $args['slug'] ) {
$args['slug'] = array();
} else {
$args['slug'] = array_map( 'sanitize_title', (array) $args['slug'] );
}
if ( ! empty( $args['slug'] ) ) {
$slug = implode( "', '", $args['slug'] );
$this->sql_clauses['where']['slug'] = "t.slug IN ('" . $slug . "')";
}
$args['term_taxonomy_id'] = is_string( $args['term_taxonomy_id'] ) && 0 === strlen( $args['term_taxonomy_id'] ) ? array() : array_map( 'intval', (array) $args['term_taxonomy_id'] );
if ( '' === $args['term_taxonomy_id'] ) {
$args['term_taxonomy_id'] = array();
} else {
$args['term_taxonomy_id'] = array_map( 'intval', (array) $args['term_taxonomy_id'] );
}
if ( ! empty( $args['term_taxonomy_id'] ) ) {
$tt_ids = implode( ',', $args['term_taxonomy_id'] );
@ -552,14 +568,25 @@ class WP_Term_Query {
}
if ( ! empty( $args['name__like'] ) ) {
$this->sql_clauses['where']['name__like'] = $wpdb->prepare( 't.name LIKE %s', '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
$this->sql_clauses['where']['name__like'] = $wpdb->prepare(
't.name LIKE %s',
'%' . $wpdb->esc_like( $args['name__like'] ) . '%'
);
}
if ( ! empty( $args['description__like'] ) ) {
$this->sql_clauses['where']['description__like'] = $wpdb->prepare( 'tt.description LIKE %s', '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
$this->sql_clauses['where']['description__like'] = $wpdb->prepare(
'tt.description LIKE %s',
'%' . $wpdb->esc_like( $args['description__like'] ) . '%'
);
}
if ( '' === $args['object_ids'] ) {
$args['object_ids'] = array();
} else {
$args['object_ids'] = array_map( 'intval', (array) $args['object_ids'] );
}
$args['object_ids'] = is_string( $args['object_ids'] ) && 0 === strlen( $args['object_ids'] ) ? array() : array_map( 'intval', (array) $args['object_ids'] );
if ( ! empty( $args['object_ids'] ) ) {
$object_ids = implode( ', ', $args['object_ids'] );
@ -665,6 +692,8 @@ class WP_Term_Query {
$where = implode( ' AND ', $this->sql_clauses['where'] );
$pieces = compact( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );
/**
* Filters the terms query SQL clauses.
*
@ -674,7 +703,7 @@ class WP_Term_Query {
* @param string[] $taxonomies An array of taxonomy names.
* @param array $args An array of term query arguments.
*/
$clauses = apply_filters( 'terms_clauses', compact( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' ), $taxonomies, $args );
$clauses = apply_filters( 'terms_clauses', $pieces, $taxonomies, $args );
$fields = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
$join = isset( $clauses['join'] ) ? $clauses['join'] : '';
@ -716,14 +745,18 @@ class WP_Term_Query {
// $args can be anything. Only use the args defined in defaults to compute the key.
$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
unset( $cache_args['pad_counts'], $cache_args['update_term_meta_cache'] );
if ( 'count' !== $_fields && 'all_with_object_id' !== $_fields ) {
$cache_args['fields'] = 'all';
}
$key = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $this->request );
$last_changed = wp_cache_get_last_changed( 'terms' );
$cache_key = "get_terms:$key:$last_changed";
$cache = wp_cache_get( $cache_key, 'terms' );
if ( false !== $cache ) {
if ( 'ids' === $_fields ) {
$term_ids = wp_list_pluck( $cache, 'term_id' );

View File

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