Fix term_exists() for parent = 0.

Passing a 0 (or '0') as the 'parent' param of term_exists() should limit
results to terms with no parent.

Adds unit test.

Fixes #29851.

git-svn-id: https://develop.svn.wordpress.org/trunk@29863 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-10-09 02:48:47 +00:00
parent 01c529e062
commit 568b43f242
2 changed files with 29 additions and 4 deletions

View File

@ -1655,12 +1655,12 @@ function get_terms( $taxonomies, $args = '' ) {
* *
* @param int|string $term The term to check * @param int|string $term The term to check
* @param string $taxonomy The taxonomy name to use * @param string $taxonomy The taxonomy name to use
* @param int $parent ID of parent term under which to confine the exists search. * @param int $parent Optional. ID of parent term under which to confine the exists search.
* @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified
* and the term ID exists. Returns an array of the term ID and the term taxonomy ID * and the term ID exists. Returns an array of the term ID and the term taxonomy ID
* if the taxonomy is specified and the pairing exists. * if the taxonomy is specified and the pairing exists.
*/ */
function term_exists($term, $taxonomy = '', $parent = 0) { function term_exists( $term, $taxonomy = '', $parent = null ) {
global $wpdb; global $wpdb;
$select = "SELECT term_id FROM $wpdb->terms as t WHERE "; $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
@ -1686,8 +1686,8 @@ function term_exists($term, $taxonomy = '', $parent = 0) {
$where_fields = array($slug); $where_fields = array($slug);
$else_where_fields = array($term); $else_where_fields = array($term);
if ( !empty($taxonomy) ) { if ( !empty($taxonomy) ) {
$parent = (int) $parent; if ( is_numeric( $parent ) ) {
if ( $parent > 0 ) { $parent = (int) $parent;
$where_fields[] = $parent; $where_fields[] = $parent;
$else_where_fields[] = $parent; $else_where_fields[] = $parent;
$where .= ' AND tt.parent = %d'; $where .= ' AND tt.parent = %d';

View File

@ -138,6 +138,31 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertEquals( $t, $found['term_id'] ); $this->assertEquals( $t, $found['term_id'] );
} }
/**
* @ticket 29851
*/
public function test_term_exists_taxonomy_nonempty_parent_0_should_return_false_for_child_term() {
register_taxonomy( 'foo', 'post', array(
'hierarchical' => true,
) );
$parent_term = $this->factory->term->create( array(
'taxonomy' => 'foo',
) );
$t = $this->factory->term->create( array(
'taxonomy' => 'foo',
'parent' => $parent_term,
'slug' => 'child-term',
) );
$found = term_exists( 'child-term', 'foo', 0 );
_unregister_taxonomy( 'foo' );
$this->assertSame( null, $found['term_id'] );
}
public function test_term_exists_taxonomy_nonempty_parent_nonempty_match_name() { public function test_term_exists_taxonomy_nonempty_parent_nonempty_match_name() {
register_taxonomy( 'foo', 'post', array( register_taxonomy( 'foo', 'post', array(
'hierarchical' => true, 'hierarchical' => true,