🎨 Move comment logic into a dedicated Composer (#3162)
* 🎨 Move comment logic into a dedicated Composer * 🎨 Swap the method order
This commit is contained in:
118
app/View/Composers/Comments.php
Normal file
118
app/View/Composers/Comments.php
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\View\Composers;
|
||||||
|
|
||||||
|
use Roots\Acorn\View\Composer;
|
||||||
|
|
||||||
|
class Comments extends Composer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* List of views served by this composer.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $views = [
|
||||||
|
'partials.comments',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data to be passed to view before rendering.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function with()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'title' => $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()),
|
||||||
|
'<span>'.get_the_title().'</span>'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +1,26 @@
|
|||||||
@if (! post_password_required())
|
@if (! post_password_required())
|
||||||
<section id="comments" class="comments">
|
<section id="comments" class="comments">
|
||||||
@if (have_comments())
|
@if ($comments)
|
||||||
<h2>
|
<h2>
|
||||||
{!! /* 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()), '<span>' . get_the_title() . '</span>') !!}
|
{!! $title !!}
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<ol class="comment-list">
|
<ol class="comment-list">
|
||||||
{!! wp_list_comments(['style' => 'ol', 'short_ping' => true]) !!}
|
{!! $comments !!}
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
@if (get_comment_pages_count() > 1 && get_option('page_comments'))
|
@if ($paginated)
|
||||||
<nav>
|
<nav>
|
||||||
<ul class="pager">
|
<ul class="pager">
|
||||||
@if (get_previous_comments_link())
|
@if ($previous)
|
||||||
<li class="previous">
|
<li class="previous">
|
||||||
{!! get_previous_comments_link(__('← Older comments', 'sage')) !!}
|
{!! $previous !!}
|
||||||
</li>
|
</li>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
@if (get_next_comments_link())
|
@if ($next)
|
||||||
<li class="next">
|
<li class="next">
|
||||||
{!! get_next_comments_link(__('Newer comments →', 'sage')) !!}
|
{!! $next !!}
|
||||||
</li>
|
</li>
|
||||||
@endif
|
@endif
|
||||||
</ul>
|
</ul>
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
@endif
|
@endif
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
@if (! comments_open() && get_comments_number() != '0' && post_type_supports(get_post_type(), 'comments'))
|
@if ($closed)
|
||||||
<x-alert type="warning">
|
<x-alert type="warning">
|
||||||
{!! __('Comments are closed.', 'sage') !!}
|
{!! __('Comments are closed.', 'sage') !!}
|
||||||
</x-alert>
|
</x-alert>
|
||||||
|
|||||||
Reference in New Issue
Block a user