From e8c7634e792b177fabec83079606fb1143cf3dc7 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 5 Sep 2013 17:14:54 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 9 ++++++--- tests/phpunit/tests/term/getTerms.php | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index c5af198cc7..fa32393b45 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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); diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index aec88e462c..05f771f70c 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -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 ); } /**