Make WordPress Core

Ticket #44033: 44033.2.diff

File 44033.2.diff, 4.1 KB (added by birgire, 7 years ago)
  • src/wp-includes/link-template.php

    diff --git src/wp-includes/link-template.php src/wp-includes/link-template.php
    index 815c539..b3a5d78 100644
    function get_avatar_url( $id_or_email, $args = null ) { 
    39273927        return $args['url'];
    39283928}
    39293929
     3930
     3931/**
     3932 * Is this an allowed comment type for retrieving avatars.
     3933 *
     3934 * @since 4.9.7
     3935 *
     3936 * @param string $comment_type Comment Type
     3937 *
     3938 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
     3939 */
     3940function is_avatar_comment_type( $comment_type ) {
     3941        /**
     3942         * Filters the list of allowed comment types for retrieving avatars.
     3943         *
     3944         * @since 3.0.0
     3945         *
     3946         * @param array $types An array of content types. Default only contains 'comment'.
     3947         */
     3948        $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
     3949
     3950        return in_array( $comment_type, (array) $allowed_comment_types, true );
     3951}
     3952
     3953
    39303954/**
    39313955 * Retrieves default data about the avatar.
    39323956 *
    function get_avatar_data( $id_or_email, $args = null ) { 
    40714095                // Post Object
    40724096                $user = get_user_by( 'id', (int) $id_or_email->post_author );
    40734097        } elseif ( $id_or_email instanceof WP_Comment ) {
    4074                 /**
    4075                  * Filters the list of allowed comment types for retrieving avatars.
    4076                  *
    4077                  * @since 3.0.0
    4078                  *
    4079                  * @param array $types An array of content types. Default only contains 'comment'.
    4080                  */
    4081                 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
    4082                 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
     4098                if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) {
    40834099                        $args['url'] = false;
    40844100                        /** This filter is documented in wp-includes/link-template.php */
    40854101                        return apply_filters( 'get_avatar_data', $args, $id_or_email );
  • new file tests/phpunit/tests/comment/isAvatarCommentType.php

    diff --git tests/phpunit/tests/comment/isAvatarCommentType.php tests/phpunit/tests/comment/isAvatarCommentType.php
    new file mode 100644
    index 0000000..d1a37ef
    - +  
     1<?php
     2/**
     3 * Test cases for the `is_avatar_comment_type()` function.
     4 *
     5 * @package WordPress\UnitTests
     6 *
     7 * @since 4.9.7
     8 */
     9
     10/**
     11 * Tests_Comment_IsAvatarCommentType class.
     12 *
     13 * @group comment
     14 * @covers is_avatar_comment_type
     15 *
     16 * @since 4.9.7
     17 */
     18class Tests_Comment_IsAvatarCommentType extends WP_UnitTestCase {
     19        /**
     20         * Test the `is_avatar_comment_type()` function.
     21         *
     22         * @since 4.9.7
     23         *
     24         * @dataProvider data_is_avatar_comment_type
     25         */
     26        public function test_function( $comment_type, $expected ) {
     27                $this->assertSame( $expected, is_avatar_comment_type( $comment_type ) );
     28        }
     29
     30        /**
     31         * Dataprovider for `is_avatar_comment_type()`.
     32         *
     33         * @since 4.9.7
     34         *
     35         * @return array {
     36         *     @type array {
     37         *         @type string Comment type.
     38         *         @type bool   Expected values.
     39         *     }
     40         * }
     41         */
     42        public function data_is_avatar_comment_type() {
     43                return array(
     44                        array( null, false ),
     45                        array( '', false ),
     46                        array( 'non-existing-comment-type', false ),
     47                        array( 'comment', true ),
     48                );
     49        }
     50
     51        /**
     52         * The function should be filterable with the `get_avatar_comment_types` filter.
     53         *
     54         * @since 4.9.7
     55         */
     56        public function test_function_should_be_filterable() {
     57                $this->assertFalse( is_avatar_comment_type( 'review' ) );
     58
     59                add_filter( 'get_avatar_comment_types', array( $this, '_filter_avatar_comment_types' ) );
     60                $actual_comment = is_avatar_comment_type( 'comment' );
     61                $actual_review  = is_avatar_comment_type( 'review' );
     62                remove_filter( 'get_avatar_comment_types', array( $this, '_filter_avatar_comment_types' ) );
     63
     64                $this->assertTrue( $actual_comment );
     65                $this->assertTrue( $actual_review );
     66        }
     67
     68        /**
     69         * Filters callback that modifies the list of allowed comment types for retrieving avatars.
     70         *
     71         * @since 4.9.7
     72         *
     73         * @param  array $types An array of content types.
     74         * @return array $types An array of content types.
     75         */
     76        public function _filter_avatar_comment_types( $types ) {
     77                $types[] = 'review';
     78                return $types;
     79        }
     80
     81}