/**
  * Formats the given user as html.
  *
  * @param WP_User $user The user to format (object of type WP_User).
  *
  * @uses apply_filters() Calls 'aa_user_template' hook
  * @return String html
  */
 function format_user($user)
 {
     $tpl_vars = array('{class}' => '', '{user}' => '');
     $avatar_size = intval($this->avatar_size);
     if (!$avatar_size) {
         $avatar_size = false;
     }
     $name = "";
     if ($this->show_name) {
         $name = $user->display_name;
     }
     $alt = $title = $name;
     $divcss = array('user');
     if ($this->show_name) {
         $divcss[] = 'with-name';
     }
     $link = false;
     $link_type = $this->user_link;
     // always use 'website' for commentators
     $type = isset($user->type) ? $user->type : null;
     if ($user->user_id == -1 && "guest-author" != $type) {
         $link_type = 'website';
     }
     switch ($link_type) {
         case 'authorpage':
             if ("guest-author" == $type) {
                 $link = get_author_posts_url($user->user_id, $user->user_nicename);
             } else {
                 $link = get_author_posts_url($user->user_id);
             }
             break;
         case 'website':
             if ("guest-author" == $type) {
                 $link = get_the_author_meta('url', $user->ID);
             } else {
                 $link = $user->user_url;
                 if (empty($link) || $link == 'http://') {
                     $link = false;
                 }
             }
             break;
         case 'blog':
             if (AA_is_wpmu()) {
                 $blog = get_active_blog_for_user($user->user_id);
                 if (!empty($blog->siteurl)) {
                     $link = $blog->siteurl;
                 }
             }
             break;
         case 'bp_memberpage':
             if (function_exists('bp_core_get_user_domain')) {
                 $link = bp_core_get_user_domain($user->user_id);
             } elseif (function_exists('bp_core_get_userurl')) {
                 // BP versions < 1.1
                 $link = bp_core_get_userurl($user->user_id);
             }
             break;
         case 'bbpress_memberpage':
             if (function_exists('bbp_get_user_profile_url')) {
                 $link = bbp_get_user_profile_url($user->user_id);
             }
             if (empty($link) || $link == 'http://') {
                 $link = false;
             }
             break;
         case 'last_post':
             $recent = get_posts(array('author' => $user->user_id, 'orderby' => 'date', 'order' => 'desc', 'numberposts' => 1));
             $link = get_permalink($recent[0]->ID);
             break;
         case 'last_post_all':
             $last_post = get_most_recent_post_of_user($user->user_id);
             $link = get_permalink($last_post['post_id']);
             break;
     }
     if ($this->show_postcount) {
         $postcount = 0;
         if ($user->user_id == -1 && "guest-author" != $type) {
             $postcount = $this->get_comment_count($user->user_email);
             $title .= ' (' . sprintf(_n("%d comment", "%d comments", $postcount, 'author-avatars'), $postcount) . ')';
         } else {
             // this is passing 1 for coauthors
             if ("guest-author" == $type && $user->linked_account) {
                 $linked_user = get_user_by('login', $user->linked_account);
                 // fetch the linked account and show thats count
                 $postcount = $this->get_user_postcount($linked_user->ID);
             } else {
                 $postcount = $this->get_user_postcount($user->user_id);
             }
             $title .= ' (' . sprintf(_n("%d post", "%d posts", $postcount, 'author-avatars'), $postcount) . ')';
         }
         $name .= sprintf(apply_filters('aa_post_count', ' (%d)', $postcount), $postcount);
     }
     if ($this->show_bbpress_post_count && AA_is_bbpress()) {
         $BBPRESS_postcount = 0;
         if (function_exists('bbp_get_user_topic_count_raw')) {
             $BBPRESS_postcount = bbp_get_user_topic_count_raw($user->user_id) + bbp_get_user_reply_count_raw($user->user_id);
             $title .= ' (' . sprintf(_n("%d BBPress post", "%d BBPress posts", $BBPRESS_postcount, 'author-avatars'), $BBPRESS_postcount) . ')';
         }
         $name .= sprintf(' (%d)', $BBPRESS_postcount);
     }
     $biography = false;
     if ($this->show_biography) {
         if ("guest-author" != $type && $user->user_id > 0) {
             $biography = get_the_author_meta('description', $user->user_id);
         } else {
             $biography = isset($user->description) ? $user->description : '';
         }
         $biography = apply_filters('aa_user_biography_filter', $biography);
         // trim $biography to bio_length
         if (0 < $this->bio_length) {
             $biography = $this->truncate_html(wpautop($biography, true), apply_filters('aa_user_bio_length', $this->bio_length));
         } else {
             $biography = wpautop($biography, true);
         }
         $divcss[] = 'with-biography bio-length-' . $this->bio_length;
         $name = '<strong>' . $name . '</strong>';
         if (empty($biography)) {
             $divcss[] = 'biography-missing';
         }
     }
     $show_last_post = false;
     if ($this->show_last_post) {
         $show_last_post = $this->aa_get_last_post($user->user_id);
         /**
          * Filter the users last post.
          *
          * @since 1.8.6.0
          *
          * @param string $show_last_post    The HTML link to users last post.
          * @param object					The Current user object.
          */
         $show_last_post = apply_filters('aa_user_show_last_post_filter', $show_last_post, $user);
         $divcss[] = 'with-last-post';
         if (empty($show_last_post)) {
             $divcss[] = 'last-post-missing';
         }
     }
     $email = false;
     if ($this->show_email && $user->user_email) {
         $userEmail = $user->user_email;
         /**
          * Filter the title tag content for an admin page.
          *
          * @since 1.8.6.0
          *
          * @param string 				The mailto href for sprintf the $1$s is where the email is inserted.
          * @param string $userEmail     The Email to be inserted.
          * @param object				The Current user object.
          */
         $email = sprintf(apply_filters('aa_user_email_url_template', '<a href="mailto:%1$s">%1$s</a>', $userEmail, $user), $userEmail);
         $divcss[] = 'with-email';
         if (empty($email)) {
             $divcss[] = 'email-missing';
         }
     }
     if ($user->user_id == -1) {
         // use email for commentators
         $avatar = get_avatar($user->user_email, $avatar_size);
     } else {
         // if on buddypress install use BP function
         if (function_exists('bp_core_fetch_avatar')) {
             $avatar = bp_core_fetch_avatar(array('item_id' => $user->user_id, 'width' => $avatar_size, 'height' => $avatar_size, 'type' => 'full', 'alt' => $alt, 'title' => $title));
         } else {
             // call the standard avatar function
             $avatar = get_avatar($user->user_id, $avatar_size);
         }
     }
     /* Strip all existing links (a tags) from the get_avatar() code to
      * remove e.g. the link which is added by the add-local-avatar plugin
      * @see http://wordpress.org/support/topic/309878 */
     if (!empty($link)) {
         $avatar = preg_replace('@<\\s*\\/?\\s*[aA]\\s*.*?>@', '', $avatar);
     }
     // the buddypress code
     if (!function_exists('bp_core_fetch_avatar')) {
         /* strip alt and title parameter */
         $avatar = preg_replace('@alt=["\'][\\w]*["\'] ?@', '', $avatar);
         $avatar = preg_replace('@title=["\'][\\w]*["\'] ?@', '', $avatar);
         /* insert alt and title parameters */
         if (!stripos($avatar, 'title=')) {
             $avatar = preg_replace('@ ?\\/>@', ' title="' . $title . '" />', $avatar);
         }
         if (!stripos($avatar, 'alt=')) {
             $avatar = preg_replace('@ ?\\/>@', ' alt="' . $alt . '"  />', $avatar);
         }
     }
     $html = '';
     /**
      * filter the span that holds the avatar
      *
      * @param string 			The sprintf template.
      * @param string @title  	The value passed to the title attr in span.
      * @param string @avatar	The HTML returned from get_avatar() etc.
      * @param object $user		The user object
      */
     $html .= sprintf(apply_filters('aa_user_avatar_template', '<span class="avatar" title="%s">%s</span>', $title, $avatar, $user), $title, $avatar);
     if ($this->show_name || $this->show_bbpress_post_count || $this->show_postcount) {
         /**
          * filter the span that contains the users name
          *
          * @param string 			The sprintf template.
          * @param string $name		The value (users name) passed into the span
          * @param object $user		The user object
          */
         $html .= sprintf(apply_filters('aa_user_name_template', '<span class="name">%s</span>', $name, $user), $name);
     }
     if ($link) {
         /**
          * filter the href that wrap's avatar and users name
          *
          * @param string 			The sprintf template.
          * @param string $link		The href value.
          * @param string $title		The value for the href title
          * @param string $html 		The HTML with avatar and name
          * @param object $user		The user object
          */
         $html = sprintf(apply_filters('aa_user_link_template', '<a href="%s" title="%s">%s</a>', $link, $title, $html, $user), $link, $title, $html);
     }
     if ($email) {
         /**
          * filter that wrap's the email link in a div
          *
          * @param string 			The sprintf template.
          * @param string $email		The HTML containing the mailto href and email string.
          * @param object $user		The user object
          */
         $html .= sprintf(apply_filters('aa_user_email_template', '<div class="email">%s</div>', $email, $user), $email);
     }
     if ($biography) {
         /**
          * filter that wrap's the BIO text in a div
          *
          * @param string 			The sprintf template.
          * @param string $biography	The Bio text.
          * @param object $user		The user object
          */
         $html .= sprintf(apply_filters('aa_user_biography_template', '<div class="biography">%s</div>', $biography, $user), $biography);
     }
     if ($show_last_post) {
         /**
          * filter that wrap's the last post link in a div
          *
          * @param string 					The sprintf template.
          * @param string $show_last_post	The last post link.
          * @param object $user		The user object
          */
         $html .= sprintf(apply_filters('aa_user_last_post_template', '<div class="show_last_post">%s</div>', $show_last_post, $user), $show_last_post);
     }
     if (!empty($this->display_extra)) {
         /**
          * filter the extra HTML block before its appended
          *
          * @param string $extra		extra HTML / string.
          * @param object $user		The user object
          */
         $html .= apply_filters('aa_user_display_extra', $this->display_extra, $user);
     }
     $tpl_vars['{class}'] = implode($divcss, ' ');
     /**
      * filter on the complete HTML for the user
      *
      * @param string $html	The generated HTML.
      * @param object $user	the user object
      */
     $tpl_vars['{user}'] = apply_filters('aa_user_final_content', $html, $user);
     /**
      * filter the outer HTML template
      *
      * @param string $html	The outer user template.
      * @param object $user	the user object
      */
     return str_replace(array_keys($tpl_vars), $tpl_vars, apply_filters('aa_user_template', $this->user_template, $user));
 }
 /**
  * The shortcode handler for the [show_avatar] shortcode.
  *
  * Example: [show_avatar id=pbearne@tycoelectronics.com avatar_size=30 align=right]
  */
 function shortcode_handler($atts, $content = null)
 {
     $extraClass = '';
     $hrefStart = '';
     $name = '';
     $bio = '';
     $last_post = '';
     $style = '';
     $email = '';
     $link = '';
     $id = '';
     // get id or email
     if (!empty($atts['id'])) {
         $id = preg_replace('[^\\w\\.\\@\\-]', '', $atts['id']);
     }
     if (empty($id) && !empty($atts['email'])) {
         $id = preg_replace('[^\\w\\.\\@\\-]', '', $atts['email']);
     }
     // get avatar size
     $bio_length = -1;
     if (!empty($atts['max_bio_length'])) {
         $bio_length = intval($atts['max_bio_length']);
     }
     // get avatar size
     $avatar_size = false;
     if (!empty($atts['avatar_size'])) {
         $avatar_size = intval($atts['avatar_size']);
     }
     // get alignment
     if (!empty($atts['align'])) {
         switch ($atts['align']) {
             case 'left':
                 $style = "float: left; margin-right: 10px;";
                 break;
             case 'right':
                 $style = "float: right; margin-left: 10px;";
                 break;
             case 'center':
                 $style = "text-align: center; width: 100%;";
                 break;
         }
     }
     if (!empty($id)) {
         $avatar = get_avatar($id, $avatar_size);
     } else {
         $avatar = __("[show_author shortcode: please set id/email attribute]");
     }
     // is there an user link request
     if (!empty($atts['user_link']) || !empty($atts['show_biography']) || !empty($atts['show_postcount']) || !empty($atts['show_name']) || !empty($atts['show_email'])) {
         // try to fetch user profile
         $isUser = true;
         if (!is_numeric($id)) {
             if (email_exists($id)) {
                 $id = email_exists($id);
             } else {
                 $isUser = false;
             }
         }
         if ($isUser) {
             $all_meta_for_user = get_user_meta($id);
             if (count($all_meta_for_user) == 0) {
                 $isUser = false;
             }
         }
         if ($isUser) {
             if (!empty($atts['user_link'])) {
                 switch ($atts['user_link']) {
                     case 'authorpage':
                         $link = get_author_posts_url($id);
                         break;
                     case 'website':
                         $link = get_the_author_meta('user_url', $id);
                         if (empty($link) || $link == 'http://') {
                             $link = false;
                         }
                         break;
                     case 'blog':
                         if (AA_is_wpmu()) {
                             $blog = get_active_blog_for_user($id);
                             if (!empty($blog->siteurl)) {
                                 $link = $blog->siteurl;
                             }
                         }
                         break;
                     case 'bp_memberpage':
                         if (function_exists('bp_core_get_user_domain')) {
                             $link = bp_core_get_user_domain($id);
                         } elseif (function_exists('bp_core_get_userurl')) {
                             // BP versions < 1.1
                             $link = bp_core_get_userurl($id);
                         }
                         break;
                     case 'bbpress_memberpage':
                         if (function_exists('bbp_get_user_profile_url')) {
                             $link = bbp_get_user_profile_url($id);
                         }
                         if (empty($link) || $link == 'http://') {
                             $link = false;
                         }
                         break;
                     case 'last_post':
                         $recent = get_posts(array('author' => $id, 'orderby' => 'date', 'order' => 'desc', 'numberposts' => 1));
                         $link = get_permalink($recent[0]->ID);
                         break;
                     case 'last_post_filtered':
                         $recent = get_posts(array('author' => $id, 'orderby' => 'date', 'order' => 'desc', 'numberposts' => 1));
                         $link = get_permalink($recent[0]->ID);
                         break;
                     case 'last_post_all':
                         $last_post = get_most_recent_post_of_user($id);
                         $link = get_permalink($last_post['post_id']);
                         break;
                 }
                 if ($link) {
                     $hrefStart = '<a href="' . $link . '">';
                 }
             }
             if (!empty($atts['show_name'])) {
                 $name = '<br />' . get_the_author_meta('display_name', $id);
                 $extraClass .= ' with-name';
             }
             if (!empty($atts['show_email'])) {
                 $userEmail = get_the_author_meta('user_email', $id);
                 $email = "<div class='email'><a href='mailto:" . $userEmail . "''>" . $userEmail . "</a></div>";
                 if (empty($email)) {
                     $extraClass .= 'email-missing';
                 } else {
                     $extraClass .= ' with-email';
                 }
             }
             if (!empty($atts['show_postcount'])) {
                 $name .= ' (' . ($postcount = $this->userlist->get_user_postcount($id) . ')');
             }
             if (!empty($atts['show_bbpress_post_count'])) {
                 if (function_exists('bbp_get_user_topic_count_raw')) {
                     $BBPRESS_postcount = bbp_get_user_topic_count_raw($id) + bbp_get_user_reply_count_raw($id);
                     $name .= ' (' . ($postcount = $BBPRESS_postcount . ')');
                 }
             }
             if (!empty($atts['show_biography'])) {
                 $biography = get_the_author_meta('description', $id);
                 if (0 < $bio_length) {
                     $biography = $this->userlist->truncate_html(wpautop($biography, true), apply_filters('aa_user_bio_length', $bio_length));
                 } else {
                     $biography = wpautop($biography, true);
                 }
                 if (!empty($atts['show_name'])) {
                     $bio = '<div class="bio bio-length-' . $atts['max_bio_length'] . '">' . $biography . '</div>';
                 }
                 if (empty($bio)) {
                     $extraClass .= ' biography-missing';
                 } else {
                     $extraClass .= ' with-biography bio-length-' . $bio_length;
                 }
             }
             // show last_post?
             if (isset($atts['show_last_post']) && strlen($atts['show_last_post']) > 0) {
                 $last_post = '<div class="last_post">' . $this->userlist->aa_get_last_post($id) . '</div>';
                 if (empty($last_post)) {
                     $extraClass .= ' last-post-missing';
                 } else {
                     $extraClass .= ' with-last-post';
                 }
             }
         }
     }
     $hrefend = '';
     if (!empty($hrefStart)) {
         $hrefend = '</a>';
     }
     if (!empty($style)) {
         $style = ' style="' . $style . '"';
     }
     return '<div class="shortcode-show-avatar ' . $extraClass . '"' . $style . '>' . $hrefStart . $avatar . $name . $last_post . $hrefend . $bio . $email . '</div>' . $content;
 }