Skip to content

Commit 97c5619

Browse files
committed
refactor: removed deprecated CommentHelper class
1 parent 207748d commit 97c5619

File tree

8 files changed

+143
-117
lines changed

8 files changed

+143
-117
lines changed

phpmyfaq/assets/src/faq/comments.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const addCommentToDOM = (commentData: CommentData): void => {
8383
const escapedUsername = escapeHtml(commentData.username);
8484
const escapedEmail = escapeHtml(commentData.email);
8585

86-
// Create the comment HTML matching the structure from CommentHelper::getComments()
8786
const commentHtml = `
8887
<div class="row mt-2 mb-2">
8988
<div class="col-sm-1">
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{#
2+
# Twig macros for comment rendering
3+
#
4+
# This Source Code Form is subject to the terms of the Mozilla Public License,
5+
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
# obtain one at https://mozilla.org/MPL/2.0/.
7+
#
8+
# @package phpMyFAQ
9+
# @author Thorsten Rinne <thorsten@phpmyfaq.de>
10+
# @copyright 2026 phpMyFAQ Team
11+
# @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
12+
# @link https://www.phpmyfaq.de
13+
#}
14+
15+
{#
16+
# Renders comments for a FAQ or news item
17+
#
18+
# @param comments array Array of comment objects with properties: id, email, username, date, comment
19+
# @param gravatarImages array Array of gravatar image HTML strings keyed by comment ID
20+
# @param safeEmails array Array of safe email addresses keyed by comment ID
21+
# @param formattedDates array Array of formatted dates keyed by comment ID
22+
# @param msgShowMore string Translation for "show more" text
23+
#}
24+
{% macro renderComments(comments, gravatarImages, safeEmails, formattedDates, msgShowMore) %}
25+
{% for comment in comments %}
26+
<div class="row mt-2 mb-2">
27+
<div class="col-sm-1">
28+
<div class="thumbnail">
29+
{{ gravatarImages[comment.id]|raw }}
30+
</div>
31+
</div>
32+
33+
<div class="col-sm-11">
34+
<div class="card">
35+
<div class="card-header card-header-comments">
36+
<strong><a href="mailto:{{ safeEmails[comment.id] }}">{{ comment.username }}</a></strong>
37+
<span class="text-muted">({{ formattedDates[comment.id] }})</span>
38+
</div>
39+
<div class="card-body">{{ comment.comment|raw }}</div>
40+
</div>
41+
</div>
42+
</div>
43+
{% endfor %}
44+
{% endmacro %}

phpmyfaq/assets/templates/default/faq.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends 'index.twig' %}
2+
{% import 'core/comment.macros.twig' as commentMacros %}
23

34
{% block richSnippets %}
45
<script type="application/ld+json">
@@ -44,7 +45,9 @@
4445
{% endif %}
4546

4647
<p class="d-print-none">{{ writeCommentMsg|raw }}</p>
47-
<div class="d-print-none" id="comments">{{ renderComments|raw }}</div>
48+
<div class="d-print-none" id="comments">
49+
{{ commentMacros.renderComments(comments.comments, comments.gravatarImages, comments.safeEmails, comments.formattedDates, msgShowMore) }}
50+
</div>
4851
</div>
4952

5053
<div class="col-md-4">

phpmyfaq/assets/templates/default/news.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends 'index.twig' %}
2+
{% import 'core/comment.macros.twig' as commentMacros %}
23

34
{% block content %}
45
<div class="row g-5">
@@ -10,7 +11,9 @@
1011
<p class="d-print-none">{{ writeCommentMsg | raw }}</p>
1112
<div id="pmf-comment-add-success"></div>
1213
<div id="pmf-comment-add-error"></div>
13-
<div class="d-print-none" id="comments">{{ renderComments | raw }}</div>
14+
<div class="d-print-none" id="comments">
15+
{{ commentMacros.renderComments(comments.comments, comments.gravatarImages, comments.safeEmails, comments.formattedDates, msgShowMore) }}
16+
</div>
1417
</div>
1518

1619
<div class="col-md-4">

phpmyfaq/src/phpMyFAQ/Controller/Administration/NewsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function edit(Request $request): Response
9191
{
9292
$this->userHasPermission(PermissionType::NEWS_ADD);
9393

94-
$newsId = (int) Filter::filterVar($request->grequest->et('newsId'), FILTER_VALIDATE_INT);
94+
$newsId = (int) Filter::filterVar($request->request->get('newsId'), FILTER_VALIDATE_INT);
9595

9696
$news = $this->container->get(id: 'phpmyfaq.news');
9797
$comment = $this->container->get(id: 'phpmyfaq.comments');

phpmyfaq/src/phpMyFAQ/Controller/Frontend/FaqController.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
use phpMyFAQ\Faq\FaqCreationService;
2929
use phpMyFAQ\Faq\FaqDisplayService;
3030
use phpMyFAQ\Filter;
31-
use phpMyFAQ\Helper\CommentHelper;
32-
use phpMyFAQ\Link;
3331
use phpMyFAQ\Seo;
3432
use phpMyFAQ\Services;
3533
use phpMyFAQ\Session\Token;
34+
use phpMyFAQ\Strings;
3635
use phpMyFAQ\Translation;
36+
use phpMyFAQ\Utils;
3737
use phpMyFAQ\Visits;
3838
use Symfony\Component\HttpFoundation\RedirectResponse;
3939
use Symfony\Component\HttpFoundation\Request;
@@ -246,8 +246,6 @@ public function show(Request $request): Response
246246

247247
// Comment permissions
248248
$expired = $faqDisplayService->isExpired();
249-
$commentHelper = new CommentHelper();
250-
$commentHelper->setConfiguration($this->configuration);
251249

252250
if (
253251
-1 === $this->currentUser->getUserId() && !$this->configuration->get('records.allowCommentsForGuests')
@@ -343,7 +341,8 @@ public function show(Request $request): Response
343341
Translation::get(key: 'msgCaptcha'),
344342
$this->currentUser->isLoggedIn(),
345343
),
346-
'renderComments' => $commentHelper->getComments($comments),
344+
'comments' => $this->prepareCommentsData($comments),
345+
'msgShowMore' => Translation::get(key: 'msgShowMore'),
347346
'msg_about_faq' => Translation::get(key: 'msg_about_faq'),
348347
'userId' => $this->currentUser->getUserId(),
349348
'permissionEditFaq' => $this->currentUser->perm->hasPermission(
@@ -404,4 +403,45 @@ public function show(Request $request): Response
404403

405404
return $this->render('faq.twig', $templateVars);
406405
}
406+
407+
/**
408+
* Prepares comment data for the Twig macro
409+
*
410+
* @param array $comments Array of Comment objects
411+
* @throws \Exception
412+
* @return array
413+
*/
414+
private function prepareCommentsData(array $comments): array
415+
{
416+
$date = $this->container->get('phpmyfaq.date');
417+
$mail = $this->container->get('phpmyfaq.mail');
418+
$gravatar = $this->container->get('phpmyfaq.services.gravatar');
419+
420+
$preparedComments = [];
421+
$gravatarImages = [];
422+
$safeEmails = [];
423+
$formattedDates = [];
424+
425+
foreach ($comments as $comment) {
426+
$commentId = $comment->getId();
427+
$preparedComments[] = [
428+
'id' => $commentId,
429+
'email' => $comment->getEmail(),
430+
'username' => Strings::htmlentities($comment->getUsername()),
431+
'date' => $comment->getDate(),
432+
'comment' => Utils::parseUrl($comment->getComment()),
433+
];
434+
435+
$gravatarImages[$commentId] = $gravatar->getImage($comment->getEmail(), ['class' => 'img-thumbnail']);
436+
$safeEmails[$commentId] = $mail->safeEmail($comment->getEmail());
437+
$formattedDates[$commentId] = $date->format($comment->getDate());
438+
}
439+
440+
return [
441+
'comments' => $preparedComments,
442+
'gravatarImages' => $gravatarImages,
443+
'safeEmails' => $safeEmails,
444+
'formattedDates' => $formattedDates,
445+
];
446+
}
407447
}

phpmyfaq/src/phpMyFAQ/Controller/Frontend/NewsController.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
use phpMyFAQ\Core\Exception;
2626
use phpMyFAQ\Entity\CommentType;
2727
use phpMyFAQ\Filter;
28-
use phpMyFAQ\Helper\CommentHelper;
2928
use phpMyFAQ\News\NewsService;
3029
use phpMyFAQ\Session\Token;
30+
use phpMyFAQ\Strings;
3131
use phpMyFAQ\Translation;
32+
use phpMyFAQ\Utils;
3233
use Symfony\Component\HttpFoundation\Request;
3334
use Symfony\Component\HttpFoundation\Response;
3435
use Symfony\Component\Routing\Attribute\Route;
@@ -65,9 +66,6 @@ public function index(Request $request): Response
6566
$captcha = $this->container->get('phpmyfaq.captcha');
6667
$captchaHelper = CaptchaHelper::getInstance($this->configuration);
6768

68-
$commentHelper = new CommentHelper();
69-
$commentHelper->setConfiguration($this->configuration);
70-
7169
$comment = new Comments($this->configuration);
7270
$comments = $comment->getCommentsData($newsId, CommentType::NEWS);
7371

@@ -103,7 +101,49 @@ public function index(Request $request): Response
103101
Translation::get(key: 'msgCaptcha'),
104102
$this->currentUser->isLoggedIn(),
105103
),
106-
'renderComments' => $commentHelper->getComments($comments),
104+
'comments' => $this->prepareCommentsData($comments),
105+
'msgShowMore' => Translation::get(key: 'msgShowMore'),
107106
]);
108107
}
108+
109+
/**
110+
* Prepares comment data for the Twig macro
111+
*
112+
* @param array $comments Array of Comment objects
113+
* @throws \Exception
114+
* @return array
115+
*/
116+
private function prepareCommentsData(array $comments): array
117+
{
118+
$date = $this->container->get('phpmyfaq.date');
119+
$mail = $this->container->get('phpmyfaq.mail');
120+
$gravatar = $this->container->get('phpmyfaq.services.gravatar');
121+
122+
$preparedComments = [];
123+
$gravatarImages = [];
124+
$safeEmails = [];
125+
$formattedDates = [];
126+
127+
foreach ($comments as $comment) {
128+
$commentId = $comment->getId();
129+
$preparedComments[] = [
130+
'id' => $commentId,
131+
'email' => $comment->getEmail(),
132+
'username' => Strings::htmlentities($comment->getUsername()),
133+
'date' => $comment->getDate(),
134+
'comment' => Utils::parseUrl($comment->getComment()),
135+
];
136+
137+
$gravatarImages[$commentId] = $gravatar->getImage($comment->getEmail(), ['class' => 'img-thumbnail']);
138+
$safeEmails[$commentId] = $mail->safeEmail($comment->getEmail());
139+
$formattedDates[$commentId] = $date->format($comment->getDate());
140+
}
141+
142+
return [
143+
'comments' => $preparedComments,
144+
'gravatarImages' => $gravatarImages,
145+
'safeEmails' => $safeEmails,
146+
'formattedDates' => $formattedDates,
147+
];
148+
}
109149
}

phpmyfaq/src/phpMyFAQ/Helper/CommentHelper.php

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)