Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2686,11 +2686,21 @@ function wp_new_comment_notify_postauthor( $comment_id ) {
/**
* Send a notification to the post author when a new note is added via the REST API.
*
* Only fires when a note is created, not when an existing one is edited.
*
* @since 6.9.0
* @since 7.3.0 Added the `$request` and `$creating` parameters.
*
* @param WP_Comment $comment The comment object.
* @param WP_Comment $comment The comment object.
* @param WP_REST_Request|null $request Optional. The REST request. Unused. Default null.
* @param bool $creating Optional. Whether this is a create (true) or update (false).
* Default true.
*/
function wp_new_comment_via_rest_notify_postauthor( $comment ) {
function wp_new_comment_via_rest_notify_postauthor( $comment, $request = null, bool $creating = true ): void {
if ( ! $creating ) {
return;
}

if ( $comment instanceof WP_Comment && 'note' === $comment->comment_type ) {
wp_new_comment_notify_postauthor( (int) $comment->comment_ID );
}
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@
// Email notifications.
add_action( 'comment_post', 'wp_new_comment_notify_moderator' );
add_action( 'comment_post', 'wp_new_comment_notify_postauthor' );
add_action( 'rest_insert_comment', 'wp_new_comment_via_rest_notify_postauthor' );
add_action( 'rest_insert_comment', 'wp_new_comment_via_rest_notify_postauthor', 10, 3 );
add_action( 'rest_insert_comment', 'wp_notify_note_mentions', 10, 3 );
add_action( 'after_password_reset', 'wp_password_change_notification' );
add_action( 'register_new_user', 'wp_send_new_user_notifications' );
Expand Down
111 changes: 111 additions & 0 deletions tests/e2e/specs/notes-notifications.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* External dependencies
*/
import { randomUUID } from 'node:crypto';

/**
* Internal dependencies
*/
import { test, expect } from '../fixtures';

test.describe( 'Note notifications', () => {
test( 'emails the post author once when a note is added and then edited', async ( {
admin,
editor,
muPlugin,
page,
postFactory,
siteOptions,
userFactory,
} ) => {
const { user: author, username } = await userFactory.create( {
roles: [ 'author' ],
} );
const authorEmail = `${ username }@example.org`;

// Record mail to this test's post author instead of sending it, so other workers' mail is untouched.
const mailLog = `e2e_note_mail_${ randomUUID().replaceAll( '-', '_' ) }`;
await siteOptions.set( { wp_notes_notify: 1, [ mailLog ]: false } );
await muPlugin.write(
'record-note-mail.php',
`<?php
add_filter(
'pre_wp_mail',
static function ( $short_circuit, $atts ) {
if ( ! in_array( '${ authorEmail }', (array) $atts['to'], true ) ) {
return $short_circuit;
}
$log = get_option( '${ mailLog }', array() );
$log[] = $atts['subject'];
update_option( '${ mailLog }', $log, false );
return true;
},
10,
2
);`
);
const getMailLog = async () =>
( await siteOptions.get( [ mailLog ] ) )[ mailLog ] || [];

const post = await postFactory.create( {
title: postFactory.uniqueTitle( 'Post with a note' ),
content:
'<!-- wp:paragraph --><p>Note target.</p><!-- /wp:paragraph -->',
status: 'draft',
author: author.id,
} );

await admin.editPost( post.id );
await editor.canvas
.getByRole( 'document', { name: 'Block: Paragraph' } )
.click();

// Notes show floating next to the block, or in the "All notes" sidebar.
const notes = page.getByRole( 'region', {
name: /^(Notes|Editor settings)$/,
} );
const notice = ( text ) =>
page
.getByRole( 'button', { name: 'Dismiss this notice' } )
.filter( { hasText: text } );

await test.step( 'Add a note', async () => {
await editor.clickBlockOptionsMenuItem( 'Add note' );
await page
.getByRole( 'textbox', { name: 'New note', exact: true } )
.fill( 'Original note.' );
await notes
.getByRole( 'button', { name: 'Add note', exact: true } )
.click();
await expect(
notes.getByRole( 'treeitem', { name: 'Note: Original note.' } )
).toBeVisible();

await expect
.poll( getMailLog, {
message: 'should email the post author about the new note',
} )
.toHaveLength( 1 );
} );

await test.step( 'Edit the note', async () => {
await notes.getByRole( 'button', { name: 'Actions' } ).click();
await page
.getByRole( 'menuitem', { name: 'Edit', exact: true } )
.click();
await page
.getByRole( 'textbox', { name: 'Edit note' } )
.fill( 'Edited note.' );
await notes
.getByRole( 'button', { name: 'Update', exact: true } )
.click();
await expect( notice( 'Note updated.' ) ).toBeVisible();
} );

// The REST update has returned, so any email it sent is already logged.
expect(
await getMailLog(),
'should not email the post author again when the note is edited'
).toHaveLength( 1 );
} );
} );
110 changes: 110 additions & 0 deletions tests/phpunit/tests/comment/wpNewCommentViaRestNotifyPostauthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* Tests for the post author notification sent when a note is saved through the REST API.
*
* @group comment
* @group notes
*
* @covers ::wp_new_comment_via_rest_notify_postauthor
*/
class Tests_Comment_WpNewCommentViaRestNotifyPostauthor extends WP_UnitTestCase {

/**
* Author of the post the notes are attached to.
*/
private static WP_User $post_author;

/**
* A user who writes notes.
*/
private static WP_User $note_author;

/**
* Post the notes are attached to.
*/
private static WP_Post $post;

/**
* Recipients of the wp_mail() calls in the current test.
*
* @var list<string>
*/
private array $sent_to = array();

/**
* Sets up shared fixtures.
*
* @param WP_UnitTest_Factory $factory Factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
self::$post_author = $factory->user->create_and_get( array( 'role' => 'author' ) );
self::$note_author = $factory->user->create_and_get( array( 'role' => 'editor' ) );
self::$post = $factory->post->create_and_get( array( 'post_author' => self::$post_author->ID ) );
}

public function set_up(): void {
parent::set_up();
update_option( 'wp_notes_notify', 1 );
$this->sent_to = array();
add_filter( 'pre_wp_mail', array( $this, 'capture_mail' ), 10, 2 );
}

/**
* Records wp_mail() recipients and short-circuits delivery.
*
* @param null|bool $short_circuit Short-circuit value.
* @param array<string, mixed> $atts wp_mail() arguments.
* @return bool Always true.
*/
public function capture_mail( ?bool $short_circuit, array $atts ): bool {
foreach ( (array) $atts['to'] as $to ) {
if ( is_string( $to ) ) {
$this->sent_to[] = $to;
}
}
return true;
}

/**
* @ticket 66891
*/
public function test_creating_a_note_notifies_the_post_author(): void {
wp_set_current_user( self::$note_author->ID );

$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->set_param( 'post', self::$post->ID );
$request->set_param( 'type', 'note' );
$request->set_param( 'content', 'A new note.' );

$response = rest_get_server()->dispatch( $request );

$this->assertSame( 201, $response->get_status() );
$this->assertSame( array( self::$post_author->user_email ), $this->sent_to );
}

/**
* @ticket 66891
*/
public function test_editing_a_note_does_not_notify_the_post_author_again(): void {
$note_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post->ID,
'comment_type' => 'note',
'comment_approved' => '0',
'user_id' => self::$note_author->ID,
'comment_content' => 'A new note.',
)
);

wp_set_current_user( self::$note_author->ID );

$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id );
$request->set_param( 'content', 'An edited note.' );

$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status() );
$this->assertSame( array(), $this->sent_to );
}
}
Loading