Make WordPress Core


Ignore:
Timestamp:
03/21/2022 11:54:01 AM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r52970 r52972  
    454454
    455455        if ( $taxonomies ) {
    456             $this->sql_clauses['where']['taxonomy'] = "tt.taxonomy IN ('" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')";
     456            $this->sql_clauses['where']['taxonomy'] =
     457                "tt.taxonomy IN ('" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')";
    457458        }
    458459
     
    527528        }
    528529
    529         $args['name'] = is_string( $args['name'] ) && 0 === strlen( $args['name'] ) ? array() : (array) $args['name'];
     530        if ( '' === $args['name'] ) {
     531            $args['name'] = array();
     532        } else {
     533            $args['name'] = (array) $args['name'];
     534        }
     535
    530536        if ( ! empty( $args['name'] ) ) {
    531537            $names = $args['name'];
     
    538544        }
    539545
    540         $args['slug'] = is_string( $args['slug'] ) && 0 === strlen( $args['slug'] ) ? array() : array_map( 'sanitize_title', (array) $args['slug'] );
     546        if ( '' === $args['slug'] ) {
     547            $args['slug'] = array();
     548        } else {
     549            $args['slug'] = array_map( 'sanitize_title', (array) $args['slug'] );
     550        }
     551
    541552        if ( ! empty( $args['slug'] ) ) {
    542553            $slug = implode( "', '", $args['slug'] );
     
    545556        }
    546557
    547         $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'] );
     558        if ( '' === $args['term_taxonomy_id'] ) {
     559            $args['term_taxonomy_id'] = array();
     560        } else {
     561            $args['term_taxonomy_id'] = array_map( 'intval', (array) $args['term_taxonomy_id'] );
     562        }
     563
    548564        if ( ! empty( $args['term_taxonomy_id'] ) ) {
    549565            $tt_ids = implode( ',', $args['term_taxonomy_id'] );
     
    553569
    554570        if ( ! empty( $args['name__like'] ) ) {
    555             $this->sql_clauses['where']['name__like'] = $wpdb->prepare( 't.name LIKE %s', '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
     571            $this->sql_clauses['where']['name__like'] = $wpdb->prepare(
     572                't.name LIKE %s',
     573                '%' . $wpdb->esc_like( $args['name__like'] ) . '%'
     574            );
    556575        }
    557576
    558577        if ( ! empty( $args['description__like'] ) ) {
    559             $this->sql_clauses['where']['description__like'] = $wpdb->prepare( 'tt.description LIKE %s', '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
    560         }
    561 
    562         $args['object_ids'] = is_string( $args['object_ids'] ) && 0 === strlen( $args['object_ids'] ) ? array() : array_map( 'intval', (array) $args['object_ids'] );
     578            $this->sql_clauses['where']['description__like'] = $wpdb->prepare(
     579                'tt.description LIKE %s',
     580                '%' . $wpdb->esc_like( $args['description__like'] ) . '%'
     581            );
     582        }
     583
     584        if ( '' === $args['object_ids'] ) {
     585            $args['object_ids'] = array();
     586        } else {
     587            $args['object_ids'] = array_map( 'intval', (array) $args['object_ids'] );
     588        }
     589
    563590        if ( ! empty( $args['object_ids'] ) ) {
    564591            $object_ids = implode( ', ', $args['object_ids'] );
     
    666693        $where = implode( ' AND ', $this->sql_clauses['where'] );
    667694
     695        $pieces = compact( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );
     696
    668697        /**
    669698         * Filters the terms query SQL clauses.
     
    675704         * @param array    $args       An array of term query arguments.
    676705         */
    677         $clauses = apply_filters( 'terms_clauses', compact( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' ), $taxonomies, $args );
     706        $clauses = apply_filters( 'terms_clauses', $pieces, $taxonomies, $args );
    678707
    679708        $fields   = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
     
    717746        // $args can be anything. Only use the args defined in defaults to compute the key.
    718747        $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
     748
    719749        unset( $cache_args['pad_counts'], $cache_args['update_term_meta_cache'] );
     750
    720751        if ( 'count' !== $_fields && 'all_with_object_id' !== $_fields ) {
    721752            $cache_args['fields'] = 'all';
    722753        }
     754
    723755        $key          = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $this->request );
    724756        $last_changed = wp_cache_get_last_changed( 'terms' );
    725757        $cache_key    = "get_terms:$key:$last_changed";
    726758        $cache        = wp_cache_get( $cache_key, 'terms' );
     759
    727760        if ( false !== $cache ) {
    728761            if ( 'ids' === $_fields ) {
Note: See TracChangeset for help on using the changeset viewer.