fix: add getCategories, getTags, getTaxonomies, getTermMeta, updateTermMeta, deleteTermMeta
This commit is contained in:
@@ -3,28 +3,82 @@ namespace AskTamMCP\Tools;
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class TaxonomyTools {
|
||||
|
||||
public static function listTerms(array $args): array {
|
||||
$terms = get_terms(['taxonomy'=>$args['taxonomy'],'hide_empty'=>false,'parent'=>$args['parent']??'','search'=>$args['search']??'','number'=>(int)($args['per_page']??50)]);
|
||||
if (is_wp_error($terms)) throw new \RuntimeException($terms->get_error_message());
|
||||
return ['terms'=>array_map(fn($t)=>['id'=>$t->term_id,'name'=>$t->name,'slug'=>$t->slug,'count'=>$t->count,'parent'=>$t->parent],$terms),'total'=>count($terms)];
|
||||
}
|
||||
|
||||
public static function createTerm(array $args): array {
|
||||
$r = wp_insert_term($args['name'],$args['taxonomy'],['slug'=>$args['slug']??'','description'=>$args['description']??'','parent'=>$args['parent']??0]);
|
||||
if (is_wp_error($r)) throw new \RuntimeException($r->get_error_message());
|
||||
return ['id'=>$r['term_id'],'name'=>$args['name'],'taxonomy'=>$args['taxonomy']];
|
||||
return ['term_id'=>$r['term_id'],'id'=>$r['term_id'],'name'=>$args['name'],'taxonomy'=>$args['taxonomy']];
|
||||
}
|
||||
|
||||
public static function updateTerm(array $args): array {
|
||||
$r = wp_update_term((int)$args['term_id'],$args['taxonomy'],array_filter(['name'=>$args['name']??null,'slug'=>$args['slug']??null,'description'=>$args['description']??null,'parent'=>$args['parent']??null],fn($v)=>$v!==null));
|
||||
if (is_wp_error($r)) throw new \RuntimeException($r->get_error_message());
|
||||
return ['id'=>(int)$args['term_id'],'updated'=>true];
|
||||
}
|
||||
|
||||
public static function deleteTerm(array $args): array {
|
||||
$r = wp_delete_term((int)$args['term_id'],$args['taxonomy']);
|
||||
if (is_wp_error($r)) throw new \RuntimeException($r->get_error_message());
|
||||
return ['id'=>(int)$args['term_id'],'deleted'=>true];
|
||||
}
|
||||
|
||||
public static function countTerms(array $args): array {
|
||||
$count = wp_count_terms(['taxonomy'=>$args['taxonomy']??'category']);
|
||||
return ['taxonomy'=>$args['taxonomy'],'count'=>(int)$count];
|
||||
}
|
||||
|
||||
public static function getCategories(array $args): array {
|
||||
$terms = get_terms(['taxonomy'=>'category','hide_empty'=>false,'parent'=>$args['parent']??'','search'=>$args['search']??'','number'=>(int)($args['per_page']??100)]);
|
||||
if (is_wp_error($terms)) throw new \RuntimeException($terms->get_error_message());
|
||||
return ['categories'=>array_map(fn($t)=>['id'=>$t->term_id,'name'=>$t->name,'slug'=>$t->slug,'count'=>$t->count,'parent'=>$t->parent],$terms),'total'=>count($terms)];
|
||||
}
|
||||
|
||||
public static function getTags(array $args): array {
|
||||
$terms = get_terms(['taxonomy'=>'post_tag','hide_empty'=>false,'search'=>$args['search']??'','number'=>(int)($args['per_page']??100)]);
|
||||
if (is_wp_error($terms)) throw new \RuntimeException($terms->get_error_message());
|
||||
return ['tags'=>array_map(fn($t)=>['id'=>$t->term_id,'name'=>$t->name,'slug'=>$t->slug,'count'=>$t->count],$terms),'total'=>count($terms)];
|
||||
}
|
||||
|
||||
public static function getTaxonomies(array $args): array {
|
||||
$taxonomies = get_taxonomies(['public'=>true],'objects');
|
||||
$result = [];
|
||||
foreach ($taxonomies as $tax) {
|
||||
$result[] = [
|
||||
'name' => $tax->name,
|
||||
'label' => $tax->label,
|
||||
'hierarchical' => (bool)$tax->hierarchical,
|
||||
'object_type' => $tax->object_type,
|
||||
'public' => (bool)$tax->public,
|
||||
];
|
||||
}
|
||||
return ['taxonomies'=>$result,'total'=>count($result)];
|
||||
}
|
||||
|
||||
public static function getTermMeta(array $args): array {
|
||||
$termId = (int)$args['term_id'];
|
||||
$key = $args['meta_key'] ?? '';
|
||||
if ($key) return ['term_id'=>$termId,'meta_key'=>$key,'meta_value'=>get_term_meta($termId,$key,true)];
|
||||
$all = get_term_meta($termId);
|
||||
$flat = [];
|
||||
foreach ($all as $k => $v) $flat[$k] = is_array($v) && count($v)===1 ? $v[0] : $v;
|
||||
return ['term_id'=>$termId,'meta'=>$flat];
|
||||
}
|
||||
|
||||
public static function updateTermMeta(array $args): array {
|
||||
$termId = (int)$args['term_id'];
|
||||
update_term_meta($termId, sanitize_key($args['meta_key']), $args['meta_value']);
|
||||
return ['term_id'=>$termId,'meta_key'=>$args['meta_key'],'updated'=>true];
|
||||
}
|
||||
|
||||
public static function deleteTermMeta(array $args): array {
|
||||
$termId = (int)$args['term_id'];
|
||||
$result = delete_term_meta($termId, sanitize_key($args['meta_key']));
|
||||
return ['term_id'=>$termId,'meta_key'=>$args['meta_key'],'deleted'=>(bool)$result];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user