diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index f4c904bc88..64b4ba8e53 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1286,11 +1286,16 @@ function comments_template( $file = '/comments.php', $separate_comments = false 'order' => 'ASC', 'status' => 'approve', 'post_id' => $post->ID, - 'hierarchical' => 'threaded', 'no_found_rows' => false, 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance. ); + if ( get_option('thread_comments') ) { + $comment_args['hierarchical'] = 'threaded'; + } else { + $comment_args['hierarchical'] = false; + } + if ( $user_ID ) { $comment_args['include_unapproved'] = array( $user_ID ); } elseif ( ! empty( $comment_author_email ) ) { @@ -1336,18 +1341,22 @@ function comments_template( $file = '/comments.php', $separate_comments = false $_comments = $comment_query->comments; // Trees must be flattened before they're passed to the walker. - $comments_flat = array(); - foreach ( $_comments as $_comment ) { - $comments_flat[] = $_comment; - $comment_children = $_comment->get_children( array( - 'format' => 'flat', - 'status' => $comment_args['status'], - 'orderby' => $comment_args['orderby'] - ) ); + if ( $comment_args['hierarchical'] ) { + $comments_flat = array(); + foreach ( $_comments as $_comment ) { + $comments_flat[] = $_comment; + $comment_children = $_comment->get_children( array( + 'format' => 'flat', + 'status' => $comment_args['status'], + 'orderby' => $comment_args['orderby'] + ) ); - foreach ( $comment_children as $comment_child ) { - $comments_flat[] = $comment_child; + foreach ( $comment_children as $comment_child ) { + $comments_flat[] = $comment_child; + } } + } else { + $comments_flat = $_comments; } /** diff --git a/tests/phpunit/tests/comment/commentsTemplate.php b/tests/phpunit/tests/comment/commentsTemplate.php index 308df77c24..3c4ddd368b 100644 --- a/tests/phpunit/tests/comment/commentsTemplate.php +++ b/tests/phpunit/tests/comment/commentsTemplate.php @@ -689,4 +689,43 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase { $commenter['comment_author_email'] = 'foo@example.com'; return $commenter; } + + /** + * @ticket 35378 + */ + public function test_hierarchy_should_be_ignored_when_threading_is_disabled() { + $now = time(); + $p = self::factory()->post->create(); + $comment_1 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '1', + 'comment_approved' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), + ) ); + $comment_2 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '2', + 'comment_approved' => '1', + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), + ) ); + $comment_3 = self::factory()->comment->create( array( + 'comment_post_ID' => $p, + 'comment_content' => '3', + 'comment_approved' => '1', + 'comment_parent' => $comment_1, + 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), + ) ); + + update_option( 'comment_order', 'asc' ); + update_option( 'thread_comments', 0 ); + + $this->go_to( get_permalink( $p ) ); + $found = get_echo( 'comments_template' ); + + // Find the found comments in the markup. + preg_match_all( '|id="comment-([0-9]+)|', $found, $matches ); + + $found_cids = array_map( 'intval', $matches[1] ); + $this->assertSame( array( $comment_2, $comment_3, $comment_1 ), $found_cids ); + } }