<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[BuddyPress] [3086] branches/1.2/bp-core/bp-core-avatars.php:
Fixes #2436 and adds some documentation to bp_core_fetch_avatar</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>3086</dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2010-06-27 05:47:40 +0000 (Sun, 27 Jun 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Fixes #2436 and adds some documentation to bp_core_fetch_avatar</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches12bpcorebpcoreavatarsphp">branches/1.2/bp-core/bp-core-avatars.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches12bpcorebpcoreavatarsphp"></a>
<div class="modfile"><h4>Modified: branches/1.2/bp-core/bp-core-avatars.php (3085 => 3086)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.2/bp-core/bp-core-avatars.php        2010-06-27 04:50:39 UTC (rev 3085)
+++ branches/1.2/bp-core/bp-core-avatars.php        2010-06-27 05:47:40 UTC (rev 3086)
</span><span class="lines">@@ -46,26 +46,47 @@
</span><span class="cx"> }
</span><span class="cx"> add_action( 'bp_init', 'bp_core_set_avatar_constants' );
</span><span class="cx">
</span><ins>+/**
+ * bp_core_fetch_avatar()
+ *
+ * Fetches an avatar from a BuddyPress object. Supports user/group/blog as
+ * default, but can be extended to include your own custom components too.
+ *
+ * @global object $bp
+ * @global object $current_blog
+ * @param array $args Determine the output of this function
+ * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg
+ */
</ins><span class="cx"> function bp_core_fetch_avatar( $args = '' ) {
</span><span class="cx">         global $bp, $current_blog;
</span><span class="cx">
</span><ins>+        // Set a few default variables
+        $object                = 'user';
+        $type                = 'thumb';
+        $class                = 'avatar';
+        $alt                = __( 'Avatar Image', 'buddypress' );
+
+        // Set the default variables array
</ins><span class="cx">         $defaults = array(
</span><del>-                'item_id' => false,
-                'object' => 'user', // user OR group OR blog OR custom type (if you use filters)
-                'type' => 'thumb',
-                'avatar_dir' => false,
-                'width' => false,
-                'height' => false,
-                'class' => 'avatar',
-                'css_id' => false,
-                'alt' => __( 'Avatar Image', 'buddypress' ),
-                'email' => false, // Pass the user email (for gravatar) to prevent querying the DB for it
-                'no_grav' => false // If there is no avatar found, return false instead of a grav?
</del><ins>+                'item_id'                => false,
+                'object'                => $object,        // user/group/blog/custom type (if you use filters)
+                'type'                        => $type,        // thumb or full
+                'avatar_dir'        => false,        // Specify a custom avatar directory for your object
+                'width'                        => false,        // Custom width (int)
+                'height'                => false,        // Custom height (int)
+                'class'                        => $class,        // Custom <img> class (string)
+                'css_id'                => false,        // Custom <img> ID (string)
+                'alt'                        => $alt,        // Custom <img> alt (string)
+                'email'                        => false,        // Pass the user email (for gravatar) to prevent querying the DB for it
+                'no_grav'                => false,        // If there is no avatar found, return false instead of a grav?
+                'html'                        => true                // Wrap the return img URL in <img />
</ins><span class="cx">         );
</span><span class="cx">
</span><ins>+        // Compare defaults to passed and extract
</ins><span class="cx">         $params = wp_parse_args( $args, $defaults );
</span><span class="cx">         extract( $params, EXTR_SKIP );
</span><span class="cx">
</span><ins>+        // Set item_id if not passed
</ins><span class="cx">         if ( !$item_id ) {
</span><span class="cx">                 if ( 'user' == $object )
</span><span class="cx">                         $item_id = $bp->displayed_user->id;
</span><span class="lines">@@ -79,6 +100,7 @@
</span><span class="cx">                 if ( !$item_id ) return false;
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        // Set avatar_dir if not passed (uses $object)
</ins><span class="cx">         if ( !$avatar_dir ) {
</span><span class="cx">                 if ( 'user' == $object )
</span><span class="cx">                         $avatar_dir = 'avatars';
</span><span class="lines">@@ -92,22 +114,26 @@
</span><span class="cx">                 if ( !$avatar_dir ) return false;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        /* Add an identifying class to each item */
</del><ins>+        // Add an identifying class to each item
</ins><span class="cx">         $class .= ' ' . $object . '-' . $item_id . '-avatar';
</span><span class="cx">
</span><del>-        if ( !empty($css_id) )
</del><ins>+        // Set CSS ID if passed
+        if ( !empty( $css_id ) )
</ins><span class="cx">                 $css_id = " id='{$css_id}'";
</span><span class="cx">
</span><ins>+        // Set avatar width
</ins><span class="cx">         if ( $width )
</span><span class="cx">                 $html_width = " width='{$width}'";
</span><span class="cx">         else
</span><span class="cx">                 $html_width = ( 'thumb' == $type ) ? ' width="' . BP_AVATAR_THUMB_WIDTH . '"' : ' width="' . BP_AVATAR_FULL_WIDTH . '"';
</span><span class="cx">
</span><ins>+        // Set avatar height
</ins><span class="cx">         if ( $height )
</span><span class="cx">                 $html_height = " height='{$height}'";
</span><span class="cx">         else
</span><span class="cx">                 $html_height = ( 'thumb' == $type ) ? ' height="' . BP_AVATAR_THUMB_HEIGHT . '"' : ' height="' . BP_AVATAR_FULL_HEIGHT . '"';
</span><span class="cx">
</span><ins>+        // Set avatar URL and DIR based on prepopulated constants
</ins><span class="cx">         $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', BP_AVATAR_URL . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
</span><span class="cx">         $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
</span><span class="cx">
</span><span class="lines">@@ -164,10 +190,18 @@
</span><span class="cx">                 // Close the avatar directory
</span><span class="cx">                 closedir( $av_dir );
</span><span class="cx">
</span><del>-                // If we found an avatar, return it wrapped in an img element
-                if ( $avatar_url )
-                        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 );
</del><ins>+                // If we found a locally uploaded avatar
+                if ( $avatar_url ) {
</ins><span class="cx">
</span><ins>+                        // Return it wrapped in an <img> element
+                        if ( true === $html ) {
+                                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 );
+
+                        // ...or only the URL
+                        } else {
+                                return apply_filters( 'bp_core_fetch_avatar_url', $avatar_url );
+                        }
+                }
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         // If no avatars could be found, try to display a gravatar
</span><span class="lines">@@ -210,7 +244,14 @@
</span><span class="cx">                 $email                = apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object );
</span><span class="cx">                 $gravatar        = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $email ) ) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
</span><span class="cx">
</span><del>-                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 );
</del><ins>+                // Return gravatar wrapped in <img />
+                if ( true === $html )
+                        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 );
+
+                // ...or only return the gravatar URL
+                else
+                        return apply_filters( 'bp_core_fetch_avatar_url', $gravatar );
+
</ins><span class="cx">         } else {
</span><span class="cx">                 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 );
</span><span class="cx">         }
</span></span></pre>
</div>
</div>
</body>
</html>