mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Taxonomy: Cache term objects in WP_Term_Query if query is filtered.
When utilizing the `terms_clauses` or `get_terms_fields` filters within `WP_Term_Query` and the selected fields are modified, the entire term object is now cached. This adjustment is necessary because filters can broaden the selected fields beyond just the term ID. Fields linked to the term object, such as the count or parent, may undergo modifications when queried. Caching the complete object ensures the accurate storage of these modified fields within the cache. Props spacedmonkey, tnolte, peterwilsoncc. Fixes #58116. git-svn-id: https://develop.svn.wordpress.org/trunk@56555 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -738,6 +738,8 @@ class WP_Term_Query {
|
||||
$order = isset( $clauses['order'] ) ? $clauses['order'] : '';
|
||||
$limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
|
||||
|
||||
$fields_is_filtered = implode( ', ', $selects ) !== $fields;
|
||||
|
||||
if ( $where ) {
|
||||
$where = "WHERE $where";
|
||||
}
|
||||
@@ -782,7 +784,7 @@ class WP_Term_Query {
|
||||
$cache = array_map( 'intval', $cache );
|
||||
} elseif ( 'count' !== $_fields ) {
|
||||
if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
|
||||
|| ( 'all' === $_fields && $args['pad_counts'] )
|
||||
|| ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered )
|
||||
) {
|
||||
$term_ids = wp_list_pluck( $cache, 'term_id' );
|
||||
} else {
|
||||
@@ -884,6 +886,8 @@ class WP_Term_Query {
|
||||
$object->count = $term->count;
|
||||
$term_cache[] = $object;
|
||||
}
|
||||
} elseif ( $fields_is_filtered ) {
|
||||
$term_cache = $term_objects;
|
||||
} else {
|
||||
$term_cache = wp_list_pluck( $term_objects, 'term_id' );
|
||||
}
|
||||
|
||||
@@ -698,6 +698,106 @@ class Tests_Term_Query extends WP_UnitTestCase {
|
||||
$this->assertSame( 0, $q->query( $args ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* If fields have filtered, cached results should work.
|
||||
*
|
||||
* @ticket 58116
|
||||
* @group cache
|
||||
*/
|
||||
public function test_query_filter_fields() {
|
||||
$post_id = self::factory()->post->create();
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
)
|
||||
);
|
||||
wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
|
||||
$post_draft_id = self::factory()->post->create( array( 'post_type' => 'draft' ) );
|
||||
wp_set_object_terms( $post_draft_id, array( $term_id ), 'wptests_tax' );
|
||||
|
||||
add_filter( 'terms_clauses', array( $this, 'filter_fields_terms_clauses' ), 10, 3 );
|
||||
|
||||
$args = array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
'hide_empty' => false,
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
|
||||
$q1 = new WP_Term_Query();
|
||||
$terms1 = $q1->query( $args );
|
||||
$q2 = new WP_Term_Query();
|
||||
$terms2 = $q2->query( $args );
|
||||
$this->assertSameSets( wp_list_pluck( $terms1, 'term_id' ), wp_list_pluck( $terms2, 'term_id' ), 'Term IDs are expected to match' );
|
||||
$this->assertSameSets( wp_list_pluck( $terms1, 'count' ), wp_list_pluck( $terms2, 'count' ), 'Term counts are expected to match' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter `terms_clauses` to change the field requested. The filter is from example code given in #58116.
|
||||
*/
|
||||
public function filter_fields_terms_clauses( $clauses, $taxonomies, $args ) {
|
||||
global $wpdb;
|
||||
|
||||
// Set to query specific posts types if set.
|
||||
if ( ! empty( $args['post_type'] ) ) {
|
||||
$clauses['fields'] = 'DISTINCT t.term_id, tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent, COUNT(p.post_type) AS count';
|
||||
$clauses['join'] .= ' LEFT JOIN ' . $wpdb->term_relationships . ' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' AS p ON p.ID = r.object_id';
|
||||
$clauses['where'] .= " AND (p.post_type = '" . $args['post_type'] . "' OR p.post_type IS NULL)";
|
||||
$clauses['orderby'] = 'GROUP BY t.term_id ' . $clauses['orderby'];
|
||||
}
|
||||
|
||||
// Set to query posts with specific status.
|
||||
if ( ! empty( $args['post_status'] ) ) {
|
||||
$clauses['where'] .= " AND (p.post_status = '" . $args['post_status'] . "')";
|
||||
}
|
||||
return $clauses;
|
||||
}
|
||||
|
||||
/**
|
||||
* If fields have filtered, cached results should work.
|
||||
*
|
||||
* @ticket 58116
|
||||
* @group cache
|
||||
*/
|
||||
public function test_query_filter_select_fields() {
|
||||
$post_id = self::factory()->post->create();
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
)
|
||||
);
|
||||
wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
|
||||
$post_draft_id = self::factory()->post->create( array( 'post_type' => 'draft' ) );
|
||||
wp_set_object_terms( $post_draft_id, array( $term_id ), 'wptests_tax' );
|
||||
|
||||
add_filter( 'get_terms_fields', array( $this, 'filter_get_terms_fields' ) );
|
||||
|
||||
$args = array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
'hide_empty' => false,
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
|
||||
$q1 = new WP_Term_Query();
|
||||
$terms1 = $q1->query( $args );
|
||||
$q2 = new WP_Term_Query();
|
||||
$terms2 = $q2->query( $args );
|
||||
$this->assertSameSets( wp_list_pluck( $terms1, 'term_id' ), wp_list_pluck( $terms2, 'term_id' ), 'Term IDs are expected to match' );
|
||||
$this->assertSameSets( wp_list_pluck( $terms1, 'parent' ), wp_list_pluck( $terms2, 'parent' ), 'Term parent are expected to match' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter `get_terms_fields` to change the field requested.
|
||||
*/
|
||||
public function filter_get_terms_fields( $select ) {
|
||||
return array( 't.term_id', 'tt.parent' );
|
||||
}
|
||||
|
||||
/**
|
||||
* The terms property should be an empty array for fields not as count and parent set.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user