fix(frontend): 修复前台页面编辑弹窗并 1:1 复刻参考主题

远端加载的弹窗在 admin-ajax 上下文里拿不到查询对象,enabled()
恒为假,直接返回 success:false;而 AksrtRequest.responseError
只读 message/data.message,读不到 msg,遂报「请求失败(HTTP 200)」。
改为参考主题原生架构:wp_footer 直出 #modal_admin_set,提交走
vendored main.js 的 aksrt_ajax 契约,不再有 AJAX 取 HTML。

对齐参考主题 zib-frontend-set.php:
- 弹窗补齐 aria-hidden=false、win-ajax-replace、role=document
- 隐藏字段改用 taxonomy + post_type
- 字段顺序与 is_page 分支(标题样式、widgets_register、容器位置)
- 多选 checkbox 改 name="id[]",描述允许富文本
- 模块布局指向定制器小工具面板
- term 保存使用 POST 的 taxonomy 字段

清理不再使用的 assets/js/page-admin-set.js。
新增 frontend-page-edit-harness.php 覆盖门禁、渲染与各保存分支。
This commit is contained in:
2026-10-06 12:22:43 +08:00
parent b75bf197a3
commit 12f60897d9
4 changed files with 630 additions and 207 deletions
+318 -130
View File
@@ -2,9 +2,9 @@
/**
* 前端页面编辑(后台「扩展&增强 → 系统工具」的 admin_frontend_set)。
*
* 对齐参考主题 zib-frontend-set.php:管理员在前台通过悬浮按钮打开弹窗,
* 快速修改当前文章/页面的标题、副标题、布局、计数、评论开关或分类术语的
* 名称与描述;保存走白名单,行为与后台 PostMetaBox / wp_update_term 一致。
* 对齐参考主题 zib-frontend-set.php:wp_footer 直出 #modal_admin_set 弹窗
* (bootstrap modal + wp-ajax-submit 走 vendored main.js 的 aksrt_ajax 契约),
* 管理员在前台快速修改当前文章/页面的参数或分类术语的名称与描述。
*
* @package AKSRT
*/
@@ -26,9 +26,8 @@ class PageAdminSet {
public function init(): void {
add_filter('aksrt_float_right_buttons', [$this, 'float_button'], 10, 2);
add_action('wp_ajax_aksrt_frontend_set_modal', [$this, 'ajax_modal']);
add_action('wp_footer', [$this, 'render_modal'], 10);
add_action('wp_ajax_aksrt_frontend_set_save', [$this, 'ajax_save']);
add_action('wp_enqueue_scripts', [$this, 'enqueue_script']);
}
/** 开启条件:设置开关 + 管理员 + 单页/术语页 + 有效对象(排除投稿与导航页模板)。 */
@@ -49,91 +48,119 @@ class PageAdminSet {
return true;
}
/** 悬浮按钮:注入右侧悬浮按钮栏(对齐参考主题 zib_admin_frontend_set_botton)。 */
public function float_button($buttons, $is_mobile) {
/**
* 悬浮按钮:追加到右侧悬浮按钮栏(对齐参考主题 zib_admin_frontend_set_botton)。
*
* 参考主题对移动端与桌面端共用同一按钮,$is_mobile 仅用于保持 filter 签名。
*/
public function float_button($buttons, $is_mobile = null) {
if (!self::enabled()) {
return $buttons;
}
$url = add_query_arg(
[
'action' => 'aksrt_frontend_set_modal',
'_wpnonce' => wp_create_nonce('aksrt_frontend_set'),
],
admin_url('admin-ajax.php')
);
$button = '<a href="javascript:;" data-toggle="modal" data-target="#modal_admin_set"'
. ' title="' . esc_attr__('设置页面参数', 'aksrt') . '" class="admin-set-page float-btn">'
. '<i class="fa fa-cog fa-spin" aria-hidden="true"></i>'
. '<div class="abs-right badg c-red px12 admin-set-info" style="width:187px;">' . esc_html__('在此编辑此页面参数', 'aksrt') . '</div></a>';
$button = '<a class="float-btn admin-set-btn" href="javascript:;"'
. ' data-aksrt-modal="remote" data-aksrt-modal-cache="false" data-aksrt-modal-kind="admin-set"'
. ' data-aksrt-modal-url="' . esc_url($url) . '"'
. aksrt_float_btn_tooltip_attr(__('在此编辑此页面参数', 'aksrt'), (bool) $is_mobile)
. '><i class="fa fas fa-cog"></i></a>';
return $button . (string) $buttons;
return (string) $buttons . $button;
}
/** 条件加载弹窗提交脚本。 */
public function enqueue_script(): void {
/** wp_footer 输出 #modal_admin_set(对齐参考主题 zib_frontend_set_modal)。 */
public function render_modal(): void {
if (!self::enabled()) {
return;
}
$src = get_template_directory() . '/assets/js/page-admin-set.js';
wp_enqueue_script(
'aksrt-page-admin-set',
get_template_directory_uri() . '/assets/js/page-admin-set.js',
[],
file_exists($src) ? (string) filemtime($src) : null,
true
);
}
/** GET 弹窗内容(注入 aksrt-modal-runtime)。 */
public function ajax_modal(): void {
check_ajax_referer('aksrt_frontend_set', '_wpnonce');
if (!self::enabled()) {
wp_send_json_error(['msg' => __('权限不足', 'aksrt')]);
}
$object_id = (int) get_queried_object_id();
if (is_category() || is_tag() || is_tax()) {
$html = $this->term_modal_html($object_id, (string) get_queried_object()->taxonomy);
$type = 'tax';
$taxonomy = (string) get_queried_object()->taxonomy;
$post_type = '';
$edit_link = get_edit_term_link($object_id, $taxonomy);
$type_label = (string) get_taxonomy($taxonomy)->labels->singular_name;
} else {
$html = $this->post_modal_html($object_id, get_post_type($object_id) ?: 'post');
$type = 'post';
$taxonomy = '';
$post_type = (string) get_post_type();
$edit_link = (string) get_edit_post_link($object_id);
$type_label = (string) get_post_type_object($post_type)->labels->singular_name;
}
wp_send_json_success(['html' => $html]);
// 对齐参考主题 zib_get_customize_widgets_url:跳转定制器小工具面板并返回当前页。
$current_url = (string) home_url(add_query_arg(null, null));
$widgets_url = add_query_arg([
'url' => rawurlencode($current_url),
'autofocus' => ['panel' => 'widgets'],
'return' => rawurlencode($current_url),
], admin_url('customize.php'));
$header = '<div class="modal-header"><strong class="modal-title">'
. '<i class="fa fa-sliders mr10" aria-hidden="true"></i>' . esc_html($type_label . __('设置', 'aksrt'))
. '</strong><button class="close" data-dismiss="modal">' . $this->close_icon() . '</button></div>';
$footer = '<div class="modal-footer">'
. '<a target="_blank" title="' . esc_attr__('使用可视化布局配置此页面', 'aksrt') . '" data-toggle="tooltip" href="' . esc_url($widgets_url) . '" class="but c-yellow-2 padding-lg"><i class="fa fa-pie-chart" aria-hidden="true"></i>' . esc_html__('模块布局', 'aksrt') . '</a>'
. ($edit_link ? '<a target="_blank" href="' . esc_url($edit_link) . '" class="but c-yellow padding-lg"><i class="fa fa-wordpress" aria-hidden="true"></i>' . esc_html__('后台编辑', 'aksrt') . '</a>' : '')
. '<button class="but jb-blue padding-lg wp-ajax-submit"><i class="fa fa-check" aria-hidden="true"></i>' . esc_html__('确认修改', 'aksrt') . '</button>'
. '</div>';
$body = '<div class="modal-body">'
. ($type === 'tax' ? $this->term_inputs($object_id, $taxonomy) : $this->post_inputs($object_id))
. '<input type="hidden" name="action" value="aksrt_frontend_set_save">'
. '<input type="hidden" name="type" value="' . esc_attr($type) . '">'
. '<input type="hidden" name="taxonomy" value="' . esc_attr($taxonomy) . '">'
. '<input type="hidden" name="post_type" value="' . esc_attr($post_type) . '">'
. '<input type="hidden" name="id" value="' . esc_attr((string) $object_id) . '">'
. wp_nonce_field('aksrt_frontend_set', '_wpnonce', true, false)
. '</div>';
echo '<div class="modal fade" id="modal_admin_set" tabindex="-1" role="dialog" aria-hidden="false" win-ajax-replace="modal_admin_set">'
. '<div class="modal-dialog" role="document">'
. '<div class="modal-content page-set-modal"><form>' . $header . $body . $footer . '</form></div>'
. '</div></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 各片段已转义
}
/** POST 保存:仅接受白名单字段。 */
/** 关闭按钮图标(本站 FA5 惯例,对齐参考主题 close 按钮位)。 */
private function close_icon(): string {
return '<i class="fa fas fa-times" aria-hidden="true"></i>';
}
/** 保存(legacy aksrt_ajax 契约:{msg, reload} / {error, ys, msg})。 */
public function ajax_save(): void {
check_ajax_referer('aksrt_frontend_set', '_wpnonce');
if (!self::enabled()) {
wp_send_json_error(['msg' => __('权限不足', 'aksrt')]);
$settings = class_exists('\AKSRT\Settings\ExtensionRuntime')
? \AKSRT\Settings\ExtensionRuntime::get_system_tools_settings()
: [];
if (empty($settings['admin_frontend_set']) || !current_user_can('manage_options')) {
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('权限不足', 'aksrt')]);
}
$object_id = isset($_POST['id']) ? absint($_POST['id']) : 0;
if (!$object_id) {
wp_send_json_error(['msg' => __('页面数据出错', 'aksrt')]);
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('页面数据出错', 'aksrt')]);
}
$type = isset($_POST['type']) ? sanitize_key((string) $_POST['type']) : '';
if ($type === 'tax') {
$taxonomy = isset($_POST['subtype']) ? sanitize_key((string) $_POST['subtype']) : '';
$this->save_term($object_id, $taxonomy);
} else {
$this->save_term($object_id, isset($_POST['taxonomy']) ? sanitize_key((string) wp_unslash($_POST['taxonomy'])) : '');
} elseif ($type === 'post') {
$this->save_post($object_id);
} else {
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('页面数据出错', 'aksrt')]);
}
wp_send_json_success(['msg' => __('保存成功,正在刷新页面', 'aksrt'), 'reload' => true]);
wp_send_json(['msg' => __('保存成功,正在刷新页面', 'aksrt'), 'reload' => true]);
}
private function save_post(int $post_id): void {
$post = get_post($post_id);
if (!$post) {
wp_send_json_error(['msg' => __('未找到可用数据', 'aksrt')]);
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('未找到可用post数据', 'aksrt')]);
}
if (!current_user_can('edit_post', $post_id)) {
wp_send_json_error(['msg' => __('权限不足', 'aksrt')]);
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('权限不足', 'aksrt')]);
}
$postarr = ['ID' => $post_id];
@@ -144,12 +171,12 @@ class PageAdminSet {
}
}
if (isset($_POST['comments_open'])) {
$postarr['comment_status'] = !empty($_POST['comments_open']) ? 'open' : 'closed';
$postarr['comment_status'] = $_POST['comments_open'] === 'open' ? 'open' : 'closed';
}
if (count($postarr) > 1) {
$result = wp_update_post($postarr, true);
if (is_wp_error($result)) {
wp_send_json_error(['msg' => $result->get_error_message()]);
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => $result->get_error_message()]);
}
}
@@ -159,21 +186,21 @@ class PageAdminSet {
}
if ($post->post_type === 'post' && class_exists('\AKSRT\Core\PostMetaBox')) {
$layout = isset($_POST['show_layout']) ? sanitize_key((string) wp_unslash($_POST['show_layout'])) : '';
if ($layout !== '' && !in_array($layout, ['false', 'no_sidebar', 'sidebar_left', 'sidebar_right'], true)) {
$layout = 'false';
}
// 保留白名单之外的既有配置键(封面、视频等),仅覆盖弹窗提交的字段。
// 复选框未勾选时浏览器不会提交键名,必须无条件写入两个开关。
// 复选框未勾选时浏览器不会提交键名,两个开关必须无条件写入。
$meta = get_post_meta($post_id, \AKSRT\Core\PostMetaBox::META_KEY, true);
$meta = is_array($meta) ? $meta : [];
if ($layout !== '') {
$meta['show_layout'] = $layout;
$layout = isset($_POST['show_layout']) ? sanitize_key((string) wp_unslash($_POST['show_layout'])) : 'false';
if (!in_array($layout, ['false', 'no_sidebar', 'sidebar_left', 'sidebar_right'], true)) {
$layout = 'false';
}
$meta['show_layout'] = $layout;
if (isset($_POST['subtitle'])) {
$meta['subtitle'] = sanitize_text_field(wp_unslash((string) $_POST['subtitle']));
}
$meta['no_article-navs'] = !empty($_POST['no_article-navs']);
$meta['article_maxheight_xz'] = !empty($_POST['article_maxheight_xz']);
update_post_meta($post_id, \AKSRT\Core\PostMetaBox::META_KEY, $meta);
@@ -185,15 +212,36 @@ class PageAdminSet {
update_post_meta($post_id, '_aksrt_like_count', max(0, (int) $_POST['like']));
}
}
if ($post->post_type === 'page') {
if (isset($_POST['page_header_style'])) {
$style = sanitize_key((string) wp_unslash($_POST['page_header_style']));
update_post_meta($post_id, 'page_header_style', in_array($style, ['', 'not', '1', '2', '3'], true) ? $style : '');
}
$register = isset($_POST['widgets_register']) && $_POST['widgets_register'] === '1';
update_post_meta($post_id, 'widgets_register', $register ? '1' : '');
// 关闭小工具容器时清空容器位置;容器位置与参考主题的注册语义保持一致。
$containers = [];
if ($register && !empty($_POST['widgets_register_container']) && is_array($_POST['widgets_register_container'])) {
$allowed = ['sidebar', 'top_fluid', 'top_content', 'bottom_content', 'bottom_fluid'];
$containers = array_values(array_unique(array_intersect(
array_map('sanitize_key', wp_unslash($_POST['widgets_register_container'])),
$allowed
)));
}
update_post_meta($post_id, 'widgets_register_container', $containers);
}
}
private function save_term(int $term_id, string $taxonomy = ''): void {
$term = $taxonomy !== '' ? get_term($term_id, $taxonomy) : get_term($term_id);
if (!$term || is_wp_error($term)) {
wp_send_json_error(['msg' => __('未找到可用数据', 'aksrt')]);
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('未找到可用term数据', 'aksrt')]);
}
if (!current_user_can('manage_categories') && !current_user_can('manage_terms')) {
wp_send_json_error(['msg' => __('权限不足', 'aksrt')]);
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('权限不足', 'aksrt')]);
}
$data = [];
@@ -212,94 +260,234 @@ class PageAdminSet {
$result = wp_update_term($term_id, (string) $term->taxonomy, $data);
if (is_wp_error($result)) {
wp_send_json_error(['msg' => $result->get_error_message()]);
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => $result->get_error_message()]);
}
}
private function post_modal_html(int $post_id, string $post_type): string {
/** 文章/页面的弹窗字段(对齐参考主题 zib_get_frontend_set_input)。 */
private function post_inputs(int $post_id): string {
$meta = class_exists('\AKSRT\Core\PostMetaBox')
? \AKSRT\Core\PostMetaBox::get_meta($post_id)
: [];
$views = (int) ($meta['views'] ?? 0);
$like = (int) ($meta['like'] ?? 0);
$is_single = is_single();
$inputs = [];
$html = '<div class="aksrt-admin-set">';
$html .= '<p class="em09 muted-3-color mb10">' . esc_html(get_the_title($post_id)) . '</p>';
$inputs[] = [
'name' => __('显示布局', 'aksrt'),
'id' => 'show_layout',
'std' => (string) ($meta['show_layout'] ?? 'false'),
'type' => 'radio',
'options' => [
'false' => __('跟随主题', 'aksrt'),
'no_sidebar' => __('无侧边栏', 'aksrt'),
'sidebar_left' => __('侧边栏靠左', 'aksrt'),
'sidebar_right' => __('侧边栏靠右', 'aksrt'),
],
];
$html .= $this->text_field('post_title', __('标题', 'aksrt'), (string) get_the_title($post_id));
$inputs[] = [
'name' => __('标题', 'aksrt'),
'id' => 'post_title',
'std' => (string) get_the_title($post_id),
'type' => 'text',
];
if ($is_single) {
$html .= $this->text_field('subtitle', __('副标题', 'aksrt'), (string) ($meta['subtitle'] ?? ''));
$layout = (string) ($meta['show_layout'] ?? 'false');
$html .= '<div class="relative line-form mb10 modal-line-field"><select name="show_layout" class="line-form-input">'
. '<option value="false"' . selected($layout, 'false', false) . '>' . esc_html__('跟随主题', 'aksrt') . '</option>'
. '<option value="no_sidebar"' . selected($layout, 'no_sidebar', false) . '>' . esc_html__('无侧边栏', 'aksrt') . '</option>'
. '<option value="sidebar_left"' . selected($layout, 'sidebar_left', false) . '>' . esc_html__('侧边栏靠左', 'aksrt') . '</option>'
. '<option value="sidebar_right"' . selected($layout, 'sidebar_right', false) . '>' . esc_html__('侧边栏靠右', 'aksrt') . '</option>'
. '</select><i class="line-form-line"></i></div>';
$html .= $this->number_field('like', __('点赞数', 'aksrt'), $like);
$html .= $this->number_field('views', __('阅读数', 'aksrt'), $views);
$html .= $this->checkbox_field('no_article-navs', __('不显示目录树', 'aksrt'), !empty($meta['no_article-navs']));
$html .= $this->checkbox_field('article_maxheight_xz', __('限制文章最大高度', 'aksrt'), !empty($meta['article_maxheight_xz']));
if (is_single()) {
$inputs[] = [
'name' => __('副标题', 'aksrt'),
'id' => 'subtitle',
'std' => (string) ($meta['subtitle'] ?? ''),
'type' => 'text',
];
$inputs[] = [
'name' => __('文章格式', 'aksrt'),
'id' => 'post_format',
'std' => (string) get_post_format($post_id),
'type' => 'select',
'options' => $this->post_format_options(),
];
$inputs[] = [
'name' => __('点赞数', 'aksrt'),
'id' => 'like',
'std' => (int) ($meta['like'] ?? 0),
'type' => 'number',
];
$inputs[] = [
'name' => __('阅读数', 'aksrt'),
'id' => 'views',
'std' => (int) ($meta['views'] ?? 0),
'type' => 'number',
];
$inputs[] = [
'name' => __('目录树', 'aksrt'),
'id' => 'no_article-navs',
'std' => !empty($meta['no_article-navs']),
'type' => 'checkbox',
'desc' => __('不显示', 'aksrt'),
];
$inputs[] = [
'name' => __('文章高度', 'aksrt'),
'id' => 'article_maxheight_xz',
'std' => !empty($meta['article_maxheight_xz']),
'type' => 'checkbox',
'desc' => __('限制文章最大高度', 'aksrt'),
];
}
$html .= '<div class="relative line-form mb10 modal-line-field"><select name="comments_open" class="line-form-input">'
. '<option value="open"' . selected(comments_open($post_id), true, false) . '>' . esc_html__('开启评论', 'aksrt') . '</option>'
. '<option value=""' . selected(comments_open($post_id), false, false) . '>' . esc_html__('关闭评论', 'aksrt') . '</option>'
. '</select><i class="line-form-line"></i></div>';
$inputs[] = [
'name' => __('评论', 'aksrt'),
'id' => 'comments_open',
'std' => comments_open($post_id) ? 'open' : '',
'type' => 'radio',
'options' => [
'open' => __('开启', 'aksrt'),
'' => __('关闭', 'aksrt'),
],
];
$html .= $this->hidden_fields('post', $post_id, $post_type);
$html .= '<div class="flex jsb mt10"><button type="button" class="but" data-aksrt-modal-close>' . esc_html__('取消', 'aksrt') . '</button>'
. '<button type="submit" class="but jb-blue aksrt-admin-set-submit"><i class="fa fas fa-check"></i> ' . esc_html__('确认修改', 'aksrt') . '</button></div>';
$html .= '</div>';
if (is_page()) {
$inputs[] = [
'name' => __('标题样式', 'aksrt'),
'id' => 'page_header_style',
'std' => (string) get_post_meta($post_id, 'page_header_style', true),
'type' => 'radio',
'options' => [
'' => __('跟随主题', 'aksrt'),
'not' => __('不显示', 'aksrt'),
'1' => __('简单样式', 'aksrt'),
'2' => __('卡片样式', 'aksrt'),
'3' => __('封面图样式', 'aksrt'),
],
];
$inputs[] = [
'name' => __('模块布局', 'aksrt'),
'id' => 'widgets_register',
'type' => 'radio',
'options' => [
'1' => __('开启', 'aksrt'),
'0' => __('关闭', 'aksrt'),
],
'std' => (string) get_post_meta($post_id, 'widgets_register', true),
];
$inputs[] = [
'id' => 'widgets_register_container',
'type' => 'checkbox',
'class' => 'compact',
'std' => (array) get_post_meta($post_id, 'widgets_register_container', true),
'name' => __('创建容器位置', 'aksrt'),
'desc' => __('请根据需要合理开启,如果用不到则不要开启', 'aksrt') . '<br>' . __('保存页面后即可进入小工具或模块配置添加模块', 'aksrt') . '<div class="c-yellow">' . __('注意:开启此功能的页面不能太多,太多会影响性能,建议控制在10个以内', 'aksrt') . '</div>',
'options' => [
'sidebar' => __('侧边栏', 'aksrt'),
'top_fluid' => __('顶部全宽度', 'aksrt'),
'top_content' => __('主内容上面', 'aksrt'),
'bottom_content' => __('主内容下面', 'aksrt'),
'bottom_fluid' => __('底部全宽度', 'aksrt'),
],
];
}
return $this->wrap_form($html);
return $this->render_inputs($inputs);
}
private function term_modal_html(int $term_id, string $taxonomy): string {
/** AKSRT 注册的文章格式(standard + add_theme_support 注册项)。 */
private function post_format_options(): array {
$options = ['standard' => __('标准', 'aksrt')];
foreach (get_theme_support('post-formats') ? (array) get_theme_support('post-formats')[0] : [] as $format) {
$options[$format] = esc_html(get_post_format_string($format));
}
return $options;
}
/** 分类术语的弹窗字段(对齐参考主题 tax 分支)。 */
private function term_inputs(int $term_id, string $taxonomy): string {
$term = get_term($term_id, $taxonomy);
if (!$term || is_wp_error($term)) {
return '<div class="aksrt-admin-set"><p class="muted-3-color">' . esc_html__('未找到可用数据', 'aksrt') . '</p></div>';
return '<p class="muted-3-color">' . esc_html__('未找到可用term数据', 'aksrt') . '</p>';
}
$html = '<div class="aksrt-admin-set">';
$html .= '<p class="em09 muted-3-color mb10">' . esc_html($term->name) . '</p>';
$html .= $this->text_field('term_name', __('名称', 'aksrt'), (string) $term->name);
$html .= $this->text_field('term_description', __('描述', 'aksrt'), (string) $term->description);
$html .= $this->hidden_fields('tax', $term_id, $taxonomy);
$html .= '<div class="flex jsb mt10"><button type="button" class="but" data-aksrt-modal-close>' . esc_html__('取消', 'aksrt') . '</button>'
. '<button type="submit" class="but jb-blue aksrt-admin-set-submit"><i class="fa fas fa-check"></i> ' . esc_html__('确认修改', 'aksrt') . '</button></div>';
$html .= '</div>';
return $this->wrap_form($html);
return $this->render_inputs([
[
'name' => __('名称', 'aksrt'),
'id' => 'term_name',
'std' => (string) $term->name,
'type' => 'text',
],
[
'name' => __('描述', 'aksrt'),
'id' => 'term_description',
'std' => (string) $term->description,
'type' => 'text',
],
]);
}
private function wrap_form(string $inner): string {
return '<form class="aksrt-admin-set-form">'
. '<input type="hidden" name="action" value="aksrt_frontend_set_save">'
. wp_nonce_field('aksrt_frontend_set', '_wpnonce', true, false)
. $inner
. '</form>';
/** 表单控件构建(对齐参考主题 zib_edit_input_construct)。 */
private function render_inputs(array $inputs): string {
$output = '';
foreach ($inputs as $meta) {
$type = (string) ($meta['type'] ?? '');
$value = $meta['std'] ?? '';
$desc = (string) ($meta['desc'] ?? '');
$class = $type !== '' ? ' option-' . $type : '';
if (isset($meta['class'])) {
$class .= ' ' . $meta['class'];
}
$control = '';
if ($type === 'text') {
$control = '<input class="form-control" name="' . esc_attr((string) ($meta['id'] ?? '')) . '" type="text" value="' . esc_attr((string) $value) . '"/>';
} elseif ($type === 'number') {
$control = '<input class="form-control" name="' . esc_attr((string) ($meta['id'] ?? '')) . '" type="number" value="' . esc_attr((string) $value) . '"/>';
} elseif ($type === 'select') {
$control = '<div class="form-select"><select class="form-control" name="' . esc_attr((string) ($meta['id'] ?? '')) . '">';
foreach ((array) ($meta['options'] ?? []) as $key => $option) {
$control .= '<option' . selected((string) $value, (string) $key, false) . ' value="' . esc_attr((string) $key) . '">' . esc_html((string) $option) . '</option>';
}
$control .= '</select></div>';
} elseif ($type === 'radio') {
foreach ((array) ($meta['options'] ?? []) as $key => $option) {
$control .= '<label class="mr10"><input type="radio" name="' . esc_attr((string) ($meta['id'] ?? '')) . '" value="' . esc_attr((string) $key) . '" ' . checked((string) $value, (string) $key, false) . ' /><span class="ml6 em09 muted-color" style=" font-weight: normal; ">' . esc_html((string) $option) . '</span></label>';
}
} elseif ($type === 'checkbox') {
$options = (array) ($meta['options'] ?? []);
if ($options) {
foreach ($options as $key => $option) {
$checked = is_array($value) ? (in_array($key, $value, true) ? ' checked' : '') : ((string) $value === (string) $key ? ' checked' : '');
$control .= '<span class="form-checkbox"><input name="' . esc_attr((string) ($meta['id'] ?? '')) . '[]" id="' . esc_attr((string) ($meta['id'] ?? '') . $key) . '" type="checkbox"' . $checked . ' value="' . esc_attr((string) $key) . '"/><label for="' . esc_attr((string) ($meta['id'] ?? '') . $key) . '" class="em09 muted-color ml6 mr10" style=" font-weight: normal; ">' . esc_html((string) $option) . '</label></span>';
}
} else {
$control = '<span class="form-checkbox"><input value="1" name="' . esc_attr((string) ($meta['id'] ?? '')) . '" id="' . esc_attr((string) ($meta['id'] ?? '')) . '" type="checkbox" ' . checked(!empty($value), true, false) . '/><label for="' . esc_attr((string) ($meta['id'] ?? '')) . '" class="em09 muted-color ml6" style=" font-weight: normal; ">' . ($desc !== '' ? $this->rich_desc($desc) : '') . '</label></span>';
}
}
$output .= '<div class="mb10 row' . esc_attr($class) . '">' . "\n";
$output .= '<div class="heading col-xs-3 text-right">' . esc_html((string) ($meta['name'] ?? '')) . '</div>' . "\n";
$output .= '<div class="option col-xs-8">' . "\n";
$output .= $control;
if ($desc !== '' && $type !== 'checkbox') {
$output .= '<div class="em09 muted-2-color">' . $this->rich_desc($desc) . '</div>' . "\n";
}
$output .= '</div>' . "\n";
$output .= '</div>' . "\n";
}
return $output;
}
private function hidden_fields(string $type, int $id, string $subtype): string {
return '<input type="hidden" name="type" value="' . esc_attr($type) . '">'
. '<input type="hidden" name="subtype" value="' . esc_attr($subtype) . '">'
. '<input type="hidden" name="id" value="' . esc_attr((string) $id) . '">';
}
private function text_field(string $name, string $label, string $value): string {
return '<div class="relative line-form mb10 modal-line-field"><input type="text" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '" class="line-form-input">'
. '<div class="scale-placeholder">' . esc_html($label) . '</div><i class="line-form-line"></i></div>';
}
private function number_field(string $name, string $label, int $value): string {
return '<div class="relative line-form mb10 modal-line-field"><input type="number" min="0" step="1" name="' . esc_attr($name) . '" value="' . esc_attr((string) $value) . '" class="line-form-input">'
. '<div class="scale-placeholder">' . esc_html($label) . '</div><i class="line-form-line"></i></div>';
}
private function checkbox_field(string $name, string $label, bool $checked): string {
return '<div class="mb10 form-switch-label"><label><input type="checkbox" name="' . esc_attr($name) . '" value="1"' . checked($checked, true, false) . '> ' . esc_html($label) . '</label></div>';
/**
* 描述文案:允许 <br>、强调标签与来源可信的主题文案(对齐参考主题的富文本描述)。
*
* @param string $desc 内部生成的描述文案。
*/
private function rich_desc(string $desc): string {
return wp_kses($desc, [
'br' => [],
'div' => ['class' => []],
'span' => ['class' => []],
'em' => [],
'strong' => [],
'b' => [],
'i' => [],
]);
}
}