Skip to content

Feat 514: add comment value score#681

Open
priyanshuhaldar007 wants to merge 5 commits into
WordPress:developfrom
priyanshuhaldar007:feat-514/add-comment-value-score
Open

Feat 514: add comment value score#681
priyanshuhaldar007 wants to merge 5 commits into
WordPress:developfrom
priyanshuhaldar007:feat-514/add-comment-value-score

Conversation

@priyanshuhaldar007

@priyanshuhaldar007 priyanshuhaldar007 commented Jun 8, 2026

Copy link
Copy Markdown

What?

Closes #514

Adds a Value Score column to the comment moderation list table. Comments are now analyzed for how relevant and valuable they are to the article they're posted on, in addition to the existing toxicity and sentiment signals.

Why?

Toxicity and sentiment alone don't tell the full story of a comment's quality. A comment can be perfectly polite but still be spam, a generic "+1", or completely off-topic. This PR introduces a value score (0–1) so moderators can quickly identify substantive, on-topic contributions versus low-effort noise — making triage faster and more informed.

How?

The implementation follows the exact same pattern as the existing toxicity_score field end-to-end:

AI / Prompt layer

  • Updated system-instruction.php to instruct the model to return a third field, value_score, scored 0–1 with clear band definitions (low/medium/high). The prompt now also passes post context (excerpt → AI summary → trimmed content fallback) so the model can actually assess relevance against the article.
  • Added get_post_context() to Comment_Analysis.php to fetch and prepare that context, with a graceful 650-char truncation fallback on raw content.
  • analyze_comment() now accepts and passes $post_id through to the prompt builder.

Schema & storage

  • output_schema() and response_schema() both declare value_score as a nullable float (0–1). It's nullable for cases where the post content is unavailable or too sparse to judge relevance.
  • sanitize_analysis_result() clamps the value to [0, 1] and preserves null.
  • Results are stored in a new comment meta key META_VALUE_SCORE (_wpai_value_score) and returned in the ability response payload.

Comment Moderation UI (PHP)

  • Added VALUE_SCORE_LOW / MEDIUM / HIGH constants and get_value_score_config() with the same range-bucket shape used by toxicity, so the frontend JS can resolve badges identically.
  • New wpai_value_score column registered in add_columns() and add_sortable_columns().
  • render_value_score_column() and render_value_score_badge() added, mirroring the toxicity equivalents.
  • Filter dropdown added for value score levels.
  • handle_sorting_and_filtering() extended to support wpai_value_score ordering via a meta query, same pattern as toxicity sorting.
  • enqueue_assets() now passes value_score label config into window.aiCommentModerationData.labels.
  • CSS: reused existing green/yellow/red badge classes by appending ai-badge--high-value, ai-badge--medium-value, and ai-badge--low-value to the existing selectors — no new colour definitions needed.

Frontend JS / TSX

  • AnalysisResult type extended with value_score: number.
  • Window declaration extended with the value_score labels shape.
  • PendingComment type extended with valueScoreBadge: HTMLElement.
  • getValueScoreDisplay() helper added alongside getToxicityDisplay(), using the same range-bucket lookup.
  • updateBadges(), findPendingComments(), analyzeComment() all updated to handle the third badge — detection, processing state, result rendering, and failure state.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Claude Sonnet 4.6
Used for: Drafting this PR description from the git diff. All code was written and reviewed by me, with some contributions from the copilot for updating and generating doc blocks

Testing Instructions

  1. Install and activate the plugin with the AI experiment enabled.
  2. Create a post with meaningful content and leave a few comments — mix of on-topic, generic ("great post!"), and spam.
  3. Navigate to Comments in wp-admin.
  4. Confirm a Value column appears alongside Sentiment and Toxicity.
  5. Trigger analysis (or wait for lazy analysis on page load). Verify badges appear with appropriate levels — high-value comments should show 🌟 High, spam/generic should show ✓ Low.
  6. Test the Value Score filter dropdown — filtering by Low, Medium, or High should correctly narrow the list.
  7. Test sorting by clicking the Value column header — ascending and descending should order comments by their score.
  8. Confirm the dashboard comment excerpt pills also show the value score badge.

Screenshots or screencast

Before After
Screenshot 2026-06-08 at 2 06 34 PM Screenshot 2026-06-08 at 2 05 47 PM

Changelog Entry

Added - Value Score column to the comment moderation table, providing a relevance signal (0–1) for each comment based on how substantive and on-topic it is relative to the post it was left on.

Open WordPress Playground Preview

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @priyanshuhaldar007.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: priyanshuhaldar007.


To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@dkotter dkotter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a number of comments and also seeing quite a few lint failures that need fixed here. In addition, we should look to add both unit tests and update our E2E tests to ensure these changes are covered

Comment on lines +256 to +259
* @param string $post_id The ID of the post.
* @return array{toxicity_score: float, sentiment: string, value_score: float|null}|\WP_Error The analysis result.
*/
private function analyze_comment( string $content, string $author ) {
private function analyze_comment( string $content, string $author, string $post_id ) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe $post_id should always be an integer

Suggested change
* @param string $post_id The ID of the post.
* @return array{toxicity_score: float, sentiment: string, value_score: float|null}|\WP_Error The analysis result.
*/
private function analyze_comment( string $content, string $author ) {
private function analyze_comment( string $content, string $author, string $post_id ) {
* @param int $post_id The ID of the post.
* @return array{toxicity_score: float, sentiment: string, value_score: float|null}|\WP_Error The analysis result.
*/
private function analyze_comment( string $content, string $author, int $post_id ) {

* @param array{toxicity_score: float, sentiment: string, value_score: float|null}|null $result Precomputed analysis result.
* @param string $content Comment content.
* @param string $author Comment author name.
* @param string $post_id The ID of the post.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param string $post_id The ID of the post.
* @param int $post_id The ID of the post.

'description' => esc_html__( 'The sentiment of the comment.', 'ai' ),
),
'value_score' => array(
'type' => array( 'number', 'null' ),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there value in having this return null? I think I'd just match what we do for toxicity scoring and have a number between 0 and 1

/**
* Gets the configuration for Value_Score levels.
*
* @since 1.0.0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 1.0.0
* @since x.x.x

/**
* Renders the value score column content.
*
* @since 1.0.0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 1.0.0
* @since x.x.x

* @param int $comment_id The comment ID.
* @param string $status The analysis status.
*/
private function render_value_score_column( int $comment_id, string $status ): void {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here, this method is basically the same as render_toxicity_column and render_sentiment_column. I'd suggest we introduce a new generic method that can be used by all three of these to avoid duplicate code

* @return string The content of the post.
*/
private function get_post_context( int $post_id ): ?string {
$post = get_post( $post_id );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The post may not exist so we need an early return check here

$post = get_post( $post_id );

// 1. Use excerpt if available (human-written, most reliable)
$excerpt = trim( $post->post_excerpt );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any benefit to using the excerpt or summary over just using the full post content?

return null;
}

return mb_substr( $content, 0, 650 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need to trim this. I understand it will save tokens but likely gives us worse results

Comment on lines +280 to 287
$post_context = $this->get_post_context( absint( $post_id ) );

$prompt = sprintf(
"Comment by %s:\n\"\"\"%s\"\"\"",
"Comment by %s:\n\"\"\"%s\"\"\"\nContext:\n\"\"\"%s\"\"\"",
$author,
$content
$content,
$post_context
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're passing in the content of the post a comment is on, I'd suggest we change how we build this to send to the LLM. You can look at our other abilities to see how they work but generally we put things in XML-like tags

@dkotter dkotter added this to the Future Release milestone Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add comment value / relevance to Comment Moderation experiment

2 participants