Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/wp-includes/class-walker-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ class Walker_Comment extends Walker {
'id' => 'comment_ID',
);

/**
* Parses arguments with defaults.
*
* @since unreleased
*
* @param array $args Arguments to parse.
* @return array Parsed arguments.
*/
protected function get_args( array $args ): array {
return array_merge(
array(
'style' => 'ul',
'avatar_size' => 32,
'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
'short_ping' => false,
'max_depth' => '',
),
$args
);
}

/**
* Starts the list before the elements are added.
*
Expand All @@ -53,6 +74,8 @@ class Walker_Comment extends Walker {
* @param array $args Optional. Uses 'style' argument for type of HTML list. Default empty array.
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$args = $this->get_args( $args );

$GLOBALS['comment_depth'] = $depth + 1;

switch ( $args['style'] ) {
Expand Down Expand Up @@ -82,6 +105,8 @@ public function start_lvl( &$output, $depth = 0, $args = array() ) {
* Default empty array.
*/
public function end_lvl( &$output, $depth = 0, $args = array() ) {
$args = $this->get_args( $args );

$GLOBALS['comment_depth'] = $depth + 1;

switch ( $args['style'] ) {
Expand Down Expand Up @@ -171,6 +196,8 @@ public function display_element( $element, &$children_elements, $max_depth, $dep
* @param int $current_object_id Optional. ID of the current comment. Default 0.
*/
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
$args = $this->get_args( $args );

// Restores the more descriptive, specific name for use within this method.
$comment = $data_object;

Expand Down Expand Up @@ -223,6 +250,8 @@ public function start_el( &$output, $data_object, $depth = 0, $args = array(), $
* @param array $args Optional. An array of arguments. Default empty array.
*/
public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
$args = $this->get_args( $args );

if ( ! empty( $args['end-callback'] ) ) {
ob_start();
call_user_func(
Expand Down Expand Up @@ -253,6 +282,8 @@ public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
* @param array $args An array of arguments.
*/
protected function ping( $comment, $depth, $args ) {
$args = $this->get_args( $args );

$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
?>
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
Expand Down Expand Up @@ -297,6 +328,8 @@ public function filter_comment_text( $comment_text, $comment ) {
* @param array $args An array of arguments.
*/
protected function comment( $comment, $depth, $args ) {
$args = $this->get_args( $args );

if ( 'div' === $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
Expand Down Expand Up @@ -407,6 +440,8 @@ protected function comment( $comment, $depth, $args ) {
* @param array $args An array of arguments.
*/
protected function html5_comment( $comment, $depth, $args ) {
$args = $this->get_args( $args );

$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';

$commenter = wp_get_current_commenter();
Expand Down
75 changes: 75 additions & 0 deletions tests/phpunit/tests/comment/walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,81 @@ public function test_has_children() {
array( $comment_child, $comment_parent )
);
}

/**
* @ticket 56539
*/
public function test_start_lvl_with_empty_args_should_not_produce_warnings() {
$walker = new Walker_Comment();
$output = '';

$walker->start_lvl( $output, 0, array() );

$this->assertStringContainsString( '<ul class="children">', $output );
}

/**
* @ticket 56539
*/
public function test_end_lvl_with_empty_args_should_not_produce_warnings() {
$walker = new Walker_Comment();
$output = '';

$walker->end_lvl( $output, 0, array() );

$this->assertStringContainsString( '</ul>', $output );
}

/**
* @ticket 56539
*/
public function test_end_el_with_empty_args_should_not_produce_warnings() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $this->post_id ) );
$comment = get_comment( $comment_id );
$walker = new Walker_Comment();
$output = '';

$walker->end_el( $output, $comment, 0, array() );

$this->assertStringContainsString( '</li>', $output );
}

/**
* @ticket 56539
*/
public function test_start_el_with_empty_args_should_not_produce_warnings() {
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $this->post_id,
'comment_type' => 'comment',
)
);
$comment = get_comment( $comment_id );
$walker = new Walker_Comment();
$output = '';

$walker->start_el( $output, $comment, 0, array() );

$this->assertNotEmpty( $output );
}

/**
* @ticket 56539
*/
public function test_walk_with_empty_args_should_not_produce_warnings() {
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $this->post_id,
'comment_type' => 'comment',
)
);
$comments = array( get_comment( $comment_id ) );
$walker = new Walker_Comment();

$output = $walker->walk( $comments, -1, array() );

$this->assertNotEmpty( $output );
}
}

class Comment_Callback_Test_Helper {
Expand Down
Loading