Skip to:
Content

BuddyPress.org

Changeset 3086


Ignore:
Timestamp:
06/27/2010 05:47:40 AM (16 years ago)
Author:
johnjamesjacoby
Message:

Fixes #2436 and adds some documentation to bp_core_fetch_avatar

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.2/bp-core/bp-core-avatars.php

    r3081 r3086  
    4747add_action( 'bp_init', 'bp_core_set_avatar_constants' );
    4848
     49/**
     50 * bp_core_fetch_avatar()
     51 *
     52 * Fetches an avatar from a BuddyPress object. Supports user/group/blog as
     53 * default, but can be extended to include your own custom components too.
     54 *
     55 * @global object $bp
     56 * @global object $current_blog
     57 * @param array $args Determine the output of this function
     58 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg
     59 */
    4960function bp_core_fetch_avatar( $args = '' ) {
    5061        global $bp, $current_blog;
    5162
     63        // Set a few default variables
     64        $object         = 'user';
     65        $type           = 'thumb';
     66        $class          = 'avatar';
     67        $alt            = __( 'Avatar Image', 'buddypress' );
     68
     69        // Set the default variables array
    5270        $defaults = array(
    53                 'item_id' => false,
    54                 'object' => 'user', // user OR group OR blog OR custom type (if you use filters)
    55                 'type' => 'thumb',
    56                 'avatar_dir' => false,
    57                 'width' => false,
    58                 'height' => false,
    59                 'class' => 'avatar',
    60                 'css_id' => false,
    61                 'alt' => __( 'Avatar Image', 'buddypress' ),
    62                 'email' => false, // Pass the user email (for gravatar) to prevent querying the DB for it
    63                 'no_grav' => false // If there is no avatar found, return false instead of a grav?
     71                'item_id'               => false,
     72                'object'                => $object,     // user/group/blog/custom type (if you use filters)
     73                'type'                  => $type,       // thumb or full
     74                'avatar_dir'    => false,       // Specify a custom avatar directory for your object
     75                'width'                 => false,       // Custom width (int)
     76                'height'                => false,       // Custom height (int)
     77                'class'                 => $class,      // Custom <img> class (string)
     78                'css_id'                => false,       // Custom <img> ID (string)
     79                'alt'                   => $alt,        // Custom <img> alt (string)
     80                'email'                 => false,       // Pass the user email (for gravatar) to prevent querying the DB for it
     81                'no_grav'               => false,       // If there is no avatar found, return false instead of a grav?
     82                'html'                  => true         // Wrap the return img URL in <img />
    6483        );
    6584
     85        // Compare defaults to passed and extract
    6686        $params = wp_parse_args( $args, $defaults );
    6787        extract( $params, EXTR_SKIP );
    6888
     89        // Set item_id if not passed
    6990        if ( !$item_id ) {
    7091                if ( 'user' == $object )
     
    80101        }
    81102
     103        // Set avatar_dir if not passed (uses $object)
    82104        if ( !$avatar_dir ) {
    83105                if ( 'user' == $object )
     
    93115        }
    94116
    95         /* Add an identifying class to each item */
     117        // Add an identifying class to each item
    96118        $class .= ' ' . $object . '-' . $item_id . '-avatar';
    97119
    98         if ( !empty($css_id) )
     120        // Set CSS ID if passed
     121        if ( !empty( $css_id ) )
    99122                $css_id = " id='{$css_id}'";
    100123
     124        // Set avatar width
    101125        if ( $width )
    102126                $html_width = " width='{$width}'";
     
    104128                $html_width = ( 'thumb' == $type ) ? ' width="' . BP_AVATAR_THUMB_WIDTH . '"' : ' width="' . BP_AVATAR_FULL_WIDTH . '"';
    105129
     130        // Set avatar height
    106131        if ( $height )
    107132                $html_height = " height='{$height}'";
     
    109134                $html_height = ( 'thumb' == $type ) ? ' height="' . BP_AVATAR_THUMB_HEIGHT . '"' : ' height="' . BP_AVATAR_FULL_HEIGHT . '"';
    110135
     136        // Set avatar URL and DIR based on prepopulated constants
    111137        $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', BP_AVATAR_URL . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
    112138        $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
     
    165191                closedir( $av_dir );
    166192
    167                 // If we found an avatar, return it wrapped in an img element
    168                 if ( $avatar_url )
    169                         return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
    170 
     193                // If we found a locally uploaded avatar
     194                if ( $avatar_url ) {
     195
     196                        // Return it wrapped in an <img> element
     197                        if ( true === $html ) {
     198                                return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
     199
     200                        // ...or only the URL
     201                        } else {
     202                                return apply_filters( 'bp_core_fetch_avatar_url', $avatar_url );
     203                        }
     204                }
    171205        }
    172206
     
    211245                $gravatar       = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $email ) ) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
    212246
    213                 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
     247                // Return gravatar wrapped in <img />
     248                if ( true === $html )
     249                        return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
     250
     251                // ...or only return the gravatar URL
     252                else
     253                        return apply_filters( 'bp_core_fetch_avatar_url', $gravatar );
     254
    214255        } else {
    215256                return apply_filters( 'bp_core_fetch_avatar', false, $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
Note: See TracChangeset for help on using the changeset viewer.