Add native aksrt_forum_topic list filters/columns/bulk/quick-edit for sticky/lock/review/plate. Replace SPA charge-card batch link with an inline batch generator on the 支付中心卡密 page. No build artifacts.
410 lines
17 KiB
PHP
410 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* Native list-table governance for aksrt_forum_topic.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Topic_Admin_List {
|
|
|
|
private static $instance = null;
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function init(): void {
|
|
if (!is_admin()) {
|
|
return;
|
|
}
|
|
|
|
add_filter('manage_edit-aksrt_forum_topic_columns', [$this, 'columns']);
|
|
add_action('manage_aksrt_forum_topic_posts_custom_column', [$this, 'render_column'], 10, 2);
|
|
add_filter('manage_edit-aksrt_forum_topic_sortable_columns', [$this, 'sortable_columns']);
|
|
add_action('restrict_manage_posts', [$this, 'filters']);
|
|
add_action('pre_get_posts', [$this, 'apply_filters']);
|
|
add_filter('bulk_actions-edit-aksrt_forum_topic', [$this, 'bulk_actions']);
|
|
add_filter('handle_bulk_actions-edit-aksrt_forum_topic', [$this, 'handle_bulk_actions'], 10, 3);
|
|
add_action('quick_edit_custom_box', [$this, 'quick_edit_box'], 10, 2);
|
|
add_action('save_post_aksrt_forum_topic', [$this, 'save_quick_edit'], 20, 2);
|
|
add_action('admin_notices', [$this, 'bulk_notices']);
|
|
add_action('admin_footer-edit.php', [$this, 'quick_edit_script']);
|
|
}
|
|
|
|
public function columns(array $columns): array {
|
|
$new = [];
|
|
foreach ($columns as $key => $label) {
|
|
$new[$key] = $label;
|
|
if ($key === 'title') {
|
|
$new['aksrt_plate'] = __('版块', 'aksrt');
|
|
$new['aksrt_sticky'] = __('置顶', 'aksrt');
|
|
$new['aksrt_locked'] = __('锁定', 'aksrt');
|
|
$new['aksrt_review'] = __('审核', 'aksrt');
|
|
$new['aksrt_bbs_type']= __('类型', 'aksrt');
|
|
$new['aksrt_views'] = __('阅读', 'aksrt');
|
|
}
|
|
}
|
|
return $new;
|
|
}
|
|
|
|
public function sortable_columns(array $columns): array {
|
|
$columns['aksrt_sticky'] = 'aksrt_sticky';
|
|
$columns['aksrt_views'] = 'aksrt_views';
|
|
return $columns;
|
|
}
|
|
|
|
public function render_column(string $column, int $post_id): void {
|
|
switch ($column) {
|
|
case 'aksrt_plate':
|
|
$plate_id = (int) get_post_meta($post_id, '_aksrt_plate_id', true);
|
|
if ($plate_id > 0) {
|
|
$plate = get_post($plate_id);
|
|
echo $plate ? esc_html(get_the_title($plate)) : esc_html('#' . $plate_id);
|
|
} else {
|
|
// fallback board taxonomy
|
|
$terms = get_the_terms($post_id, 'aksrt_forum_board');
|
|
if (is_array($terms) && $terms) {
|
|
echo esc_html($terms[0]->name);
|
|
} else {
|
|
echo '—';
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'aksrt_sticky':
|
|
$sticky = (int) get_post_meta($post_id, '_aksrt_sticky', true);
|
|
$map = [
|
|
0 => __('否', 'aksrt'),
|
|
1 => __('版块', 'aksrt'),
|
|
2 => __('超级', 'aksrt'),
|
|
3 => __('全局', 'aksrt'),
|
|
];
|
|
echo esc_html($map[$sticky] ?? (string) $sticky);
|
|
echo '<div class="hidden" id="aksrt-inline-' . esc_attr((string) $post_id) . '"';
|
|
echo ' data-sticky="' . esc_attr((string) $sticky) . '"';
|
|
echo ' data-locked="' . esc_attr((string) (int) get_post_meta($post_id, '_aksrt_locked', true)) . '"';
|
|
echo ' data-review="' . esc_attr((string) get_post_meta($post_id, '_aksrt_review_status', true)) . '"';
|
|
echo ' data-plate="' . esc_attr((string) (int) get_post_meta($post_id, '_aksrt_plate_id', true)) . '"';
|
|
echo '></div>';
|
|
break;
|
|
|
|
case 'aksrt_locked':
|
|
$locked = (int) get_post_meta($post_id, '_aksrt_locked', true);
|
|
echo $locked ? esc_html__('是', 'aksrt') : esc_html__('否', 'aksrt');
|
|
break;
|
|
|
|
case 'aksrt_review':
|
|
$status = (string) get_post_meta($post_id, '_aksrt_review_status', true);
|
|
if ($status === '') {
|
|
$status = 'approved';
|
|
}
|
|
$map = [
|
|
'approved' => __('已通过', 'aksrt'),
|
|
'pending' => __('待审核', 'aksrt'),
|
|
'rejected' => __('已拒绝', 'aksrt'),
|
|
];
|
|
echo esc_html($map[$status] ?? $status);
|
|
break;
|
|
|
|
case 'aksrt_bbs_type':
|
|
$type = (string) (get_post_meta($post_id, '_aksrt_bbs_type', true) ?: 'standard');
|
|
echo esc_html($type === 'question' ? __('提问', 'aksrt') : __('标准', 'aksrt'));
|
|
break;
|
|
|
|
case 'aksrt_views':
|
|
echo esc_html((string) (int) get_post_meta($post_id, '_aksrt_views', true));
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function filters(string $post_type): void {
|
|
if ($post_type !== 'aksrt_forum_topic') {
|
|
return;
|
|
}
|
|
|
|
$sticky = isset($_GET['aksrt_sticky']) ? sanitize_key((string) wp_unslash($_GET['aksrt_sticky'])) : '';
|
|
$locked = isset($_GET['aksrt_locked']) ? sanitize_key((string) wp_unslash($_GET['aksrt_locked'])) : '';
|
|
$review = isset($_GET['aksrt_review']) ? sanitize_key((string) wp_unslash($_GET['aksrt_review'])) : '';
|
|
$plate = isset($_GET['aksrt_plate']) ? (int) $_GET['aksrt_plate'] : 0;
|
|
|
|
echo '<select name="aksrt_sticky">';
|
|
echo '<option value="">' . esc_html__('置顶筛选', 'aksrt') . '</option>';
|
|
foreach ([
|
|
'0' => __('未置顶', 'aksrt'),
|
|
'1' => __('版块置顶', 'aksrt'),
|
|
'2' => __('超级置顶', 'aksrt'),
|
|
'3' => __('全局置顶', 'aksrt'),
|
|
] as $k => $label) {
|
|
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected($sticky, $k, false), esc_html($label));
|
|
}
|
|
echo '</select> ';
|
|
|
|
echo '<select name="aksrt_locked">';
|
|
echo '<option value="">' . esc_html__('锁定筛选', 'aksrt') . '</option>';
|
|
printf('<option value="1"%s>%s</option>', selected($locked, '1', false), esc_html__('已锁定', 'aksrt'));
|
|
printf('<option value="0"%s>%s</option>', selected($locked, '0', false), esc_html__('未锁定', 'aksrt'));
|
|
echo '</select> ';
|
|
|
|
echo '<select name="aksrt_review">';
|
|
echo '<option value="">' . esc_html__('审核筛选', 'aksrt') . '</option>';
|
|
foreach ([
|
|
'pending' => __('待审核', 'aksrt'),
|
|
'approved' => __('已通过', 'aksrt'),
|
|
'rejected' => __('已拒绝', 'aksrt'),
|
|
] as $k => $label) {
|
|
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected($review, $k, false), esc_html($label));
|
|
}
|
|
echo '</select> ';
|
|
|
|
$plates = get_posts([
|
|
'post_type' => 'aksrt_forum_plate',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => 200,
|
|
'orderby' => 'title',
|
|
'order' => 'ASC',
|
|
]);
|
|
if ($plates) {
|
|
echo '<select name="aksrt_plate">';
|
|
echo '<option value="0">' . esc_html__('全部版块', 'aksrt') . '</option>';
|
|
foreach ($plates as $p) {
|
|
printf(
|
|
'<option value="%d"%s>%s</option>',
|
|
(int) $p->ID,
|
|
selected($plate, (int) $p->ID, false),
|
|
esc_html(get_the_title($p))
|
|
);
|
|
}
|
|
echo '</select> ';
|
|
}
|
|
}
|
|
|
|
public function apply_filters(WP_Query $query): void {
|
|
if (!is_admin() || !$query->is_main_query()) {
|
|
return;
|
|
}
|
|
$post_type = $query->get('post_type');
|
|
if ($post_type !== 'aksrt_forum_topic') {
|
|
return;
|
|
}
|
|
|
|
$meta_query = (array) $query->get('meta_query');
|
|
|
|
if (isset($_GET['aksrt_sticky']) && $_GET['aksrt_sticky'] !== '') {
|
|
$meta_query[] = [
|
|
'key' => '_aksrt_sticky',
|
|
'value' => (int) $_GET['aksrt_sticky'],
|
|
];
|
|
}
|
|
if (isset($_GET['aksrt_locked']) && $_GET['aksrt_locked'] !== '') {
|
|
$meta_query[] = [
|
|
'key' => '_aksrt_locked',
|
|
'value' => (int) $_GET['aksrt_locked'],
|
|
];
|
|
}
|
|
if (!empty($_GET['aksrt_review'])) {
|
|
$review = sanitize_key((string) wp_unslash($_GET['aksrt_review']));
|
|
if ($review === 'approved') {
|
|
$meta_query[] = [
|
|
'relation' => 'OR',
|
|
[
|
|
'key' => '_aksrt_review_status',
|
|
'value' => 'approved',
|
|
],
|
|
[
|
|
'key' => '_aksrt_review_status',
|
|
'compare' => 'NOT EXISTS',
|
|
],
|
|
[
|
|
'key' => '_aksrt_review_status',
|
|
'value' => '',
|
|
],
|
|
];
|
|
} else {
|
|
$meta_query[] = [
|
|
'key' => '_aksrt_review_status',
|
|
'value' => $review,
|
|
];
|
|
}
|
|
}
|
|
if (!empty($_GET['aksrt_plate'])) {
|
|
$meta_query[] = [
|
|
'key' => '_aksrt_plate_id',
|
|
'value' => (int) $_GET['aksrt_plate'],
|
|
];
|
|
}
|
|
|
|
if ($meta_query) {
|
|
$query->set('meta_query', $meta_query);
|
|
}
|
|
|
|
$orderby = $query->get('orderby');
|
|
if ($orderby === 'aksrt_sticky') {
|
|
$query->set('meta_key', '_aksrt_sticky');
|
|
$query->set('orderby', 'meta_value_num');
|
|
} elseif ($orderby === 'aksrt_views') {
|
|
$query->set('meta_key', '_aksrt_views');
|
|
$query->set('orderby', 'meta_value_num');
|
|
}
|
|
}
|
|
|
|
public function bulk_actions(array $actions): array {
|
|
$actions['aksrt_sticky_1'] = __('设为版块置顶', 'aksrt');
|
|
$actions['aksrt_sticky_0'] = __('取消置顶', 'aksrt');
|
|
$actions['aksrt_lock'] = __('锁定', 'aksrt');
|
|
$actions['aksrt_unlock'] = __('解锁', 'aksrt');
|
|
$actions['aksrt_approve'] = __('审核通过', 'aksrt');
|
|
$actions['aksrt_reject'] = __('审核拒绝', 'aksrt');
|
|
return $actions;
|
|
}
|
|
|
|
public function handle_bulk_actions(string $redirect_to, string $action, array $post_ids): string {
|
|
$count = 0;
|
|
foreach ($post_ids as $post_id) {
|
|
$post_id = (int) $post_id;
|
|
if ($post_id <= 0 || get_post_type($post_id) !== 'aksrt_forum_topic') {
|
|
continue;
|
|
}
|
|
switch ($action) {
|
|
case 'aksrt_sticky_1':
|
|
update_post_meta($post_id, '_aksrt_sticky', 1);
|
|
$count++;
|
|
break;
|
|
case 'aksrt_sticky_0':
|
|
update_post_meta($post_id, '_aksrt_sticky', 0);
|
|
$count++;
|
|
break;
|
|
case 'aksrt_lock':
|
|
update_post_meta($post_id, '_aksrt_locked', 1);
|
|
$count++;
|
|
break;
|
|
case 'aksrt_unlock':
|
|
update_post_meta($post_id, '_aksrt_locked', 0);
|
|
$count++;
|
|
break;
|
|
case 'aksrt_approve':
|
|
update_post_meta($post_id, '_aksrt_review_status', 'approved');
|
|
$count++;
|
|
break;
|
|
case 'aksrt_reject':
|
|
update_post_meta($post_id, '_aksrt_review_status', 'rejected');
|
|
$count++;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return add_query_arg('aksrt_topic_bulk', $count, $redirect_to);
|
|
}
|
|
|
|
public function bulk_notices(): void {
|
|
if (empty($_GET['aksrt_topic_bulk'])) {
|
|
return;
|
|
}
|
|
$count = (int) $_GET['aksrt_topic_bulk'];
|
|
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html(sprintf(
|
|
/* translators: %d count */
|
|
__('已更新 %d 个帖子。', 'aksrt'),
|
|
$count
|
|
)) . '</p></div>';
|
|
}
|
|
|
|
public function quick_edit_box(string $column_name, string $post_type): void {
|
|
if ($post_type !== 'aksrt_forum_topic' || $column_name !== 'aksrt_sticky') {
|
|
return;
|
|
}
|
|
?>
|
|
<fieldset class="inline-edit-col-right">
|
|
<div class="inline-edit-col">
|
|
<label>
|
|
<span class="title"><?php esc_html_e('置顶', 'aksrt'); ?></span>
|
|
<select name="aksrt_sticky">
|
|
<option value="0"><?php esc_html_e('否', 'aksrt'); ?></option>
|
|
<option value="1"><?php esc_html_e('版块', 'aksrt'); ?></option>
|
|
<option value="2"><?php esc_html_e('超级', 'aksrt'); ?></option>
|
|
<option value="3"><?php esc_html_e('全局', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
<label>
|
|
<span class="title"><?php esc_html_e('锁定', 'aksrt'); ?></span>
|
|
<select name="aksrt_locked">
|
|
<option value="0"><?php esc_html_e('否', 'aksrt'); ?></option>
|
|
<option value="1"><?php esc_html_e('是', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
<label>
|
|
<span class="title"><?php esc_html_e('审核', 'aksrt'); ?></span>
|
|
<select name="aksrt_review_status">
|
|
<option value="approved"><?php esc_html_e('已通过', 'aksrt'); ?></option>
|
|
<option value="pending"><?php esc_html_e('待审核', 'aksrt'); ?></option>
|
|
<option value="rejected"><?php esc_html_e('已拒绝', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
<?php wp_nonce_field('aksrt_topic_quick_edit', 'aksrt_topic_qe_nonce'); ?>
|
|
<input type="hidden" name="aksrt_topic_qe_flag" value="1">
|
|
</div>
|
|
</fieldset>
|
|
<?php
|
|
}
|
|
|
|
public function save_quick_edit(int $post_id, WP_Post $post): void {
|
|
if (!isset($_POST['aksrt_topic_qe_flag'])) {
|
|
return;
|
|
}
|
|
if (!isset($_POST['aksrt_topic_qe_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash((string) $_POST['aksrt_topic_qe_nonce'])), 'aksrt_topic_quick_edit')) {
|
|
return;
|
|
}
|
|
if (!current_user_can('edit_post', $post_id)) {
|
|
return;
|
|
}
|
|
|
|
if (isset($_POST['aksrt_sticky'])) {
|
|
update_post_meta($post_id, '_aksrt_sticky', max(0, min(3, (int) $_POST['aksrt_sticky'])));
|
|
}
|
|
if (isset($_POST['aksrt_locked'])) {
|
|
update_post_meta($post_id, '_aksrt_locked', (int) $_POST['aksrt_locked'] ? 1 : 0);
|
|
}
|
|
if (isset($_POST['aksrt_review_status'])) {
|
|
$status = sanitize_key((string) wp_unslash($_POST['aksrt_review_status']));
|
|
if (in_array($status, ['approved', 'pending', 'rejected'], true)) {
|
|
update_post_meta($post_id, '_aksrt_review_status', $status);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function quick_edit_script(): void {
|
|
$screen = function_exists('get_current_screen') ? get_current_screen() : null;
|
|
if (!$screen || $screen->post_type !== 'aksrt_forum_topic') {
|
|
return;
|
|
}
|
|
?>
|
|
<script>
|
|
(function($){
|
|
var $wp_inline_edit = inlineEditPost.edit;
|
|
inlineEditPost.edit = function(id){
|
|
$wp_inline_edit.apply(this, arguments);
|
|
var postId = 0;
|
|
if (typeof(id) === 'object') postId = parseInt(this.getId(id));
|
|
if (postId > 0) {
|
|
var $row = $('#post-' + postId);
|
|
var $data = $('#aksrt-inline-' + postId);
|
|
if ($data.length) {
|
|
var sticky = $data.data('sticky');
|
|
var locked = $data.data('locked');
|
|
var review = $data.data('review') || 'approved';
|
|
var $edit = $('#edit-' + postId);
|
|
$edit.find('select[name="aksrt_sticky"]').val(String(sticky));
|
|
$edit.find('select[name="aksrt_locked"]').val(String(locked));
|
|
$edit.find('select[name="aksrt_review_status"]').val(String(review));
|
|
}
|
|
}
|
|
};
|
|
})(jQuery);
|
|
</script>
|
|
<?php
|
|
}
|
|
}
|