From 27c8977af9394716a2af7ae23617d71be1afd03c Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 29 Aug 2015 19:45:35 +0000 Subject: [PATCH] In `wp_list_categories()`, 'current_category' should accept an array of values. This allows the 'current-cat' or 'current-cat-parent' classes to be applied to more than one item in the list. Props vilkatis. Fixes #33565. git-svn-id: https://develop.svn.wordpress.org/trunk@33804 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/category-template.php | 23 +++++++++++++------ .../tests/category/wpListCategories.php | 17 ++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 797d07a20f..e621067e67 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -461,7 +461,8 @@ function wp_dropdown_categories( $args = '' ) { * Display or retrieve the HTML list of categories. * * @since 2.1.0 - * @since 4.4.0 Introduced the `hide_title_if_empty` argument. + * @since 4.4.0 Introduced the `hide_title_if_empty` argument. The `current_category` argument was modified to + * optionally accept an array of values. * * @param string|array $args { * Array of optional arguments. @@ -488,7 +489,8 @@ function wp_dropdown_categories( $args = '' ) { * @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along * with their descendants. See {@link get_terms()}. Default empty string. * @type bool|int $echo True to echo markup, false to return it. Default 1. - * @type int $current_category Category that should get the 'current-cat' class. Default 0. + * @type int|array $current_category ID of category, or array of IDs of categories, that should get the + * 'current-cat' class. Default 0. * @type bool $hierarchical Whether to include terms that have non-empty descendants. * See {@link get_terms()}. Default true. * @type string $title_li Text to use for the list title `
  • ` element. Pass an empty string @@ -1121,11 +1123,18 @@ class Walker_Category extends Walker { ); if ( ! empty( $args['current_category'] ) ) { - $_current_category = get_term( $args['current_category'], $category->taxonomy ); - if ( $category->term_id == $args['current_category'] ) { - $css_classes[] = 'current-cat'; - } elseif ( $category->term_id == $_current_category->parent ) { - $css_classes[] = 'current-cat-parent'; + // 'current_category' can be an array, so we use `get_terms()`. + $_current_terms = get_terms( $category->taxonomy, array( + 'include' => $args['current_category'], + 'hide_empty' => false, + ) ); + + foreach ( $_current_terms as $_current_term ) { + if ( $category->term_id == $_current_term->term_id ) { + $css_classes[] = 'current-cat'; + } elseif ( $category->term_id == $_current_term->parent ) { + $css_classes[] = 'current-cat-parent'; + } } } diff --git a/tests/phpunit/tests/category/wpListCategories.php b/tests/phpunit/tests/category/wpListCategories.php index 92284f8f9f..56ae395a9e 100644 --- a/tests/phpunit/tests/category/wpListCategories.php +++ b/tests/phpunit/tests/category/wpListCategories.php @@ -45,6 +45,23 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase { $this->assertNotRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat-parent[^"]*"/', $found ); } + /** + * @ticket 33565 + */ + public function test_current_category_should_accept_an_array_of_ids() { + $cats = $this->factory->category->create_many( 3 ); + + $found = wp_list_categories( array( + 'echo' => false, + 'hide_empty' => false, + 'current_category' => array( $cats[0], $cats[2] ), + ) ); + + $this->assertRegExp( '/class="[^"]*cat-item-' . $cats[0] . '[^"]*current-cat[^"]*"/', $found ); + $this->assertNotRegExp( '/class="[^"]*cat-item-' . $cats[1] . '[^"]*current[^"]*"/', $found ); + $this->assertRegExp( '/class="[^"]*cat-item-' . $cats[2] . '[^"]*current-cat[^"]*"/', $found ); + } + /** * @ticket 16792 */