diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 3d6935084c7f..426c54a07650 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -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']; @@ -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 ) { @@ -537,14 +543,24 @@ 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'] ); @@ -552,14 +568,25 @@ public function get_terms() { } 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 @@ 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. * @@ -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'] : ''; @@ -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' );