Skip to:
Content

BuddyPress.org

Changeset 11614


Ignore:
Timestamp:
06/23/2017 05:58:21 PM (7 years ago)
Author:
boonebgorges
Message:

Filter WP avatars at get_avatar_url instead of get_avatar.

The get_avatar_url filter, introduced in WP 4.2, is a more general
point for filtering avatar URLs than the legacy get_avatar. The
URL-specific filter allows us to simplify our filter logic, and
ensures that developers fetching avatar data with get_avatar_data()
get BP avatars when appropriate.

Props elhardoum, r-a-y.
Fixes #7424.

Location:
trunk/src
Files:
1 added
2 edited

Legend:

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

    r11392 r11614  
    13621362
    13631363/**
    1364  * Replace default WordPress avatars with BP avatars, if available.
    1365  *
    1366  * See 'get_avatar' filter description in wp-includes/pluggable.php.
    1367  *
    1368  * @since 1.1.0
    1369  * @since 2.4.0 Added $args parameter to coincide with WordPress 4.2.0.
    1370  *
    1371  * @param string            $avatar  The avatar path passed to 'get_avatar'.
    1372  * @param int|string|object $user    A user ID, email address, or comment object.
    1373  * @param int               $size    Size of the avatar image ('thumb' or 'full').
    1374  * @param string            $default URL to a default image to use if no avatar is available.
    1375  * @param string            $alt     Alternate text to use in image tag. Default: ''.
    1376  * @param array             $args    Arguments passed to get_avatar_data(), after processing.
    1377  * @return string BP avatar path, if found; else the original avatar path.
    1378  */
    1379 function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt = '', $args = array() ) {
    1380     global $pagenow;
    1381 
    1382     // Don't filter if inside WordPress options page and force_default is true.
    1383     if ( 'options-discussion.php' === $pagenow && true === $args['force_default'] ) {
    1384         return $avatar;
    1385     }
    1386 
    1387     // If passed an object, assume $user->user_id.
    1388     if ( is_object( $user ) ) {
    1389         if ( isset( $user->user_id ) ) {
    1390             $id = $user->user_id;
    1391         } else {
    1392             $id = $user->ID;
    1393         }
    1394 
    1395     // If passed a number, assume it was a $user_id.
    1396     } elseif ( is_numeric( $user ) ) {
    1397         $id = $user;
    1398 
    1399     // If passed a string and that string returns a user, get the $id.
    1400     } elseif ( is_string( $user ) && ( $user_by_email = get_user_by( 'email', $user ) ) ) {
    1401         $id = $user_by_email->ID;
    1402     }
    1403 
    1404     // If somehow $id hasn't been assigned, return the result of get_avatar.
    1405     if ( empty( $id ) ) {
    1406         return !empty( $avatar ) ? $avatar : $default;
    1407     }
    1408 
    1409     // Image alt tag.
    1410     if ( empty( $alt ) ) {
    1411         $alt = sprintf( __( 'Profile photo of %s', 'buddypress' ), bp_core_get_user_displayname( $id ) );
    1412     }
    1413 
    1414     // Use the 'thumb' type, unless the requested width is bigger than
    1415     // BP's thumb width.
    1416     $type = 'thumb';
    1417     if ( (int) $size > bp_core_avatar_thumb_width() ) {
    1418         $type = 'full';
    1419     }
    1420 
    1421     $avatar_args = array(
    1422         'item_id' => $id,
    1423         'type'    => $type,
    1424         'width'   => $size,
    1425         'height'  => $size,
    1426         'alt'     => $alt,
    1427     );
    1428 
    1429     // Support new arguments as of WordPress 4.2.0.
    1430     if ( ! empty( $args['width'] ) ) {
    1431         $avatar_args['width'] = $args['width'];
    1432     }
    1433     if ( ! empty( $args['height'] ) ) {
    1434         $avatar_args['height'] = $args['height'];
    1435     }
    1436     if ( ! empty( $args['class'] ) ) {
    1437         $avatar_args['class'] = $args['class'];
    1438     }
    1439     if ( ! empty( $args['class'] ) ) {
    1440         $avatar_args['class'] = $args['class'];
    1441     }
    1442     if ( ! empty( $args['extra_attr'] ) ) {
    1443         $avatar_args['extra_attr'] = $args['extra_attr'];
    1444     }
    1445     if ( ! empty( $args['scheme'] ) ) {
    1446         $avatar_args['scheme'] = $args['scheme'];
    1447     }
    1448     if ( ! empty( $args['force_default'] ) ) {
    1449         $avatar_args['force_default'] = $args['force_default'];
    1450     }
    1451     if ( ! empty( $args['rating'] ) ) {
    1452         $avatar_args['rating'] = $args['rating'];
    1453     }
    1454 
    1455     // Let BuddyPress handle the fetching of the avatar.
    1456     $bp_avatar = bp_core_fetch_avatar( $avatar_args );
    1457 
    1458     // If BuddyPress found an avatar, use it. If not, use the result of get_avatar.
    1459     return ( !$bp_avatar ) ? $avatar : $bp_avatar;
    1460 }
    1461 add_filter( 'get_avatar', 'bp_core_fetch_avatar_filter', 10, 6 );
     1364 * Filter {@link get_avatar_url()} to use the BuddyPress user avatar URL.
     1365 *
     1366 * @since 2.9.0
     1367 *
     1368 * @param  string $retval      The URL of the avatar.
     1369 * @param  mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
     1370 *                             user email, WP_User object, WP_Post object, or WP_Comment object.
     1371 * @param  array  $args        Arguments passed to get_avatar_data(), after processing.
     1372 * @return string
     1373 */
     1374function bp_core_get_avatar_data_url_filter( $retval, $id_or_email, $args ) {
     1375    $user = null;
     1376
     1377    // Ugh, hate duplicating code; process the user identifier.
     1378    if ( is_numeric( $id_or_email ) ) {
     1379        $user = get_user_by( 'id', absint( $id_or_email ) );
     1380    } elseif ( $id_or_email instanceof WP_User ) {
     1381        // User Object
     1382        $user = $id_or_email;
     1383    } elseif ( $id_or_email instanceof WP_Post ) {
     1384        // Post Object
     1385        $user = get_user_by( 'id', (int) $id_or_email->post_author );
     1386    } elseif ( $id_or_email instanceof WP_Comment && ! empty( $id_or_email->user_id ) ) {
     1387        $user = get_user_by( 'id', (int) $id_or_email->user_id );
     1388    }
     1389
     1390    // No user, so bail.
     1391    if ( null === $user ) {
     1392        return $retval;
     1393    }
     1394
     1395    // Set BuddyPress-specific avatar args.
     1396    $args['item_id'] = $user->ID;
     1397    $args['html']    = false;
     1398
     1399    // Get the BuddyPress avatar URL.
     1400    if ( $bp_avatar = bp_core_fetch_avatar( $args ) ) {
     1401        return $bp_avatar;
     1402    }
     1403
     1404    return $retval;
     1405}
     1406add_filter( 'get_avatar_url', 'bp_core_get_avatar_data_url_filter', 10, 3 );
    14621407
    14631408/**
  • trunk/src/class-buddypress.php

    r11593 r11614  
    478478            require( $this->plugin_dir . 'bp-core/deprecated/2.6.php' );
    479479            require( $this->plugin_dir . 'bp-core/deprecated/2.7.php' );
     480            require( $this->plugin_dir . 'bp-core/deprecated/2.8.php' );
     481            require( $this->plugin_dir . 'bp-core/deprecated/2.9.php' );
    480482        }
    481483    }
Note: See TracChangeset for help on using the changeset viewer.