Add WP-native ops UI without SPA: plate moderator list/application review meta box, product SKU variants editor meta box, and shop_discount taxonomy add/edit form fields. No admin build artifacts included.
141 lines
6.0 KiB
PHP
141 lines
6.0 KiB
PHP
<?php
|
|
/**
|
|
* Native meta box for aksrt_forum_plate moderators & applications.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Plate_Meta_Box {
|
|
|
|
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_action('add_meta_boxes', [$this, 'register_meta_box']);
|
|
add_action('save_post_aksrt_forum_plate', [$this, 'save'], 20, 2);
|
|
}
|
|
|
|
public function register_meta_box(): void {
|
|
add_meta_box(
|
|
'aksrt_plate_moderators',
|
|
__('版主与申请', 'aksrt'),
|
|
[$this, 'render'],
|
|
'aksrt_forum_plate',
|
|
'side',
|
|
'high'
|
|
);
|
|
}
|
|
|
|
public function render(WP_Post $post): void {
|
|
wp_nonce_field('aksrt_plate_meta_save', 'aksrt_plate_meta_nonce');
|
|
|
|
$moderator_ids = [];
|
|
$applications = [];
|
|
if (class_exists('AKSRT_Forum')) {
|
|
$forum = AKSRT_Forum::get_instance();
|
|
$moderator_ids = $forum->get_plate_moderators((int) $post->ID);
|
|
$applications = $forum->get_plate_moderator_applications((int) $post->ID);
|
|
} else {
|
|
$raw = get_post_meta($post->ID, '_aksrt_moderators', true);
|
|
$moderator_ids = is_array($raw) ? array_map('intval', $raw) : [];
|
|
$applications = get_post_meta($post->ID, '_aksrt_moderator_applications', true);
|
|
$applications = is_array($applications) ? $applications : [];
|
|
}
|
|
|
|
$moderator_labels = [];
|
|
foreach ($moderator_ids as $uid) {
|
|
$user = get_userdata((int) $uid);
|
|
$moderator_labels[] = $user
|
|
? sprintf('%s (#%d)', $user->user_login, (int) $uid)
|
|
: (string) (int) $uid;
|
|
}
|
|
?>
|
|
<p>
|
|
<label for="aksrt_plate_moderators"><strong><?php esc_html_e('版主用户 ID', 'aksrt'); ?></strong></label>
|
|
<textarea id="aksrt_plate_moderators" name="aksrt_plate_moderators" rows="3" class="widefat" placeholder="1,2,3"><?php echo esc_textarea(implode(',', array_map('intval', $moderator_ids))); ?></textarea>
|
|
<span class="description"><?php esc_html_e('逗号或换行分隔的用户 ID。超级版主为版块作者。', 'aksrt'); ?></span>
|
|
</p>
|
|
<?php if ($moderator_labels) : ?>
|
|
<p class="description"><?php echo esc_html(implode(' · ', $moderator_labels)); ?></p>
|
|
<?php endif; ?>
|
|
|
|
<hr>
|
|
<p><strong><?php esc_html_e('版主申请', 'aksrt'); ?></strong></p>
|
|
<?php
|
|
$pending = array_values(array_filter($applications, static function ($app) {
|
|
return is_array($app) && (($app['status'] ?? '') === 'pending' || ($app['status'] ?? '') === AKSRT_Forum::APPLICATION_PENDING);
|
|
}));
|
|
if (!$pending) {
|
|
echo '<p class="description">' . esc_html__('暂无待处理申请。', 'aksrt') . '</p>';
|
|
return;
|
|
}
|
|
|
|
echo '<ul style="margin:0;padding-left:16px;">';
|
|
foreach ($pending as $app) {
|
|
$uid = (int) ($app['user_id'] ?? 0);
|
|
$user = $uid > 0 ? get_userdata($uid) : null;
|
|
$aid = (string) ($app['id'] ?? '');
|
|
$reason = (string) ($app['reason'] ?? '');
|
|
echo '<li style="margin-bottom:10px;">';
|
|
echo '<div><code>' . esc_html($user ? $user->display_name . ' (#' . $uid . ')' : (string) $uid) . '</code></div>';
|
|
if ($reason !== '') {
|
|
echo '<div class="description">' . esc_html($reason) . '</div>';
|
|
}
|
|
echo '<label style="display:block;margin-top:4px;"><input type="radio" name="aksrt_plate_app[' . esc_attr($aid) . ']" value="keep" checked> ' . esc_html__('保留', 'aksrt') . '</label>';
|
|
echo '<label style="display:block;"><input type="radio" name="aksrt_plate_app[' . esc_attr($aid) . ']" value="approved"> ' . esc_html__('通过', 'aksrt') . '</label>';
|
|
echo '<label style="display:block;"><input type="radio" name="aksrt_plate_app[' . esc_attr($aid) . ']" value="rejected"> ' . esc_html__('拒绝', 'aksrt') . '</label>';
|
|
echo '</li>';
|
|
}
|
|
echo '</ul>';
|
|
}
|
|
|
|
public function save(int $post_id, WP_Post $post): void {
|
|
if (!isset($_POST['aksrt_plate_meta_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash((string) $_POST['aksrt_plate_meta_nonce'])), 'aksrt_plate_meta_save')) {
|
|
return;
|
|
}
|
|
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
|
|
return;
|
|
}
|
|
if ($post->post_type !== 'aksrt_forum_plate' || !current_user_can('edit_post', $post_id)) {
|
|
return;
|
|
}
|
|
|
|
$raw = isset($_POST['aksrt_plate_moderators']) ? (string) wp_unslash($_POST['aksrt_plate_moderators']) : '';
|
|
$ids = preg_split('/[\s,;]+/', $raw) ?: [];
|
|
$ids = array_values(array_unique(array_filter(array_map('intval', $ids))));
|
|
update_post_meta($post_id, '_aksrt_moderators', $ids);
|
|
|
|
$actions = isset($_POST['aksrt_plate_app']) && is_array($_POST['aksrt_plate_app'])
|
|
? wp_unslash($_POST['aksrt_plate_app'])
|
|
: [];
|
|
if (!$actions || !class_exists('AKSRT_Forum')) {
|
|
return;
|
|
}
|
|
|
|
$forum = AKSRT_Forum::get_instance();
|
|
foreach ($actions as $application_id => $action) {
|
|
$application_id = sanitize_text_field((string) $application_id);
|
|
$action = sanitize_key((string) $action);
|
|
if ($application_id === '' || $action === 'keep') {
|
|
continue;
|
|
}
|
|
if ($action === 'approved') {
|
|
$forum->review_moderator_application($application_id, AKSRT_Forum::APPLICATION_APPROVED, '', get_current_user_id());
|
|
} elseif ($action === 'rejected') {
|
|
$forum->review_moderator_application($application_id, AKSRT_Forum::APPLICATION_REJECTED, '', get_current_user_id());
|
|
}
|
|
}
|
|
}
|
|
}
|