Skip to content

Commit abe210b

Browse files
committed
FIX: add default args to Walker_Comment methods to prevent PHP warnings
Walker_Comment methods access $args keys (style, short_ping, format, avatar_size, max_depth) without checking if they exist. When called with an empty $args array, this produces PHP warnings on PHP 8.0+. Added wp_parse_args() with defaults matching wp_list_comments() at the top of each affected method: start_lvl, end_lvl, start_el, end_el, ping, comment, html5_comment. Added PHPUnit tests to verify no warnings are produced when Walker_Comment methods are called with an empty args array. Trac: https://core.trac.wordpress.org/ticket/56539
1 parent 6c22d69 commit abe210b

2 files changed

Lines changed: 129 additions & 0 deletions

File tree

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class Walker_Comment extends Walker {
5353
* @param array $args Optional. Uses 'style' argument for type of HTML list. Default empty array.
5454
*/
5555
public function start_lvl( &$output, $depth = 0, $args = array() ) {
56+
$args = wp_parse_args(
57+
$args,
58+
array(
59+
'style' => 'ul',
60+
)
61+
);
62+
5663
$GLOBALS['comment_depth'] = $depth + 1;
5764

5865
switch ( $args['style'] ) {
@@ -82,6 +89,13 @@ public function start_lvl( &$output, $depth = 0, $args = array() ) {
8289
* Default empty array.
8390
*/
8491
public function end_lvl( &$output, $depth = 0, $args = array() ) {
92+
$args = wp_parse_args(
93+
$args,
94+
array(
95+
'style' => 'ul',
96+
)
97+
);
98+
8599
$GLOBALS['comment_depth'] = $depth + 1;
86100

87101
switch ( $args['style'] ) {
@@ -171,6 +185,14 @@ public function display_element( $element, &$children_elements, $max_depth, $dep
171185
* @param int $current_object_id Optional. ID of the current comment. Default 0.
172186
*/
173187
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
188+
$args = wp_parse_args(
189+
$args,
190+
array(
191+
'short_ping' => false,
192+
'format' => 'xhtml',
193+
)
194+
);
195+
174196
// Restores the more descriptive, specific name for use within this method.
175197
$comment = $data_object;
176198

@@ -223,6 +245,13 @@ public function start_el( &$output, $data_object, $depth = 0, $args = array(), $
223245
* @param array $args Optional. An array of arguments. Default empty array.
224246
*/
225247
public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
248+
$args = wp_parse_args(
249+
$args,
250+
array(
251+
'style' => 'ul',
252+
)
253+
);
254+
226255
if ( ! empty( $args['end-callback'] ) ) {
227256
ob_start();
228257
call_user_func(
@@ -253,6 +282,13 @@ public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
253282
* @param array $args An array of arguments.
254283
*/
255284
protected function ping( $comment, $depth, $args ) {
285+
$args = wp_parse_args(
286+
$args,
287+
array(
288+
'style' => 'ul',
289+
)
290+
);
291+
256292
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
257293
?>
258294
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
@@ -297,6 +333,15 @@ public function filter_comment_text( $comment_text, $comment ) {
297333
* @param array $args An array of arguments.
298334
*/
299335
protected function comment( $comment, $depth, $args ) {
336+
$args = wp_parse_args(
337+
$args,
338+
array(
339+
'style' => 'ul',
340+
'avatar_size' => 32,
341+
'max_depth' => '',
342+
)
343+
);
344+
300345
if ( 'div' === $args['style'] ) {
301346
$tag = 'div';
302347
$add_below = 'comment';
@@ -407,6 +452,15 @@ protected function comment( $comment, $depth, $args ) {
407452
* @param array $args An array of arguments.
408453
*/
409454
protected function html5_comment( $comment, $depth, $args ) {
455+
$args = wp_parse_args(
456+
$args,
457+
array(
458+
'style' => 'ul',
459+
'avatar_size' => 32,
460+
'max_depth' => '',
461+
)
462+
);
463+
410464
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
411465

412466
$commenter = wp_get_current_commenter();

tests/phpunit/tests/comment/walker.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,81 @@ public function test_has_children() {
5454
array( $comment_child, $comment_parent )
5555
);
5656
}
57+
58+
/**
59+
* @ticket 56539
60+
*/
61+
public function test_start_lvl_with_empty_args_should_not_produce_warnings() {
62+
$walker = new Walker_Comment();
63+
$output = '';
64+
65+
$walker->start_lvl( $output, 0, array() );
66+
67+
$this->assertStringContainsString( '<ul class="children">', $output );
68+
}
69+
70+
/**
71+
* @ticket 56539
72+
*/
73+
public function test_end_lvl_with_empty_args_should_not_produce_warnings() {
74+
$walker = new Walker_Comment();
75+
$output = '';
76+
77+
$walker->end_lvl( $output, 0, array() );
78+
79+
$this->assertStringContainsString( '</ul>', $output );
80+
}
81+
82+
/**
83+
* @ticket 56539
84+
*/
85+
public function test_end_el_with_empty_args_should_not_produce_warnings() {
86+
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $this->post_id ) );
87+
$comment = get_comment( $comment_id );
88+
$walker = new Walker_Comment();
89+
$output = '';
90+
91+
$walker->end_el( $output, $comment, 0, array() );
92+
93+
$this->assertStringContainsString( '</li>', $output );
94+
}
95+
96+
/**
97+
* @ticket 56539
98+
*/
99+
public function test_start_el_with_empty_args_should_not_produce_warnings() {
100+
$comment_id = self::factory()->comment->create(
101+
array(
102+
'comment_post_ID' => $this->post_id,
103+
'comment_type' => 'comment',
104+
)
105+
);
106+
$comment = get_comment( $comment_id );
107+
$walker = new Walker_Comment();
108+
$output = '';
109+
110+
$walker->start_el( $output, $comment, 0, array() );
111+
112+
$this->assertNotEmpty( $output );
113+
}
114+
115+
/**
116+
* @ticket 56539
117+
*/
118+
public function test_walk_with_empty_args_should_not_produce_warnings() {
119+
$comment_id = self::factory()->comment->create(
120+
array(
121+
'comment_post_ID' => $this->post_id,
122+
'comment_type' => 'comment',
123+
)
124+
);
125+
$comments = array( get_comment( $comment_id ) );
126+
$walker = new Walker_Comment();
127+
128+
$output = $walker->walk( $comments, -1, array() );
129+
130+
$this->assertNotEmpty( $output );
131+
}
57132
}
58133

59134
class Comment_Callback_Test_Helper {

0 commit comments

Comments
 (0)