Skip to content

Use comment status and type to set $maybe_notify default#10489

Open
adamsilverstein wants to merge 20 commits into
WordPress:trunkfrom
adamsilverstein:fix/notification-filter-default
Open

Use comment status and type to set $maybe_notify default#10489
adamsilverstein wants to merge 20 commits into
WordPress:trunkfrom
adamsilverstein:fix/notification-filter-default

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Nov 7, 2025

Copy link
Copy Markdown
Member

Trac ticket: https://core.trac.wordpress.org/ticket/64217


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

github-actions Bot commented Nov 7, 2025

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props adamsilverstein, westonruter, jorbin.

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

@github-actions

github-actions Bot commented Nov 7, 2025

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/comment.php Outdated
Comment thread src/wp-includes/comment.php
@adamsilverstein adamsilverstein force-pushed the fix/notification-filter-default branch from b823913 to 87f55c9 Compare February 25, 2026 10:01
Co-authored-by: Weston Ruter <westonruter@gmail.com>
@adamsilverstein adamsilverstein force-pushed the fix/notification-filter-default branch from 87f55c9 to 7c217dd Compare February 25, 2026 10:03
Comment thread src/wp-includes/comment.php Outdated
Comment thread src/wp-includes/comment.php
Comment thread src/wp-includes/comment.php Outdated
Comment thread src/wp-includes/comment.php
adamsilverstein and others added 4 commits March 17, 2026 10:52
Co-authored-by: Weston Ruter <westonruter@gmail.com>
The get_comment() return value is filtered, so reassign
$comment_id from the comment object to ensure consistency
between the property checks and subsequent function calls.
Also removes a redundant null check on $comment.
@adamsilverstein

Copy link
Copy Markdown
Member Author

@westonruter whenever you have a moment, this is ready for review again.

The filter override and default-value tests fail against trunk, where the
approval check runs after the filter and silently vetoes a true result for
unapproved comments.
The test passed 0 as an invalid comment ID, but get_comment() treats any
empty ID as a request for the global $comment, which earlier tests in
the same file leave populated - so the instanceof guard never triggered
and the filter fired. Use a non-existent ID instead, which bypasses the
fallback and exercises the early return. Test-only change.
The approval check moving ahead of the notify_post_author filter and the
early return for invalid comment IDs are both observable to extenders, so
they need @SInCE 7.1.0 entries on the function as well as the filter. Also
moves the function description above the @SInCE tag per the inline
documentation standards.
WP_Comment::$comment_ID is a numeric string, while both hooked call sites
previously delivered an int and the filter docblock documents the param as
int. Casting preserves strict comparisons in existing filter callbacks and
keeps the documented type accurate.
Protects the two remaining default paths of the reordered logic: an
unapproved note must still default to notifying (the note carve-out), and
an approved comment's default must follow the comments_notify option.
@adamsilverstein

Copy link
Copy Markdown
Member Author

Draft dev note for the 7.1 field guide

The notify_post_author filter now has the final say on post author notifications

WordPress 7.1 changes when the comment approval status is checked in wp_new_comment_notify_postauthor(). See #64217.

Previously, the notify_post_author filter received a default value derived only from the comments_notify (or wp_notes_notify) option. The comment's approval status was checked after the filter ran, which had two consequences:

  1. The filter received a misleading default of true for unapproved comments, even though no notification would be sent.
  2. Returning true from the filter could not force a notification for an unapproved comment - the return value was silently discarded.

As of 7.1, the approval status is incorporated into the default value passed to the filter, and the filter's return value is final:

  • The default is false for unapproved comments (including spam and trash).
  • The default for notes continues to follow the wp_notes_notify option regardless of approval status, and the default for approved comments continues to follow the comments_notify option.
  • Returning true from the filter now sends the notification, even for an unapproved comment.

In addition, the filter no longer fires when the passed comment ID does not resolve to a valid comment; the function returns false immediately.

Who is affected: Sites or plugins using a callback such as __return_true on notify_post_author to force notifications will now also receive emails for comments held in moderation, marked as spam, or trashed. If that is not desired, the callback should check the comment's approval status:

add_filter(
	'notify_post_author',
	function ( $maybe_notify, $comment_id ) {
		$comment = get_comment( $comment_id );

		// Only force notifications for approved comments.
		if ( $comment && '1' === $comment->comment_approved ) {
			return true;
		}

		return $maybe_notify;
	},
	10,
	2
);

Callbacks that only suppress notifications (returning false) are unaffected.

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.

3 participants