diff --git a/src/wp-includes/class-walker-category.php b/src/wp-includes/class-walker-category.php index 997993710a..16ad852e5b 100644 --- a/src/wp-includes/class-walker-category.php +++ b/src/wp-includes/class-walker-category.php @@ -139,7 +139,7 @@ class Walker_Category extends Walker { $attributes = ''; foreach ( $atts as $attr => $value ) { - if ( ! empty( $value ) ) { + if ( is_scalar( $value ) && '' !== $value && false !== $value ) { $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' ' . $attr . '="' . $value . '"'; } diff --git a/src/wp-includes/class-walker-nav-menu.php b/src/wp-includes/class-walker-nav-menu.php index 06070ed0c8..1d0fd714f7 100644 --- a/src/wp-includes/class-walker-nav-menu.php +++ b/src/wp-includes/class-walker-nav-menu.php @@ -203,7 +203,7 @@ class Walker_Nav_Menu extends Walker { $attributes = ''; foreach ( $atts as $attr => $value ) { - if ( ! empty( $value ) ) { + if ( is_scalar( $value ) && '' !== $value && false !== $value ) { $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' ' . $attr . '="' . $value . '"'; } diff --git a/src/wp-includes/class-walker-page.php b/src/wp-includes/class-walker-page.php index 27e06c9b5d..eac5a4f9fd 100644 --- a/src/wp-includes/class-walker-page.php +++ b/src/wp-includes/class-walker-page.php @@ -182,7 +182,7 @@ class Walker_Page extends Walker { $attributes = ''; foreach ( $atts as $attr => $value ) { - if ( ! empty( $value ) ) { + if ( is_scalar( $value ) && '' !== $value && false !== $value ) { $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' ' . $attr . '="' . $value . '"'; } diff --git a/tests/phpunit/tests/category/walkerCategory.php b/tests/phpunit/tests/category/walkerCategory.php new file mode 100644 index 0000000000..aec86e173a --- /dev/null +++ b/tests/phpunit/tests/category/walkerCategory.php @@ -0,0 +1,92 @@ +walker = new Walker_Category(); + } + + /** + * @ticket 47720 + * + * @dataProvider data_start_el_with_empty_attributes + */ + public function test_start_el_with_empty_attributes( $value, $expected ) { + $output = ''; + $category = $this->factory->category->create_and_get(); + $link = get_term_link( $category ); + + $args = array( + 'use_desc_for_title' => 0, + 'style' => 'list', + ); + + add_filter( + 'category_list_link_attributes', + function( $atts ) use ( $value ) { + $atts['data-test'] = $value; + return $atts; + } + ); + + $this->walker->start_el( $output, $category, 0, $args ); + + if ( '' !== $expected ) { + $expected = sprintf( ' data-test="%s"', $expected ); + } + + $this->assertSame( "