From fd8b7faff6e872771dff5849926f4dce1a6079c2 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 3 Jan 2015 02:04:17 +0000 Subject: [PATCH] In `Walker_Category`, don't generate list elements for empty cat names. This change allows the 'list_cats' filter to be used to suppress certain items in category lists, without creating invalid or superfluous markup. Props samo9789. Fixes #16792. git-svn-id: https://develop.svn.wordpress.org/trunk@31025 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/category-template.php | 5 +++ .../tests/category/wpListCategories.php | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/phpunit/tests/category/wpListCategories.php diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 7f2c539676..ad66987f7b 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -996,6 +996,11 @@ class Walker_Category extends Walker { $category ); + // Don't generate an element if the category name is empty. + if ( ! $cat_name ) { + return; + } + $link = 'description ) ) { /** diff --git a/tests/phpunit/tests/category/wpListCategories.php b/tests/phpunit/tests/category/wpListCategories.php new file mode 100644 index 0000000000..dbd4cbb98d --- /dev/null +++ b/tests/phpunit/tests/category/wpListCategories.php @@ -0,0 +1,39 @@ +factory->category->create( array( + 'name' => 'Test Cat 1', + ) ); + $c2 = $this->factory->category->create( array( + 'name' => 'Test Cat 2', + ) ); + + add_filter( 'list_cats', array( $this, 'list_cats_callback' ) ); + $found = wp_list_categories( array( + 'hide_empty' => false, + 'echo' => false, + ) ); + remove_filter( 'list_cats', array( $this, 'list_cats_callback' ) ); + + $this->assertContains( "cat-item-$c2", $found ); + $this->assertContains( 'Test Cat 2', $found ); + + $this->assertNotContains( "cat-item-$c1", $found ); + $this->assertNotContains( 'Test Cat 1', $found ); + } + + public function list_cats_callback( $cat ) { + if ( 'Test Cat 1' === $cat ) { + return ''; + } + + return $cat; + } +}