feat(forum): add complete forum workflows
This commit is contained in:
1 parent
a254cbd3e9
commit
ee65fc7fd2
39 files changed
+3425
-4
No files matched your search
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
use Jio\Forum\Access;
|
||||
use Jio\Forum\Core;
|
||||
use Jio\Forum\View;
|
||||
|
||||
$edit_id = absint(get_query_var('jio_forum_edit'));
|
||||
$post = $edit_id > 0 ? get_post($edit_id) : null;
|
||||
if (!is_user_logged_in()) {
|
||||
wp_safe_redirect(jio_get_login_url(Core::edit_url($edit_id)));
|
||||
exit;
|
||||
}
|
||||
if ($edit_id > 0 && (!$post instanceof WP_Post || !Access::can_manage_post(get_current_user_id(), $edit_id))) {
|
||||
status_header(403);
|
||||
wp_die(esc_html__('无权编辑此帖子', 'jio'));
|
||||
}
|
||||
|
||||
wp_enqueue_editor();
|
||||
|
||||
$plate_id = $post ? (int) get_post_meta($post->ID, '_jio_forum_plate_id', true) : absint($_GET['plate'] ?? 0);
|
||||
$vote_started = $post ? (int) get_post_meta($post->ID, '_jio_forum_vote_started', true) : 0;
|
||||
$vote_deadline = $post ? (int) get_post_meta($post->ID, '_jio_forum_vote_deadline', true) : 0;
|
||||
$vote_days = $vote_started && $vote_deadline ? (int) round(($vote_deadline - $vote_started) / DAY_IN_SECONDS) : 0;
|
||||
$plates = get_posts(['post_type' => Core::PLATE, 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC']);
|
||||
View::open($post ? '编辑帖子' : '发布帖子', 'edit');
|
||||
?>
|
||||
<div class="jio-forum__layout"><div class="jio-forum__main">
|
||||
<h1><?php echo $post ? '编辑帖子' : '发布帖子'; ?></h1>
|
||||
<form class="jio-forum__form" data-forum-form="post" data-id="<?php echo esc_attr((string) $edit_id); ?>">
|
||||
<label>标题<input name="title" required maxlength="120" value="<?php echo esc_attr($post ? $post->post_title : ''); ?>"></label>
|
||||
<label>版块<select name="plate_id" required><option value="">选择版块</option><?php foreach ($plates as $plate) : ?><option value="<?php echo esc_attr((string) $plate->ID); ?>" <?php selected($plate_id, $plate->ID); ?>><?php echo esc_html($plate->post_title); ?></option><?php endforeach; ?></select></label>
|
||||
<label>类型<select name="type"><option value="normal">普通帖子</option><option value="question" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_type', true) : '', 'question'); ?>>问答</option><option value="vote" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_type', true) : '', 'vote'); ?>>投票</option></select></label>
|
||||
<label>正文</label>
|
||||
<?php wp_editor($post ? $post->post_content : '', 'jio_forum_content', ['textarea_name' => 'content', 'textarea_rows' => 16, 'media_buttons' => false, 'quicktags' => true, 'tinymce' => ['toolbar1' => 'formatselect,bold,italic,bullist,numlist,blockquote,link,unlink,alignleft,aligncenter,alignright,undo,redo', 'toolbar2' => 'forecolor,removeformat']]); ?>
|
||||
<div class="jio-forum__editor-tools">
|
||||
<?php foreach (['image' => '插入图片', 'video' => '插入视频', 'resource' => '插入附件'] as $kind => $label) : ?>
|
||||
<?php if (jio_get_setting('jio_forum_upload_' . ($kind === 'resource' ? 'file' : $kind), $kind === 'image')) : ?><button type="button" class="jio-forum__action" data-forum-upload="<?php echo esc_attr($kind); ?>"><?php echo esc_html($label); ?></button><?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
<input type="file" data-forum-file hidden>
|
||||
</div>
|
||||
<label>话题<input name="topics" value="<?php echo esc_attr($post ? implode(', ', wp_get_post_terms($post->ID, Core::TOPIC, ['fields' => 'names'])) : ''); ?>" placeholder="多个话题以逗号分隔"></label>
|
||||
<label>标签<input name="tags" value="<?php echo esc_attr($post ? implode(', ', wp_get_post_terms($post->ID, Core::TAG, ['fields' => 'names'])) : ''); ?>" placeholder="多个标签以逗号分隔"></label>
|
||||
<div class="jio-forum__form-extra" data-forum-type="vote">
|
||||
<label>投票标题<input name="vote_title" maxlength="60" value="<?php echo esc_attr($post ? (string) get_post_meta($post->ID, '_jio_forum_vote_title', true) : ''); ?>"></label>
|
||||
<label>投票方式<select name="vote_type"><option value="single">单选</option><option value="multiple" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_vote_type', true) : '', 'multiple'); ?>>多选</option><option value="pk" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_vote_type', true) : '', 'pk'); ?>>双选 PK</option></select></label>
|
||||
<label>投票选项<textarea name="vote_options" rows="4" placeholder="每行一个选项"><?php echo esc_textarea($post ? implode("\n", (array) get_post_meta($post->ID, '_jio_forum_vote_options', true)) : ''); ?></textarea></label>
|
||||
<label>有效期<select name="vote_days"><option value="0" <?php selected($vote_days, 0); ?>>永久</option><option value="1" <?php selected($vote_days, 1); ?>>1 天</option><option value="7" <?php selected($vote_days, 7); ?>>7 天</option><option value="30" <?php selected($vote_days, 30); ?>>30 天</option></select></label>
|
||||
</div>
|
||||
<label>访问权限<select name="view_limit"><option value="all">所有人</option><option value="login" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_view_limit', true) : '', 'login'); ?>>登录后</option><option value="follow" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_view_limit', true) : '', 'follow'); ?>>关注版块后</option><option value="points" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_view_limit', true) : '', 'points'); ?>>积分解锁</option><option value="pay" <?php selected($post ? get_post_meta($post->ID, '_jio_forum_view_limit', true) : '', 'pay'); ?>>付费解锁</option></select></label>
|
||||
<label>解锁价格<input name="price" type="number" min="0" step="0.01" value="<?php echo esc_attr($post ? (string) get_post_meta($post->ID, '_jio_forum_price', true) : '0'); ?>"></label>
|
||||
<label>会员一级价格<input name="price_vip1" type="number" min="0" step="0.01" value="<?php echo esc_attr($post ? (string) get_post_meta($post->ID, '_jio_forum_price_vip1', true) : ''); ?>"></label>
|
||||
<label>会员二级价格<input name="price_vip2" type="number" min="0" step="0.01" value="<?php echo esc_attr($post ? (string) get_post_meta($post->ID, '_jio_forum_price_vip2', true) : ''); ?>"></label>
|
||||
<p class="jio-forum__message" role="status" aria-live="polite"></p><button type="submit" class="jio-forum__primary">保存帖子</button>
|
||||
</form>
|
||||
</div><?php View::sidebar('edit'); ?></div>
|
||||
<?php View::close(); ?>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
defined('ABSPATH') || exit;
|
||||
\Jio\Forum\View::listing();
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
use Jio\Forum\Access;
|
||||
use Jio\Forum\Core;
|
||||
use Jio\Forum\View;
|
||||
|
||||
the_post();
|
||||
$post = get_post();
|
||||
$id = (int) $post->ID;
|
||||
$plate_id = (int) get_post_meta($id, '_jio_forum_plate_id', true);
|
||||
$type = (string) get_post_meta($id, '_jio_forum_type', true);
|
||||
$user_id = get_current_user_id();
|
||||
$can_view = Access::can_view($user_id, $id);
|
||||
$can_manage = Access::can_manage_post($user_id, $id);
|
||||
$can_moderate = Access::is_moderator($user_id, $plate_id);
|
||||
View::open($post->post_title, 'post');
|
||||
\Jio\WidgetSystem\WidgetAreas::render('forum_post_top_fluid');
|
||||
?>
|
||||
<div class="jio-forum__layout"><div class="jio-forum__main">
|
||||
<article class="jio-forum__single">
|
||||
<header class="jio-forum__single-head">
|
||||
<div class="jio-forum__single-meta"><?php echo get_avatar((int) $post->post_author, 40); ?><span><?php echo esc_html(get_the_author_meta('display_name', (int) $post->post_author)); ?></span><time datetime="<?php echo esc_attr(get_the_date(DATE_W3C)); ?>"><?php echo esc_html(get_the_date()); ?></time></div>
|
||||
<h1><?php echo esc_html($post->post_title); ?></h1>
|
||||
<div class="jio-forum__badges">
|
||||
<?php if (get_post_meta($id, '_jio_forum_pinned', true)) : ?><span>置顶</span><?php endif; ?>
|
||||
<?php if ($type === 'question') : ?><span>问答</span><?php elseif ($type === 'vote') : ?><span>投票</span><?php endif; ?>
|
||||
<?php if ($type === 'question' && get_post_meta($id, '_jio_forum_adopted_comment', true)) : ?><span>已解决</span><?php endif; ?>
|
||||
<?php if (get_post_meta($id, '_jio_forum_essence', true)) : ?><span>精华</span><?php endif; ?>
|
||||
<?php if ($plate_id > 0) : ?><a href="<?php echo esc_url(get_permalink($plate_id)); ?>"><?php echo esc_html(get_the_title($plate_id)); ?></a><?php endif; ?>
|
||||
<?php foreach (wp_get_post_terms($id, Core::TOPIC) as $term) : ?><a href="<?php echo esc_url(get_term_link($term)); ?>">#<?php echo esc_html($term->name); ?></a><?php endforeach; ?>
|
||||
</div>
|
||||
</header>
|
||||
<?php if ($can_view) : ?>
|
||||
<div class="jio-forum__single-content"><?php echo apply_filters('the_content', $post->post_content); ?></div>
|
||||
<?php if ($type === 'vote') : ?>
|
||||
<?php
|
||||
$options = (array) get_post_meta($id, '_jio_forum_vote_options', true);
|
||||
$votes = (array) get_post_meta($id, '_jio_forum_vote_counts', true);
|
||||
$vote_type = (string) get_post_meta($id, '_jio_forum_vote_type', true);
|
||||
$deadline = (int) get_post_meta($id, '_jio_forum_vote_deadline', true);
|
||||
$expired = $deadline > 0 && current_time('timestamp') > $deadline;
|
||||
$vote_display = (string) jio_get_setting('jio_forum_vote_number_type', 'percentage');
|
||||
$already_voted = $user_id > 0 && get_user_meta($user_id, '_jio_forum_vote_' . $id, true) !== '';
|
||||
?>
|
||||
<form class="jio-forum__vote" data-forum-form="vote" data-id="<?php echo esc_attr((string) $id); ?>"><h2><?php echo esc_html((string) get_post_meta($id, '_jio_forum_vote_title', true) ?: '参与投票'); ?></h2>
|
||||
<?php foreach ($options as $index => $option) : ?>
|
||||
<label><input type="<?php echo $vote_type === 'multiple' ? 'checkbox' : 'radio'; ?>" name="<?php echo $vote_type === 'multiple' ? 'option[]' : 'option'; ?>" value="<?php echo esc_attr((string) $index); ?>" <?php disabled($already_voted || $expired); ?> <?php if ($vote_type !== 'multiple') echo 'required'; ?>><span><?php echo esc_html($option); ?></span>
|
||||
<?php if ($vote_display !== 'hide') : ?><span class="jio-forum__vote-count"><?php echo esc_html($vote_display === 'number' ? (string) (int) ($votes[$index] ?? 0) : (string) ((int) get_post_meta($id, '_jio_forum_vote_total', true) > 0 ? round(100 * (int) ($votes[$index] ?? 0) / (int) get_post_meta($id, '_jio_forum_vote_total', true)) : 0) . '%'); ?></span><?php endif; ?></label>
|
||||
<?php endforeach; ?>
|
||||
<p class="jio-forum__message" role="status"></p><?php if ($already_voted) : ?><p>已投票</p><?php elseif ($expired) : ?><p>投票已截止</p><?php else : ?><button type="submit" class="jio-forum__primary">提交投票</button><?php endif; ?>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
<?php else : ?>
|
||||
<section class="jio-forum__locked"><h2>内容已隐藏</h2><p>请完成访问条件后查看。</p>
|
||||
<?php if ($user_id > 0) : ?>
|
||||
<?php
|
||||
$plate_rule = (string) get_post_meta($plate_id, '_jio_forum_view_limit', true);
|
||||
$post_rule = (string) get_post_meta($id, '_jio_forum_view_limit', true);
|
||||
if (in_array($plate_rule, ['points', 'pay'], true) && !\Jio\Forum\Commerce::has_plate_entitlement($user_id, $plate_id)) {
|
||||
View::action('plate-unlock', $plate_id, '解锁版块');
|
||||
} elseif (($plate_rule === 'follow' || $post_rule === 'follow') && !Access::follows($user_id, $plate_id)) {
|
||||
echo '<a href="' . esc_url(get_permalink($plate_id)) . '">关注版块</a>';
|
||||
} elseif (in_array($post_rule, ['points', 'pay'], true)) {
|
||||
View::action('unlock', $id, '解锁帖子');
|
||||
}
|
||||
?>
|
||||
<?php else : ?><a href="<?php echo esc_url(jio_get_login_url(get_permalink($id))); ?>">登录查看</a><?php endif; ?>
|
||||
</section>
|
||||
<?php endif; ?>
|
||||
<div class="jio-forum__actions">
|
||||
<?php View::action('score', $id, '赞 ' . (int) get_post_meta($id, '_jio_forum_score', true)); ?>
|
||||
<button type="button" class="jio-forum__action" data-forum-action="score" data-direction="down" data-id="<?php echo esc_attr((string) $id); ?>">踩</button>
|
||||
<?php View::action('favorite', $id, '收藏'); ?>
|
||||
<?php View::action('report', $id, '举报'); ?>
|
||||
<?php if ($can_manage) : ?><a href="<?php echo esc_url(Core::edit_url($id)); ?>">编辑</a><?php View::action('delete', $id, '删除'); endif; ?>
|
||||
<?php if ($can_moderate) : View::action('essence', $id, '精华'); View::action('pin', $id, '置顶'); View::action('audit', $id, '审核'); View::action('comments', $id, $post->comment_status === 'open' ? '关闭回复' : '开放回复'); View::action('move', $id, '移动版块'); endif; ?>
|
||||
</div>
|
||||
</article>
|
||||
<?php if ($can_view) : ?>
|
||||
<?php if ($type === 'question' && get_post_meta($id, '_jio_forum_adopted_comment', true)) : ?><p class="jio-forum__adopted">已采纳回复 #<?php echo (int) get_post_meta($id, '_jio_forum_adopted_comment', true); ?></p><?php endif; ?>
|
||||
<?php View::replies($id); ?>
|
||||
<?php endif; ?>
|
||||
</div><?php View::sidebar('post'); ?></div>
|
||||
<?php \Jio\WidgetSystem\WidgetAreas::render('forum_post_bottom_fluid'); View::close(); ?>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/**
|
||||
* Template Name: Jio - 论坛首页
|
||||
* Template Post Type: page
|
||||
*/
|
||||
defined('ABSPATH') || exit;
|
||||
if (!\Jio\Forum\Core::enabled()) {
|
||||
status_header(404);
|
||||
include get_404_template();
|
||||
return;
|
||||
}
|
||||
\Jio\Forum\View::home();
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
defined('ABSPATH') || exit;
|
||||
\Jio\Forum\View::user_panel(get_current_user_id(), true);
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
defined('ABSPATH') || exit;
|
||||
$user_id = (int) ($args['user_id'] ?? 0);
|
||||
if ($user_id > 0) {
|
||||
\Jio\Forum\View::user_panel($user_id, $user_id === get_current_user_id());
|
||||
}
|
||||
Reference in new issue
Block a user