[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/bp-core/ -> bp-core-avatars.php (source)

   1  <?php
   2  
   3  /**
   4   * BuddyPress Avatars.
   5   */
   6  
   7  // Exit if accessed directly
   8  if ( !defined( 'ABSPATH' ) ) exit;
   9  
  10  /***
  11   * Set up the constants we need for avatar support.
  12   */
  13  function bp_core_set_avatar_constants() {
  14  
  15      $bp = buddypress();
  16  
  17      if ( !defined( 'BP_AVATAR_THUMB_WIDTH' ) )
  18          define( 'BP_AVATAR_THUMB_WIDTH', 50 );
  19  
  20      if ( !defined( 'BP_AVATAR_THUMB_HEIGHT' ) )
  21          define( 'BP_AVATAR_THUMB_HEIGHT', 50 );
  22  
  23      if ( !defined( 'BP_AVATAR_FULL_WIDTH' ) )
  24          define( 'BP_AVATAR_FULL_WIDTH', 150 );
  25  
  26      if ( !defined( 'BP_AVATAR_FULL_HEIGHT' ) )
  27          define( 'BP_AVATAR_FULL_HEIGHT', 150 );
  28  
  29      if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_WIDTH' ) )
  30          define( 'BP_AVATAR_ORIGINAL_MAX_WIDTH', 450 );
  31  
  32      if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE' ) ) {
  33  
  34          if ( !isset( $bp->site_options['fileupload_maxk'] ) ) {
  35              define( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE', 5120000 ); // 5mb
  36          } else {
  37              define( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE', $bp->site_options['fileupload_maxk'] * 1024 );
  38          }
  39      }
  40  
  41      if ( ! defined( 'BP_SHOW_AVATARS' ) ) {
  42          define( 'BP_SHOW_AVATARS', bp_get_option( 'show_avatars' ) );
  43      }
  44  }
  45  add_action( 'bp_init', 'bp_core_set_avatar_constants', 3 );
  46  
  47  /**
  48   * Set up global variables related to avatars.
  49   *
  50   * @since BuddyPress (1.5.0)
  51   */
  52  function bp_core_set_avatar_globals() {
  53      $bp = buddypress();
  54  
  55      $bp->avatar        = new stdClass;
  56      $bp->avatar->thumb = new stdClass;
  57      $bp->avatar->full  = new stdClass;
  58  
  59      // Dimensions
  60      $bp->avatar->thumb->width  = BP_AVATAR_THUMB_WIDTH;
  61      $bp->avatar->thumb->height = BP_AVATAR_THUMB_HEIGHT;
  62      $bp->avatar->full->width   = BP_AVATAR_FULL_WIDTH;
  63      $bp->avatar->full->height  = BP_AVATAR_FULL_HEIGHT;
  64  
  65      // Upload maximums
  66      $bp->avatar->original_max_width    = BP_AVATAR_ORIGINAL_MAX_WIDTH;
  67      $bp->avatar->original_max_filesize = BP_AVATAR_ORIGINAL_MAX_FILESIZE;
  68  
  69      // Defaults
  70      $bp->avatar->thumb->default = bp_core_avatar_default_thumb();
  71      $bp->avatar->full->default  = bp_core_avatar_default();
  72  
  73      // These have to be set on page load in order to avoid infinite filter loops at runtime
  74      $bp->avatar->upload_path = bp_core_avatar_upload_path();
  75      $bp->avatar->url = bp_core_avatar_url();
  76  
  77      // Cache the root blog's show_avatars setting, to avoid unnecessary
  78      // calls to switch_to_blog()
  79      $bp->avatar->show_avatars = (bool) BP_SHOW_AVATARS;
  80  
  81      // Backpat for pre-1.5
  82      if ( ! defined( 'BP_AVATAR_UPLOAD_PATH' ) )
  83          define( 'BP_AVATAR_UPLOAD_PATH', $bp->avatar->upload_path );
  84  
  85      // Backpat for pre-1.5
  86      if ( ! defined( 'BP_AVATAR_URL' ) )
  87          define( 'BP_AVATAR_URL', $bp->avatar->url );
  88  
  89      do_action( 'bp_core_set_avatar_globals' );
  90  }
  91  add_action( 'bp_setup_globals', 'bp_core_set_avatar_globals' );
  92  
  93  /**
  94   * Get an avatar for a BuddyPress object.
  95   *
  96   * Supports avatars for users, groups, and blogs by default, but can be
  97   * extended to support custom components as well.
  98   *
  99   * This function gives precedence to locally-uploaded avatars. When a local
 100   * avatar is not found, Gravatar is queried. To disable Gravatar fallbacks
 101   * locally:
 102   *    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
 103   *
 104   * @param array $args {
 105   *     An array of arguments. All arguments are technically optional; some
 106   *     will, if not provided, be auto-detected by bp_core_fetch_avatar(). This
 107   *     auto-detection is described more below, when discussing specific
 108   *     arguments.
 109   *
 110   *     @type int|bool $item_id The numeric ID of the item for which you're
 111   *           requesting an avatar (eg, a user ID). If no 'item_id' is present,
 112   *           the function attempts to infer an ID from the 'object' + the
 113   *           current context: if 'object' is 'user' and the current page is a
 114   *           user page, 'item_id' will default to the displayed user ID; if
 115   *           'group' and on a group page, to the current group ID; if 'blog',
 116   *           to the current blog's ID. If no 'item_id' can be determined in
 117   *           this way, the function returns false. Default: false.
 118   *     @type string $object The kind of object for which you're getting an
 119   *           avatar. BuddyPress natively supports three options: 'user',
 120   *           'group', 'blog'; a plugin may register more.  Default: 'user'.
 121   *     @type string $type When a new avatar is uploaded to BP, 'thumb' and
 122   *           'full' versions are saved. This parameter specifies whether you'd
 123   *           like the 'full' or smaller 'thumb' avatar. Default: 'thumb'.
 124   *     @type string|bool $avatar_dir The name of the subdirectory where the
 125   *           requested avatar should be found. If no value is passed,
 126   *           'avatar_dir' is inferred from 'object': 'user' becomes 'avatars',
 127   *           'group' becomes 'group-avatars', 'blog' becomes 'blog-avatars'.
 128   *           Remember that this string denotes a subdirectory of BP's main
 129   *           avatar directory (usually based on {@link wp_upload_dir()}); it's a
 130   *           string like 'group-avatars' rather than the full directory path.
 131   *           Generally, it'll only be necessary to override the default value if
 132   *           storing avatars in a non-default location. Defaults to false
 133   *           (auto-detected).
 134   *     @type int|bool $width Requested avatar width. The unit is px. This value
 135   *           is used to build the 'width' attribute for the <img> element. If
 136   *           no value is passed, BP uses the global avatar width for this
 137   *           avatar type. Default: false (auto-detected).
 138   *     @type int|bool $height Requested avatar height. The unit is px. This
 139   *           value is used to build the 'height' attribute for the <img>
 140   *           element. If no value is passed, BP uses the global avatar height
 141   *           for this avatar type. Default: false (auto-detected).
 142   *     @type string $class The CSS class for the <img> element. Note that BP
 143   *           uses the 'avatar' class fairly extensively in its default styling,
 144   *           so if you plan to pass a custom value, consider appending it to
 145   *           'avatar' (eg 'avatar foo') rather than replacing it altogether.
 146   *           Default: 'avatar'.
 147   *     @type string|bool $css_id The CSS id for the <img> element.
 148   *           Default: false.
 149   *     @type string $title The title attribute for the <img> element.
 150   *           Default: false.
 151   *     @type string $alt The alt attribute for the <img> element. In BP, this
 152   *           value is generally passed by the wrapper functions, where the data
 153   *           necessary for concatenating the string is at hand; see
 154   *           {@link bp_get_activity_avatar()} for an example. Default: ''.
 155   *     @type string|bool $email An email to use in Gravatar queries. Unless
 156   *           otherwise configured, BP uses Gravatar as a fallback for avatars
 157   *           that are not provided locally. Gravatar's API requires using a hash
 158   *           of the user's email address; this argument provides it. If not
 159   *           provided, the function will infer it: for users, by getting the
 160   *           user's email from the database, for groups/blogs, by concatenating
 161   *           "{$item_id}-{$object}@{bp_get_root_domain()}". The user query adds
 162   *           overhead, so it's recommended that wrapper functions provide a
 163   *           value for 'email' when querying user IDs. Default: false.
 164   *     @type bool $no_grav Whether to disable the default Gravatar fallback.
 165   *           By default, BP will fall back on Gravatar when it cannot find a
 166   *           local avatar. In some cases, this may be undesirable, in which
 167   *           case 'no_grav' should be set to true. To disable Gravatar
 168   *           fallbacks globally, see the 'bp_core_fetch_avatar_no_grav' filter.
 169   *           Default: false.
 170   *     @type bool $html Whether to return an <img> HTML element, vs a raw URL
 171   *           to an avatar. If false, <img>-specific arguments (like 'css_id')
 172   *           will be ignored. Default: true.
 173   * }
 174   * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg.
 175   */
 176  function bp_core_fetch_avatar( $args = '' ) {
 177  
 178      // If avatars are disabled for the root site, obey that request and bail
 179      if ( ! buddypress()->avatar->show_avatars )
 180          return;
 181  
 182      global $current_blog;
 183  
 184      $bp = buddypress();
 185  
 186      // Set a few default variables
 187      $def_object = 'user';
 188      $def_type   = 'thumb';
 189      $def_class  = 'avatar';
 190  
 191      // Set the default variables array
 192      $params = wp_parse_args( $args, array(
 193          'item_id'    => false,
 194          'object'     => $def_object, // user/group/blog/custom type (if you use filters)
 195          'type'       => $def_type,   // thumb or full
 196          'avatar_dir' => false,       // Specify a custom avatar directory for your object
 197          'width'      => false,       // Custom width (int)
 198          'height'     => false,       // Custom height (int)
 199          'class'      => $def_class,  // Custom <img> class (string)
 200          'css_id'     => false,       // Custom <img> ID (string)
 201          'alt'        => '',             // Custom <img> alt (string)
 202          'email'      => false,       // Pass the user email (for gravatar) to prevent querying the DB for it
 203          'no_grav'    => false,       // If there is no avatar found, return false instead of a grav?
 204          'html'       => true,        // Wrap the return img URL in <img />
 205          'title'      => ''           // Custom <img> title (string)
 206      ) );
 207      extract( $params, EXTR_SKIP );
 208  
 209      /** Set item_id ***********************************************************/
 210  
 211      if ( empty( $item_id ) ) {
 212  
 213          switch ( $object ) {
 214  
 215              case 'blog'  :
 216                  $item_id = $current_blog->id;
 217                  break;
 218  
 219              case 'group' :
 220                  if ( bp_is_active( 'groups' ) ) {
 221                      $item_id = $bp->groups->current_group->id;
 222                  } else {
 223                      $item_id = false;
 224                  }
 225  
 226                  break;
 227  
 228              case 'user'  :
 229              default      :
 230                  $item_id = bp_displayed_user_id();
 231                  break;
 232          }
 233  
 234          $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object, $params );
 235  
 236          if ( empty( $item_id ) ) {
 237              return false;
 238          }
 239      }
 240  
 241      $class = apply_filters( 'bp_core_avatar_class', $class, $item_id, $object, $params );
 242  
 243      /** Set avatar_dir ********************************************************/
 244  
 245      if ( empty( $avatar_dir ) ) {
 246  
 247          switch ( $object ) {
 248  
 249              case 'blog'  :
 250                  $avatar_dir = 'blog-avatars';
 251                  break;
 252  
 253              case 'group' :
 254                  if ( bp_is_active( 'groups' ) ) {
 255                      $avatar_dir = 'group-avatars';
 256                  } else {
 257                      $avatar_dir = false;
 258                  }
 259  
 260                  break;
 261  
 262              case 'user'  :
 263              default      :
 264                  $avatar_dir = 'avatars';
 265                  break;
 266          }
 267  
 268          $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object, $params );
 269  
 270          if ( empty( $avatar_dir ) ) {
 271              return false;
 272          }
 273      }
 274  
 275      /** <img> alt *************************************************************/
 276  
 277      if ( false !== strpos( $alt, '%s' ) || false !== strpos( $alt, '%1$s' ) ) {
 278  
 279          switch ( $object ) {
 280  
 281              case 'blog'  :
 282                  $item_name = get_blog_option( $item_id, 'blogname' );
 283                  break;
 284  
 285              case 'group' :
 286                  $item_name = bp_get_group_name( groups_get_group( array( 'group_id' => $item_id ) ) );
 287                  break;
 288  
 289              case 'user'  :
 290              default :
 291                  $item_name = bp_core_get_user_displayname( $item_id );
 292                  break;
 293          }
 294  
 295          $item_name = apply_filters( 'bp_core_avatar_alt', $item_name, $item_id, $object, $params );
 296          $alt       = sprintf( $alt, $item_name );
 297      }
 298  
 299      /** Sanity Checks *********************************************************/
 300  
 301      // Get a fallback for the 'alt' parameter
 302      if ( empty( $alt ) )
 303          $alt = __( 'Avatar Image', 'buddypress' );
 304  
 305      $html_alt = ' alt="' . esc_attr( $alt ) . '"';
 306  
 307      // Set title tag, if it's been provided
 308      if ( !empty( $title ) ) {
 309          $title = " title='" . esc_attr( apply_filters( 'bp_core_avatar_title', $title, $item_id, $object, $params ) ) . "'";
 310      }
 311  
 312      // Set CSS ID if passed
 313      if ( !empty( $css_id ) ) {
 314          $css_id = ' id="' . esc_attr( $css_id ) . '"';
 315      }
 316  
 317      // Set image width
 318      if ( false !== $width ) {
 319          $html_width = ' width="' . $width . '"';
 320      } elseif ( 'thumb' == $type ) {
 321          $html_width = ' width="' . bp_core_avatar_thumb_width() . '"';
 322      } else {
 323          $html_width = ' width="' . bp_core_avatar_full_width() . '"';
 324      }
 325  
 326      // Set image height
 327      if ( false !== $height ) {
 328          $html_height = ' height="' . $height . '"';
 329      } elseif ( 'thumb' == $type ) {
 330          $html_height = ' height="' . bp_core_avatar_thumb_height() . '"';
 331      } else {
 332          $html_height = ' height="' . bp_core_avatar_full_height() . '"';
 333      }
 334  
 335      // Set img URL and DIR based on prepopulated constants
 336      $avatar_loc        = new stdClass();
 337      $avatar_loc->path  = trailingslashit( bp_core_avatar_upload_path() );
 338      $avatar_loc->url   = trailingslashit( bp_core_avatar_url() );
 339  
 340      $avatar_loc->dir   = trailingslashit( $avatar_dir );
 341      $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', ( $avatar_loc->url  . $avatar_loc->dir . $item_id ), $item_id, $object, $avatar_dir );
 342      $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', ( $avatar_loc->path . $avatar_loc->dir . $item_id ), $item_id, $object, $avatar_dir );
 343  
 344      // Add an identifying class
 345      $class .= ' ' . $object . '-' . $item_id . '-avatar ' . sanitize_html_class( "avatar-$width" ) . ' photo';
 346  
 347      /**
 348       * Look for uploaded avatar first. Use it if it exists.
 349       * Set the file names to search for, to select the full size
 350       * or thumbnail image.
 351       */
 352      $avatar_size              = ( 'full' == $type ) ? '-bpfull' : '-bpthumb';
 353      $legacy_user_avatar_name  = ( 'full' == $type ) ? '-avatar2' : '-avatar1';
 354      $legacy_group_avatar_name = ( 'full' == $type ) ? '-groupavatar-full' : '-groupavatar-thumb';
 355  
 356      // Check for directory
 357      if ( file_exists( $avatar_folder_dir ) ) {
 358  
 359          // Open directory
 360          if ( $av_dir = opendir( $avatar_folder_dir ) ) {
 361  
 362              // Stash files in an array once to check for one that matches
 363              $avatar_files = array();
 364              while ( false !== ( $avatar_file = readdir( $av_dir ) ) ) {
 365                  // Only add files to the array (skip directories)
 366                  if ( 2 < strlen( $avatar_file ) ) {
 367                      $avatar_files[] = $avatar_file;
 368                  }
 369              }
 370  
 371              // Check for array
 372              if ( 0 < count( $avatar_files ) ) {
 373  
 374                  // Check for current avatar
 375                  foreach( $avatar_files as $key => $value ) {
 376                      if ( strpos ( $value, $avatar_size )!== false ) {
 377                          $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
 378                      }
 379                  }
 380  
 381                  // Legacy avatar check
 382                  if ( !isset( $avatar_url ) ) {
 383                      foreach( $avatar_files as $key => $value ) {
 384                          if ( strpos ( $value, $legacy_user_avatar_name )!== false ) {
 385                              $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
 386                          }
 387                      }
 388  
 389                      // Legacy group avatar check
 390                      if ( !isset( $avatar_url ) ) {
 391                          foreach( $avatar_files as $key => $value ) {
 392                              if ( strpos ( $value, $legacy_group_avatar_name )!== false ) {
 393                                  $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
 394                              }
 395                          }
 396                      }
 397                  }
 398              }
 399          }
 400  
 401          // Close the avatar directory
 402          closedir( $av_dir );
 403  
 404          // If we found a locally uploaded avatar
 405          if ( isset( $avatar_url ) ) {
 406  
 407              // Return it wrapped in an <img> element
 408              if ( true === $html ) {
 409                  return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '" class="' . esc_attr( $class ) . '"' . $css_id . $html_width . $html_height . $html_alt . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
 410  
 411              // ...or only the URL
 412              } else {
 413                  return apply_filters( 'bp_core_fetch_avatar_url', $avatar_url, $params );
 414              }
 415          }
 416      }
 417  
 418      // If no avatars could be found, try to display a gravatar
 419  
 420      // Skips gravatar check if $no_grav is passed
 421      if ( ! apply_filters( 'bp_core_fetch_avatar_no_grav', $no_grav ) ) {
 422  
 423          // Set gravatar size
 424          if ( false !== $width ) {
 425              $grav_size = $width;
 426          } else if ( 'full' == $type ) {
 427              $grav_size = bp_core_avatar_full_width();
 428          } else if ( 'thumb' == $type ) {
 429              $grav_size = bp_core_avatar_thumb_width();
 430          }
 431  
 432          // Set gravatar type
 433          if ( empty( $bp->grav_default->{$object} ) ) {
 434              $default_grav = 'wavatar';
 435          } else if ( 'mystery' == $bp->grav_default->{$object} ) {
 436              $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $grav_size );
 437          } else {
 438              $default_grav = $bp->grav_default->{$object};
 439          }
 440  
 441          // Set gravatar object
 442          if ( empty( $email ) ) {
 443              if ( 'user' == $object ) {
 444                  $email = bp_core_get_user_email( $item_id );
 445              } else if ( 'group' == $object || 'blog' == $object ) {
 446                  $email = "{$item_id}-{$object}@{bp_get_root_domain()}";
 447              }
 448          }
 449  
 450          // Set host based on if using ssl
 451          $host = 'http://gravatar.com/avatar/';
 452          if ( is_ssl() ) {
 453              $host = 'https://secure.gravatar.com/avatar/';
 454          }
 455  
 456          // Filter gravatar vars
 457          $email    = apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object );
 458          $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $email ) ) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
 459  
 460          // Gravatar rating; http://bit.ly/89QxZA
 461          $rating = get_option( 'avatar_rating' );
 462          if ( ! empty( $rating ) ) {
 463              $gravatar .= "&amp;r={$rating}";
 464          }
 465  
 466      // No avatar was found, and we've been told not to use a gravatar.
 467      } else {
 468          $gravatar = apply_filters( "bp_core_default_avatar_$object", bp_core_avatar_default( 'local' ), $params );
 469      }
 470  
 471      if ( true === $html ) {
 472          return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '" class="' . esc_attr( $class ) . '"' . $css_id . $html_width . $html_height . $html_alt . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
 473      } else {
 474          return apply_filters( 'bp_core_fetch_avatar_url', $gravatar, $params );
 475      }
 476  }
 477  
 478  /**
 479   * Delete an existing avatar.
 480   *
 481   * @param array $args {
 482   *     Array of function parameters.
 483   *     @type bool|int $item_id ID of the item whose avatar you're deleting.
 484   *           Defaults to the current item of type $object.
 485   *     @type string $object Object type of the item whose avatar you're
 486   *           deleting. 'user', 'group', 'blog', or custom. Default: 'user'.
 487   *     @type bool|string $avatar_dir Subdirectory where avatar is located.
 488   *           Default: false, which falls back on the default location
 489   *           corresponding to the $object.
 490   * }
 491   * @return bool True on success, false on failure.
 492   */
 493  function bp_core_delete_existing_avatar( $args = '' ) {
 494  
 495      $defaults = array(
 496          'item_id'    => false,
 497          'object'     => 'user', // user OR group OR blog OR custom type (if you use filters)
 498          'avatar_dir' => false
 499      );
 500  
 501      $args = wp_parse_args( $args, $defaults );
 502      extract( $args, EXTR_SKIP );
 503  
 504      if ( empty( $item_id ) ) {
 505          if ( 'user' == $object )
 506              $item_id = bp_displayed_user_id();
 507          else if ( 'group' == $object )
 508              $item_id = buddypress()->groups->current_group->id;
 509          else if ( 'blog' == $object )
 510              $item_id = $current_blog->id;
 511  
 512          $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object );
 513  
 514          if ( !$item_id ) return false;
 515      }
 516  
 517      if ( empty( $avatar_dir ) ) {
 518          if ( 'user' == $object )
 519              $avatar_dir = 'avatars';
 520          else if ( 'group' == $object )
 521              $avatar_dir = 'group-avatars';
 522          else if ( 'blog' == $object )
 523              $avatar_dir = 'blog-avatars';
 524  
 525          $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );
 526  
 527          if ( !$avatar_dir ) return false;
 528      }
 529  
 530      $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
 531  
 532      if ( !file_exists( $avatar_folder_dir ) )
 533          return false;
 534  
 535      if ( $av_dir = opendir( $avatar_folder_dir ) ) {
 536          while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
 537              if ( ( preg_match( "/-bpfull/", $avatar_file ) || preg_match( "/-bpthumb/", $avatar_file ) ) && '.' != $avatar_file && '..' != $avatar_file )
 538                  @unlink( $avatar_folder_dir . '/' . $avatar_file );
 539          }
 540      }
 541      closedir($av_dir);
 542  
 543      @rmdir( $avatar_folder_dir );
 544  
 545      do_action( 'bp_core_delete_existing_avatar', $args );
 546  
 547      return true;
 548  }
 549  
 550  /**
 551   * Handle avatar uploading.
 552   *
 553   * The functions starts off by checking that the file has been uploaded
 554   * properly using bp_core_check_avatar_upload(). It then checks that the file
 555   * size is within limits, and that it has an accepted file extension (jpg, gif,
 556   * png). If everything checks out, crop the image and move it to its real
 557   * location.
 558   *
 559   * @see bp_core_check_avatar_upload()
 560   * @see bp_core_check_avatar_type()
 561   *
 562   * @param array $file The appropriate entry the from $_FILES superglobal.
 563   * @param string $upload_dir_filter A filter to be applied to 'upload_dir'.
 564   * @return bool True on success, false on failure.
 565   */
 566  function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) {
 567  
 568      /***
 569       * You may want to hook into this filter if you want to override this function.
 570       * Make sure you return false.
 571       */
 572      if ( !apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) )
 573          return true;
 574  
 575      require_once( ABSPATH . '/wp-admin/includes/file.php' );
 576  
 577      $uploadErrors = array(
 578          0 => __( 'The image was uploaded successfully', 'buddypress' ),
 579          1 => __( 'The image exceeds the maximum allowed file size of: ', 'buddypress' ) . size_format( bp_core_avatar_original_max_filesize() ),
 580          2 => __( 'The image exceeds the maximum allowed file size of: ', 'buddypress' ) . size_format( bp_core_avatar_original_max_filesize() ),
 581          3 => __( 'The uploaded file was only partially uploaded.', 'buddypress' ),
 582          4 => __( 'The image was not uploaded.', 'buddypress' ),
 583          6 => __( 'Missing a temporary folder.', 'buddypress' )
 584      );
 585  
 586      if ( ! bp_core_check_avatar_upload( $file ) ) {
 587          bp_core_add_message( sprintf( __( 'Your upload failed, please try again. Error was: %s', 'buddypress' ), $uploadErrors[$file['file']['error']] ), 'error' );
 588          return false;
 589      }
 590  
 591      if ( ! bp_core_check_avatar_size( $file ) ) {
 592          bp_core_add_message( sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'buddypress' ), size_format( bp_core_avatar_original_max_filesize() ) ), 'error' );
 593          return false;
 594      }
 595  
 596      if ( ! bp_core_check_avatar_type( $file ) ) {
 597          bp_core_add_message( __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ), 'error' );
 598          return false;
 599      }
 600  
 601      // Filter the upload location
 602      add_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
 603  
 604      $bp = buddypress();
 605  
 606      $bp->avatar_admin->original = wp_handle_upload( $file['file'], array( 'action'=> 'bp_avatar_upload' ) );
 607  
 608      // Remove the upload_dir filter, so that other upload URLs on the page
 609      // don't break
 610      remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
 611  
 612      // Move the file to the correct upload location.
 613      if ( !empty( $bp->avatar_admin->original['error'] ) ) {
 614          bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->original['error'] ), 'error' );
 615          return false;
 616      }
 617  
 618      // Get image size
 619      $size  = @getimagesize( $bp->avatar_admin->original['file'] );
 620      $error = false;
 621  
 622      // Check image size and shrink if too large
 623      if ( $size[0] > bp_core_avatar_original_max_width() ) {
 624          $editor = wp_get_image_editor( $bp->avatar_admin->original['file'] );
 625  
 626          if ( ! is_wp_error( $editor ) ) {
 627              $editor->set_quality( 100 );
 628  
 629              $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
 630              if ( ! is_wp_error( $resized ) ) {
 631                  $thumb = $editor->save( $editor->generate_filename() );
 632              } else {
 633                  $error = $resized;
 634              }
 635  
 636              // Check for thumbnail creation errors
 637              if ( false === $error && is_wp_error( $thumb ) ) {
 638                  $error = $thumb;
 639              }
 640  
 641              // Thumbnail is good so proceed
 642              if ( false === $error ) {
 643                  $bp->avatar_admin->resized = $thumb;
 644              }
 645  
 646          } else {
 647              $error = $editor;
 648          }
 649  
 650          if ( false !== $error ) {
 651              bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $error->get_error_message() ), 'error' );
 652              return false;
 653          }
 654      }
 655  
 656      if ( ! isset( $bp->avatar_admin->image ) )
 657          $bp->avatar_admin->image = new stdClass();
 658  
 659      // We only want to handle one image after resize.
 660      if ( empty( $bp->avatar_admin->resized ) ) {
 661          $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file'] );
 662      } else {
 663          $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized['path'] );
 664          @unlink( $bp->avatar_admin->original['file'] );
 665      }
 666  
 667      // Check for WP_Error on what should be an image
 668      if ( is_wp_error( $bp->avatar_admin->image->dir ) ) {
 669          bp_core_add_message( sprintf( __( 'Upload failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->image->dir->get_error_message() ), 'error' );
 670          return false;
 671      }
 672  
 673      // Set the url value for the image
 674      $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
 675  
 676      return true;
 677  }
 678  
 679  /**
 680   * Crop an uploaded avatar.
 681   *
 682   * $args has the following parameters:
 683   *  object - What component the avatar is for, e.g. "user"
 684   *  avatar_dir  The absolute path to the avatar
 685   *  item_id - Item ID
 686   *  original_file - The absolute path to the original avatar file
 687   *  crop_w - Crop width
 688   *  crop_h - Crop height
 689   *  crop_x - The horizontal starting point of the crop
 690   *  crop_y - The vertical starting point of the crop
 691   *
 692   * @param array $args {
 693   *     Array of function parameters.
 694   *     @type string $object Object type of the item whose avatar you're
 695   *           handling. 'user', 'group', 'blog', or custom. Default: 'user'.
 696   *     @type string $avatar_dir Subdirectory where avatar should be stored.
 697   *           Default: 'avatars'.
 698   *     @type bool|int $item_id ID of the item that the avatar belongs to.
 699   *     @type bool|string $original_file Absolute papth to the original avatar
 700   *           file.
 701   *     @type int $crop_w Crop width. Default: the global 'full' avatar width,
 702   *           as retrieved by bp_core_avatar_full_width().
 703   *     @type int $crop_h Crop height. Default: the global 'full' avatar height,
 704   *           as retrieved by bp_core_avatar_full_height().
 705   *     @type int $crop_x The horizontal starting point of the crop. Default: 0.
 706   *     @type int $crop_y The vertical starting point of the crop. Default: 0.
 707   * }
 708   * @return bool True on success, false on failure.
 709   */
 710  function bp_core_avatar_handle_crop( $args = '' ) {
 711  
 712      $r = wp_parse_args( $args, array(
 713          'object'        => 'user',
 714          'avatar_dir'    => 'avatars',
 715          'item_id'       => false,
 716          'original_file' => false,
 717          'crop_w'        => bp_core_avatar_full_width(),
 718          'crop_h'        => bp_core_avatar_full_height(),
 719          'crop_x'        => 0,
 720          'crop_y'        => 0
 721      ) );
 722  
 723      /***
 724       * You may want to hook into this filter if you want to override this function.
 725       * Make sure you return false.
 726       */
 727      if ( !apply_filters( 'bp_core_pre_avatar_handle_crop', true, $r ) )
 728          return true;
 729  
 730      extract( $r, EXTR_SKIP );
 731  
 732      if ( empty( $original_file ) )
 733          return false;
 734  
 735      $original_file = bp_core_avatar_upload_path() . $original_file;
 736  
 737      if ( !file_exists( $original_file ) )
 738          return false;
 739  
 740      if ( empty( $item_id ) ) {
 741          $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', dirname( $original_file ), $item_id, $object, $avatar_dir );
 742      } else {
 743          $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
 744      }
 745  
 746      if ( !file_exists( $avatar_folder_dir ) )
 747          return false;
 748  
 749      require_once( ABSPATH . '/wp-admin/includes/image.php' );
 750      require_once( ABSPATH . '/wp-admin/includes/file.php' );
 751  
 752      // Delete the existing avatar files for the object
 753      $existing_avatar = bp_core_fetch_avatar( array(
 754          'object'  => $object,
 755          'item_id' => $item_id,
 756          'html' => false,
 757      ) );
 758  
 759      if ( ! empty( $existing_avatar ) ) {
 760          // Check that the new avatar doesn't have the same name as the
 761          // old one before deleting
 762          $upload_dir           = wp_upload_dir();
 763          $existing_avatar_path = str_replace( $upload_dir['baseurl'], '', $existing_avatar );
 764          $new_avatar_path      = str_replace( $upload_dir['basedir'], '', $original_file );
 765  
 766          if ( $existing_avatar_path !== $new_avatar_path ) {
 767              bp_core_delete_existing_avatar( array( 'object' => $object, 'item_id' => $item_id, 'avatar_path' => $avatar_folder_dir ) );
 768          }
 769      }
 770  
 771  
 772  
 773      // Make sure we at least have a width and height for cropping
 774      if ( empty( $crop_w ) ) {
 775          $crop_w = bp_core_avatar_full_width();
 776      }
 777  
 778      if ( empty( $crop_h ) ) {
 779          $crop_h = bp_core_avatar_full_height();
 780      }
 781  
 782      // Get the file extension
 783      $data = @getimagesize( $original_file );
 784      $ext  = $data['mime'] == 'image/png' ? 'png' : 'jpg';
 785  
 786      // Set the full and thumb filenames
 787      $full_filename  = wp_hash( $original_file . time() ) . '-bpfull.'  . $ext;
 788      $thumb_filename = wp_hash( $original_file . time() ) . '-bpthumb.' . $ext;
 789  
 790      // Crop the image
 791      $full_cropped  = wp_crop_image( $original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, bp_core_avatar_full_width(),  bp_core_avatar_full_height(),  false, $avatar_folder_dir . '/' . $full_filename  );
 792      $thumb_cropped = wp_crop_image( $original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, bp_core_avatar_thumb_width(), bp_core_avatar_thumb_height(), false, $avatar_folder_dir . '/' . $thumb_filename );
 793  
 794      // Check for errors
 795      if ( empty( $full_cropped ) || empty( $thumb_cropped ) || is_wp_error( $full_cropped ) || is_wp_error( $thumb_cropped ) )
 796          return false;
 797  
 798      // Remove the original
 799      @unlink( $original_file );
 800  
 801      return true;
 802  }
 803  
 804  /**
 805   * Replace default WordPress avatars with BP avatars, if available.
 806   *
 807   * Filters 'get_avatar'.
 808   *
 809   * @param string $avatar The avatar path passed to 'get_avatar'.
 810   * @param int|string|object $user A user ID, email address, or comment object.
 811   * @param int $size Size of the avatar image ('thumb' or 'full').
 812   * @param string $default URL to a default image to use if no avatar is available.
 813   * @param string $alt Alternate text to use in image tag. Default: ''.
 814   * @return string BP avatar path, if found; else the original avatar path.
 815   */
 816  function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt = '' ) {
 817      global $pagenow;
 818  
 819      // Do not filter if inside WordPress options page
 820      if ( 'options-discussion.php' == $pagenow )
 821          return $avatar;
 822  
 823      // If passed an object, assume $user->user_id
 824      if ( is_object( $user ) ) {
 825          $id = $user->user_id;
 826  
 827      // If passed a number, assume it was a $user_id
 828      } else if ( is_numeric( $user ) ) {
 829          $id = $user;
 830  
 831      // If passed a string and that string returns a user, get the $id
 832      } elseif ( is_string( $user ) && ( $user_by_email = get_user_by( 'email', $user ) ) ) {
 833          $id = $user_by_email->ID;
 834      }
 835  
 836      // If somehow $id hasn't been assigned, return the result of get_avatar
 837      if ( empty( $id ) ) {
 838          return !empty( $avatar ) ? $avatar : $default;
 839      }
 840  
 841      // Image alt tag
 842      if ( empty( $alt ) ) {
 843          $alt = sprintf( __( 'Avatar of %s', 'buddypress' ), bp_core_get_user_displayname( $id ) );
 844      }
 845  
 846      // Use the 'thumb' type, unless the requested width is bigger than
 847      // BP's thumb width.
 848      $type = 'thumb';
 849      if ( (int) $size > bp_core_avatar_thumb_width() ) {
 850          $type = 'full';
 851      }
 852  
 853      // Let BuddyPress handle the fetching of the avatar
 854      $bp_avatar = bp_core_fetch_avatar( array(
 855          'item_id' => $id,
 856          'type'    => $type,
 857          'width'   => $size,
 858          'height'  => $size,
 859          'alt'     => $alt,
 860      ) );
 861  
 862      // If BuddyPress found an avatar, use it. If not, use the result of get_avatar
 863      return ( !$bp_avatar ) ? $avatar : $bp_avatar;
 864  }
 865  add_filter( 'get_avatar', 'bp_core_fetch_avatar_filter', 10, 5 );
 866  
 867  /**
 868   * Is the current avatar upload error-free?
 869   *
 870   * @param array $file The $_FILES array.
 871   * @return bool True if no errors are found. False if there are errors.
 872   */
 873  function bp_core_check_avatar_upload( $file ) {
 874      if ( isset( $file['error'] ) && $file['error'] )
 875          return false;
 876  
 877      return true;
 878  }
 879  
 880  /**
 881   * Is the file size of the current avatar upload permitted?
 882   *
 883   * @param array $file The $_FILES array.
 884   * @return bool True if the avatar is under the size limit, otherwise false.
 885   */
 886  function bp_core_check_avatar_size( $file ) {
 887      if ( $file['file']['size'] > bp_core_avatar_original_max_filesize() )
 888          return false;
 889  
 890      return true;
 891  }
 892  
 893  /**
 894   * Does the current avatar upload have an allowed file type?
 895   *
 896   * Permitted file types are JPG, GIF and PNG.
 897   *
 898   * @param array $file The $_FILES array.
 899   * @return bool True if the file extension is permitted, otherwise false.
 900   */
 901  function bp_core_check_avatar_type($file) {
 902      if ( ( !empty( $file['file']['type'] ) && !preg_match('/(jpe?g|gif|png)$/i', $file['file']['type'] ) ) || !preg_match( '/(jpe?g|gif|png)$/i', $file['file']['name'] ) )
 903          return false;
 904  
 905      return true;
 906  }
 907  
 908  /**
 909   * Fetch data from the BP root blog's upload directory.
 910   *
 911   * Handy for multisite instances because all uploads are made on the BP root
 912   * blog and we need to query the BP root blog for the upload directory data.
 913   *
 914   * This function ensures that we only need to use {@link switch_to_blog()}
 915   * once to get what we need.
 916   *
 917   * @since BuddyPress (1.8.0)
 918   *
 919   * @uses wp_upload_dir()
 920   *
 921   * @param string $type The variable we want to return from the $bp->avatars
 922   *        object. Only 'upload_path' and 'url' are supported. Default: 'upload_path'.
 923   * @return string The avatar upload directory path.
 924   */
 925  function bp_core_get_upload_dir( $type = 'upload_path' ) {
 926      $bp = buddypress();
 927  
 928      switch ( $type ) {
 929          case 'upload_path' :
 930              $constant = 'BP_AVATAR_UPLOAD_PATH';
 931              $key      = 'basedir';
 932  
 933              break;
 934  
 935          case 'url' :
 936              $constant = 'BP_AVATAR_URL';
 937              $key      = 'baseurl';
 938  
 939              break;
 940  
 941          default :
 942              return false;
 943  
 944              break;
 945      }
 946  
 947      // See if the value has already been calculated and stashed in the $bp global
 948      if ( isset( $bp->avatar->$type ) ) {
 949          $retval = $bp->avatar->$type;
 950      } else {
 951          // If this value has been set in a constant, just use that
 952          if ( defined( $constant ) ) {
 953              $retval = constant( $constant );
 954          } else {
 955  
 956              // Use cached upload dir data if available
 957              if ( ! empty( $bp->avatar->upload_dir ) ) {
 958                  $upload_dir = $bp->avatar->upload_dir;
 959  
 960              // No cache, so query for it
 961              } else {
 962                  // We need to switch to the root blog on multisite installs
 963                  if ( is_multisite() ) {
 964                      switch_to_blog( bp_get_root_blog_id() );
 965                  }
 966  
 967                  // Get upload directory information from current site
 968                  $upload_dir = wp_upload_dir();
 969  
 970                  // Will bail if not switched
 971                  restore_current_blog();
 972  
 973                  // Stash upload directory data for later use
 974                  $bp->avatar->upload_dir = $upload_dir;
 975              }
 976  
 977              // Directory does not exist and cannot be created
 978              if ( ! empty( $upload_dir['error'] ) ) {
 979                  $retval = '';
 980  
 981              } else {
 982                  $retval = $upload_dir[$key];
 983  
 984                  // If $key is 'baseurl', check to see if we're on SSL
 985                  // Workaround for WP13941, WP15928, WP19037.
 986                  if ( $key == 'baseurl' && is_ssl() ) {
 987                      $retval = str_replace( 'http://', 'https://', $retval );
 988                  }
 989              }
 990  
 991          }
 992  
 993          // Stash in $bp for later use
 994          $bp->avatar->$type = $retval;
 995      }
 996  
 997      return $retval;
 998  }
 999  
1000  /**
1001   * Get the absolute upload path for the WP installation.
1002   *
1003   * @uses wp_upload_dir To get upload directory info
1004   *
1005   * @return string Absolute path to WP upload directory.
1006   */
1007  function bp_core_avatar_upload_path() {
1008      return apply_filters( 'bp_core_avatar_upload_path', bp_core_get_upload_dir() );
1009  }
1010  
1011  /**
1012   * Get the raw base URL for root site upload location.
1013   *
1014   * @uses wp_upload_dir To get upload directory info.
1015   *
1016   * @return string Full URL to current upload location.
1017   */
1018  function bp_core_avatar_url() {
1019      return apply_filters( 'bp_core_avatar_url', bp_core_get_upload_dir( 'url' ) );
1020  }
1021  
1022  /**
1023   * Check if a given user ID has an uploaded avatar.
1024   *
1025   * @since BuddyPress (1.0.0)
1026   *
1027   * @param int $user_id ID of the user whose avatar is being checked.
1028   * @return bool True if the user has uploaded a local avatar. Otherwise false.
1029   */
1030  function bp_get_user_has_avatar( $user_id = 0 ) {
1031  
1032      if ( empty( $user_id ) )
1033          $user_id = bp_displayed_user_id();
1034  
1035      $retval = false;
1036      if ( bp_core_fetch_avatar( array( 'item_id' => $user_id, 'no_grav' => true, 'html' => false ) ) != bp_core_avatar_default( 'local' ) )
1037          $retval = true;
1038  
1039      return (bool) apply_filters( 'bp_get_user_has_avatar', $retval, $user_id );
1040  }
1041  
1042  /**
1043   * Utility function for fetching an avatar dimension setting.
1044   *
1045   * @since BuddyPress (1.5.0)
1046   *
1047   * @param string $type Dimension type you're fetching dimensions for. 'thumb'
1048   *        or 'full'. Default: 'thumb'.
1049   * @param string $h_or_w Which dimension is being fetched. 'height' or 'width'.
1050   *        Default: 'height'.
1051   * @return int $dim The dimension.
1052   */
1053  function bp_core_avatar_dimension( $type = 'thumb', $h_or_w = 'height' ) {
1054      $bp  = buddypress();
1055      $dim = isset( $bp->avatar->{$type}->{$h_or_w} ) ? (int) $bp->avatar->{$type}->{$h_or_w} : false;
1056  
1057      return apply_filters( 'bp_core_avatar_dimension', $dim, $type, $h_or_w );
1058  }
1059  
1060  /**
1061   * Get the 'thumb' avatar width setting.
1062   *
1063   * @since BuddyPress (1.5.0)
1064   *
1065   * @return int The 'thumb' width.
1066   */
1067  function bp_core_avatar_thumb_width() {
1068      return apply_filters( 'bp_core_avatar_thumb_width', bp_core_avatar_dimension( 'thumb', 'width' ) );
1069  }
1070  
1071  /**
1072   * Get the 'thumb' avatar height setting.
1073   *
1074   * @since BuddyPress (1.5.0)
1075   *
1076   * @return int The 'thumb' height.
1077   */
1078  function bp_core_avatar_thumb_height() {
1079      return apply_filters( 'bp_core_avatar_thumb_height', bp_core_avatar_dimension( 'thumb', 'height' ) );
1080  }
1081  
1082  /**
1083   * Get the 'full' avatar width setting
1084   *
1085   * @since BuddyPress (1.5.0)
1086   *
1087   * @return int The 'full' width.
1088   */
1089  function bp_core_avatar_full_width() {
1090      return apply_filters( 'bp_core_avatar_full_width', bp_core_avatar_dimension( 'full', 'width' ) );
1091  }
1092  
1093  /**
1094   * Get the 'full' avatar height setting.
1095   *
1096   * @since BuddyPress (1.5.0)
1097   *
1098   * @return int The 'full' height.
1099   */
1100  function bp_core_avatar_full_height() {
1101      return apply_filters( 'bp_core_avatar_full_height', bp_core_avatar_dimension( 'full', 'height' ) );
1102  }
1103  
1104  /**
1105   * Get the max width for original avatar uploads.
1106   *
1107   * @since BuddyPress (1.5.0)
1108   *
1109   * @return int The max width for original avatar uploads.
1110   */
1111  function bp_core_avatar_original_max_width() {
1112      return apply_filters( 'bp_core_avatar_original_max_width', (int) buddypress()->avatar->original_max_width );
1113  }
1114  
1115  /**
1116   * Get the max filesize for original avatar uploads.
1117   *
1118   * @since BuddyPress (1.5.0)
1119   *
1120   * @return int The max filesize for original avatar uploads.
1121   */
1122  function bp_core_avatar_original_max_filesize() {
1123      return apply_filters( 'bp_core_avatar_original_max_filesize', (int) buddypress()->avatar->original_max_filesize );
1124  }
1125  
1126  /**
1127   * Get the URL of the 'full' default avatar.
1128   *
1129   * @since BuddyPress (1.5.0)
1130   *
1131   * @param string $type 'local' if the fallback should be the locally-hosted
1132   *        version of the mystery-man, 'gravatar' if the fallback should be
1133   *        Gravatar's version. Default: 'gravatar'.
1134   * @return string The URL of the default avatar.
1135   */
1136  function bp_core_avatar_default( $type = 'gravatar' ) {
1137      // Local override
1138      if ( defined( 'BP_AVATAR_DEFAULT' ) ) {
1139          $avatar = BP_AVATAR_DEFAULT;
1140  
1141      // Use the local default image
1142      } else if ( 'local' === $type ) {
1143          $avatar = buddypress()->plugin_url . 'bp-core/images/mystery-man.jpg';
1144  
1145      // Use Gravatar's mystery man as fallback
1146      } else {
1147          if ( is_ssl() ) {
1148              $host = 'https://secure.gravatar.com';
1149          } else {
1150              $host = 'http://www.gravatar.com';
1151          }
1152  
1153          $avatar = $host . '/avatar/00000000000000000000000000000000?d=mm&amp;s=' . bp_core_avatar_full_width();
1154      }
1155  
1156      return apply_filters( 'bp_core_avatar_default', $avatar );
1157  }
1158  
1159  /**
1160   * Get the URL of the 'thumb' default avatar.
1161   *
1162   * Uses Gravatar's mystery-man avatar, unless BP_AVATAR_DEFAULT_THUMB has been
1163   * defined.
1164   *
1165   * @since BuddyPress (1.5.0)
1166   *
1167   * @param string $type 'local' if the fallback should be the locally-hosted
1168   *        version of the mystery-man, 'gravatar' if the fallback should be
1169   *        Gravatar's version. Default: 'gravatar'.
1170   * @return string The URL of the default avatar thumb.
1171   */
1172  function bp_core_avatar_default_thumb( $type = 'gravatar' ) {
1173      // Local override
1174      if ( defined( 'BP_AVATAR_DEFAULT_THUMB' ) ) {
1175          $avatar = BP_AVATAR_DEFAULT_THUMB;
1176  
1177      // Use the local default image
1178      } else if ( 'local' === $type ) {
1179          $avatar = buddypress()->plugin_url . 'bp-core/images/mystery-man-50.jpg';
1180  
1181      // Use Gravatar's mystery man as fallback
1182      } else {
1183          if ( is_ssl() ) {
1184              $host = 'https://secure.gravatar.com';
1185          } else {
1186              $host = 'http://www.gravatar.com';
1187          }
1188  
1189          $avatar = $host . '/avatar/00000000000000000000000000000000?d=mm&amp;s=' . bp_core_avatar_thumb_width();
1190      }
1191  
1192      return apply_filters( 'bp_core_avatar_thumb', $avatar );
1193  }


Generated: Sun May 4 03:58:45 2014 Hosted by follow the white rabbit.