Files
advanced-mcp-plugin/includes/Tools/CommentTools.php
T

28 lines
1.6 KiB
PHP

<?php
namespace AskTamMCP\Tools;
defined('ABSPATH') || exit;
class CommentTools {
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)];
if (!$q['post_id']) unset($q['post_id']);
$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)];
}
public static function approveComment(array $args): array {
$r = wp_set_comment_status((int)$args['comment_id'],'approve');
if (!$r) throw new \RuntimeException("Failed to approve comment {$args['comment_id']}");
return ['id'=>(int)$args['comment_id'],'approved'=>true];
}
public static function spamComment(array $args): array {
$r = wp_set_comment_status((int)$args['comment_id'],'spam');
if (!$r) throw new \RuntimeException("Failed to spam comment {$args['comment_id']}");
return ['id'=>(int)$args['comment_id'],'spam'=>true];
}
public static function deleteComment(array $args): array {
$r = wp_delete_comment((int)$args['comment_id'],(bool)($args['force']??false));
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)];
}
}