register( 'comments_list', array( 'description' => 'List comments with filters.', 'domain' => 'content', 'capability' => 'moderate_comments', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'status' => array( 'type' => 'string', 'default' => 'all' ), 'post_id' => array( 'type' => 'integer' ), 'per_page' => array( 'type' => 'integer', 'default' => 20 ), ), ), ), function ( $args ) { $query_args = array( 'status' => isset( $args['status'] ) ? sanitize_key( $args['status'] ) : 'all', 'number' => isset( $args['per_page'] ) ? min( 100, absint( $args['per_page'] ) ) : 20, ); if ( ! empty( $args['post_id'] ) ) { $query_args['post_id'] = absint( $args['post_id'] ); } $comments = get_comments( $query_args ); $items = array(); foreach ( $comments as $comment ) { $items[] = agentdesk_format_comment( $comment ); } return array( 'comments' => $items ); } ); $registry->register( 'comments_moderate', array( 'description' => 'Approve, spam, trash, or delete a comment.', 'domain' => 'content', 'capability' => 'moderate_comments', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'action' => array( 'type' => 'string', 'enum' => array( 'approve', 'spam', 'trash', 'delete' ) ), ), 'required' => array( 'id', 'action' ), ), ), function ( $args ) { $id = absint( $args['id'] ); $action = sanitize_key( $args['action'] ); if ( ! current_user_can( 'edit_comment', $id ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to moderate this comment.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } // Permanent deletion is destructive; keep it behind Safe Mode. if ( 'delete' === $action && Agentdesk_Security::is_safe_mode() ) { return new WP_Error( 'agentdesk_safe_mode_blocked', __( 'Permanently deleting a comment is blocked while Safe Mode is enabled.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } switch ( $action ) { case 'approve': $result = wp_set_comment_status( $id, 'approve' ); break; case 'spam': $result = wp_spam_comment( $id ); break; case 'trash': $result = wp_trash_comment( $id ); break; case 'delete': $result = wp_delete_comment( $id, true ); break; default: return new WP_Error( 'agentdesk_invalid_action', 'Invalid moderation action' ); } return array( 'id' => $id, 'action' => $action, 'success' => (bool) $result ); } ); } ); /** * Format comment. * * @param WP_Comment $comment Comment. * @return array */ function agentdesk_format_comment( $comment ) { return array( 'id' => (int) $comment->comment_ID, 'post_id' => (int) $comment->comment_post_ID, 'author' => $comment->comment_author, 'email' => $comment->comment_author_email, 'content' => $comment->comment_content, 'status' => wp_get_comment_status( $comment ), 'date' => $comment->comment_date, ); }