From aa38888d60b3d9191568b310541b829a6005d24e Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 18 Dec 2015 18:37:41 +0000 Subject: [PATCH] Add `current-cat-ancestor` class to ancestor items in `wp_list_categories()`. Pairs nicely with `current-cat-parent`. Props jrchamp, swisssipdy, ardathksheyna, wonderboymusic. Fixes #10676. git-svn-id: https://develop.svn.wordpress.org/trunk@36008 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-walker-category.php | 7 +++++++ .../tests/category/wpListCategories.php | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/wp-includes/class-walker-category.php b/src/wp-includes/class-walker-category.php index da518c9ee1..8ef3c58d52 100644 --- a/src/wp-includes/class-walker-category.php +++ b/src/wp-includes/class-walker-category.php @@ -170,6 +170,13 @@ class Walker_Category extends Walker { } elseif ( $category->term_id == $_current_term->parent ) { $css_classes[] = 'current-cat-parent'; } + while ( $_current_term->parent ) { + if ( $category->term_id == $_current_term->parent ) { + $css_classes[] = 'current-cat-ancestor'; + break; + } + $_current_term = get_term( $_current_term->parent, $category->taxonomy ); + } } } diff --git a/tests/phpunit/tests/category/wpListCategories.php b/tests/phpunit/tests/category/wpListCategories.php index dad221f7e7..061496a8a6 100644 --- a/tests/phpunit/tests/category/wpListCategories.php +++ b/tests/phpunit/tests/category/wpListCategories.php @@ -381,4 +381,25 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase { $this->assertNotContains( '
  • ', $actual ); $this->assertNotContains( '
  • ', $actual ); } + + /** + * @ticket 10676 + */ + public function test_class_containing_current_cat_ancestor() { + $parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) ); + $child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) ); + $child2 = self::factory()->category->create( array( 'name' => 'Child 2', 'slug' => 'child2', 'parent' => $parent ) ); + $grandchild = self::factory()->category->create( array( 'name' => 'Grand Child', 'slug' => 'child', 'parent' => $child ) ); + + $actual = wp_list_categories( array( + 'echo' => 0, + 'hide_empty' => false, + 'current_category' => $grandchild, + ) ); + + $this->assertRegExp( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual ); + $this->assertRegExp( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual ); + $this->assertNotRegExp( '/class="[^"]*cat-item-' . $grandchild . '[^"]*current-cat-ancestor[^"]*"/', $actual ); + $this->assertNotRegExp( '/class="[^"]*cat-item-' . $child2 . '[^"]*current-cat-ancestor[^"]*"/', $actual ); + } }