Skip to content

Comment Moderation: Add pre-submission sentiment and toxicity checks - #1392

Open
himanshupathak95 wants to merge 37 commits into
WordPress:developfrom
himanshupathak95:feat/1363-comment-form-sentiment-toxicity
Open

himanshupathak95 wants to merge 37 commits into
WordPress:developfrom
himanshupathak95:feat/1363-comment-form-sentiment-toxicity

Conversation

@himanshupathak95

@himanshupathak95 himanshupathak95 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What?

Closes #1363.

Adds an opt-in draft check to the Comment Moderation experiment.

Why?

Readers can revise a comment before posting, while moderators still have the existing Pending Moderation queue.

How?

The form reuses the existing ai/comment-analysis ability to check draft text after a 1.5-second pause or on submit. Automatic checks start at 20 characters. Results are reused for unchanged text, and checks run sequentially with a four-second timeout per request.

If sentiment is negative and toxicity reaches 0.7 by default, a red notice asks the reader to revise the comment and disables submission. A spinner and checking message appear beside Post Comment. Developers can adjust the draft threshold and request limits through filters.

If a check fails, times out, reaches a request limit, or JavaScript is disabled, WordPress handles the submission through its usual moderation flow.

Use of AI Tools

AI assistance: Yes
Implemented and tested with assistance from OpenAI Codex (Astra 6)

Testing Instructions

  1. In Settings → AI, enable Comment Moderation and Advanced settings. Turn on Check comments before submission and save.
  2. On a guest comment form, fill any required name and email fields. Type I just wanted to diss you, clown. You're such a fool. and pause. Confirm the red notice appears and Post Comment is disabled.
  3. Change the text to This plugin did not work for me. Confirm the notice clears and submission is allowed. Try the toxic comment again, clicking Post Comment immediately to test the submit check.
  4. With a slow check running, revise the text and submit. Confirm the checking indicator stays near Post Comment and the revised text gets its own check after the older one finishes.
  5. Repeat the form checks as an editor and subscriber.
  6. With JavaScript disabled, submit a toxic test comment and confirm the existing server moderation sends it to Pending Moderation.

Screenshots or screencast

Screen.Recording.2026-11-26.at.16.10.20.mov
Screen.Recording.2026-11-26.at.16.11.46.mov

Changelog Entry

Added - Optional sentiment and toxicity checks on comment forms before submission.

Open WordPress Playground Preview

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.94382% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.77%. Comparing base (c4666bb) to head (035ef09).

Files with missing lines Patch % Lines
...eriments/Comment_Moderation/Comment_Moderation.php 76.66% 7 Missing ⚠️
...des/Experiments/Comment_Moderation/Draft_Check.php 97.87% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #1392      +/-   ##
=============================================
+ Coverage      85.59%   85.77%   +0.18%     
- Complexity      5091     5145      +54     
=============================================
  Files            193      194       +1     
  Lines          18425    18589     +164     
=============================================
+ Hits           15770    15945     +175     
+ Misses          2655     2644      -11     
Flag Coverage Δ
unit 85.77% <94.94%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@himanshupathak95
himanshupathak95 force-pushed the feat/1363-comment-form-sentiment-toxicity branch from 10019bb to 420f5e3 Compare November 16, 2026 13:15
@himanshupathak95
himanshupathak95 marked this pull request as ready for review November 16, 2026 14:10
@himanshupathak95
himanshupathak95 requested a review from a team November 16, 2026 14:10
@github-actions

github-actions Bot commented Nov 16, 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.

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

Co-authored-by: himanshupathak95 <abcd95@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@jeffpaul jeffpaul added this to the 1.6.0 milestone Nov 18, 2026
jeffpaul
jeffpaul previously approved these changes Nov 18, 2026

@jeffpaul jeffpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Works as expected, thanks!

@jeffpaul
jeffpaul requested a review from dkotter November 18, 2026 15:56

@dkotter dkotter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've not fully reviewed or tested things yet but I do have one main question I'd love an answer on before proceeding further:

We already have an ability that will return a toxicity score, a sentiment value and a value score. It seems like we should be able to re-use all of that for this new check, with the only difference being when/where the check is run. Instead this PR introduces an entirely new request with it's own system instructions.

Was there a specific reason this approach was taken instead of just using what we already have? I'd prefer not to introduce such similar functionality if we can keep things simpler here.

@himanshupathak95

Copy link
Copy Markdown
Contributor Author

Hey @dkotter, I split this out because the existing ability expects a saved comment ID and stores its results as comment metadata. The form needs to check unsaved text, including from guests, so calling that ability directly required some changes.

I also kept the draft request for comment text and the two scores the form uses, to keep the background checks lightweight. If you prefer it that way, I would maybe proceed with extracting the existing analysis into a shared method that would handle both cases. What do you think about this?

@himanshupathak95

Copy link
Copy Markdown
Contributor Author

Was there a specific reason this approach was taken instead of just using what we already have? I'd prefer not to introduce such similar functionality if we can keep things simpler here.

The main reason was keeping the live front-end check fast and lightweight. The existing ability expects a saved comment, pulls in up to 4,000 characters of the parent article, and calculates a value score for the discussion. Since draft checks run synchronously while visitors type and submit, sending the full article text on every debounced pause felt like unnecessary token overhead and latency when we only need toxicity and sentiment for the draft text itself.

That said, I kinda do agree that having a single ability and system instruction is cleaner and easier to maintain (especially since both the jobs do the same thing). We can maybe update Comment_Analysis to accept raw text directly and skip the article context when analyzing a draft?

Happy to refactor it to reuse the existing ability if you prefer that approach.

@dkotter

dkotter commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The main reason was keeping the live front-end check fast and lightweight. The existing ability expects a saved comment, pulls in up to 4,000 characters of the parent article, and calculates a value score for the discussion. Since draft checks run synchronously while visitors type and submit, sending the full article text on every debounced pause felt like unnecessary token overhead and latency when we only need toxicity and sentiment for the draft text itself.

That said, I kinda do agree that having a single ability and system instruction is cleaner and easier to maintain (especially since both the jobs do the same thing). We can maybe update Comment_Analysis to accept raw text directly and skip the article context when analyzing a draft?

Happy to refactor it to reuse the existing ability if you prefer that approach.

Thanks for all the details.

I would say I think it's worth exploring updating the existing ability to accept comment text (instead of just comment ID) and see if we can get it working for this as well, just to keep things consistent. I do think we already skip adding extra post context if a post ID isn't passed so that part may be mostly handled already.

@dkotter dkotter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Handful of comments that I think need addressed.

In addition to those, the UI is not great right now. If I have a comment form that requires a name and email, after I hit submit, I don't even see the processing message because that is above the comment form (which is out of my viewport at this point). I think ideally we should show a loading state next to the submit button, not above the form. And should ideally show some sort of loading indicator, not just text, as it's easy to miss.

Comment thread docs/experiments/comment-moderation.md Resolved
Comment on lines +4 to +6
import { Notice } from '@wordpress/components';
import domReady from '@wordpress/dom-ready';
import { createRoot } from '@wordpress/element';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So we're not doing anything too crazy in this file that benefits from these packages but by including these, we're loading wp-components and React on the front-end. Likely fine but may want to do a slight refactor here to remove those dependencies and keep things simple

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've switched this to plain DOM and CSS in the latest commits.

I understand the concern about loading React and wp-components on public pages. My preference would still be to reuse Notice and Spinner for their shared styling and behavior, so we don't maintain them separately. Would you be open to using those components here for consistency with other parts of the site, or would you prefer keeping the current implementation?

return running.result;
}

// A newer draft can wait for an older check, but never use its verdict.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So if I'm following this code correctly, I think we could easily run into timing issues here. As an example, if I type a comment and then pause, the check starts. If I then modify the comment and immediately click Post, we do wait to run the second check until the first check is finished. But our timer starts immediately and so likely that someone could hit the 4 second limit before the second check actually finishes. The comment will still be analyzed on the server-side but that does somewhat defeat the purpose of this addition

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I capped the whole submit wait at four seconds so a slow provider wouldn’t hold up posting. That also counted the wait for an older draft, leaving less time to check the revision (as you pointed, to make the uesr not wait long). The latest commits keep checks sequential and give each request its own four-second timeout, allowing up to eight seconds when an older check is still running.

Comment thread docs/experiments/comment-moderation.md Resolved
try {
// Use the server-observed address for guests and do not store it in plaintext.
// phpcs:ignore WordPressVIPMinimum.Variables -- This uncached REST callback uses only a validated network address for throttling.
$address = (string) filter_var( $_SERVER['REMOTE_ADDR'] ?? '', FILTER_VALIDATE_IP );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While I appreciate this light-weight rate limiting, this will certainly cause problems with sites behind a load balancer or things like Cloudflare, which lots of sites are. I'm wondering if we need a more robust rate limiting feature, likely handled outside of this PR? Or at least some documentation that details out how this works and the downsides, and maybe some filters around the limits?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added this as a basic cap on AI requests from the public form. I used the server-observed address because trusting forwarded headers requires a known proxy configuration. Shared proxy addresses are still a limitation, so the latest commits document that behavior and make both limits filterable. I’ve left broader rate limiting outside this PR. I do think this needs to be taken up separately as a broader project-wide idea.

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 sentiment and toxicity detection to comment forms

3 participants