forum-list.php
<?php if (!defined('ABSPATH')) exit; ?>
<h2>フォーラム一覧</h2>
<p>新しいスレッドを作成するには、以下にフォーラム名とタイトルを入力してください。</p>
<?php if (is_user_logged_in()) : ?>
<form method="post" action="">
<label>フォーラム:
<select name="forum_id" required>
<?php
global $wpdb;
$forums = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}forum_forums");
foreach ($forums as $forum) {
echo '<option value="' . esc_attr($forum->id) . '">' . esc_html($forum->forum_name) . '</option>';
}
?>
</select>
</label><br>
<label>スレッドタイトル: <input type="text" name="thread_title" required></label><br>
<label>最初の投稿: <textarea name="first_post" required></textarea></label><br>
<input type="submit" name="create_thread" value="スレッドを作成">
</form>
<?php endif; ?>
<ul>
<?php if ($threads) : ?>
<?php
$grouped_threads = [];
foreach ($threads as $thread) {
$forum = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}forum_forums WHERE id = {$thread->forum_id}");
$grouped_threads[$forum->forum_name][] = $thread;
}
foreach ($grouped_threads as $forum_name => $forum_threads) : ?>
<li><?php echo esc_html($forum_name); ?>
<ul>
<?php foreach ($forum_threads as $thread) : ?>
<li>
<a href="<?php echo site_url('/thread-page/?forum_id=' . esc_attr($thread->forum_id) . '&thread_id=' . $thread->id); ?>">
<?php echo esc_html($thread->thread_title); ?>
</a>
(投稿数: <?php echo $thread->post_count; ?>)
<?php if (current_user_can('manage_options')) : ?>
<form method="post" action="" style="display:inline;" onsubmit="return confirmDelete(<?php echo $thread->id; ?>);">
<input type="hidden" name="thread_id" value="<?php echo $thread->id; ?>">
<input type="submit" name="delete_thread" value="削除" class="button-secondary">
</form>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
<?php else : ?>
<li>まだスレッドがありません。</li>
<?php endif; ?>
</ul>
<script>
function confirmDelete(threadId) {
return confirm('スレッド #' + threadId + ' を本当に削除しますか?');
}
</script>
thread-view.php
<?php if (!defined('ABSPATH')) exit; ?>
<h3><?php echo esc_html($thread->thread_title); ?></h3>
<div>
<?php
$display_mode = isset($_GET['display_mode']) ? $_GET['display_mode'] : 'number';
?>
<label><input type="radio" name="display_mode" value="number" <?php echo $display_mode === 'number' ? 'checked' : ''; ?> onclick="toggleDisplay('number')"> 番号順</label>
<label><input type="radio" name="display_mode" value="tree" <?php echo $display_mode === 'tree' ? 'checked' : ''; ?> onclick="toggleDisplay('tree')"> ツリー表示</label>
</div>
<div class="posts" id="posts-number" style="display: <?php echo $display_mode === 'number' ? 'block' : 'none'; ?>;">
<?php if ($posts) : ?>
<?php foreach ($posts as $post) : ?>
<p id="number-post-<?php echo $post->id; ?>">
#<?php echo $post->post_number; ?>
<?php if ($post->parent_post_id) echo '>>' . $post->parent_post_id; ?>
<?php echo esc_html($post->content); ?> -
<?php echo esc_html(get_userdata($post->author_id)->user_login); ?>
(投稿時間: <?php echo date_i18n('Y年m月d日 H:i', strtotime($post->created_at)); ?>)
(称号: <?php echo esc_html($wpdb->get_var("SELECT title FROM {$wpdb->prefix}forum_points WHERE user_id = {$post->author_id}") ?: '初心者'); ?>)
<span class="likes-count" data-post-id="<?php echo $post->id; ?>">(いいね: <?php echo $post->likes; ?>)</span>
<?php if (is_user_logged_in()) : ?>
<?php $liked = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}forum_likes WHERE user_id = " . get_current_user_id() . " AND post_id = $post->id"); ?>
<button class="like-button" data-post-id="<?php echo $post->id; ?>"><?php echo $liked ? 'いいねを取り消す' : 'いいね'; ?></button>
<button class="reply-button" data-post-id="<?php echo $post->id; ?>">返信</button>
<?php endif; ?>
</p>
<?php endforeach; ?>
<?php else : ?>
<p>まだ投稿がありません。</p>
<?php endif; ?>
</div>
<div class="posts" id="posts-tree" style="display: <?php echo $display_mode === 'tree' ? 'block' : 'none'; ?>;">
<?php
function display_tree($posts, $parent_id = null, $level = 0) {
global $wpdb;
foreach ($posts as $post) {
if ($post->parent_post_id == $parent_id) {
echo '<p style="margin-left:' . ($level * 20) . 'px;" id="tree-post-' . $post->id . '">';
echo '#' . $post->post_number . ' ';
if ($post->parent_post_id) echo '>>' . $post->parent_post_id . ' ';
echo esc_html($post->content) . ' - ';
echo esc_html(get_userdata($post->author_id)->user_login) . ' ';
echo '(投稿時間: ' . date_i18n('Y年m月d日 H:i', strtotime($post->created_at)) . ') ';
echo '(称号: ' . esc_html($wpdb->get_var("SELECT title FROM {$wpdb->prefix}forum_points WHERE user_id = {$post->author_id}") ?: '初心者') . ') ';
echo '<span class="likes-count" data-post-id="' . $post->id . '">(いいね: ' . $post->likes . ')</span>';
if (is_user_logged_in()) {
$liked = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}forum_likes WHERE user_id = " . get_current_user_id() . " AND post_id = $post->id");
echo ' <button class="like-button" data-post-id="' . $post->id . '">' . ($liked ? 'いいねを取り消す' : 'いいね') . '</button>';
echo ' <button class="reply-button" data-post-id="' . $post->id . '">返信</button>';
}
echo '</p>';
display_tree($posts, $post->id, $level + 1);
}
}
}
if ($posts) display_tree($posts);
?>
</div>
<?php if (is_user_logged_in()) : ?>
<button id="show-post-form" style="display:none;">新規投稿フォームを表示</button>
<form method="post" id="post-form" style="display:block;">
<textarea name="content" required></textarea>
<input type="hidden" name="thread_id" value="<?php echo $thread->id; ?>">
<input type="hidden" name="parent_post_id" id="parent_post_id">
<input type="hidden" name="display_mode" id="display_mode_input" value="<?php echo $display_mode; ?>">
<input type="submit" name="submit_post" value="投稿">
</form>
<?php endif; ?>
<script>
function toggleDisplay(mode) {
console.log('Toggle display mode: ' + mode);
document.getElementById('posts-number').style.display = mode === 'number' ? 'block' : 'none';
document.getElementById('posts-tree').style.display = mode === 'tree' ? 'block' : 'none';
document.getElementById('display_mode_input').value = mode;
var form = document.getElementById('post-form');
var defaultPosition = document.getElementById('show-post-form').parentNode;
console.log('Resetting form to default position');
defaultPosition.appendChild(form);
form.style.display = 'block';
document.getElementById('parent_post_id').value = '';
document.getElementById('show-post-form').style.display = 'none';
// URLを更新
const url = new URL(window.location);
url.searchParams.set('display_mode', mode);
window.history.pushState({}, '', url);
}
document.addEventListener('DOMContentLoaded', function() {
var form = document.getElementById('post-form');
var defaultPosition = form.parentNode;
var showPostFormButton = document.getElementById('show-post-form');
if (!form) {
console.error('Post form not found');
return;
}
console.log('Post form found');
// 初期表示モードをPHPから反映
var initialMode = '<?php echo $display_mode; ?>';
document.getElementById('posts-number').style.display = initialMode === 'number' ? 'block' : 'none';
document.getElementById('posts-tree').style.display = initialMode === 'tree' ? 'block' : 'none';
document.getElementById('display_mode_input').value = initialMode;
if (form.style.display === 'block') {
showPostFormButton.style.display = 'none';
}
showPostFormButton.addEventListener('click', function() {
console.log('Show post form button clicked');
document.getElementById('parent_post_id').value = '';
defaultPosition.appendChild(form);
form.style.display = 'block';
showPostFormButton.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (event.target.classList.contains('reply-button')) {
var postId = event.target.getAttribute('data-post-id');
console.log('Reply button clicked for post ID: ' + postId);
var isTreeMode = document.getElementById('posts-tree').style.display === 'block';
var postElementId = isTreeMode ? 'tree-post-' + postId : 'number-post-' + postId;
var postElement = document.getElementById(postElementId);
console.log('Looking for element with ID: ' + postElementId);
if (postElement) {
console.log('Post element found for ID: ' + postId);
document.getElementById('parent_post_id').value = postId;
console.log('Inserting form after post ID: ' + postId);
var parentNode = postElement.parentNode;
var nextSibling = postElement.nextSibling;
console.log('Parent node: ', parentNode);
console.log('Next sibling: ', nextSibling);
if (nextSibling) {
parentNode.insertBefore(form, nextSibling);
} else {
parentNode.appendChild(form);
}
console.log('Setting form display to block');
form.style.display = 'block';
console.log('Current form display: ' + form.style.display);
showPostFormButton.style.display = 'block';
} else {
console.error('Post element not found for ID: ' + postElementId);
}
}
});
form.addEventListener('submit', function(event) {
console.log('Form submitted with parent_post_id: ' + document.getElementById('parent_post_id').value);
document.getElementById('parent_post_id').value = ''; // 送信後にリセット
showPostFormButton.style.display = 'none';
});
});
</script>
コメント