feat(admin): topic governance meta box and product category term fields

Add native side meta box for forum topics (plate/sticky/lock/review/pay
visibility) and taxonomy form fields for aksrt_product_cat inheritance
settings. Source-only changes; no SPA build artifacts.
This commit is contained in:
kate522 committed 2026-07-15 02:24:00 +08:00
1 parent e0279e1a23
commit 1770e8e0db
4 files changed
+309

No files matched your search

+112
View File
@@ -39,6 +39,12 @@ class AKSRT_Shop_Term_Meta {
add_action('shop_discount_edit_form_fields', [$this, 'render_discount_edit_fields'], 10, 1);
add_action('created_shop_discount', [$this, 'save_discount_term_fields']);
add_action('edited_shop_discount', [$this, 'save_discount_term_fields']);
// Product category inheritance fields.
add_action('aksrt_product_cat_add_form_fields', [$this, 'render_cat_add_fields']);
add_action('aksrt_product_cat_edit_form_fields', [$this, 'render_cat_edit_fields'], 10, 1);
add_action('created_aksrt_product_cat', [$this, 'save_cat_term_fields']);
add_action('edited_aksrt_product_cat', [$this, 'save_cat_term_fields']);
}
// ─── Taxonomy Registration ───────────────────────────────────
@@ -565,4 +571,110 @@ class AKSRT_Shop_Term_Meta {
update_term_meta($term_id, $key, $value);
}
}
public function render_cat_add_fields(): void {
foreach ($this->get_cat_field_definitions() as $key => $def) {
echo '<div class="form-field">';
echo '<label for="' . esc_attr($key) . '">' . esc_html((string) ($def['description'] ?? $key)) . '</label>';
$this->render_cat_field_control($key, $def['default'] ?? '', false);
echo '</div>';
}
}
public function render_cat_edit_fields($term): void {
if (!($term instanceof WP_Term)) {
return;
}
foreach ($this->get_cat_field_definitions() as $key => $def) {
$value = get_term_meta($term->term_id, $key, true);
if ($value === '' || $value === false) {
$value = $def['default'] ?? '';
}
echo '<tr class="form-field"><th scope="row"><label for="' . esc_attr($key) . '">' . esc_html((string) ($def['description'] ?? $key)) . '</label></th><td>';
$this->render_cat_field_control($key, $value, true);
echo '</td></tr>';
}
}
/**
* @param mixed $value
*/
private function render_cat_field_control(string $key, $value, bool $edit): void {
$val = (string) $value;
if (in_array($key, ['shop_cat_guest_buy', 'shop_cat_email_fill', 'shop_cat_content_show_bg'], true)) {
echo '<label><input type="checkbox" name="' . esc_attr($key) . '" value="1" ' . checked($val, '1', false) . '> ' . esc_html__('启用', 'aksrt') . '</label>';
return;
}
if (in_array($key, ['shop_cat_service', 'shop_cat_single_tab', 'shop_cat_content_after'], true)) {
echo '<textarea class="large-text" rows="3" name="' . esc_attr($key) . '" id="' . esc_attr($key) . '">' . esc_textarea($val) . '</textarea>';
return;
}
if ($key === 'shop_cat_shipping_fee') {
$opts = ['' => __('继承', 'aksrt'), 'free' => __('包邮', 'aksrt'), 'fixed' => __('固定运费', 'aksrt'), 'amount' => __('满额包邮', 'aksrt')];
echo '<select name="' . esc_attr($key) . '" id="' . esc_attr($key) . '">';
foreach ($opts as $k => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected($val, $k, false), esc_html($label));
}
echo '</select>';
return;
}
if ($key === 'shop_cat_after_sale') {
$opts = ['' => __('继承', 'aksrt'), 'refund' => __('仅退款', 'aksrt'), 'refund_return' => __('退货退款', 'aksrt'), 'none' => __('不支持', 'aksrt')];
echo '<select name="' . esc_attr($key) . '" id="' . esc_attr($key) . '">';
foreach ($opts as $k => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected($val, $k, false), esc_html($label));
}
echo '</select>';
return;
}
if ($key === 'shop_cat_content_layout') {
$opts = ['' => __('继承', 'aksrt'), 'adapt' => __('自适应', 'aksrt'), 'full' => __('通栏', 'aksrt'), 'sidebar' => __('侧栏', 'aksrt')];
echo '<select name="' . esc_attr($key) . '" id="' . esc_attr($key) . '">';
foreach ($opts as $k => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected($val, $k, false), esc_html($label));
}
echo '</select>';
return;
}
if ($key === 'shop_cat_list_style') {
$opts = ['' => __('继承', 'aksrt'), 'default' => __('默认', 'aksrt'), 'card-lg' => __('大卡片', 'aksrt'), 'card-sm' => __('小卡片', 'aksrt')];
echo '<select name="' . esc_attr($key) . '" id="' . esc_attr($key) . '">';
foreach ($opts as $k => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($k), selected($val, $k, false), esc_html($label));
}
echo '</select>';
return;
}
$type = $key === 'shop_cat_limit_buy' ? 'number' : 'text';
printf(
'<input type="%s" class="%s" name="%s" id="%s" value="%s" />',
esc_attr($type),
$edit ? 'regular-text' : '',
esc_attr($key),
esc_attr($key),
esc_attr($val)
);
}
public function save_cat_term_fields(int $term_id): void {
if (!current_user_can('manage_categories')) {
return;
}
foreach ($this->get_cat_field_definitions() as $key => $def) {
if (in_array($key, ['shop_cat_guest_buy', 'shop_cat_email_fill', 'shop_cat_content_show_bg'], true)) {
$raw = isset($_POST[$key]) ? '1' : '';
} else {
if (!isset($_POST[$key])) {
continue;
}
$raw = wp_unslash($_POST[$key]);
}
if (isset($def['sanitize']) && is_callable($def['sanitize'])) {
$value = call_user_func($def['sanitize'], $raw);
} else {
$value = sanitize_text_field((string) $raw);
}
update_term_meta($term_id, $key, $value);
}
}
}