Skip to content

Commit 3be9ff5

Browse files
committed
feat: add comments view and composer
1 parent b756e07 commit 3be9ff5

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

app/View/Composers/Comments.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace App\View\Composers;
4+
5+
use Roots\Acorn\View\Composer;
6+
7+
class Comments extends Composer
8+
{
9+
/**
10+
* List of views served by this composer.
11+
*
12+
* @var array
13+
*/
14+
protected static $views = [
15+
'partials.comments',
16+
];
17+
18+
/**
19+
* Data to be passed to view before rendering.
20+
*
21+
* @return array
22+
*/
23+
public function with()
24+
{
25+
return [
26+
'title' => $this->title(),
27+
'responses' => $this->responses(),
28+
'previous' => $this->previous(),
29+
'next' => $this->next(),
30+
'paginated' => $this->paginated(),
31+
'closed' => $this->closed(),
32+
];
33+
}
34+
35+
/**
36+
* The comment title.
37+
*
38+
* @return string
39+
*/
40+
public function title()
41+
{
42+
return sprintf(
43+
/* translators: %1$s is replaced with the number of comments and %2$s with the post title */
44+
_nx('%1$s response to &ldquo;%2$s&rdquo;', '%1$s responses to &ldquo;%2$s&rdquo;', get_comments_number(), 'comments title', 'sage'),
45+
get_comments_number() === 1 ? _x('One', 'comments title', 'sage') : number_format_i18n(get_comments_number()),
46+
get_the_title()
47+
);
48+
}
49+
50+
/**
51+
* Retrieve the comments.
52+
*
53+
* @return string
54+
*/
55+
public function responses()
56+
{
57+
if (! have_comments()) {
58+
return;
59+
}
60+
61+
return wp_list_comments([
62+
'style' => 'ol',
63+
'short_ping' => true,
64+
'echo' => false,
65+
]);
66+
}
67+
68+
/**
69+
* The previous comments link.
70+
*
71+
* @return string
72+
*/
73+
public function previous()
74+
{
75+
if (! get_previous_comments_link()) {
76+
return;
77+
}
78+
79+
return get_previous_comments_link(
80+
__('&larr; Older comments', 'sage')
81+
);
82+
}
83+
84+
/**
85+
* The next comments link.
86+
*
87+
* @return string
88+
*/
89+
public function next()
90+
{
91+
if (! get_next_comments_link()) {
92+
return;
93+
}
94+
95+
return get_next_comments_link(
96+
__('Newer comments &rarr;', 'sage')
97+
);
98+
}
99+
100+
/**
101+
* Determine if the comments are paginated.
102+
*
103+
* @return bool
104+
*/
105+
public function paginated()
106+
{
107+
return get_comment_pages_count() > 1 && get_option('page_comments');
108+
}
109+
110+
/**
111+
* Determine if the comments are closed.
112+
*
113+
* @return bool
114+
*/
115+
public function closed()
116+
{
117+
return ! comments_open() && get_comments_number() != '0' && post_type_supports(get_post_type(), 'comments');
118+
}
119+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@if (!post_password_required())
2+
<section id="comments" class="comments">
3+
@if ($responses)
4+
<h2>
5+
{!! $title !!}
6+
</h2>
7+
8+
<ol class="comment-list">
9+
{!! $responses !!}
10+
</ol>
11+
12+
@if ($paginated)
13+
<nav aria-label="Comment">
14+
<ul class="pager">
15+
@if ($previous)
16+
<li class="previous">
17+
{!! $previous !!}
18+
</li>
19+
@endif
20+
21+
@if ($next)
22+
<li class="next">
23+
{!! $next !!}
24+
</li>
25+
@endif
26+
</ul>
27+
</nav>
28+
@endif
29+
@endif
30+
31+
@if ($closed)
32+
<x-alert type="warning">
33+
{!! __('Comments are closed.', 'sage') !!}
34+
</x-alert>
35+
@endif
36+
37+
@php(comment_form())
38+
</section>
39+
@endif

0 commit comments

Comments
 (0)