Skip to:
Content

BuddyPress.org

Changeset 9381


Ignore:
Timestamp:
01/19/2015 11:41:33 PM (7 years ago)
Author:
imath
Message:

Allow multiple classes to be passed to the class argument of bp_core_fetch_avatar()

Using an array of classes or a list of classes separated by a space for the class argument of bp_core_fetch_avatar() will output each one of them into the class attribute of the image tag.

Fixes #6069

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-avatars.php

    r9351 r9381  
    335335    // Create CSS class html string
    336336    $params['class'] = apply_filters( 'bp_core_avatar_class', $params['class'], $params['item_id'], $params['object'], $params );
    337     $html_class = ' class="' . sanitize_html_class( $params['class'] ) . ' ' . sanitize_html_class( $params['object'] . '-' . $params['item_id'] . '-avatar' ) . ' ' . sanitize_html_class( 'avatar-' . $params['width'] ) . ' photo"';
     337
     338    // Use an alias to leave the param unchanged
     339    $avatar_classes = $params['class'];
     340    if ( ! is_array( $avatar_classes ) ) {
     341        $avatar_classes = explode( ' ', $avatar_classes );
     342    }
     343
     344    // merge classes
     345    $avatar_classes = array_merge( $avatar_classes, array(
     346        $params['object'] . '-' . $params['item_id'] . '-avatar',
     347        'avatar-' . $params['width'],
     348    ) );
     349
     350    // Sanitize each class
     351    $avatar_classes = array_map( 'sanitize_html_class', $avatar_classes );
     352
     353    // populate the class attribute
     354    $html_class = ' class="' . join( ' ', $avatar_classes ) . ' photo"';
    338355
    339356    // Set img URL and DIR based on prepopulated constants
  • trunk/tests/phpunit/testcases/core/avatars.php

    r9316 r9381  
    179179        $this->assertEquals( $html, $expected_html );
    180180    }
     181
     182    /**
     183     * @group bp_core_fetch_avatar
     184     */
     185    public function test_bp_core_fetch_avatar_class_attribute() {
     186        $u = $this->factory->user->create();
     187
     188        $hw = 100;
     189        $args = array(
     190            'item_id'    => $u,
     191            'object'     => 'user',
     192            'type'       => 'full',
     193            'width'      => $hw,
     194            'height'     => $hw,
     195            'class'      => '',
     196            'no_grav'    => true,
     197            'html'       => true,
     198        );
     199
     200        // Class attribute is empty
     201        $avatar = bp_core_fetch_avatar( $args );
     202        $expected = array( 'avatar', 'user-' . $u . '-avatar', 'avatar-' . $hw );
     203        preg_match( '/class=["\']?([^"\']*)["\' ]/is', $avatar, $matches );
     204        $classes = explode( ' ', $matches[1] );
     205        $this->assertSame( $expected, array_intersect_key( $expected, $classes ) );
     206
     207        // Class attribute is a String
     208        $args['class'] = 'custom-class class-custom';
     209        $avatar = bp_core_fetch_avatar( $args );
     210        $expected = array_merge( explode( ' ', $args['class'] ), array( 'user-' . $u . '-avatar', 'avatar-' . $hw ) );
     211        preg_match( '/class=["\']?([^"\']*)["\' ]/is', $avatar, $matches );
     212        $classes = explode( ' ', $matches[1] );
     213        $this->assertSame( $expected, array_intersect_key( $expected, $classes ) );
     214
     215        // Class attribute is an Array
     216        $args['class'] = array( 'custom-class', 'class-custom' );
     217        $avatar = bp_core_fetch_avatar( $args );
     218        $expected = array_merge( $args['class'], array( 'user-' . $u . '-avatar', 'avatar-' . $hw ) );
     219        preg_match( '/class=["\']?([^"\']*)["\' ]/is', $avatar, $matches );
     220        $classes = explode( ' ', $matches[1] );
     221        $this->assertSame( $expected, array_intersect_key( $expected, $classes ) );
     222    }
    181223}
Note: See TracChangeset for help on using the changeset viewer.