From 7f242699114499b53164ef6621173598b2f607a8 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 Nov 2021 16:58:13 +0100 Subject: [PATCH 1/7] Use comment-template code in order to render the comments --- .../src/comment-template/index.php | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index 2843c56a38adc..242f43428934b 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -24,10 +24,52 @@ function render_block_core_comment_template( $attributes, $content, $block ) { return ''; } - $number = $block->context['queryPerPage']; - + $per_page = $block->context['queryPerPage']; // Get an array of comments for the current post. - $comments = get_approved_comments( $post_id, array( 'number' => $number ) ); + + $page = (int) get_query_var( 'cpage' ); + + $comment_args = array( + 'number' => $per_page, + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'post_id' => $post_id, + 'no_found_rows' => false, + 'update_comment_meta_cache' => false, + 'offset' => 0, + ); + + if ( get_option( 'thread_comments' ) ) { + $comment_args['hierarchical'] = 'threaded'; + } else { + $comment_args['hierarchical'] = false; + } + + if ( $page ) { + $comment_args['offset'] = ( $page - 1 ) * $per_page; + } else { + $top_level_query = new WP_Comment_Query(); + $top_level_args = array( + 'count' => true, + 'orderby' => false, + 'post_id' => $post_id, + 'status' => 'approve', + ); + + if ( $comment_args['hierarchical'] ) { + $top_level_args['parent'] = 0; + } + + if ( isset( $comment_args['include_unapproved'] ) ) { + $top_level_args['include_unapproved'] = $comment_args['include_unapproved']; + } + $top_level_args = apply_filters( 'comments_template_top_level_query_args', $top_level_args ); + $top_level_count = $top_level_query->query( $top_level_args ); + $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page; + } + + $comments = get_comments( $comment_args ); if ( count( $comments ) === 0 ) { return ''; From 6c0265de1b5256e33ae9f4f39f9374197ebed505 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 Nov 2021 17:17:45 +0100 Subject: [PATCH 2/7] Refactor code to keep it more simple --- .../src/comment-template/index.php | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index 242f43428934b..80227d5ea3c79 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -40,33 +40,20 @@ function render_block_core_comment_template( $attributes, $content, $block ) { 'offset' => 0, ); - if ( get_option( 'thread_comments' ) ) { - $comment_args['hierarchical'] = 'threaded'; - } else { - $comment_args['hierarchical'] = false; - } - if ( $page ) { $comment_args['offset'] = ( $page - 1 ) * $per_page; } else { - $top_level_query = new WP_Comment_Query(); - $top_level_args = array( + $top_level_args = array( 'count' => true, 'orderby' => false, 'post_id' => $post_id, 'status' => 'approve', ); - if ( $comment_args['hierarchical'] ) { - $top_level_args['parent'] = 0; - } + // We are not taking into account nested comments yet + $comment_count = get_comments( $top_level_args ); - if ( isset( $comment_args['include_unapproved'] ) ) { - $top_level_args['include_unapproved'] = $comment_args['include_unapproved']; - } - $top_level_args = apply_filters( 'comments_template_top_level_query_args', $top_level_args ); - $top_level_count = $top_level_query->query( $top_level_args ); - $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page; + $comment_args['offset'] = ( ceil( $comment_count / $per_page ) - 1 ) * $per_page; } $comments = get_comments( $comment_args ); From 512f63e5fd69872f92da05cca797ca9b9f98dc3c Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 Nov 2021 17:22:08 +0100 Subject: [PATCH 3/7] Some more code refactor --- .../block-library/src/comment-template/index.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index 80227d5ea3c79..dcc3f2892f812 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -30,14 +30,12 @@ function render_block_core_comment_template( $attributes, $content, $block ) { $page = (int) get_query_var( 'cpage' ); $comment_args = array( - 'number' => $per_page, - 'orderby' => 'comment_date_gmt', - 'order' => 'ASC', - 'status' => 'approve', - 'post_id' => $post_id, - 'no_found_rows' => false, - 'update_comment_meta_cache' => false, - 'offset' => 0, + 'number' => $per_page, + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'post_id' => $post_id, + 'offset' => 0, ); if ( $page ) { From 19b53298a0be456b370a0076465d12617da82aa2 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 18 Nov 2021 17:55:12 +0100 Subject: [PATCH 4/7] Adjust php linting --- packages/block-library/src/comment-template/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index dcc3f2892f812..dcaeebadccb13 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -48,7 +48,7 @@ function render_block_core_comment_template( $attributes, $content, $block ) { 'status' => 'approve', ); - // We are not taking into account nested comments yet + // We don't count nested comments yet. $comment_count = get_comments( $top_level_args ); $comment_args['offset'] = ( ceil( $comment_count / $per_page ) - 1 ) * $per_page; @@ -78,7 +78,7 @@ function render_block_core_comment_template( $attributes, $content, $block ) { } /** - * Registers the `core/comment-template` block on the server. + * Registers the `core/comment-teOrmplate` block on the server. */ function register_block_core_comment_template() { register_block_type_from_metadata( From 033ac706e26e43071aef8588fb9739265e196d82 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 22 Nov 2021 16:40:38 +0100 Subject: [PATCH 5/7] Work only with top level comments --- .../block-library/src/comment-template/index.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index dcaeebadccb13..dc2b642d8bd69 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -30,12 +30,13 @@ function render_block_core_comment_template( $attributes, $content, $block ) { $page = (int) get_query_var( 'cpage' ); $comment_args = array( - 'number' => $per_page, - 'orderby' => 'comment_date_gmt', - 'order' => 'ASC', - 'status' => 'approve', - 'post_id' => $post_id, - 'offset' => 0, + 'number' => $per_page, + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'post_id' => $post_id, + 'offset' => 0, + 'parent_id' => 0, ); if ( $page ) { From 3f68980b0919074edfc78253649ae0c48462718b Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Nov 2021 22:52:56 +0100 Subject: [PATCH 6/7] Adjust to only work with 1st level comments --- .../src/comment-template/index.php | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index dc2b642d8bd69..b17b6c8936731 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -25,34 +25,29 @@ function render_block_core_comment_template( $attributes, $content, $block ) { } $per_page = $block->context['queryPerPage']; - // Get an array of comments for the current post. $page = (int) get_query_var( 'cpage' ); $comment_args = array( - 'number' => $per_page, - 'orderby' => 'comment_date_gmt', - 'order' => 'ASC', - 'status' => 'approve', - 'post_id' => $post_id, - 'offset' => 0, - 'parent_id' => 0, + 'number' => $per_page, + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'post_id' => $post_id, + 'offset' => 0, + 'parent' => 0, // Only show top-level comments. Needs to be updated with responses. ); if ( $page ) { $comment_args['offset'] = ( $page - 1 ) * $per_page; } else { - $top_level_args = array( - 'count' => true, - 'orderby' => false, + $top_level_args = array( 'post_id' => $post_id, - 'status' => 'approve', + 'count' => true, + 'parent' => 0, ); - - // We don't count nested comments yet. - $comment_count = get_comments( $top_level_args ); - - $comment_args['offset'] = ( ceil( $comment_count / $per_page ) - 1 ) * $per_page; + $top_level_count = get_comments( $top_level_args ); + $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page; } $comments = get_comments( $comment_args ); From 57cbc446e70aceae85cfa37b0e3d030cbef3c9fb Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 29 Nov 2021 16:32:49 +0100 Subject: [PATCH 7/7] Fix small typo on template word --- packages/block-library/src/comment-template/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index b17b6c8936731..5684374843d32 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -74,7 +74,7 @@ function render_block_core_comment_template( $attributes, $content, $block ) { } /** - * Registers the `core/comment-teOrmplate` block on the server. + * Registers the `core/comment-template` block on the server. */ function register_block_core_comment_template() { register_block_type_from_metadata(