Skip to content

Commit aa03861

Browse files
committed
Taxonomy: Ensure _pad_term_counts() handles post types safely.This commit updates _pad_term_counts() to use $wpdb->prepare() for all database queries, improving security and consistency. Additionally, it introduces a filter using post_type_exists() to ensure that only registered post types are queried, aligning the behavior with _update_post_term_count().Includes comprehensive unit tests to verify the logic and prevent regressions.Fixes #65055.See WordPress#11542.
1 parent 4d3b0b9 commit aa03861

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/wp-includes/taxonomy.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4065,8 +4065,20 @@ function _pad_term_counts( &$terms, $taxonomy ) {
40654065

40664066
// Get the object and term IDs and stick them in a lookup table.
40674067
$tax_obj = get_taxonomy( $taxonomy );
4068-
$object_types = esc_sql( $tax_obj->object_type );
4069-
$results = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode( ',', array_keys( $term_ids ) ) . ") AND post_type IN ('" . implode( "', '", $object_types ) . "') AND post_status = 'publish'" );
4068+
$object_types = (array) $tax_obj->object_type;
4069+
4070+
//Filter invalid post types for consistency with _update_post_term_count().
4071+
$object_types = array_filter( $object_types, 'post_type_exists' );
4072+
if ( empty( $object_types ) ) {
4073+
return;
4074+
}
4075+
$object_types = esc_sql( $object_types );
4076+
$results = $wpdb->get_results(
4077+
$wpdb->prepare(
4078+
"SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode( ',', array_fill( 0, count( $term_ids ), '%d' ) ) . ') AND post_type IN (' . implode( ',', array_fill( 0, count( $object_types ), '%s' ) ) . ") AND post_status = 'publish'",
4079+
array_merge( array_keys( $term_ids ), $object_types )
4080+
)
4081+
);
40704082

40714083
foreach ( $results as $row ) {
40724084
$id = $term_ids[ $row->term_taxonomy_id ];

tests/phpunit/tests/taxonomy.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,4 +1124,40 @@ public function test_default_term_for_post_in_multiple_taxonomies() {
11241124
$this->assertContains( $tax1, $taxonomies );
11251125
$this->assertContains( $tax2, $taxonomies );
11261126
}
1127+
1128+
/**
1129+
* @ticket 65055
1130+
* Filter invalid post types before SQL query
1131+
*/
1132+
public function test_pad_term_counts_with_standard_post_types() {
1133+
register_post_type( 'book' );
1134+
register_taxonomy( 'genre', 'book' );
1135+
1136+
$parent = self::factory()->term->create( array( 'taxonomy' => 'genre' ) );
1137+
$child = self::factory()->term->create( array( 'taxonomy' => 'genre', 'parent' => $parent ) );
1138+
1139+
$post_id = self::factory()->post->create( array( 'post_type' => 'book' ) );
1140+
wp_set_object_terms( $post_id, $child, 'genre' );
1141+
1142+
$terms = get_terms( array( 'taxonomy' => 'genre', 'hide_empty' => false ) );
1143+
_pad_term_counts( $terms, 'genre' );
1144+
1145+
$parent_term = wp_list_filter( $terms, array( 'term_id' => $parent ) );
1146+
$this->assertEquals( 1, current( $parent_term )->count, 'Parent terms should include post counts from their child terms' );
1147+
}
1148+
1149+
/**
1150+
* @ticket 65055
1151+
*/
1152+
public function test_pad_term_counts_with_invalid_post_types() {
1153+
register_taxonomy( 'invalid_tax', array( 'non_existent_type' ) );
1154+
1155+
self::factory()->term->create( array( 'taxonomy' => 'invalid_tax' ) );
1156+
$terms = get_terms( array( 'taxonomy' => 'invalid_tax', 'hide_empty' => false ) );
1157+
1158+
_pad_term_counts( $terms, 'invalid_tax' );
1159+
1160+
$this->assertEquals( 0, $terms[0]->count );
1161+
}
1162+
11271163
}

0 commit comments

Comments
 (0)