diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index cda3444af8..0e746becb0 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -3937,6 +3937,29 @@ function get_avatar_url( $id_or_email, $args = null ) { return $args['url']; } + +/** + * Check if this comment type allows avatars to be retrieved. + * + * @since 5.1.0 + * + * @param string $comment_type Comment type to check. + * @return bool Whether the comment type is allowed for retrieving avatars. + */ +function is_avatar_comment_type( $comment_type ) { + /** + * Filters the list of allowed comment types for retrieving avatars. + * + * @since 3.0.0 + * + * @param array $types An array of content types. Default only contains 'comment'. + */ + $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); + + return in_array( $comment_type, (array) $allowed_comment_types, true ); +} + + /** * Retrieves default data about the avatar. * @@ -4082,15 +4105,7 @@ function get_avatar_data( $id_or_email, $args = null ) { // Post Object $user = get_user_by( 'id', (int) $id_or_email->post_author ); } elseif ( $id_or_email instanceof WP_Comment ) { - /** - * Filters the list of allowed comment types for retrieving avatars. - * - * @since 3.0.0 - * - * @param array $types An array of content types. Default only contains 'comment'. - */ - $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); - if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { + if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) { $args['url'] = false; /** This filter is documented in wp-includes/link-template.php */ return apply_filters( 'get_avatar_data', $args, $id_or_email ); diff --git a/tests/phpunit/tests/avatar.php b/tests/phpunit/tests/avatar.php index b2a767f94f..3722f6ec1f 100644 --- a/tests/phpunit/tests/avatar.php +++ b/tests/phpunit/tests/avatar.php @@ -240,4 +240,44 @@ class Tests_Avatar extends WP_UnitTestCase { return $this->fakeURL; } + /** + * The `get_avatar_data()` function should return gravatar url when comment type allowed to retrieve avatars. + * + * @ticket 44033 + */ + public function test_get_avatar_data_should_return_gravatar_url_when_input_avatar_comment_type() { + $comment_type = 'comment'; + $comment = self::factory()->comment->create_and_get( + array( + 'comment_author_email' => 'commenter@example.com', + 'comment_type' => $comment_type, + ) + ); + + $actual_data = get_avatar_data( $comment ); + + $this->assertTrue( is_avatar_comment_type( $comment_type ) ); + $this->assertRegexp( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] ); + } + + /** + * The `get_avatar_data()` function should return invalid url when comment type not allowed to retrieve avatars. + * + * @ticket 44033 + */ + public function test_get_avatar_data_should_return_invalid_url_when_input_not_avatar_comment_type() { + $comment_type = 'review'; + $comment = self::factory()->comment->create_and_get( + array( + 'comment_author_email' => 'commenter@example.com', + 'comment_type' => $comment_type, + ) + ); + + $actual_data = get_avatar_data( $comment ); + + $this->assertFalse( is_avatar_comment_type( $comment_type ) ); + $this->assertFalse( $actual_data['url'] ); + } + }