Avoid database error when `include` or `exclude` is not really a `term_id`. Adds more unit tests.

Props kovshenin.
Fixes #11823.



git-svn-id: https://develop.svn.wordpress.org/trunk@25257 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-09-05 17:14:54 +00:00
parent 4286692d7d
commit e8c7634e79
2 changed files with 19 additions and 5 deletions

View File

@ -1310,9 +1310,10 @@ function get_terms($taxonomies, $args = '') {
$inclusions = implode( ',', wp_parse_id_list( $include ) );
}
if ( ! empty( $inclusions ) )
if ( ! empty( $inclusions ) ) {
$inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )';
$where .= $inclusions;
$where .= $inclusions;
}
$exclusions = '';
if ( ! empty( $exclude_tree ) ) {
@ -1339,7 +1340,9 @@ function get_terms($taxonomies, $args = '') {
$exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args );
$where .= $exclusions;
if ( ! empty( $exclusions ) )
$where .= $exclusions;
if ( !empty($slug) ) {
$slug = sanitize_title($slug);

View File

@ -121,10 +121,11 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
}
/**
* @ti
* cket 11823
* @ticket 11823
*/
function test_get_terms_include_exclude() {
global $wpdb;
$term_id1 = $this->factory->tag->create();
$term_id2 = $this->factory->tag->create();
$inc_terms = get_terms( 'post_tag', array(
@ -138,6 +139,16 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
'hide_empty' => false
) );
$this->assertEquals( array(), wp_list_pluck( $exc_terms, 'term_id' ) );
// These should not generate query errors.
get_terms( 'post_tag', array( 'exclude' => array( 0 ), 'hide_empty' => false ) );
$this->assertEmpty( $wpdb->last_error );
get_terms( 'post_tag', array( 'exclude' => array( 'unexpected-string' ), 'hide_empty' => false ) );
$this->assertEmpty( $wpdb->last_error );
get_terms( 'post_tag', array( 'include' => array( 'unexpected-string' ), 'hide_empty' => false ) );
$this->assertEmpty( $wpdb->last_error );
}
/**