From c72f14c68dea81a9cd1f4ee8c0cb1bb43c1c3a50 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 18 Jan 2024 12:37:47 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Move=20comment=20logic=20into=20?= =?UTF-8?q?a=20dedicated=20Composer=20(#3162)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🎨 Move comment logic into a dedicated Composer * 🎨 Swap the method order --- app/View/Composers/Comments.php | 118 ++++++++++++++++++++ resources/views/partials/comments.blade.php | 18 +-- 2 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 app/View/Composers/Comments.php diff --git a/app/View/Composers/Comments.php b/app/View/Composers/Comments.php new file mode 100644 index 0000000000..8a82f0f5dc --- /dev/null +++ b/app/View/Composers/Comments.php @@ -0,0 +1,118 @@ + $this->title(), + 'comments' => $this->comments(), + 'previous' => $this->previous(), + 'next' => $this->next(), + 'paginated' => $this->paginated(), + 'closed' => $this->closed(), + ]; + } + + /** + * The comment title. + * + * @return string + */ + public function title() + { + /* translators: %1$s is replaced with the number of comments and %2$s with the post title */ + return sprintf( + _nx('%1$s response to “%2$s”', '%1$s responses to “%2$s”', get_comments_number(), 'comments title', 'sage'), + get_comments_number() === 1 ? _x('One', 'comments title', 'sage') : number_format_i18n(get_comments_number()), + ''.get_the_title().'' + ); + } + + /** + * Retrieve the comments. + * + * @return string + */ + public function comments() + { + if (! have_comments()) { + return; + } + + return wp_list_comments([ + 'style' => 'ol', + 'short_ping' => true, + ]); + } + + /** + * The previous comments link. + * + * @return string + */ + public function previous() + { + if (! get_previous_comments_link()) { + return; + } + + return get_previous_comments_link( + __('← Older comments', 'sage') + ); + } + + /** + * The next comments link. + * + * @return string + */ + public function next() + { + if (! get_next_comments_link()) { + return; + } + + return get_next_comments_link( + __('Newer comments →', 'sage') + ); + } + + /** + * Determine if the comments are paginated. + * + * @return bool + */ + public function paginated() + { + return get_comment_pages_count() > 1 && get_option('page_comments'); + } + + /** + * Determine if the comments are closed. + * + * @return bool + */ + public function closed() + { + return ! comments_open() && get_comments_number() != '0' && post_type_supports(get_post_type(), 'comments'); + } +} diff --git a/resources/views/partials/comments.blade.php b/resources/views/partials/comments.blade.php index 84c228f8eb..5dd7b58c78 100644 --- a/resources/views/partials/comments.blade.php +++ b/resources/views/partials/comments.blade.php @@ -1,26 +1,26 @@ @if (! post_password_required())
- @if (have_comments()) + @if ($comments)

- {!! /* translators: %1$s is replaced with the number of comments and %2$s with the post title */ sprintf(_nx('%1$s response to “%2$s”', '%1$s responses to “%2$s”', get_comments_number(), 'comments title', 'sage'), get_comments_number() === 1 ? _x('One', 'comments title', 'sage') : number_format_i18n(get_comments_number()), '' . get_the_title() . '') !!} + {!! $title !!}

    - {!! wp_list_comments(['style' => 'ol', 'short_ping' => true]) !!} + {!! $comments !!}
- @if (get_comment_pages_count() > 1 && get_option('page_comments')) + @if ($paginated)