v2.0.0: add includes/Tools/TaxonomyTools.php
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
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']];
|
||||||
|
}
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user