fix: add getPendingComments, trashComment, createComment

This commit is contained in:
2026-06-02 11:35:05 +00:00
parent 6f39a269af
commit 606109bca7
+31 -2
View File
@@ -3,25 +3,54 @@ namespace AskTamMCP\Tools;
defined('ABSPATH') || exit; defined('ABSPATH') || exit;
class CommentTools { class CommentTools {
public static function listComments(array $args): array { public static function listComments(array $args): array {
$q = ['post_id'=>$args['post_id']??0,'status'=>$args['status']??'approve','number'=>(int)($args['per_page']??20),'paged'=>(int)($args['page']??1)]; $q = ['status'=>$args['status']??'approve','number'=>(int)($args['per_page']??20),'paged'=>(int)($args['page']??1)];
if (!$q['post_id']) unset($q['post_id']); if (!empty($args['post_id'])) $q['post_id'] = (int)$args['post_id'];
$comments = get_comments($q); $comments = get_comments($q);
return ['comments'=>array_map(fn($c)=>['id'=>(int)$c->comment_ID,'post_id'=>(int)$c->comment_post_ID,'author'=>$c->comment_author,'email'=>$c->comment_author_email,'content'=>$c->comment_content,'status'=>$c->comment_approved,'date'=>$c->comment_date],$comments),'total'=>count($comments)]; return ['comments'=>array_map(fn($c)=>['id'=>(int)$c->comment_ID,'post_id'=>(int)$c->comment_post_ID,'author'=>$c->comment_author,'email'=>$c->comment_author_email,'content'=>$c->comment_content,'status'=>$c->comment_approved,'date'=>$c->comment_date],$comments),'total'=>count($comments)];
} }
public static function getPendingComments(array $args): array {
$comments = get_comments(['status'=>'hold','number'=>(int)($args['per_page']??20)]);
return ['comments'=>array_map(fn($c)=>['id'=>(int)$c->comment_ID,'post_id'=>(int)$c->comment_post_ID,'author'=>$c->comment_author,'content'=>$c->comment_content,'date'=>$c->comment_date],$comments),'total'=>count($comments)];
}
public static function approveComment(array $args): array { public static function approveComment(array $args): array {
$r = wp_set_comment_status((int)$args['comment_id'],'approve'); $r = wp_set_comment_status((int)$args['comment_id'],'approve');
if (!$r) throw new \RuntimeException("Failed to approve comment {$args['comment_id']}"); if (!$r) throw new \RuntimeException("Failed to approve comment {$args['comment_id']}");
return ['id'=>(int)$args['comment_id'],'approved'=>true]; return ['id'=>(int)$args['comment_id'],'approved'=>true];
} }
public static function spamComment(array $args): array { public static function spamComment(array $args): array {
$r = wp_set_comment_status((int)$args['comment_id'],'spam'); $r = wp_set_comment_status((int)$args['comment_id'],'spam');
if (!$r) throw new \RuntimeException("Failed to spam comment {$args['comment_id']}"); if (!$r) throw new \RuntimeException("Failed to spam comment {$args['comment_id']}");
return ['id'=>(int)$args['comment_id'],'spam'=>true]; return ['id'=>(int)$args['comment_id'],'spam'=>true];
} }
public static function trashComment(array $args): array {
$r = wp_trash_comment((int)$args['comment_id']);
if (!$r) throw new \RuntimeException("Failed to trash comment {$args['comment_id']}");
return ['id'=>(int)$args['comment_id'],'trashed'=>true];
}
public static function deleteComment(array $args): array { public static function deleteComment(array $args): array {
$r = wp_delete_comment((int)$args['comment_id'],(bool)($args['force']??false)); $r = wp_delete_comment((int)$args['comment_id'],(bool)($args['force']??false));
if (!$r) throw new \RuntimeException("Failed to delete comment {$args['comment_id']}"); if (!$r) throw new \RuntimeException("Failed to delete comment {$args['comment_id']}");
return ['id'=>(int)$args['comment_id'],'deleted'=>true,'permanent'=>(bool)($args['force']??false)]; return ['id'=>(int)$args['comment_id'],'deleted'=>true,'permanent'=>(bool)($args['force']??false)];
} }
public static function createComment(array $args): array {
$data = [
'comment_post_ID' => (int)($args['post_id'] ?? 0),
'comment_content' => $args['content'] ?? '',
'comment_author' => $args['author_name'] ?? 'MCP',
'comment_author_email' => $args['author_email'] ?? '',
'comment_parent' => (int)($args['parent'] ?? 0),
'comment_approved' => $args['status'] ?? 'approve',
];
$id = wp_insert_comment($data);
if (!$id) throw new \RuntimeException("Failed to create comment.");
return ['id'=>(int)$id,'created'=>true];
}
} }