Skip to content

Commit 73fe9d5

Browse files
committed
Ensure that a has_children parameter is given to Walker::start_el().
Adds unit tests. Props scribu, obenland. Fixes #14041. git-svn-id: https://develop.svn.wordpress.org/trunk@28824 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 94be170 commit 73fe9d5

3 files changed

Lines changed: 65 additions & 6 deletions

File tree

src/wp-includes/class-wp-walker.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ class Walker {
3939
*/
4040
protected $max_pages = 1;
4141

42+
/**
43+
* Wether the current element has children or not. To be used in start_el()
44+
*
45+
* @since 4.0.0
46+
* @var bool
47+
* @access protected
48+
*/
49+
protected $has_children;
50+
4251
/**
4352
* Make private properties readable for backwards compatibility
4453
*
@@ -172,15 +181,17 @@ public function display_element( $element, &$children_elements, $max_depth, $dep
172181
return;
173182

174183
$id_field = $this->db_fields['id'];
184+
$id = $element->$id_field;
175185

176186
//display this element
177-
if ( isset( $args[0] ) && is_array( $args[0] ) )
178-
$args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
187+
$this->has_children = ! empty( $children_elements[ $id ] );
188+
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
189+
$args[0]['has_children'] = $this->has_children; // Backwards compatibility.
190+
}
191+
179192
$cb_args = array_merge( array(&$output, $element, $depth), $args);
180193
call_user_func_array(array($this, 'start_el'), $cb_args);
181194

182-
$id = $element->$id_field;
183-
184195
// descend only when the depth is right and there are childrens for this element
185196
if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
186197

src/wp-includes/comment-template.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ protected function comment( $comment, $depth, $args ) {
17841784
$add_below = 'div-comment';
17851785
}
17861786
?>
1787-
<<?php echo $tag; ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>">
1787+
<<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '' ); ?> id="comment-<?php comment_ID(); ?>">
17881788
<?php if ( 'div' != $args['style'] ) : ?>
17891789
<div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
17901790
<?php endif; ?>
@@ -1830,7 +1830,7 @@ protected function comment( $comment, $depth, $args ) {
18301830
protected function html5_comment( $comment, $depth, $args ) {
18311831
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
18321832
?>
1833-
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
1833+
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '' ); ?>>
18341834
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
18351835
<footer class="comment-meta">
18361836
<div class="comment-author vcard">
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* @group comment
5+
*/
6+
class Tests_Comment_Walker extends WP_UnitTestCase {
7+
8+
function setUp() {
9+
parent::setUp();
10+
11+
$this->post_id = $this->factory->post->create();
12+
}
13+
14+
/**
15+
* @ticket 14041
16+
*/
17+
function test_has_children() {
18+
$comment_parent = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
19+
$comment_child = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_parent' => $comment_parent ) );
20+
$comment_parent = get_comment( $comment_parent );
21+
$comment_child = get_comment( $comment_child );
22+
23+
$comment_walker = new Walker_Comment();
24+
$comment_callback = new Comment_Callback_Test( $this, $comment_walker );
25+
26+
wp_list_comments( array( 'callback' => array( $comment_callback, 'comment' ), 'walker' => $comment_walker, 'echo' => false ), array( $comment_parent, $comment_child ) );
27+
wp_list_comments( array( 'callback' => array( $comment_callback, 'comment' ), 'walker' => $comment_walker, 'echo' => false ), array( $comment_child, $comment_parent ) );
28+
}
29+
}
30+
31+
class Comment_Callback_Test {
32+
public function __construct( Tests_Comment_Walker $test_walker, Walker_Comment $walker ) {
33+
$this->test_walker = $test_walker;
34+
$this->walker = $walker;
35+
}
36+
37+
public function comment( $comment, $args, $depth ) {
38+
if ( 1 == $depth ) {
39+
$this->test_walker->assertTrue( $this->walker->has_children );
40+
$this->test_walker->assertTrue( $args['has_children'] ); // Back compat
41+
}
42+
43+
else if ( 2 == $depth ) {
44+
$this->test_walker->assertFalse( $this->walker->has_children );
45+
$this->test_walker->assertFalse( $args['has_children'] ); // Back compat
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)