Task:6241 | исправил работу кнопки Войдите в комментариях вынес шаблон комментариев в отдельный компонент и twig template
parent
9faa1f1fe6
commit
604248157b
@ -1,211 +1,10 @@ |
||||
<?php |
||||
if (!defined('ABSPATH')) { |
||||
exit; |
||||
} |
||||
|
||||
function count_nested_replies($comment_id) { |
||||
$count = 0; |
||||
$replies = get_comments(array( |
||||
'post_id' => get_the_ID(), |
||||
'status' => 'approve', |
||||
'parent' => $comment_id, |
||||
'order' => 'ASC' |
||||
)); |
||||
|
||||
if ($replies) { |
||||
$count += count($replies); |
||||
foreach ($replies as $reply) { |
||||
$count += count_nested_replies($reply->comment_ID); |
||||
} |
||||
} |
||||
|
||||
return $count; |
||||
} |
||||
|
||||
if (post_password_required()) { |
||||
return; |
||||
} |
||||
|
||||
if (!is_user_logged_in()) { |
||||
?> |
||||
<div class="comment-block"> |
||||
<p class="comment-block_text"> |
||||
<a href="#modal"><?php pll_e('Войдите'); ?></a>, <?php pll_e('чтобы оставлять комментарии'); ?> |
||||
</p> |
||||
</div> |
||||
<?php |
||||
} |
||||
|
||||
$top_level_comments = get_comments(array( |
||||
'post_id' => get_the_ID(), |
||||
'status' => 'approve', |
||||
'parent' => 0, |
||||
'order' => 'ASC' |
||||
)); |
||||
|
||||
if ($top_level_comments) { |
||||
echo '<div class="comment-block_wrapper">'; |
||||
|
||||
foreach ($top_level_comments as $comment) { |
||||
?> |
||||
<div class="comment-block" id="comment-<?php echo $comment->comment_ID; ?>">
|
||||
<div class="comment-user"> |
||||
<?php echo get_avatar($comment, 50); ?> |
||||
<div class="comment-user_text"> |
||||
<h6><?php echo get_comment_author($comment); ?></h6>
|
||||
<span><?php echo get_comment_date('d F Y', $comment); ?></span>
|
||||
</div> |
||||
</div> |
||||
<p><?php echo get_comment_text($comment); ?></p>
|
||||
<div class="comment-btns"> |
||||
<button class="reply-to-comment" data-comment-id="<?php echo $comment->comment_ID; ?>" data-post-id="<?php echo get_the_ID(); ?>" data-reply-to="<?php echo esc_attr(get_comment_author($comment)); ?>">
|
||||
<svg width="11" height="8" viewBox="0 0 11 8" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M9.5 3H3.25C2.00736 3 1 4.00736 1 5.25V5.25C1 6.49264 2.00736 7.5 3.25 7.5H3.5" stroke="#666666"></path> |
||||
<path d="M7 0.5L9.5 3L7 5.5" stroke="#666666"></path> |
||||
</svg> |
||||
<?php pll_e('Ответить'); ?> |
||||
</button> |
||||
<button class="comment-like-btn<?php echo is_user_liked_comment($comment->comment_ID) ? ' active' : ''; ?>" data-comment-id="<?php echo $comment->comment_ID; ?>">
|
||||
<svg width="15" height="13" viewBox="0 0 15 13" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M10.7745 1C8.67239 1 7.7426 2.5355 7.54047 3.30325C7.39224 2.5355 6.49748 1 4.10427 1C1.11276 1 -0.221288 4.85199 2.40639 8.14801C4.50853 10.7848 6.705 11.8147 7.54047 12C8.47026 11.8279 10.8311 10.761 12.8363 7.87004C15.3427 4.25632 13.4022 1 10.7745 1Z" <?php echo is_user_liked_comment($comment->comment_ID) ? 'stroke="#ff0000" fill="rgba(255, 0, 0, 0.7)"' : 'stroke="#666666"'; ?>></path>
|
||||
</svg> |
||||
<?php echo get_comment_likes_count($comment->comment_ID); ?> |
||||
</button> |
||||
<?php |
||||
comment_reply_link(array( |
||||
'reply_text' => pll__('Ответить'), |
||||
'depth' => 1, |
||||
'max_depth' => get_option('thread_comments_depth'), |
||||
'before' => '<span style="display:none;">', |
||||
'after' => '</span>', |
||||
), $comment->comment_ID, get_the_ID()); |
||||
?> |
||||
</div> |
||||
<div class="reply-form-container" id="reply-form-<?php echo $comment->comment_ID; ?>" style="display: none;"></div>
|
||||
</div> |
||||
<?php |
||||
|
||||
$all_nested_replies = get_all_nested_replies($comment->comment_ID); |
||||
|
||||
if (!empty($all_nested_replies)) { |
||||
$total_reply_count = count($all_nested_replies); |
||||
?> |
||||
<button class="answer-btn"> |
||||
<svg width="15" height="10" viewBox="0 0 15 10" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M14 1.41406L7.5 7.91406L1 1.41406" stroke="#76CE75" stroke-width="2"></path> |
||||
</svg> |
||||
<?php echo $total_reply_count; ?> |
||||
<span> |
||||
<?php echo ($total_reply_count == 1) ? pll__('ответ') : pll__('ответа'); ?> |
||||
</span> |
||||
</button> |
||||
<?php |
||||
|
||||
foreach ($all_nested_replies as $reply) { |
||||
?> |
||||
<div class="comment-block answer-block" id="comment-<?php echo $reply->comment_ID; ?>">
|
||||
<div class="comment-user"> |
||||
<?php echo get_avatar($reply, 50); ?> |
||||
<div class="comment-user_text"> |
||||
<h6><?php echo get_comment_author($reply); ?></h6>
|
||||
<span><?php echo get_comment_date('d F Y', $reply); ?></span>
|
||||
</div> |
||||
</div> |
||||
<p><?php echo get_comment_text($reply); ?></p>
|
||||
<div class="comment-btns"> |
||||
<button class="reply-to-comment" data-comment-id="<?php echo $reply->comment_ID; ?>" data-post-id="<?php echo get_the_ID(); ?>" data-reply-to="<?php echo esc_attr(get_comment_author($reply)); ?>">
|
||||
<svg width="11" height="8" viewBox="0 0 11 8" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M9.5 3H3.25C2.00736 3 1 4.00736 1 5.25V5.25C1 6.49264 2.00736 7.5 3.25 7.5H3.5" stroke="#666666"></path> |
||||
<path d="M7 0.5L9.5 3L7 5.5" stroke="#666666"></path> |
||||
</svg> |
||||
<?php pll_e('Ответить'); ?> |
||||
</button> |
||||
<button class="comment-like-btn<?php echo is_user_liked_comment($reply->comment_ID) ? ' active' : ''; ?>" data-comment-id="<?php echo $reply->comment_ID; ?>">
|
||||
<svg width="15" height="13" viewBox="0 0 15 13" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M10.7745 1C8.67239 1 7.7426 2.5355 7.54047 3.30325C7.39224 2.5355 6.49748 1 4.10427 1C1.11276 1 -0.221288 4.85199 2.40639 8.14801C4.50853 10.7848 6.705 11.8147 7.54047 12C8.47026 11.8279 10.8311 10.761 12.8363 7.87004C15.3427 4.25632 13.4022 1 10.7745 1Z" <?php echo is_user_liked_comment($reply->comment_ID) ? 'stroke="#ff0000" fill="rgba(255, 0, 0, 0.7)"' : 'stroke="#666666"'; ?>></path>
|
||||
</svg> |
||||
<?php echo get_comment_likes_count($reply->comment_ID); ?> |
||||
</button> |
||||
<?php |
||||
comment_reply_link(array( |
||||
'reply_text' => pll__('Ответить'), |
||||
'depth' => 2, |
||||
'max_depth' => get_option('thread_comments_depth'), |
||||
'before' => '<span style="display:none;">', |
||||
'after' => '</span>', |
||||
), $reply->comment_ID, get_the_ID()); |
||||
?> |
||||
</div> |
||||
<div class="reply-form-container" id="reply-form-<?php echo $reply->comment_ID; ?>" style="display: none;"></div>
|
||||
</div> |
||||
<?php |
||||
} |
||||
} |
||||
} |
||||
|
||||
echo '</div>'; |
||||
} |
||||
|
||||
function get_all_nested_replies($comment_id) { |
||||
$all_replies = array(); |
||||
|
||||
$direct_replies = get_comments(array( |
||||
'post_id' => get_the_ID(), |
||||
'status' => 'approve', |
||||
'parent' => $comment_id, |
||||
'order' => 'ASC' |
||||
)); |
||||
|
||||
$all_replies = array_merge($all_replies, $direct_replies); |
||||
|
||||
foreach ($direct_replies as $reply) { |
||||
$deeper_replies = get_all_nested_replies_recursive($reply->comment_ID); |
||||
$all_replies = array_merge($all_replies, $deeper_replies); |
||||
} |
||||
|
||||
return $all_replies; |
||||
} |
||||
|
||||
function get_all_nested_replies_recursive($comment_id) { |
||||
$replies = get_comments(array( |
||||
'post_id' => get_the_ID(), |
||||
'status' => 'approve', |
||||
'parent' => $comment_id, |
||||
'order' => 'ASC' |
||||
)); |
||||
|
||||
$all_replies = $replies; |
||||
|
||||
foreach ($replies as $reply) { |
||||
$deeper_replies = get_all_nested_replies_recursive($reply->comment_ID); |
||||
$all_replies = array_merge($all_replies, $deeper_replies); |
||||
} |
||||
|
||||
return $all_replies; |
||||
} |
||||
|
||||
if (is_user_logged_in()) { |
||||
$commenter = wp_get_current_commenter(); |
||||
$args = array( |
||||
'fields' => array( |
||||
'author' => '', |
||||
'email' => '', |
||||
'url' => '', |
||||
'cookies' => '', |
||||
), |
||||
'comment_field' => '<div class="comment-form-comment"><textarea id="comment" name="comment" placeholder="' . pll__('Ваш комментарий') . '" required></textarea></div>', |
||||
'title_reply' => pll__('Оставить комментарий'), |
||||
'title_reply_to' => pll__('Ответить %s'), |
||||
'class_submit' => 'comment-submit-btn', |
||||
'submit_button' => '<button name="%1$s" type="submit" id="%2$s" class="%3$s">%4$s</button>', |
||||
'submit_field' => '<div class="form-submit">%1$s %2$s</div>', |
||||
'cancel_reply_link' => pll__('Отменить'), |
||||
'id_form' => 'commentform', |
||||
'id_submit' => 'submit', |
||||
'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">', |
||||
'title_reply_after' => '</h3>', |
||||
); |
||||
|
||||
comment_form($args); |
||||
} |
||||
?> |
||||
/** |
||||
* Start: comments.php |
||||
* Стандартный файл WordPress для отображения комментариев к записи |
||||
*/ |
||||
include_component('blog', 'comments'); |
||||
|
||||
/** |
||||
* End: comments.php |
||||
*/ |
@ -0,0 +1,53 @@ |
||||
<?php |
||||
use Timber\Timber; |
||||
|
||||
$context = Timber::context(); |
||||
$post = Timber::get_post(); |
||||
$context['post'] = $post; |
||||
$context['top_level_comments'] = get_comments([ |
||||
'post_id' => $post->ID, |
||||
'status' => 'approve', |
||||
'parent' => 0, |
||||
'order' => 'ASC' |
||||
]); |
||||
|
||||
$context['is_user_logged_in'] = is_user_logged_in(); |
||||
$context['comment_form_args'] = [ |
||||
'comment_field' => '<div class="comment-form-comment"><textarea id="comment" name="comment" placeholder="' . pll__('Ваш комментарий') . '" required></textarea></div>', |
||||
'title_reply' => pll__('Оставить комментарий'), |
||||
'title_reply_to' => pll__('Ответить %s'), |
||||
'cancel_reply_link' => pll__('Отменить'), |
||||
'id_form' => 'commentform', |
||||
'id_submit' => 'submit', |
||||
'class_submit' => 'comment-submit-btn', |
||||
'submit_button' => '<button name="%1$s" type="submit" id="%2$s" class="%3$s">%4$s</button>', |
||||
'submit_field' => '<div class="form-submit">%1$s %2$s</div>', |
||||
'fields' => [ |
||||
'author' => '', |
||||
'email' => '', |
||||
'url' => '', |
||||
'cookies' => '', |
||||
], |
||||
]; |
||||
|
||||
function get_all_nested_replies($comment_id, $post_id) { |
||||
$all_replies = []; |
||||
$direct_replies = get_comments([ |
||||
'post_id' => $post_id, |
||||
'status' => 'approve', |
||||
'parent' => $comment_id, |
||||
'order' => 'ASC' |
||||
]); |
||||
$all_replies = array_merge($all_replies, $direct_replies); |
||||
foreach ($direct_replies as $reply) { |
||||
$all_replies = array_merge($all_replies, get_all_nested_replies($reply->comment_ID, $post_id)); |
||||
} |
||||
return $all_replies; |
||||
} |
||||
|
||||
foreach ($context['top_level_comments'] as $comment) { |
||||
$comment->replies = get_all_nested_replies($comment->comment_ID, $post->ID); |
||||
} |
||||
|
||||
Timber::render('/blog/comments/comments.twig', $context); |
||||
?> |
@ -0,0 +1,63 @@ |
||||
<div class="comment-block{% if depth > 1 %} answer-block{% endif %}" id="comment-{{ comment.comment_ID }}"> |
||||
<div class="comment-user"> |
||||
{{ fn('get_avatar', comment, 50) }} |
||||
<div class="comment-user_text"> |
||||
<h6>{{ comment.comment_author }}</h6> |
||||
<span>{{ fn('get_comment_date', 'd F Y', comment) }}</span> |
||||
</div> |
||||
</div> |
||||
<p>{{ comment.comment_content }}</p> |
||||
|
||||
<div class="comment-btns"> |
||||
<button class="reply-to-comment" |
||||
data-comment-id="{{ comment.comment_ID }}" |
||||
data-post-id="{{ post.ID }}" |
||||
data-reply-to="{{ comment.comment_author|e }}"> |
||||
<svg width="11" height="8" viewBox="0 0 11 8" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M9.5 3H3.25C2.00736 3 1 4.00736 1 5.25V5.25C1 6.49264 2.00736 7.5 3.25 7.5H3.5" stroke="#666666"/> |
||||
<path d="M7 0.5L9.5 3L7 5.5" stroke="#666666"/> |
||||
</svg> |
||||
{{ fn('pll_e', 'Ответить') }} |
||||
</button> |
||||
|
||||
<button class="comment-like-btn{% if fn('is_user_liked_comment', comment.comment_ID) %} active{% endif %}" |
||||
data-comment-id="{{ comment.comment_ID }}"> |
||||
<svg width="15" height="13" viewBox="0 0 15 13" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M10.7745 1C8.67239 1 7.7426 2.5355 7.54047 3.30325C7.39224 2.5355 6.49748 1 4.10427 1C1.11276 1 -0.221288 4.85199 2.40639 8.14801C4.50853 10.7848 6.705 11.8147 7.54047 12C8.47026 11.8279 10.8311 10.761 12.8363 7.87004C15.3427 4.25632 13.4022 1 10.7745 1Z" |
||||
{% if fn('is_user_liked_comment', comment.comment_ID) %} |
||||
stroke="#ff0000" fill="rgba(255, 0, 0, 0.7)" |
||||
{% else %} |
||||
stroke="#666666" |
||||
{% endif %} |
||||
/> |
||||
</svg> |
||||
{{ fn('get_comment_likes_count', comment.comment_ID) }} |
||||
</button> |
||||
|
||||
{{ fn('comment_reply_link', { |
||||
reply_text: fn('pll_e', 'Ответить'), |
||||
depth: depth, |
||||
max_depth: fn('get_option', 'thread_comments_depth'), |
||||
before: '<span style="display:none;">', |
||||
after: '</span>' |
||||
}, comment.comment_ID, post.ID) }} |
||||
</div> |
||||
|
||||
<div class="reply-form-container" id="reply-form-{{ comment.comment_ID }}" style="display: none;"></div> |
||||
|
||||
{% if comment.replies %} |
||||
<button class="answer-btn"> |
||||
<svg width="15" height="10" viewBox="0 0 15 10" fill="none" xmlns="http://www.w3.org/2000/svg"> |
||||
<path d="M14 1.41406L7.5 7.91406L1 1.41406" stroke="#76CE75" stroke-width="2" /> |
||||
</svg> |
||||
{{ comment.replies|length }} |
||||
<span> |
||||
{{ comment.replies|length == 1 ? fn('pll_e', 'ответ') : fn('pll_e', 'ответа') }} |
||||
</span> |
||||
</button> |
||||
|
||||
{% for reply in comment.replies %} |
||||
{% include 'partials/comment.twig' with { comment: reply, depth: depth + 1 } %} |
||||
{% endfor %} |
||||
{% endif %} |
||||
</div> |
@ -0,0 +1,23 @@ |
||||
{% if not post.password_required %} |
||||
{% if not is_user_logged_in %} |
||||
<div class="comment-block"> |
||||
<p class="comment-block_text"> |
||||
<button class="login-open">{{ fn('pll_e', 'Войдите') }}</button>, {{ fn('pll_e', 'чтобы оставлять комментарии') }} |
||||
</p> |
||||
</div> |
||||
{% endif %} |
||||
|
||||
{% if top_level_comments %} |
||||
<div class="comment-block_wrapper"> |
||||
{% for comment in top_level_comments %} |
||||
{% include 'blog/comments/comment-single.twig' with { comment: comment, depth: 1 } %} |
||||
{% endfor %} |
||||
</div> |
||||
{% endif %} |
||||
|
||||
{% if is_user_logged_in %} |
||||
<div class="comment-form-container"> |
||||
{{ fn('comment_form', comment_form_args) }} |
||||
</div> |
||||
{% endif %} |
||||
{% endif %} |
Loading…
Reference in new issue