Make WordPress Core

Changeset 44499


Ignore:
Timestamp:
01/09/2019 05:59:49 AM (6 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r44351 r44499  
    39383938}
    39393939
     3940
     3941/**
     3942 * Check if this comment type allows avatars to be retrieved.
     3943 *
     3944 * @since 5.1.0
     3945 *
     3946 * @param string $comment_type Comment type to check.
     3947 * @return bool Whether the comment type is allowed for retrieving avatars.
     3948 */
     3949function is_avatar_comment_type( $comment_type ) {
     3950    /**
     3951     * Filters the list of allowed comment types for retrieving avatars.
     3952     *
     3953     * @since 3.0.0
     3954     *
     3955     * @param array $types An array of content types. Default only contains 'comment'.
     3956     */
     3957    $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
     3958
     3959    return in_array( $comment_type, (array) $allowed_comment_types, true );
     3960}
     3961
     3962
    39403963/**
    39413964 * Retrieves default data about the avatar.
     
    40834106        $user = get_user_by( 'id', (int) $id_or_email->post_author );
    40844107    } elseif ( $id_or_email instanceof WP_Comment ) {
    4085         /**
    4086          * Filters the list of allowed comment types for retrieving avatars.
    4087          *
    4088          * @since 3.0.0
    4089          *
    4090          * @param array $types An array of content types. Default only contains 'comment'.
    4091          */
    4092         $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
    4093         if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
     4108        if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) {
    40944109            $args['url'] = false;
    40954110            /** This filter is documented in wp-includes/link-template.php */
  • trunk/tests/phpunit/tests/avatar.php

    r42343 r44499  
    241241    }
    242242
     243    /**
     244     * The `get_avatar_data()` function should return gravatar url when comment type allowed to retrieve avatars.
     245     *
     246     * @ticket 44033
     247     */
     248    public function test_get_avatar_data_should_return_gravatar_url_when_input_avatar_comment_type() {
     249        $comment_type = 'comment';
     250        $comment      = self::factory()->comment->create_and_get(
     251            array(
     252                'comment_author_email' => 'commenter@example.com',
     253                'comment_type'         => $comment_type,
     254            )
     255        );
     256
     257        $actual_data = get_avatar_data( $comment );
     258
     259        $this->assertTrue( is_avatar_comment_type( $comment_type ) );
     260        $this->assertRegexp( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
     261    }
     262
     263    /**
     264     * The `get_avatar_data()` function should return invalid url when comment type not allowed to retrieve avatars.
     265     *
     266     * @ticket 44033
     267     */
     268    public function test_get_avatar_data_should_return_invalid_url_when_input_not_avatar_comment_type() {
     269        $comment_type = 'review';
     270        $comment      = self::factory()->comment->create_and_get(
     271            array(
     272                'comment_author_email' => 'commenter@example.com',
     273                'comment_type'         => $comment_type,
     274            )
     275        );
     276
     277        $actual_data = get_avatar_data( $comment );
     278
     279        $this->assertFalse( is_avatar_comment_type( $comment_type ) );
     280        $this->assertFalse( $actual_data['url'] );
     281    }
     282
    243283}
Note: See TracChangeset for help on using the changeset viewer.