diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 81a5fcfa89..3d6935084c 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -526,11 +526,9 @@ class WP_Term_Query { $this->sql_clauses['where']['exclusions'] = preg_replace( '/^\s*AND\s*/', '', $exclusions ); } - if ( - ( ! empty( $args['name'] ) ) || - ( is_string( $args['name'] ) && 0 !== strlen( $args['name'] ) ) - ) { - $names = (array) $args['name']; + $args['name'] = is_string( $args['name'] ) && 0 === strlen( $args['name'] ) ? array() : (array) $args['name']; + if ( ! empty( $args['name'] ) ) { + $names = $args['name']; foreach ( $names as &$_name ) { // `sanitize_term_field()` returns slashed data. $_name = stripslashes( sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' ) ); @@ -539,26 +537,18 @@ class WP_Term_Query { $this->sql_clauses['where']['name'] = "t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')"; } - if ( - ( ! empty( $args['slug'] ) ) || - ( is_string( $args['slug'] ) && 0 !== strlen( $args['slug'] ) ) - ) { - if ( is_array( $args['slug'] ) ) { - $slug = array_map( 'sanitize_title', $args['slug'] ); - $this->sql_clauses['where']['slug'] = "t.slug IN ('" . implode( "', '", $slug ) . "')"; - } else { - $slug = sanitize_title( $args['slug'] ); - $this->sql_clauses['where']['slug'] = "t.slug = '$slug'"; - } + $args['slug'] = is_string( $args['slug'] ) && 0 === strlen( $args['slug'] ) ? array() : 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 ( ! empty( $args['term_taxonomy_id'] ) ) { - if ( is_array( $args['term_taxonomy_id'] ) ) { - $tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) ); - $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})"; - } else { - $this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( 'tt.term_taxonomy_id = %d', $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'] ) ) { @@ -569,13 +559,10 @@ class WP_Term_Query { $this->sql_clauses['where']['description__like'] = $wpdb->prepare( 'tt.description LIKE %s', '%' . $wpdb->esc_like( $args['description__like'] ) . '%' ); } + $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 = $args['object_ids']; - if ( ! is_array( $object_ids ) ) { - $object_ids = array( $object_ids ); - } + $object_ids = implode( ', ', $args['object_ids'] ); - $object_ids = implode( ', ', array_map( 'intval', $object_ids ) ); $this->sql_clauses['where']['object_ids'] = "tr.object_id IN ($object_ids)"; } @@ -728,7 +715,12 @@ class WP_Term_Query { } // $args can be anything. Only use the args defined in defaults to compute the key. - $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request ); + $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' ); diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index d4d235798b..b1901886a8 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -4,13 +4,40 @@ * @group taxonomy */ class Tests_Term_getTerms extends WP_UnitTestCase { + + protected static $taxonomy = 'wptests_tax_3'; + public function set_up() { parent::set_up(); + register_taxonomy( self::$taxonomy, 'post', array( 'hierarchical' => true ) ); + _clean_term_filters(); wp_cache_delete( 'last_changed', 'terms' ); } + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + register_taxonomy( self::$taxonomy, 'page', array( 'hierarchical' => true ) ); + $term_id1 = $factory->term->create( + array( + 'name' => 'Foo', + 'slug' => 'Foo', + 'taxonomy' => self::$taxonomy, + ) + ); + $term_id2 = $factory->term->create( + array( + 'name' => 'Bar', + 'slug' => 'bar', + 'taxonomy' => self::$taxonomy, + ) + ); + $posts = $factory->post->create_many( 3, array( 'post_type' => 'page' ) ); + foreach ( $posts as $i => $post ) { + wp_set_object_terms( $post, array( $term_id1, $term_id2 ), self::$taxonomy ); + } + } + /** * @ticket 37568 */ @@ -2988,6 +3015,214 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertSameSets( array( $term_id ), wp_list_pluck( $found, 'term_id' ) ); } + + /** + * @ticket 55352 + */ + public function test_cache_key_generation_cache_domain() { + $args_1 = array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + ); + $args_2 = array_merge( $args_1, array( 'cache_domain' => microtime() ) ); + $query1 = get_terms( $args_1 ); + $num_queries_1 = get_num_queries(); + $query2 = get_terms( $args_2 ); + $this->assertNotSame( $num_queries_1, get_num_queries() ); + $this->assertSameSets( $query1, $query2 ); + } + + /** + * @ticket 55352 + */ + public function test_cache_key_generation_all_with_object_id() { + $args_1 = array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + ); + $args_2 = array_merge( $args_1, array( 'fields' => 'all_with_object_id' ) ); + $query1 = get_terms( $args_1 ); + $num_queries_1 = get_num_queries(); + $query2 = get_terms( $args_2 ); + $this->assertNotSame( $num_queries_1, get_num_queries() ); + $this->assertSameSets( $query1, wp_list_pluck( $query2, 'term_id' ) ); + } + + + /** + * @ticket 55352 + * + * @dataProvider data_same_term_args + */ + public function test_cache_key_generation( $args_1, $args_2 ) { + $query1 = get_terms( $args_1 ); + $num_queries_1 = get_num_queries(); + $query2 = get_terms( $args_2 ); + $this->assertSame( $num_queries_1, get_num_queries() ); + $this->assertSame( count( $query1 ), count( $query2 ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_same_term_args() { + return array( + 'all fields vs ids' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + ), + ), + 'array taxonomy vs string taxonomy' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + ), + array( + 'taxonomy' => array( self::$taxonomy ), + 'fields' => 'all', + ), + ), + 'slug fields vs names fields' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'names', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'slugs', + ), + ), + 'meta cache off, pad count on vs meta cache on, pad count off' => array( + array( + 'taxonomy' => self::$taxonomy, + 'pad_counts' => true, + 'update_term_meta_cache' => false, + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + ), + ), + 'array slug vs string slug' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'slug' => '', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'slug' => array(), + ), + ), + 'array object_ids vs string object_ids' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'object_ids' => '', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'object_ids' => array(), + ), + ), + 'array term_taxonomy_id vs string term_taxonomy_id' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'term_taxonomy_id' => '', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'term_taxonomy_id' => array(), + ), + ), + 'array 1 slug vs string slug' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'slug' => 'bar', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'slug' => array( 'bar' ), + ), + ), + 'int object_ids vs array object_ids' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'object_ids' => 1, + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'object_ids' => array( 1 ), + ), + ), + 'string object_ids vs array object_ids' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'object_ids' => '1', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'object_ids' => array( 1 ), + ), + ), + 'int term_taxonomy_id vs array term_taxonomy_id and fields different' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'term_taxonomy_id' => 1, + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'term_taxonomy_id' => array( 1 ), + ), + ), + 'same arguments in a different order' => array( + array( + 'fields' => 'ids', + 'taxonomy' => self::$taxonomy, + 'term_taxonomy_id' => 1, + ), + array( + 'term_taxonomy_id' => 1, + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + ), + ), + 'invalid arguments discarded in cache key' => array( + array( + 'fields' => 'ids', + 'taxonomy' => self::$taxonomy, + 'term_taxonomy_id' => 1, + 'ticket_number' => '55352', + ), + array( + 'fields' => 'all', + 'taxonomy' => self::$taxonomy, + 'term_taxonomy_id' => array( 1 ), + 'focus' => 'performance', + ), + ), + ); + } + /** * @ticket 21760 */