Skip to content

Commit

Permalink
Coding Standards: Simplify some long conditions in `wp-includes/class…
Browse files Browse the repository at this point in the history
…-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.

git-svn-id: https://develop.svn.wordpress.org/trunk@52972 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Mar 21, 2022
1 parent 97c716c commit 9f2a856
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/wp-includes/class-wp-term-query.php
Expand Up @@ -453,7 +453,8 @@ public function get_terms() {
$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'];
Expand Down Expand Up @@ -526,7 +527,12 @@ public function get_terms() {
$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 ) {
Expand All @@ -537,29 +543,50 @@ public function get_terms() {
$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'] );

$this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
}

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'] );

Expand Down Expand Up @@ -665,6 +692,8 @@ public function get_terms() {

$where = implode( ' AND ', $this->sql_clauses['where'] );

$pieces = compact( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );

/**
* Filters the terms query SQL clauses.
*
Expand All @@ -674,7 +703,7 @@ public function get_terms() {
* @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'] : '';
Expand Down Expand Up @@ -716,14 +745,18 @@ public function get_terms() {

// $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' );
Expand Down

0 comments on commit 9f2a856

Please sign in to comment.