esc_html__('显示当前版块的版主与超级版主。', 'aksrt'),
'classname' => 'widget_aksrt_forum_plate_moderator',
]
);
}
/**
* Build apply entry HTML (modal form).
*/
public static function render_apply_html(int $plate_id, bool $show_apply = true): string {
if (!$show_apply || $plate_id <= 0) {
return '';
}
$forum = class_exists('\AKSRT\Forum\Forum') ? \AKSRT\Forum\Forum::get_instance() : null;
$cuid = get_current_user_id();
$show_always = true;
if (function_exists('aksrt_get_setting')) {
$show_always = (bool) aksrt_get_setting('bbs_show_apply_moderator', true);
}
$can_apply = $cuid > 0 && $forum && method_exists($forum, 'forum_user_can')
? $forum->forum_user_can($cuid, 'moderator_apply')
: ($cuid > 0);
if (!$show_always && !$can_apply) {
return '';
}
$is_mod = false;
if ($cuid > 0 && $forum && method_exists($forum, 'is_board_moderator')) {
$is_mod = $forum->is_board_moderator($cuid, $plate_id);
}
if ($is_mod || ($cuid > 0 && user_can($cuid, 'manage_options'))) {
return '';
}
if ($cuid <= 0) {
$login = function_exists('aksrt_get_login_url') ? aksrt_get_login_url(get_permalink($plate_id)) : wp_login_url(get_permalink($plate_id));
return '' . esc_html__('登录后申请版主', 'aksrt') . '';
}
$pending = false;
if ($forum && method_exists($forum, 'get_board_moderator_applications')) {
foreach ((array) $forum->get_board_moderator_applications($plate_id) as $app) {
if ((int) ($app['user_id'] ?? 0) === $cuid && (string) ($app['status'] ?? '') === 'pending') {
$pending = true;
break;
}
}
}
if ($pending) {
return '' . esc_html__('申请审核中', 'aksrt') . '';
}
$uid = 'aksrt-mod-apply-' . $plate_id . '-' . wp_rand(1000, 9999);
$desc = function_exists('aksrt_get_setting') ? (string) aksrt_get_setting('bbs_apply_moderator_desc', '') : '';
if ($desc === '') {
$desc = __('欢迎有热情的用户申请成为版主,协助管理社区秩序。', 'aksrt');
}
obs_start();
echo '';
echo '
';
return (string) ob_get_clean();
}
/**
* @return array{html:string}
*/
public static function render_payload(array $instance, ?\WP_Post $plate = null): array {
$plate = $plate ?: WidgetForumPlateInfo::resolve_current_plate_static();
if (!$plate) {
return ['html' => ''];
}
$id = (int) $plate->ID;
$author_id = (int) $plate->post_author;
$mods = get_post_meta($id, '_aksrt_moderators', true);
if (!is_array($mods)) {
$mods = [];
}
$mods = array_values(array_unique(array_filter(array_map('intval', $mods))));
// Super-mods meta (zibll-like).
$super = get_post_meta($id, '_aksrt_super_moderators', true);
if (!is_array($super)) {
$super = [];
}
$super = array_values(array_unique(array_filter(array_map('intval', $super))));
$people = [];
if ($author_id > 0) {
$people[] = ['id' => $author_id, 'role' => 'author'];
}
foreach ($super as $sid) {
if ($sid === $author_id) {
continue;
}
$people[] = ['id' => $sid, 'role' => 'super'];
}
foreach ($mods as $mid) {
if ($mid === $author_id || in_array($mid, $super, true)) {
continue;
}
$people[] = ['id' => $mid, 'role' => 'moderator'];
}
$title_text = (string) ($instance['title_text'] ?? __('版主', 'aksrt'));
$show_avatar = !isset($instance['show_avatar']) || !empty($instance['show_avatar']);
$style = sanitize_key((string) ($instance['style'] ?? 'list'));
if (!in_array($style, ['list', 'grid'], true)) {
$style = 'list';
}
$show_apply = !isset($instance['apply_btn']) || !empty($instance['apply_btn']);
$apply_html = self::render_apply_html($id, $show_apply);
if (!$people && $apply_html === '') {
if (current_user_can('manage_options')) {
return [
'html' => function_exists('aksrt_get_widget_null')
? aksrt_get_widget_null(__('当前版块暂无版主', 'aksrt'))
: '' . esc_html__('当前版块暂无版主', 'aksrt') . '
',
];
}
return ['html' => ''];
}
obs_start();
echo '';
return ['html' => (string) ob_get_clean()];
}
public function widget($args, $instance): void {
$payload = self::render_payload($instance);
if ($payload['html'] === '') {
return;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
// title_text is rendered inside payload head (zibll-like); optional WP title still allowed.
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function form($instance): void {
?>
render_common_form_section($instance); ?>
update_common_fields($new_instance, $old_instance, [
'title_text' => sanitize_text_field((string) ($new_instance['title_text'] ?? '')),
'style' => ($new_instance['style'] ?? '') === 'grid' ? 'grid' : 'list',
'show_avatar' => !empty($new_instance['show_avatar']) ? 1 : 0,
'apply_btn' => !empty($new_instance['apply_btn']) ? 1 : 0,
]);
}
}