Skip to:
Content

BuddyPress.org

Changeset 10287


Ignore:
Timestamp:
10/19/2015 08:09:00 PM (6 years ago)
Author:
r-a-y
Message:

Avatars: Support new parameters from WordPress 4.2.0 when filtering 'get_avatar'.

WordPress 4.2.0 added a new parameter - $args - to their 'get_avatar'
filter. This $args parameter is an array that houses additional options
including the width, height and CSS class, as well as the following
parameters:

  • 'extra_attr' - unsanitized HTML attributes to add to the <img> element.
  • 'scheme' - URL scheme to use.
  • 'rating' - Gravatar rating.
  • 'force_default' - Only applicable to Gravatars. Always use the default image even if a valid Gravatar exists.

Consequently, bp_core_fetch_avatar() now supports the listed options
above as well.

Fixes #6622.

File:
1 edited

Legend:

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

    r10206 r10287  
    103103 * locally:
    104104 *    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
     105 *
     106 * @since 2.4.0 Added 'extra_attr', 'scheme', 'rating' and 'force_default' for $args.
     107 *              These are inherited from WordPress 4.2.0. See {@link get_avatar()}.
    105108 *
    106109 * @param array|string $args {
     
    173176 *                                   to an avatar. If false, <img>-specific arguments (like 'css_id')
    174177 *                                   will be ignored. Default: true.
     178 *     @type string     $extra_attr  HTML attributes to insert in the IMG element. Not sanitized. Default: ''.
     179 *     @type string     $scheme      URL scheme to use. See set_url_scheme() for accepted values.
     180 *                                   Default null.
     181 *     @type string     $rating      What rating to display Gravatars for. Accepts 'G', 'PG', 'R', 'X'.
     182 *                                   Default is the value of the 'avatar_rating' option.
     183 *     @type bool       $force_default Used when creating the Gravatar URL. Whether to force the default
     184 *                                     image regardless if the Gravatar exists. Default: false.
    175185 * }
    176186 *
     
    189199    // Set the default variables array and parse it against incoming $args array.
    190200    $params = wp_parse_args( $args, array(
    191         'item_id'    => false,
    192         'object'     => 'user',
    193         'type'       => 'thumb',
    194         'avatar_dir' => false,
    195         'width'      => false,
    196         'height'     => false,
    197         'class'      => 'avatar',
    198         'css_id'     => false,
    199         'alt'        => '',
    200         'email'      => false,
    201         'no_grav'    => false,
    202         'html'       => true,
    203         'title'      => '',
     201        'item_id'       => false,
     202        'object'        => 'user',
     203        'type'          => 'thumb',
     204        'avatar_dir'    => false,
     205        'width'         => false,
     206        'height'        => false,
     207        'class'         => 'avatar',
     208        'css_id'        => false,
     209        'alt'           => '',
     210        'email'         => false,
     211        'no_grav'       => false,
     212        'html'          => true,
     213        'title'         => '',
     214        'extra_attr'    => '',
     215        'scheme'        => null,
     216        'rating'        => get_option( 'avatar_rating' ),
     217        'force_default' => false,
    204218    ) );
    205219
     
    347361    }
    348362
     363    // Extra attributes
     364    $extra_attr = ! empty( $args['extra_attr'] ) ? ' ' . $args['extra_attr'] : '';
     365
    349366    // Set CSS ID and create html string.
    350367    $html_css_id = '';
     
    506523        // If we found a locally uploaded avatar
    507524        if ( isset( $avatar_url ) ) {
     525            // Support custom scheme
     526            $avatar_url = set_url_scheme( $avatar_url, $params['scheme'] );
    508527
    509528            // Return it wrapped in an <img> element
     
    525544                 * @param string $avatar_folder_dir Avatar dir path.
    526545                 */
    527                 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '"' . $html_class . $html_css_id  . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
     546                return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '"' . $html_class . $html_css_id  . $html_width . $html_height . $html_alt . $html_title . $extra_attr . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
    528547
    529548            // ...or only the URL
     
    580599        }
    581600
    582         $host = '//www.gravatar.com/avatar/';
    583 
    584601        /**
    585602         * Filters the Gravatar email to use.
     
    594611
    595612        /**
    596          * Filters the Gravatar URL path.
     613         * Filters the Gravatar URL host.
    597614         *
    598615         * @since 1.0.2
    599616         *
    600          * @param string $value Gravatar URL path.
     617         * @param string $value Gravatar URL host.
    601618         */
    602         $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $params['email'] ) ) . '?d=' . $default_grav . '&amp;s=' . $params['width'];
    603 
    604         // Gravatar rating; http://bit.ly/89QxZA
    605         $rating = get_option( 'avatar_rating' );
    606         if ( ! empty( $rating ) ) {
    607             $gravatar .= "&amp;r={$rating}";
    608         }
     619        $gravatar = apply_filters( 'bp_gravatar_url', '//www.gravatar.com/avatar/' );
     620
     621        // Append email hash to Gravatar
     622        $gravatar .=  md5( strtolower( $params['email'] ) );
     623
     624        // Main Gravatar URL args
     625        $url_args = array(
     626            'd' => $default_grav,
     627            's' => $params['width']
     628        );
     629
     630        // Custom Gravatar URL args
     631        if ( ! empty( $params['force_default'] ) ) {
     632            $url_args['f'] = 'y';
     633        }
     634        if ( ! empty( $params['rating'] ) ) {
     635            $url_args['r'] = strtolower( $params['rating'] );
     636        }
     637
     638        // Set up the Gravatar URL
     639        $gravatar = esc_url( add_query_arg(
     640            rawurlencode_deep( array_filter( $url_args ) ),
     641            $gravatar
     642        ) );
    609643
    610644    // No avatar was found, and we've been told not to use a gravatar.
     
    627661
    628662        /** This filter is documented in bp-core/bp-core-avatars.php */
    629         return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '"' . $html_css_id . $html_class . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
     663        return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '"' . $html_css_id . $html_class . $html_width . $html_height . $html_alt . $html_title . $extra_attr . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
    630664    } else {
    631665
     
    12821316 * Replace default WordPress avatars with BP avatars, if available.
    12831317 *
    1284  * Filters 'get_avatar'.
     1318 * See 'get_avatar' filter description in wp-includes/pluggable.php.
     1319 *
     1320 * @since 2.4.0 Added $args parameter to coincide with WordPress 4.2.0.
    12851321 *
    12861322 * @param string            $avatar  The avatar path passed to 'get_avatar'.
     
    12891325 * @param string            $default URL to a default image to use if no avatar is available.
    12901326 * @param string            $alt     Alternate text to use in image tag. Default: ''.
     1327 * @param array             $args    Arguments passed to get_avatar_data(), after processing.
    12911328 *
    12921329 * @return string BP avatar path, if found; else the original avatar path.
    12931330 */
    1294 function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt = '' ) {
     1331function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt = '', $args = array() ) {
    12951332    global $pagenow;
    12961333
     
    13331370    }
    13341371
    1335     // Let BuddyPress handle the fetching of the avatar
    1336     $bp_avatar = bp_core_fetch_avatar( array(
     1372    $avatar_args = array(
    13371373        'item_id' => $id,
    13381374        'type'    => $type,
     
    13401376        'height'  => $size,
    13411377        'alt'     => $alt,
    1342     ) );
     1378    );
     1379
     1380    // Support new arguments as of WordPress 4.2.0
     1381    if ( ! empty( $args['width'] ) ) {
     1382        $avatar_args['width'] = $args['width'];
     1383    }
     1384    if ( ! empty( $args['height'] ) ) {
     1385        $avatar_args['height'] = $args['height'];
     1386    }
     1387    if ( ! empty( $args['class'] ) ) {
     1388        $avatar_args['class'] = $args['class'];
     1389    }
     1390    if ( ! empty( $args['class'] ) ) {
     1391        $avatar_args['class'] = $args['class'];
     1392    }
     1393    if ( ! empty( $args['extra_attr'] ) ) {
     1394        $avatar_args['extra_attr'] = $args['extra_attr'];
     1395    }
     1396    if ( ! empty( $args['scheme'] ) ) {
     1397        $avatar_args['scheme'] = $args['scheme'];
     1398    }
     1399    if ( ! empty( $args['force_default'] ) ) {
     1400        $avatar_args['force_default'] = $args['force_default'];
     1401    }
     1402    if ( ! empty( $args['rating'] ) ) {
     1403        $avatar_args['rating'] = $args['rating'];
     1404    }
     1405
     1406    // Let BuddyPress handle the fetching of the avatar
     1407    $bp_avatar = bp_core_fetch_avatar( $avatar_args );
    13431408
    13441409    // If BuddyPress found an avatar, use it. If not, use the result of get_avatar
    13451410    return ( !$bp_avatar ) ? $avatar : $bp_avatar;
    13461411}
    1347 add_filter( 'get_avatar', 'bp_core_fetch_avatar_filter', 10, 5 );
     1412add_filter( 'get_avatar', 'bp_core_fetch_avatar_filter', 10, 6 );
    13481413
    13491414/**
Note: See TracChangeset for help on using the changeset viewer.