Ticket #33717: 33717.10.diff
| File 33717.10.diff, 8.7 KB (added by , 11 years ago) |
|---|
-
src/wp-includes/class-walker-comment.php
diff --git src/wp-includes/class-walker-comment.php src/wp-includes/class-walker-comment.php index e82ec66..c589c93 100644
class Walker_Comment extends Walker { 270 270 <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em> 271 271 <br /> 272 272 <?php endif; ?> 273 <?php if ( has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_commenter' ) ) : ?> 274 <em class="comment-awaiting-moderation"><?php _e( 'You will receive an email when your comment has been approved.' ) ?></em> 275 <br/> 276 <?php endif; ?> 273 277 274 278 <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>"> 275 279 <?php … … class Walker_Comment extends Walker { 334 338 <?php if ( '0' == $comment->comment_approved ) : ?> 335 339 <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p> 336 340 <?php endif; ?> 341 <?php if ( has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_commenter' ) ) : ?> 342 <p class="comment-awaiting-moderation"><?php _e( 'You will receive an email when it gets approved.' ) ?></p> 343 <?php endif; ?> 337 344 </footer><!-- .comment-meta --> 338 345 339 346 <div class="comment-content"> -
src/wp-includes/comment.php
diff --git src/wp-includes/comment.php src/wp-includes/comment.php index c62a920..6a75abb 100644
function wp_new_comment_notify_postauthor( $comment_ID ) { 1813 1813 } 1814 1814 1815 1815 /** 1816 * Notify a comment author when their comment gets approved. 1817 * 1818 * This notification is only sent once when the comment status 1819 * changes from unapproved to approved. 1820 * 1821 * @since 4.5.0 1822 * 1823 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 1824 * @return bool Whether the email was sent successfully. 1825 */ 1826 function wp_new_comment_notify_comment_author( $comment_id ) { 1827 $comment = get_comment( $comment_id ); 1828 1829 if ( ! $comment ) { 1830 return false; 1831 } 1832 1833 $post = get_post( $comment->comment_post_ID ); 1834 $comment_author = get_user_by( 'email', $comment->comment_author_email ); 1835 1836 1837 if ( ! $post ) { 1838 return false; 1839 } 1840 1841 // The comment was left by the post author. 1842 if ( $comment->user_id === $post->post_author || $comment_author === get_userdata( $post->post_author ) ) { 1843 return false; 1844 } 1845 1846 if ( 1 === intval( get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_sent', true ) ) ) { 1847 return false; 1848 } 1849 1850 if ( empty( $comment->comment_author_email ) ) { 1851 return false; 1852 } 1853 1854 /* 1855 * The blogname option is escaped with esc_html 1856 * on the way into the database in sanitize_option. 1857 * We want to reverse this for the plain text arena of emails. 1858 */ 1859 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 1860 1861 /* translators: 1: blog name, 2: post title */ 1862 $subject = sprintf( __( '[%1$s] Your comment on "%2$s" has been approved' ), $blogname, $post->post_title ); 1863 1864 if ( ! empty( $comment->comment_author ) ) { 1865 /* translators: 1: comment author's name */ 1866 $notify_message = sprintf( __( 'Howdy %s,' ), $comment->comment_author ) . "\r\n\r\n"; 1867 } else { 1868 $notify_message = __( 'Howdy,' ) . "\r\n\r\n"; 1869 } 1870 1871 /* translators: 1: post title */ 1872 $notify_message .= sprintf( __( 'Your comment on the post "%1$s" has been approved.' ), $post->post_title ) . "\r\n\r\n"; 1873 /* translators: 1: comment permalink */ 1874 $notify_message .= sprintf( __( 'View comment: %s' ), get_comment_link( $comment ) ) . "\r\n"; 1875 1876 /** 1877 * Filter the comment approval notification email text. 1878 * 1879 * @since 4.5.0 1880 * 1881 * @param string $notify_message The comment notification email text. 1882 * @param WP_Comment $comment Comment object. 1883 */ 1884 $notify_message = apply_filters( 'comment_approval_notification_text', $notify_message, $comment ); 1885 1886 /** 1887 * Filter the comment approval notification email subject. 1888 * 1889 * @since 4.5.0 1890 * 1891 * @param string $subject The comment notification email subject. 1892 * @param WP_Comment $comment Comment object. 1893 */ 1894 $subject = apply_filters( 'comment_approval_notification_subject', $subject, $comment ); 1895 1896 $sent = wp_mail( $comment->comment_author_email, wp_specialchars_decode( $subject ), $notify_message ); 1897 1898 update_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_sent', intval( $sent ) ); 1899 1900 return $sent; 1901 } 1902 1903 /** 1816 1904 * Sets the status of a comment. 1817 1905 * 1818 1906 * The 'wp_set_comment_status' action is called after the comment is handled. -
src/wp-includes/default-filters.php
diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php index 1faa084..360923d 100644
add_action( 'comment_post', 'wp_new_comment_notify_postauthor' ); 358 358 add_action( 'after_password_reset', 'wp_password_change_notification' ); 359 359 add_action( 'register_new_user', 'wp_send_new_user_notifications' ); 360 360 add_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 ); 361 add_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' ); 361 362 362 363 // REST API actions. 363 364 add_action( 'init', 'rest_api_init' ); -
tests/phpunit/tests/comment.php
diff --git tests/phpunit/tests/comment.php tests/phpunit/tests/comment.php index 844e4d1..2680d69 100644
class Tests_Comment extends WP_UnitTestCase { 407 407 ) ); 408 408 409 409 $comment = $this->factory->comment->create( array( 410 'comment_post_ID' => $post, 410 'comment_post_ID' => $post, 411 'comment_author' => rand_str(), 412 'comment_author_email' => rand_str() . '@example.com', 411 413 ) ); 412 414 413 415 return array( … … class Tests_Comment extends WP_UnitTestCase { 499 501 } 500 502 501 503 /** 504 * @ticket 33717 505 */ 506 public function test_wp_new_comment_notify_comment_author() { 507 $comment_data = $this->setup_notify_comment(); 508 $comment = get_comment( $comment_data['comment'] ); 509 510 $this->assertTrue( $this->try_sending_comment_author_notification( $comment ) ); 511 $this->assertSame( 1, (int) get_comment_meta( $comment->comment_ID, '_wp_commenter_notification_sent', true ) ); 512 } 513 514 /** 515 * @ticket 33717 516 */ 517 public function test_wp_new_comment_notify_comment_author_invalid_comment_id() { 518 $this->assertFalse( wp_new_comment_notify_comment_author( 999 ) ); 519 } 520 521 /** 522 * @ticket 33717 523 */ 524 public function test_wp_new_comment_notify_comment_author_invalid_post_id() { 525 $comment = $this->factory->comment->create( array( 526 'comment_post_ID' => 999, 527 'comment_author' => rand_str(), 528 'comment_author_email' => rand_str() . '@example.com', 529 ) ); 530 531 $this->assertFalse( wp_new_comment_notify_comment_author( $comment ) ); 532 } 533 534 /** 535 * @ticket 33717 536 */ 537 public function test_wp_new_comment_notify_comment_author_has_no_email_address() { 538 $post = $this->factory->post->create( array( 539 'post_author' => self::$user_id, 540 ) ); 541 542 $comment = $this->factory->comment->create( array( 543 'comment_post_ID' => $post, 544 ) ); 545 546 $this->assertFalse( wp_new_comment_notify_comment_author( $comment ) ); 547 } 548 549 /** 550 * @ticket 33717 551 */ 552 public function test_wp_new_comment_notify_comment_author_is_post_author() { 553 $post = $this->factory->post->create( array( 554 'post_author' => self::$user_id, 555 ) ); 556 557 $comment = $this->factory->comment->create( array( 558 'comment_post_ID' => $post, 559 'user_id' => self::$user_id, 560 ) ); 561 562 $this->assertFalse( wp_new_comment_notify_comment_author( $comment ) ); 563 } 564 565 /** 566 * @ticket 33717 567 */ 568 public function test_wp_new_comment_notify_comment_author_multiple_times() { 569 $comment_data = $this->setup_notify_comment(); 570 $comment = get_comment( $comment_data['comment'] ); 571 572 $this->assertTrue($this->try_sending_comment_author_notification( $comment ) ); 573 $this->assertSame( 1, (int) get_comment_meta( $comment->comment_ID, '_wp_commenter_notification_sent', true ) ); 574 $this->assertFalse($this->try_sending_comment_author_notification( $comment ) ); 575 } 576 577 /** 578 * Helper function to test comment author notifications. 579 * 580 * @param WP_Comment $comment 581 * @return bool 582 */ 583 public function try_sending_comment_author_notification( $comment ) { 584 wp_new_comment_notify_comment_author( $comment->comment_ID ); 585 586 if ( isset( $GLOBALS['phpmailer']->mock_sent ) 587 && ! empty( $GLOBALS['phpmailer']->mock_sent ) 588 && $comment->comment_author_email == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] 589 ) { 590 $email_sent = true; 591 } else { 592 $email_sent = false; 593 } 594 unset( $GLOBALS['phpmailer']->mock_sent ); 595 596 return $email_sent; 597 } 598 599 /** 502 600 * Helper function to test moderator notifications. 503 601 * 504 602 * @since 4.4.0
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)