Default `$parent` in `category_exists()` should default to null rather than 0.
[29863] made the corresponding change in `term_exists()`. Failure to change the default value in `category_exists()` meant that an unspecified value for `$parent` would limit results to top-level categories. Includes unit tests and corrected function documentation. Props hissy. Fixes #30975 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@31140 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f323dc9b48
commit
77128eb048
|
@ -11,14 +11,17 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@internal Missing Short Description}}
|
* Check whether a category exists.
|
||||||
*
|
*
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*
|
*
|
||||||
* @param int|string $cat_name
|
* @see term_exists()
|
||||||
* @return int
|
*
|
||||||
|
* @param int|string $cat_name Category name.
|
||||||
|
* @param int $parent Optional. ID of parent term.
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function category_exists($cat_name, $parent = 0) {
|
function category_exists( $cat_name, $parent = null ) {
|
||||||
$id = term_exists($cat_name, 'category', $parent);
|
$id = term_exists($cat_name, 'category', $parent);
|
||||||
if ( is_array($id) )
|
if ( is_array($id) )
|
||||||
$id = $id['term_id'];
|
$id = $id['term_id'];
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Tests_Term_CategoryExists extends WP_UnitTestCase {
|
||||||
|
/**
|
||||||
|
* @ticket 30975
|
||||||
|
*/
|
||||||
|
public function test_category_exists_should_return_only_top_level_categories_when_parent_is_0() {
|
||||||
|
$c1 = $this->factory->category->create();
|
||||||
|
$c2 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
'parent' => $c1,
|
||||||
|
) );
|
||||||
|
$c3 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$found = category_exists( 'Foo', 0 );
|
||||||
|
|
||||||
|
$this->assertEquals( $found, $c3 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30975
|
||||||
|
*/
|
||||||
|
public function test_category_exists_should_select_oldest_matching_category_when_no_parent_is_specified_1() {
|
||||||
|
// Foo child of c1 is created first.
|
||||||
|
$c1 = $this->factory->category->create();
|
||||||
|
$c2 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
'parent' => $c1,
|
||||||
|
) );
|
||||||
|
$c3 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$found = category_exists( 'Foo' );
|
||||||
|
|
||||||
|
$this->assertEquals( $found, $c2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30975
|
||||||
|
*/
|
||||||
|
public function test_category_exists_should_select_oldest_matching_category_when_no_parent_is_specified_2() {
|
||||||
|
// Top-level Foo is created first.
|
||||||
|
$c1 = $this->factory->category->create();
|
||||||
|
$c2 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
) );
|
||||||
|
$c3 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
'parent' => $c1,
|
||||||
|
) );
|
||||||
|
|
||||||
|
$found = category_exists( 'Foo' );
|
||||||
|
|
||||||
|
$this->assertEquals( $found, $c2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 30975
|
||||||
|
*/
|
||||||
|
public function test_category_exists_should_respect_nonempty_parent() {
|
||||||
|
$c1 = $this->factory->category->create();
|
||||||
|
$c2 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
'parent' => $c1,
|
||||||
|
) );
|
||||||
|
$c3 = $this->factory->category->create( array(
|
||||||
|
'name' => 'Foo',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$found = category_exists( 'Foo', $c1 );
|
||||||
|
|
||||||
|
$this->assertEquals( $found, $c2 );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue