feat: require reader to set name when commenting#4647
Conversation
There was a problem hiding this comment.
Tests well! I left one non-blocking comment inline, but otherwise looks good to me.
Other non-blocking feedback from Claude to consider below.
Code Review
Functional Testing — All scenarios pass:
- ✅ Reader with generic display name sees the "Name *" field in the comment form
- ✅
requiredattribute blocks empty submission (client-side); server-sidewp_die()provides fallback- ✅ Server-side validation rejects empty names and email-derived names
- ✅ Submitting with a valid name updates comment_author, display_name, first_name, and last_name
- ✅ Field disappears on subsequent pages after name is set
- ✅ Non-reader users and readers with custom display names don't see the field
- ✅ PHPUnit: 10/10 pass, 18 assertions
Code Quality — Clean, well-scoped implementation:
- Good use of
preprocess_commentto both validate and overridecomment_authorin one pass — avoids the race where WP reads the stale profile name beforewp_insert_comment.- Proper sanitization with
sanitize_text_field/wp_unslashon$_POSTdata.- Validation covers both
generate_user_nicename()andstrip_email_domain()patterns.NEWSPACK_ALLOW_GENERIC_READER_DISPLAY_NAMESconstant escape hatch (from the existingreader_has_generic_display_name) is a nice touch for sites that don't want this behavior.Suggestions (non-blocking):
Inline style on input (
class-comment-display-name.php:116):style="display:block;width:100%"— the Newspack theme's comment form already stylesinput[type="text"]to be full-width. Consider removing the inline style or using a CSS class to keep styling in the theme layer. Worth verifying against the block theme as well.No error handling on
wp_update_user()(class-comment-display-name.php:92): Ifwp_update_user()returns aWP_Error, the comment is still posted with the new name incomment_authorbut the user profile isn't actually updated. On the next page load, the reader would be prompted again. A minor edge case, but worth noting.Case-sensitive email-derived name check (
class-comment-display-name.php:72-73): Thestrip_email_domain()comparison is case-sensitive. A reader with emailJane.Doe@example.comwhose generic display name isjane-doe(lowercased viagenerate_user_nicename) could submitJane.Doe(matchingstrip_email_domainoutput) and be blocked, but submittingjane.doewith different casing than the raw email handle would pass. This is a very minor edge case since the intent is to prevent accidental reuse, not strict enforcement — just flagging for awareness.Overall, this is a clean, focused PR with good test coverage. Nice work! 👍
|
Hey @miguelpeixe, good job getting this PR merged! 🎉 Now, the Please check if this PR needs to be included in the "Upcoming Changes" and "Release Notes" doc. If it doesn't, simply remove the label. If it does, please add an entry to our shared document, with screenshots and testing instructions if applicable, then remove the label. Thank you! ❤️ |
All Submissions:
Changes proposed in this Pull Request:
Readers may register with just their email. In this case, their name defaults to the email handle. This should be fine for internal purposes, but shouldn't be used as a public display name in comments.
This PR implements a new input in the comment form, requiring a reader to set their display name if it has not yet been set. Once set, the input should no longer render.
The input placement follows the same standard as when commenting anonymously.
Closes NPPM-2701
How to test the changes in this Pull Request:
Other information: