Ticket #10653: 10653.diff
File 10653.diff, 4.4 KB (added by , 7 years ago) |
---|
-
src/wp-includes/comment-template.php
13 13 * 14 14 * If the comment has an empty comment_author field, then 'Anonymous' person is 15 15 * assumed. 16 * If the commenter name has changed, both the new one and the old one are shown. 16 17 * 17 18 * @since 1.5.0 18 19 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. 20 * @since 5.0.0 Added `$before_alias` and `$after_alias` parameters to customize former commenter name. 19 21 * 20 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to retrieve the author. 21 * Default current comment. 22 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to retrieve the author. 23 * Default current comment. 24 * @param string $before_alias The custom text before the former author name. 25 * @param string $after_alias The custom text after the former author name. 22 26 * @return string The comment author 23 27 */ 24 function get_comment_author( $comment_ID = 0 ) {28 function get_comment_author( $comment_ID = 0, $before_alias = null, $after_alias = null ) { 25 29 $comment = get_comment( $comment_ID ); 26 30 27 if ( empty( $comment->comment_author ) ) { 28 if ( $comment->user_id && $user = get_userdata( $comment->user_id ) ) { 29 $author = $user->display_name; 30 } else { 31 $author = __( 'Anonymous' ); 32 } 33 } else { 31 $author = __( 'Anonymous' ); 32 $user = $comment->user_id ? get_userdata( $comment->user_id ) : null; 33 34 if ( $comment->comment_author ) { 34 35 $author = $comment->comment_author; 36 37 if ( $user && $author != $user->display_name ) { 38 if ( is_null( $before_alias ) ) { 39 $before_alias = __(' (Initially posted by ' ); 40 } 41 42 if ( is_null( $after_alias ) ) { 43 $after_alias = __(')'); 44 } 45 46 $author = $user->display_name . $before_alias . $author . $after_alias; 47 } 48 } elseif ( $user ) { 49 $author = $user->display_name; 35 50 } 36 51 37 52 /** -
tests/phpunit/tests/comment/getCommentAuthor.php
1 <?php 2 3 /** 4 * @group comment 5 */ 6 class Tests_Comment_GetCommentAuthor extends WP_UnitTestCase { 7 8 public function test_comment_author_with_name() { 9 $comment = self::factory()->comment->create_and_get( 10 array( 11 'comment_author' => 'Commenter Name', 12 ) 13 ); 14 15 $this->assertEquals( 'Commenter Name', get_comment_author( $comment ) ); 16 } 17 18 public function test_comment_author_with_user_id_and_no_author() { 19 $user_id = self::factory()->user->create( 20 array( 21 'display_name' => 'Commenter Name', 22 ) 23 ); 24 $comment = self::factory()->comment->create_and_get( 25 array( 26 'user_id' => $user_id, 27 'comment_author' => '', 28 ) 29 ); 30 31 $this->assertEquals( 'Commenter Name', get_comment_author( $comment ) ); 32 } 33 34 public function test_comment_author_with_no_user_id_and_no_author() { 35 $comment = self::factory()->comment->create_and_get( 36 array( 37 'user_id' => '', 38 'comment_author' => '', 39 ) 40 ); 41 42 $this->assertEquals( 'Anonymous', get_comment_author( $comment ) ); 43 } 44 45 /** 46 * @ticket 10653 47 */ 48 public function test_comment_author_with_name_different_from_user_name() { 49 $user_id = self::factory()->user->create( 50 array( 51 'display_name' => 'Commenter Name', 52 ) 53 ); 54 $comment = self::factory()->comment->create_and_get( 55 array( 56 'user_id' => $user_id, 57 'comment_author' => 'Old Commenter Name', 58 ) 59 ); 60 61 $this->assertEquals( 'Commenter Name (Initially posted by Old Commenter Name)', get_comment_author( $comment ) ); 62 } 63 64 public function test_comment_author_with_name_different_from_user_name_with_custom_before_after() { 65 $user_id = self::factory()->user->create( 66 array( 67 'display_name' => 'Commenter Name', 68 ) 69 ); 70 $comment = self::factory()->comment->create_and_get( 71 array( 72 'user_id' => $user_id, 73 'comment_author' => 'Old Commenter Name', 74 ) 75 ); 76 77 $this->assertEquals( 'Commenter Name AKA Old Commenter Name', get_comment_author( $comment, ' AKA ', '' ) ); 78 } 79 80 }