render_hide_shortcode($atts, $content); } /** * [hide]...[/hide] — 评论后可见(默认 mode=comment) */ public function render_hide_comment(array $atts = [], string $content = ''): string { $atts = shortcode_atts(['message' => ''], $atts, 'hide'); return $this->render_hide(['mode' => 'comment', 'message' => $atts['message']], $content); } /** * [reply]...[/reply] — 回复(评论)后可见 */ public function render_reply(array $atts = [], string $content = ''): string { $atts = shortcode_atts(['message' => ''], $atts, 'reply'); $msg = $atts['message'] ?: __('回复本文后可查看隐藏内容', 'aksrt'); return $this->render_hide(['mode' => 'comment', 'message' => $msg], $content); } /** * [vip]...[/vip] — VIP 用户可见 */ public function render_vip(array $atts = [], string $content = ''): string { $atts = shortcode_atts(['message' => ''], $atts, 'vip'); $uid = get_current_user_id(); $is_vip = $uid > 0 && function_exists('aksrt_is_vip') && aksrt_is_vip($uid); if ($is_vip) { return '
' . do_shortcode($content) . '
'; } $message = $atts['message'] ?: __('此内容仅 VIP 用户可见', 'aksrt'); $html = '
'; $html .= '
'; $html .= ' '; $html .= '' . esc_html($message) . ''; $html .= '
'; if ($uid <= 0) { $html .= ''; $html .= esc_html__('登录后查看', 'aksrt') . ''; } $html .= '
'; return $html; } /** * [payshow]...[/payshow] — 付费后可见 */ public function render_payshow(array $atts = [], string $content = ''): string { $atts = shortcode_atts(['message' => ''], $atts, 'payshow'); $pid = get_the_ID(); $can_view = false; if ($pid && class_exists('AKSRT_Content_Visibility')) { $can_view = AKSRT_Content_Visibility::can_view_paid_content($pid); } if ($can_view) { return '
' . do_shortcode($content) . '
'; } $message = $atts['message'] ?: __('此内容需付费后查看', 'aksrt'); $uid = get_current_user_id(); $html = '
'; $html .= '
'; $html .= ' '; $html .= '' . esc_html($message) . ''; $html .= '
'; if ($uid <= 0) { $html .= ''; $html .= esc_html__('登录后购买查看', 'aksrt') . ''; } $html .= '
'; return $html; } /** * [content type="admin|author|vip"]...[/content] — 按角色/身份控制可见性 */ public function render_content_gate(array $atts = [], string $content = ''): string { $atts = shortcode_atts(['type' => 'admin', 'message' => ''], $atts, 'content'); $type = sanitize_key((string) $atts['type']); $uid = get_current_user_id(); $pid = get_the_ID(); $allowed = false; switch ($type) { case 'admin': $allowed = $uid > 0 && user_can($uid, 'manage_options'); break; case 'author': if ($uid > 0 && $pid) { $post = get_post($pid); $allowed = $post && (int) $post->post_author === $uid; } break; case 'vip': $allowed = $uid > 0 && function_exists('aksrt_is_vip') && aksrt_is_vip($uid); break; case 'login': $allowed = $uid > 0; break; } if ($allowed) { return '
' . do_shortcode($content) . '
'; } $default_msgs = [ 'admin' => __('此内容仅管理员可见', 'aksrt'), 'author' => __('此内容仅作者可见', 'aksrt'), 'vip' => __('此内容仅 VIP 用户可见', 'aksrt'), 'login' => __('此内容仅登录用户可见', 'aksrt'), ]; $message = $atts['message'] ?: ($default_msgs[$type] ?? __('此内容已隐藏', 'aksrt')); $html = '
'; $html .= '
'; $html .= ' '; $html .= '' . esc_html($message) . ''; $html .= '
'; $html .= '
'; return $html; } /** * [aksrt_archive] 文章归档 */ public function render_archive(array $atts = [], string $content = ''): string { if (!class_exists('AKSRT_Templates')) { return ''; } return AKSRT_Templates::get_instance()->render_archive_shortcode($atts, $content); } /** * [aksrt_profile] 个人资料卡 */ public function render_profile(array $atts = [], string $content = ''): string { if (!class_exists('AKSRT_Templates')) { return ''; } return AKSRT_Templates::get_instance()->render_profile_shortcode($atts, $content); } /** * [aksrt_posts count="6" cat="" tag="" orderby="date" order="DESC" mode="card"] * * 用统一渲染器输出文章列表,避免内容里手写卡片循环。 */ public function render_posts(array $atts = [], string $content = ''): string { if (!function_exists('aksrt_render_post_list')) { return ''; } $a = shortcode_atts([ 'count' => 6, 'cat' => '', 'tag' => '', 'orderby' => 'date', 'order' => 'DESC', 'mode' => 'card', ], $atts, 'aksrt_posts'); $query_args = [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => max(1, min(24, (int) $a['count'])), 'orderby' => sanitize_key((string) $a['orderby']) ?: 'date', 'order' => strtoupper((string) $a['order']) === 'ASC' ? 'ASC' : 'DESC', 'ignore_sticky_posts' => true, 'no_found_rows' => true, ]; $cat = sanitize_text_field((string) $a['cat']); if ($cat !== '') { $query_args[is_numeric($cat) ? 'cat' : 'category_name'] = $cat; } $tag = sanitize_text_field((string) $a['tag']); if ($tag !== '') { $query_args[is_numeric($tag) ? 'tag_id' : 'tag'] = $tag; } $mode = in_array($a['mode'], ['card', 'list', 'mini'], true) ? $a['mode'] : 'card'; $query = new WP_Query($query_args); $html = aksrt_render_post_list($query, [ 'context' => 'widget', 'mode' => $mode, ], false); wp_reset_postdata(); return '
' . $html . '
'; } } endif;