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
This commit is contained in:
parent
4cead42b26
commit
fd8b7faff6
@ -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 = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
|
||||
if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
|
||||
/**
|
||||
|
39
tests/phpunit/tests/category/wpListCategories.php
Normal file
39
tests/phpunit/tests/category/wpListCategories.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group category
|
||||
*/
|
||||
class Tests_Category_WpListCategories extends WP_UnitTestCase {
|
||||
/**
|
||||
* @ticket 16792
|
||||
*/
|
||||
public function test_should_not_create_element_when_cat_name_is_filtered_to_empty_string() {
|
||||
$c1 = $this->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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user