feat(shop): add product taxonomy write REST for editor

Expose POST /shop/products/taxonomy to create category/tag terms,
enrich GET term payload with slug/parent/count, and let
ProductEditorDrawer create categories and ensure tags without WP
native UI.
This commit is contained in:
2026-07-11 18:22:10 +08:00
parent e0b60fb3ea
commit 3d021d08f6
3 changed files with 198 additions and 33 deletions
@@ -47,35 +47,114 @@ trait AKSRT_Admin_Shop_Controller_Product_Operations {
}
public function get_product_taxonomy(): WP_REST_Response {
$format_terms = static function (string $taxonomy): array {
if (!taxonomy_exists($taxonomy)) {
return [];
}
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
]);
if (is_wp_error($terms)) {
return [];
}
return array_map(static function (WP_Term $term): array {
return [
'id' => $term->term_id,
'name' => $term->name,
];
}, $terms);
};
return $this->success([
'data' => [
'categories' => $format_terms('aksrt_product_cat'),
'tags' => $format_terms('aksrt_product_tag'),
'categories' => $this->format_product_terms('aksrt_product_cat'),
'tags' => $this->format_product_terms('aksrt_product_tag'),
],
]);
}
/**
* POST /shop/products/taxonomy — create category or tag term.
*
* Body: { taxonomy: 'category'|'tag'|'aksrt_product_cat'|'aksrt_product_tag', name: string, parent?: int }
*/
public function create_product_taxonomy_term(WP_REST_Request $request): WP_REST_Response {
$params = $this->get_json_params($request);
$raw_tax = sanitize_key((string) ($params['taxonomy'] ?? 'category'));
$name = sanitize_text_field((string) ($params['name'] ?? ''));
$parent = max(0, (int) ($params['parent'] ?? 0));
$taxonomy_map = [
'category' => 'aksrt_product_cat',
'tag' => 'aksrt_product_tag',
'aksrt_product_cat' => 'aksrt_product_cat',
'aksrt_product_tag' => 'aksrt_product_tag',
];
$taxonomy = $taxonomy_map[$raw_tax] ?? '';
if ($taxonomy === '' || !taxonomy_exists($taxonomy)) {
return $this->error(__('无效的分类法', 'aksrt'));
}
if ($name === '') {
return $this->error(__('名称不能为空', 'aksrt'));
}
$existing = term_exists($name, $taxonomy, $taxonomy === 'aksrt_product_cat' ? $parent : 0);
if (is_array($existing) && !empty($existing['term_id'])) {
$term = get_term((int) $existing['term_id'], $taxonomy);
if ($term && !is_wp_error($term)) {
return $this->success([
'message' => __('术语已存在', 'aksrt'),
'data' => [
'id' => (int) $term->term_id,
'name' => (string) $term->name,
'slug' => (string) $term->slug,
'parent' => (int) $term->parent,
'taxonomy' => $taxonomy === 'aksrt_product_cat' ? 'category' : 'tag',
'existing' => true,
],
]);
}
}
$args = [];
if ($taxonomy === 'aksrt_product_cat' && $parent > 0) {
$args['parent'] = $parent;
}
$result = wp_insert_term($name, $taxonomy, $args);
if (is_wp_error($result)) {
return $this->error($result->get_error_message());
}
$term_id = (int) ($result['term_id'] ?? 0);
$term = get_term($term_id, $taxonomy);
if (!$term || is_wp_error($term)) {
return $this->error(__('创建失败', 'aksrt'));
}
return $this->success([
'message' => __('术语已创建', 'aksrt'),
'data' => [
'id' => (int) $term->term_id,
'name' => (string) $term->name,
'slug' => (string) $term->slug,
'parent' => (int) $term->parent,
'taxonomy' => $taxonomy === 'aksrt_product_cat' ? 'category' : 'tag',
'existing' => false,
],
], 201);
}
/**
* @return array<int, array{id:int,name:string,slug:string,parent:int,count:int}>
*/
private function format_product_terms(string $taxonomy): array {
if (!taxonomy_exists($taxonomy)) {
return [];
}
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
]);
if (is_wp_error($terms) || !is_array($terms)) {
return [];
}
return array_map(static function (WP_Term $term): array {
return [
'id' => (int) $term->term_id,
'name' => (string) $term->name,
'slug' => (string) $term->slug,
'parent' => (int) $term->parent,
'count' => (int) $term->count,
];
}, $terms);
}
/**
* GET /shop/products — 商品列表,支持 search、status、page、per_page
*/
@@ -175,6 +175,13 @@ class AKSRT_Admin_Shop_Controller extends AKSRT_Admin_Controller_Base {
'permission_callback' => [$this, 'check_permission'],
]);
// Create product category or tag term for the product editor.
register_rest_route(self::API_NAMESPACE, '/shop/products/taxonomy', [
'methods' => 'POST',
'callback' => [$this, 'create_product_taxonomy_term'],
'permission_callback' => [$this, 'check_permission'],
]);
// Product list
register_rest_route(self::API_NAMESPACE, '/shop/products', [
'methods' => 'GET',