|
| 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 “%2$s”', '%1$s responses to “%2$s”', 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 | + __('← 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 →', '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 | +} |
0 commit comments