wp_notify_note_mentions()
Notifies mentioned users about a new note.
Runs on rest_insert_comment alongside the post author notification. The recipient set is the users mentioned in this note, minus the note's own author (a user is not notified about their own note) and the post author, who is already notified about every note by wp_new_comment_via_rest_notify_postauthor().
Only fires when a note is created, not when an existing one is edited, so correcting a note does not re-notify everyone who already received it.
No Hooks.
Returns
null. Nothing (null).
Usage
wp_notify_note_mentions( ?WP_Comment $comment, $request, $creating ): void;
- ?WP_Comment $comment(required)
- .
- $request(mixed)
- The REST request. Unused.
Default:null - $creating(true|false)
- Whether this is a create (true) or update (false).
Default:true
Changelog
| Since 7.1.0 | Introduced. |
wp_notify_note_mentions() wp notify note mentions code WP 7.1
function wp_notify_note_mentions( ?WP_Comment $comment, $request = null, bool $creating = true ): void {
if ( ! $creating || ! $comment ) {
return;
}
if ( 'note' !== $comment->comment_type ) {
return;
}
// Share the single user-facing notes notification preference.
if ( ! get_option( 'wp_notes_notify', 1 ) ) {
return;
}
$mentioned = wp_get_note_mentioned_user_ids( $comment->comment_content );
$author_id = (int) $comment->user_id;
$comment_post_id = (int) $comment->comment_post_ID;
$post = $comment_post_id ? get_post( $comment_post_id ) : null;
$post_author_id = $post ? (int) $post->post_author : 0;
/*
* The recipient set is bounded and small (one note's mentions), so emails
* are sent synchronously here. If notification volume ever warrants it,
* the right fix is to offload delivery to a background queue rather than
* throttle within the request.
*/
foreach ( $mentioned as $user_id ) {
// Never notify the author about their own note.
if ( $user_id === $author_id ) {
continue;
}
// The post author is already notified of every note.
if ( $user_id === $post_author_id ) {
continue;
}
$user = get_userdata( $user_id );
if ( ! $user || empty( $user->user_email ) ) {
continue;
}
/*
* Only notify users who can actually read the note. Notes are
* internal: WP_REST_Comments_Controller::check_read_permission()
* only exposes a note to its author or to users who can edit it, so
* the email audience is held to the same bar. A plain read_post
* check would leak note content to, for example, subscribers on a
* public post, who cannot see the note in the editor.
*/
if ( ! user_can( $user_id, 'edit_comment', $comment->comment_ID ) ) {
continue;
}
wp_send_note_notification( $user, $comment, $post );
}
}