wppaste
WordPress

wp_notify_note_mentions( WP_Comment|null $comment, mixed $request = null, bool $creating = true )

Since
7.1.0
Source
wp-includes/comment.php:2654
Notifies mentioned users about a new note.

Description

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.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$commentWP_Comment|null
The note that was just inserted. (May only be null as an edge case.)
$requestmixedoptional
The REST request. Unused.Default: null
$creatingbooloptional
Whether this is a create (true) or update (false).Default: true

Performance profile

How much work a call to wp_notify_note_mentions() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Expensive

Sends mail via wp_mail().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–39

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 69.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • optionoption read or writeget_option()called directly
  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()one call below wp_notify_note_mentions()
  • cacheobject cachewp_cache_get()one call below wp_notify_note_mentions()
  • serializeserialisationmaybe_unserialize()one call below wp_notify_note_mentions()
  • mailsends mailwp_mail()one call below wp_notify_note_mentions()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_notify_note_mentions() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–9none
!get_option()14get_option()
get_option()32–35get_option(), wp_get_note_mentioned_user_ids()
get_option()36–39get_option(), wp_get_note_mentioned_user_ids(), get_post()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev685–38131 fewer instruction than PHP 8.5
8.5695–3913
8.4695–3913
8.3695–3913
8.2695–3913
8.1695–39131 fewer instruction than PHP 7.4
7.4705–4013

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

Source code

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 );	}}

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.