Files
kjweiji/comments.php
T

88 lines
3.8 KiB
PHP

<?php
/**
* 空笺未寄 — 评论模板
*/
if (post_password_required()) return;
?>
<div id="comments" class="comments-section">
<?php if (have_comments()) : ?>
<h3 class="comment-reply-title">笺下留言</h3>
<ol class="comment-list">
<?php
$comments = get_comments(array('post_id' => get_the_ID(), 'status' => 'approve', 'order' => 'ASC'));
foreach ($comments as $c) :
$is_reply = $c->comment_parent > 0;
$parent = $is_reply ? get_comment($c->comment_parent) : null;
?>
<li class="comment-item<?php echo $is_reply ? ' comment-item--reply' : ''; ?>">
<div class="comment-item__meta">
<?php echo get_avatar($c, 18, '', '', array('class' => 'comment-avatar')); ?>
<span class="comment-item__author"><?php echo esc_html($c->comment_author); ?></span>
<?php if ($parent) : ?>
<span class="comment-item__replyto">回复 @<?php echo esc_html($parent->comment_author); ?></span>
<?php endif; ?>
<span class="comment-item__time"><?php echo get_comment_date('Y-m-d H:i:s', $c); ?></span>
</div>
<div class="comment-item__text"><?php echo esc_html($c->comment_content); ?></div>
<div class="comment-item__reply">
<a href="javascript:void(0)" onclick="replyTo(<?php echo $c->comment_ID; ?>)">回复</a>
</div>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
<?php if (is_user_logged_in()) : ?>
<?php comment_form(array(
'title_reply' => '',
'comment_notes_before' => '',
'comment_notes_after' => '',
'logged_in_as' => '',
'fields' => array(),
'comment_field' => '<textarea id="comment" name="comment" rows="3" placeholder="写点什么..." required></textarea>',
'submit_button' => '<button type="submit" class="submit">落笔留痕</button>',
'submit_field' => '<div class="form-submit">%1$s %2$s</div>',
'label_submit' => '落笔留痕',
)); ?>
<?php else : ?>
<form id="commentform-guest" class="comment-form" novalidate>
<textarea id="comment" name="comment" rows="3" placeholder="写点什么..." required></textarea>
<input type="hidden" id="comment_parent" name="comment_parent" value="0">
<div class="form-submit">
<button type="submit" class="submit">落笔留痕</button>
</div>
</form>
<?php endif; ?>
<?php if (!is_user_logged_in()) : ?>
<div id="login-modal" class="login-modal" style="display:none">
<div class="login-modal__content">
<p>登录后才能留言</p>
<button onclick="goLogin()" class="submit">去登录</button>
<button onclick="closeLoginModal()" class="cancel-btn">取消</button>
</div>
</div>
<script>
function replyTo(cid) {
document.getElementById('comment_parent').value = cid;
document.getElementById('comment').focus();
}
function goLogin() {
window.location.href = '/wp-login.php?redirect_to=' + encodeURIComponent(window.location.href);
}
function closeLoginModal() {
var m = document.getElementById('login-modal');
if (m) m.style.display = 'none';
}
document.getElementById('commentform-guest').addEventListener('submit', function(e) {
e.preventDefault();
var m = document.getElementById('login-modal');
if (m) m.style.display = 'flex';
});
</script>
<?php endif; ?>
</div>