File: /var/www/html/wp-content/themes/ganryujima_new_1602/_comment-template.php
<?php
$current_user_id = get_current_user_id();
// 全件表示、ページング表示の状態をセッションで管理
if(!isset($_SESSION['discussion-view'])) {
$_SESSION['discussion-view'] = 'paging';
}
// GET で view が指定されている場合はそちらに変更
if(isset($_GET['view'])) {
if($_GET['view'] == 'all') {
$_SESSION['discussion-view'] = 'all';
} else if($_GET['view'] == 'paging') {
$_SESSION['discussion-view'] = 'paging';
}
}
?>
<script>var DISCUSSION_VIEW = "<?= $_SESSION['discussion-view'] ?>";</script>
<section id="post_list">
<?php
// コメント表示のテンプレート
$comment_template = '
<a id="post_id_{{POST_ID}}" class="post_id_a"></a><!-- しおりのアンカー -->
<article class="l-paper l-article m_discussion_comment table" data-comment-id="{{POST_ID}}" id="post_{{POST_ID}}">
<div class="avatar">
<div class="icon">
{{USER_ICON}}
</div>
</div>
<div class="com_cell">
<p class="com_name">{{USER_DISPLAY_NAME}}</p>
<p class="com_date">
<span class="com_num">No.{{INDEX}}</span>
{{POST_TIME}}
<!-- 削除リンク -->
{{DELETE_LINK}}
</p>
<p>
<!-- 投稿コメント -->
{{COMMENT_BODY}}
</p>
{{COMMENT_IMAGE}}
{{OPEN_GRAPH}}
<!-- しおり/トップ/ラスト -->
<ul class="shiori_box">
<li class="add_shiori" data-comment-id="{{POST_ID}}">
<a href="#">
<i class="fa fa-bookmark"></i>
しおりを付ける
</a>
</li>
<li class="remove_shiori" data-comment-id="{{POST_ID}}">
<a href="#">
<i class="fa fa-bookmark-o"></i>
しおりをはずす
</a>
</li>
<li class="pc">
<a href="#target" class="back_to_top">
<i class="fa fa-arrow-circle-up"></i>
トップへもどる
</a>
</li>
<li>
<a href="#footer" class="go_to_bottom">
<i class="fa fa-arrow-circle-down"></i>
ラストへいく
</a>
</li>
</ul>
<!-- しおりマーク -->
<p class="shiori_mark" data-comment-id="{{POST_ID}}">
<a>
<i class="fa fa-bookmark"></i>
</a>
</p>
</div>
</article>
';
// Open Graph 専用パーツ, アップロードされている場合にも表示される
$open_graph_template = '
<div class="ogp_disp_area">
<a class="table" href="{{URL}}" target="_blank">
<div class="img_cell">
<figure class="inner">
<img src="{{IMAGE_DATA}}">
</figure>
</div>
<div class="cell">
<p class="preview_title">{{TITLE}}</p>
<p>{{DESCRIPTION}}</p>
</div>
</a>
</div>
';
// ページ番号の取得
echo '</section><section id="pagenation_sec">';
$revComments = array_reverse($comments);
$countComment = count($revComments);
if($_SESSION['discussion-view'] == 'all') {
// 全件表示の場合
for($i = 0; $i < $countComment; $i++) {
$comment = $revComments[$i];
$user = get_user_by('email', $comment->comment_author_email);
$post_id = $comment->comment_ID;
if(empty($user->ID)){
$rand_num = rand(1, 3);
$user_icon = '<img src="'.home_url().'/assets/images/avatar/avatar'.$rand_num.'.svg">';
} else {
$user_icon = get_avatar($user->ID);
}
if($user == false) {
$user_display_name = '退会済み';
} else {
$user_display_name = $user->display_name;
}
$post_time = mysql2date('Y/m/d H:i', $comment->comment_date);
$comment_body = $comment->comment_content;
$comment_body = auto_url($comment_body);
$index = sprintf('%03d', $countComment - $i);
$html = $comment_template;
$html = str_replace('{{POST_ID}}', $post_id, $html);
$html = str_replace('{{USER_ICON}}', $user_icon, $html);
$html = str_replace('{{USER_DISPLAY_NAME}}', $user_display_name, $html);
$html = str_replace('{{INDEX}}', $index, $html);
$html = str_replace('{{POST_TIME}}', $post_time, $html);
$html = str_replace('{{COMMENT_BODY}}', nl2br($comment_body), $html);
$html = str_replace('{{HOME_URL}}', home_url(), $html);
// 画像がアップロードされていたら表示する
$comment_image = get_comment_meta($comment->comment_ID, 'comment_image', true);
if($comment_image == '') {
$html = str_replace('{{COMMENT_IMAGE}}', '', $html);
} else {
$comment_html = '
<div class="comment_img">
<figure>
<a href="{{IMAGE_SRC}}" class="lightbox"><img alt="Image" class="linkbg_blue" src="{{IMAGE_SRC}}"></a>
</figure>
</div>
';
$comment_html = str_replace('{{IMAGE_SRC}}', $comment_image, $comment_html);
$html = str_replace('{{COMMENT_IMAGE}}', $comment_html, $html);
}
// Open Graph のパーツが設定されていたら表示する
$open_graph = get_comment_meta($comment->comment_ID, 'open_graph', true);
if($open_graph != '') {
$open_graph = json_decode($open_graph, true);
$open_graph_html = $open_graph_template;
$open_graph_html = str_replace('{{URL}}', $open_graph['url'], $open_graph_html);
$open_graph_html = str_replace('{{TITLE}}', $open_graph['title'], $open_graph_html);
$open_graph_html = str_replace('{{DESCRIPTION}}', $open_graph['description'], $open_graph_html);
$open_graph_html = str_replace('{{IMAGE_DATA}}', $open_graph['thumbnail'], $open_graph_html);
} else {
$open_graph_html = '';
}
$html = str_replace('{{OPEN_GRAPH}}', $open_graph_html, $html);
// 自分のコメントは削除できるようにする
$delete_link = '';
if($comment->user_id == $current_user_id) {
$delete_link = '<a href="#" data-comment-id="' . $comment->comment_ID . '" class="post-delete-link" data-method="delete" data-remote="true" rel="nofollow">削除する</a>';
}
$html = str_replace('{{DELETE_LINK}}', $delete_link, $html);
echo $html;
}
echo '
<div class="pagination-centered pagination_area" style="position:relative; min-height:100px;">
<label>
<a href="' . get_the_permalink() . '/?view=paging" class="button small radius login">ページング表示</a>
</label>
</div>
';
} else {
// ページング表示の場合
// ページ番号の取得
$per_page = get_option('comments_per_page', 50);
if(!isset($_GET['paging']) || $_GET['paging'] < 0 || $_GET['paging'] > $countComment / $per_page + 1) {
$current = 1;
} else {
$current = intval($_GET['paging']);
}
// しおり機能のために現在のページ番号を JS へ書き出す
echo '<script>var CURRENT_PAGE = ' . $current . ';</script>';
// コメントの表示
$start_index = ($current - 1) * $per_page;
for($i = $start_index; $i < $start_index + $per_page && $i < $countComment; $i++) {
$comment = $revComments[$i];
$user = get_user_by('email', $comment->comment_author_email);
$post_id = $comment->comment_ID;
if(empty($user->ID)){
$rand_num = rand(1, 3);
$user_icon = '<img src="'.home_url().'/assets/images/avatar/avatar'.$rand_num.'.svg">';
} else {
$user_icon = get_avatar($user->ID);
}
if($user == false) {
$user_display_name = '退会済み';
} else {
$user_display_name = $user->display_name;
}
$post_time = mysql2date('Y/m/d H:i', $comment->comment_date);
$comment_body = $comment->comment_content;
$comment_body = auto_url($comment_body);
$index = sprintf('%03d', $countComment - $i);
$html = $comment_template;
$html = str_replace('{{POST_ID}}', $post_id, $html);
$html = str_replace('{{USER_ICON}}', $user_icon, $html);
$html = str_replace('{{USER_DISPLAY_NAME}}', $user_display_name, $html);
$html = str_replace('{{INDEX}}', $index, $html);
$html = str_replace('{{POST_TIME}}', $post_time, $html);
$html = str_replace('{{COMMENT_BODY}}', nl2br($comment_body), $html);
$html = str_replace('{{HOME_URL}}', home_url(), $html);
// 画像がアップロードされていたら表示する
$comment_image = get_comment_meta($comment->comment_ID, 'comment_image', true);
if($comment_image == '') {
$html = str_replace('{{COMMENT_IMAGE}}', '', $html);
} else {
$comment_html = '
<div class="comment_img">
<figure>
<a href="{{IMAGE_SRC}}" class="lightbox"><img alt="Image" class="linkbg_blue" src="{{IMAGE_SRC}}"></a>
</figure>
</div>
';
$comment_html = str_replace('{{IMAGE_SRC}}', $comment_image, $comment_html);
$html = str_replace('{{COMMENT_IMAGE}}', $comment_html, $html);
}
// Open Graph のパーツが設定されていたら表示する
$open_graph = get_comment_meta($comment->comment_ID, 'open_graph', true);
if($open_graph != '') {
$open_graph = json_decode($open_graph, true);
$open_graph_html = $open_graph_template;
$open_graph_html = str_replace('{{URL}}', $open_graph['url'], $open_graph_html);
$open_graph_html = str_replace('{{TITLE}}', $open_graph['title'], $open_graph_html);
$open_graph_html = str_replace('{{DESCRIPTION}}', $open_graph['description'], $open_graph_html);
$open_graph_html = str_replace('{{IMAGE_DATA}}', $open_graph['thumbnail'], $open_graph_html);
} else {
$open_graph_html = '';
}
$html = str_replace('{{OPEN_GRAPH}}', $open_graph_html, $html);
// 自分のコメントは削除できるようにする
$delete_link = '';
if($comment->user_id == $current_user_id) {
$delete_link = '<a href="#" data-comment-id="' . $comment->comment_ID . '" class="post-delete-link" data-method="delete" data-remote="true" rel="nofollow">削除する</a>';
}
$html = str_replace('{{DELETE_LINK}}', $delete_link, $html);
echo $html;
}
// ページャ
$paging_template = '
<div class="pagination">
<div class="m_pagination">
<ul>
{{PAGING}}
</ul>
</div>
<p>
<a class="all_comments btn" href="' . get_the_permalink() . '/?view=all">全コメント表示</a>
</p>
</div>
';
// ページ数の計算
$page_count = ceil($countComment / $per_page);
if($page_count == 0) {
$page_count = 1;
}
// ページリンクの表示
$page_links = '';
$dot_flag = false;
for($i = 1; $i <= $page_count; $i++) {
if($i == $current) {
$page_links .= '<li><span class="page_numbers current">' . $i . '</span></li>';
$dot_flag = true;
} else if(($i <= 2 || $page_count - 1 <= $i) || ($current - 1 <= $i && $i <= $current + 1)) {
$page_links .= '<li><a class="page_numbers" rel="next" href="' . get_the_permalink() . '?paging=' . $i . '">' . $i . '</a></li>';
$dot_flag = true;
} else {
if($dot_flag) {
$page_links .= '<li class="gap">...</li>';
$dot_flag = false;
}
}
}
// 前後リンクの作成
if($current == 1) {
$prev = '';
} else {
$prev = '<li><a class="page_numbers previous" rel="prev" href="' . get_the_permalink() . '?paging=' . ($current - 1) . '">« 前へ</a></li>';
}
if($current == $page_count) {
$next = '';
} else {
$next = '<li><a class="page_numbers next" rel="next" href="' . get_the_permalink() . '?paging=' . ($current + 1) . '">次へ »</a></li>';
}
$html = $paging_template;
$html = str_replace('{{HOME_URL}}', home_url(), $html);
$html = str_replace('{{PAGING}}', $prev . $page_links . $next, $html);
echo $html;
}
function auto_url($str){
preg_match_all("/((http|https):\/\/[\w\/\@\$()!?&%#:;.,~'=*+-]+)/i",strip_tags($str), $array_url);
for($i=0;$i<count($array_url[1]);++$i){
$linkhtml="<a href=\"$array_url[1][$i]\">$array_url[1][$i]</a>";
$str=str_replace($array_url[1][$i],"<a href=\"".$array_url[1][$i]."\" target=\"_blank\">".$array_url[1][$i]."</a>",$str);
}
return $str;
}
?>
</section>