diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 8a48108effa43..2f0f42be6b652 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -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 ); } diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 3c1234f9cfa8e..f81e59b429b11 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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' ); diff --git a/tests/e2e/specs/notes-notifications.test.js b/tests/e2e/specs/notes-notifications.test.js new file mode 100644 index 0000000000000..79b8a0ab37260 --- /dev/null +++ b/tests/e2e/specs/notes-notifications.test.js @@ -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', + ` + ( await siteOptions.get( [ mailLog ] ) )[ mailLog ] || []; + + const post = await postFactory.create( { + title: postFactory.uniqueTitle( 'Post with a note' ), + content: + '

Note target.

', + 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 ); + } ); +} ); diff --git a/tests/phpunit/tests/comment/wpNewCommentViaRestNotifyPostauthor.php b/tests/phpunit/tests/comment/wpNewCommentViaRestNotifyPostauthor.php new file mode 100644 index 0000000000000..ed6c5e3dd8012 --- /dev/null +++ b/tests/phpunit/tests/comment/wpNewCommentViaRestNotifyPostauthor.php @@ -0,0 +1,110 @@ + + */ + 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 $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 ); + } +}