mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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:
parent
943dcbbcef
commit
e5bdc271cb
@ -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 );
|
||||
|
||||
@ -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'] );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user