Comments: Add a new is_avatar_comment_type() function.

This function splits the `get_avatar_comment_types` filter out of `get_avatar_data()`.

Props dshanske, birgire.
Fixes #44033.



git-svn-id: https://develop.svn.wordpress.org/trunk@44499 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2019-01-09 05:59:49 +00:00
parent 943dcbbcef
commit e5bdc271cb
2 changed files with 64 additions and 9 deletions

View File

@ -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 );

View File

@ -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'] );
}
}