[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Avatars.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Set up the constants we need for avatar support.
  15   *
  16   * @since 1.2.0
  17   */
  18  function bp_core_set_avatar_constants() {
  19  
  20      $bp = buddypress();
  21  
  22      if ( !defined( 'BP_AVATAR_THUMB_WIDTH' ) )
  23          define( 'BP_AVATAR_THUMB_WIDTH', 50 );
  24  
  25      if ( !defined( 'BP_AVATAR_THUMB_HEIGHT' ) )
  26          define( 'BP_AVATAR_THUMB_HEIGHT', 50 );
  27  
  28      if ( !defined( 'BP_AVATAR_FULL_WIDTH' ) )
  29          define( 'BP_AVATAR_FULL_WIDTH', 150 );
  30  
  31      if ( !defined( 'BP_AVATAR_FULL_HEIGHT' ) )
  32          define( 'BP_AVATAR_FULL_HEIGHT', 150 );
  33  
  34      if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_WIDTH' ) )
  35          define( 'BP_AVATAR_ORIGINAL_MAX_WIDTH', 450 );
  36  
  37      if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE' ) ) {
  38          define( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE', bp_attachments_get_max_upload_file_size( 'avatar' ) );
  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 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      /**
  90       * Fires at the end of the core avatar globals setup.
  91       *
  92       * @since 1.5.0
  93       */
  94      do_action( 'bp_core_set_avatar_globals' );
  95  }
  96  add_action( 'bp_setup_globals', 'bp_core_set_avatar_globals' );
  97  
  98  /**
  99   * Get an avatar for a BuddyPress object.
 100   *
 101   * Supports avatars for users, groups, and blogs by default, but can be
 102   * extended to support custom components as well.
 103   *
 104   * This function gives precedence to locally-uploaded avatars. When a local
 105   * avatar is not found, Gravatar is queried. To disable Gravatar fallbacks
 106   * locally:
 107   *    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
 108   *
 109   * @since 1.1.0
 110   * @since 2.4.0 Added 'extra_attr', 'scheme', 'rating' and 'force_default' for $args.
 111   *              These are inherited from WordPress 4.2.0. See {@link get_avatar()}.
 112   *
 113   * @param array|string $args {
 114   *     An array of arguments. All arguments are technically optional; some
 115   *     will, if not provided, be auto-detected by bp_core_fetch_avatar(). This
 116   *     auto-detection is described more below, when discussing specific
 117   *     arguments.
 118   *
 119   *     @type int|bool    $item_id    The numeric ID of the item for which you're requesting
 120   *                                   an avatar (eg, a user ID). If no 'item_id' is present,
 121   *                                   the function attempts to infer an ID from the 'object' + the
 122   *                                   current context: if 'object' is 'user' and the current page is a
 123   *                                   user page, 'item_id' will default to the displayed user ID; if
 124   *                                   'group' and on a group page, to the current group ID; if 'blog',
 125   *                                   to the current blog's ID. If no 'item_id' can be determined in
 126   *                                   this way, the function returns false. Default: false.
 127   *     @type string      $object     The kind of object for which you're getting an
 128   *                                   avatar. BuddyPress natively supports three options: 'user',
 129   *                                   'group', 'blog'; a plugin may register more.  Default: 'user'.
 130   *     @type string      $type       When a new avatar is uploaded to BP, 'thumb' and
 131   *                                   'full' versions are saved. This parameter specifies whether you'd
 132   *                                   like the 'full' or smaller 'thumb' avatar. Default: 'thumb'.
 133   *     @type string|bool $avatar_dir The name of the subdirectory where the
 134   *                                   requested avatar should be found. If no value is passed,
 135   *                                   'avatar_dir' is inferred from 'object': 'user' becomes 'avatars',
 136   *                                   'group' becomes 'group-avatars', 'blog' becomes 'blog-avatars'.
 137   *                                   Remember that this string denotes a subdirectory of BP's main
 138   *                                   avatar directory (usually based on {@link wp_upload_dir()}); it's a
 139   *                                   string like 'group-avatars' rather than the full directory path.
 140   *                                   Generally, it'll only be necessary to override the default value if
 141   *                                   storing avatars in a non-default location. Defaults to false
 142   *                                   (auto-detected).
 143   *     @type int|bool    $width      Requested avatar width. The unit is px. This value
 144   *                                   is used to build the 'width' attribute for the <img> element. If
 145   *                                   no value is passed, BP uses the global avatar width for this
 146   *                                   avatar type. Default: false (auto-detected).
 147   *     @type int|bool    $height     Requested avatar height. The unit is px. This
 148   *                                   value is used to build the 'height' attribute for the <img>
 149   *                                   element. If no value is passed, BP uses the global avatar height
 150   *                                   for this avatar type. Default: false (auto-detected).
 151   *     @type string      $class      The CSS class for the <img> element. Note that BP
 152   *                                   uses the 'avatar' class fairly extensively in its default styling,
 153   *                                   so if you plan to pass a custom value, consider appending it to
 154   *                                   'avatar' (eg 'avatar foo') rather than replacing it altogether.
 155   *                                   Default: 'avatar'.
 156   *     @type string|bool $css_id     The CSS id for the <img> element.
 157   *                                   Default: false.
 158   *     @type string      $title      The title attribute for the <img> element.
 159   *                                   Default: false.
 160   *     @type string      $alt        The alt attribute for the <img> element. In BP, this
 161   *                                   value is generally passed by the wrapper functions, where the data
 162   *                                   necessary for concatenating the string is at hand; see
 163   *                                   {@link bp_get_activity_avatar()} for an example. Default: ''.
 164   *     @type string|bool $email      An email to use in Gravatar queries. Unless
 165   *                                   otherwise configured, BP uses Gravatar as a fallback for avatars
 166   *                                   that are not provided locally. Gravatar's API requires using a hash
 167   *                                   of the user's email address; this argument provides it. If not
 168   *                                   provided, the function will infer it: for users, by getting the
 169   *                                   user's email from the database, for groups/blogs, by concatenating
 170   *                                   "{$item_id}-{$object}@{bp_get_root_domain()}". The user query adds
 171   *                                   overhead, so it's recommended that wrapper functions provide a
 172   *                                   value for 'email' when querying user IDs. Default: false.
 173   *     @type bool       $no_grav     Whether to disable the default Gravatar fallback.
 174   *                                   By default, BP will fall back on Gravatar when it cannot find a
 175   *                                   local avatar. In some cases, this may be undesirable, in which
 176   *                                   case 'no_grav' should be set to true. To disable Gravatar
 177   *                                   fallbacks globally, see the 'bp_core_fetch_avatar_no_grav' filter.
 178   *                                   Default: true for groups, otherwise false.
 179   *     @type bool       $html        Whether to return an <img> HTML element, vs a raw URL
 180   *                                   to an avatar. If false, <img>-specific arguments (like 'css_id')
 181   *                                   will be ignored. Default: true.
 182   *     @type string     $extra_attr  HTML attributes to insert in the IMG element. Not sanitized. Default: ''.
 183   *     @type string     $scheme      URL scheme to use. See set_url_scheme() for accepted values.
 184   *                                   Default null.
 185   *     @type string     $rating      What rating to display Gravatars for. Accepts 'G', 'PG', 'R', 'X'.
 186   *                                   Default is the value of the 'avatar_rating' option.
 187   *     @type bool       $force_default Used when creating the Gravatar URL. Whether to force the default
 188   *                                     image regardless if the Gravatar exists. Default: false.
 189   * }
 190   * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg.
 191   */
 192  function bp_core_fetch_avatar( $args = '' ) {
 193      $bp = buddypress();
 194  
 195      // If avatars are disabled for the root site, obey that request and bail.
 196      if ( ! $bp->avatar->show_avatars ) {
 197          return;
 198      }
 199  
 200      global $current_blog;
 201  
 202      // Set the default variables array and parse it against incoming $args array.
 203      $params = wp_parse_args( $args, array(
 204          'item_id'       => false,
 205          'object'        => 'user',
 206          'type'          => 'thumb',
 207          'avatar_dir'    => false,
 208          'width'         => false,
 209          'height'        => false,
 210          'class'         => 'avatar',
 211          'css_id'        => false,
 212          'alt'           => '',
 213          'email'         => false,
 214          'no_grav'       => null,
 215          'html'          => true,
 216          'title'         => '',
 217          'extra_attr'    => '',
 218          'scheme'        => null,
 219          'rating'        => get_option( 'avatar_rating' ),
 220          'force_default' => false,
 221      ) );
 222  
 223      /* Set item_id ***********************************************************/
 224  
 225      if ( empty( $params['item_id'] ) ) {
 226  
 227          switch ( $params['object'] ) {
 228  
 229              case 'blog'  :
 230                  $params['item_id'] = $current_blog->id;
 231                  break;
 232  
 233              case 'group' :
 234                  if ( bp_is_active( 'groups' ) ) {
 235                      $params['item_id'] = $bp->groups->current_group->id;
 236                  } else {
 237                      $params['item_id'] = false;
 238                  }
 239  
 240                  break;
 241  
 242              case 'user'  :
 243              default      :
 244                  $params['item_id'] = bp_displayed_user_id();
 245                  break;
 246          }
 247  
 248          /**
 249           * Filters the ID of the item being requested.
 250           *
 251           * @since 1.1.0
 252           *
 253           * @param string $value  ID of avatar item being requested.
 254           * @param string $value  Avatar type being requested.
 255           * @param array  $params Array of parameters for the request.
 256           */
 257          $params['item_id'] = apply_filters( 'bp_core_avatar_item_id', $params['item_id'], $params['object'], $params );
 258  
 259          if ( empty( $params['item_id'] ) ) {
 260              return false;
 261          }
 262      }
 263  
 264      /* Set avatar_dir ********************************************************/
 265  
 266      if ( empty( $params['avatar_dir'] ) ) {
 267  
 268          switch ( $params['object'] ) {
 269  
 270              case 'blog'  :
 271                  $params['avatar_dir'] = 'blog-avatars';
 272                  break;
 273  
 274              case 'group' :
 275                  if ( bp_is_active( 'groups' ) ) {
 276                      $params['avatar_dir'] = 'group-avatars';
 277                  } else {
 278                      $params['avatar_dir'] = false;
 279                  }
 280  
 281                  break;
 282  
 283              case 'user'  :
 284              default      :
 285                  $params['avatar_dir'] = 'avatars';
 286                  break;
 287          }
 288  
 289          /**
 290           * Filters the avatar directory to use.
 291           *
 292           * @since 1.1.0
 293           *
 294           * @param string $value  Name of the subdirectory where the requested avatar should be found.
 295           * @param string $value  Avatar type being requested.
 296           * @param array  $params Array of parameters for the request.
 297           */
 298          $params['avatar_dir'] = apply_filters( 'bp_core_avatar_dir', $params['avatar_dir'], $params['object'], $params );
 299  
 300          if ( empty( $params['avatar_dir'] ) ) {
 301              return false;
 302          }
 303      }
 304  
 305      /* <img> alt *************************************************************/
 306  
 307      if ( false !== strpos( $params['alt'], '%s' ) || false !== strpos( $params['alt'], '%1$s' ) ) {
 308  
 309          switch ( $params['object'] ) {
 310  
 311              case 'blog'  :
 312                  $item_name = get_blog_option( $params['item_id'], 'blogname' );
 313                  break;
 314  
 315              case 'group' :
 316                  $item_name = bp_get_group_name( groups_get_group( $params['item_id'] ) );
 317                  break;
 318  
 319              case 'user'  :
 320              default :
 321                  $item_name = bp_core_get_user_displayname( $params['item_id'] );
 322                  break;
 323          }
 324  
 325          /**
 326           * Filters the alt attribute value to be applied to avatar.
 327           *
 328           * @since 1.5.0
 329           *
 330           * @param string $value  alt to be applied to avatar.
 331           * @param string $value  ID of avatar item being requested.
 332           * @param string $value  Avatar type being requested.
 333           * @param array  $params Array of parameters for the request.
 334           */
 335          $item_name = apply_filters( 'bp_core_avatar_alt', $item_name, $params['item_id'], $params['object'], $params );
 336          $params['alt'] = sprintf( $params['alt'], $item_name );
 337      }
 338  
 339      /* Sanity Checks *********************************************************/
 340  
 341      // Get a fallback for the 'alt' parameter, create html output.
 342      if ( empty( $params['alt'] ) ) {
 343          $params['alt'] = __( 'Profile Photo', 'buddypress' );
 344      }
 345      $html_alt = ' alt="' . esc_attr( $params['alt'] ) . '"';
 346  
 347      // Filter image title and create html string.
 348      $html_title = '';
 349  
 350      /**
 351       * Filters the title attribute value to be applied to avatar.
 352       *
 353       * @since 1.5.0
 354       *
 355       * @param string $value  Title to be applied to avatar.
 356       * @param string $value  ID of avatar item being requested.
 357       * @param string $value  Avatar type being requested.
 358       * @param array  $params Array of parameters for the request.
 359       */
 360      $params['title'] = apply_filters( 'bp_core_avatar_title', $params['title'], $params['item_id'], $params['object'], $params );
 361  
 362      if ( ! empty( $params['title'] ) ) {
 363          $html_title = ' title="' . esc_attr( $params['title'] ) . '"';
 364      }
 365  
 366      // Extra attributes.
 367      $extra_attr = ! empty( $args['extra_attr'] ) ? ' ' . $args['extra_attr'] : '';
 368  
 369      // Set CSS ID and create html string.
 370      $html_css_id = '';
 371  
 372      /**
 373       * Filters the ID attribute to be applied to avatar.
 374       *
 375       * @since 2.2.0
 376       *
 377       * @param string $value  ID to be applied to avatar.
 378       * @param string $value  ID of avatar item being requested.
 379       * @param string $value  Avatar type being requested.
 380       * @param array  $params Array of parameters for the request.
 381       */
 382      $params['css_id'] = apply_filters( 'bp_core_css_id', $params['css_id'], $params['item_id'], $params['object'], $params );
 383  
 384      if ( ! empty( $params['css_id'] ) ) {
 385          $html_css_id = ' id="' . esc_attr( $params['css_id'] ) . '"';
 386      }
 387  
 388      // Set image width.
 389      if ( false !== $params['width'] ) {
 390          // Width has been specified. No modification necessary.
 391      } elseif ( 'thumb' == $params['type'] ) {
 392          $params['width'] = bp_core_avatar_thumb_width();
 393      } else {
 394          $params['width'] = bp_core_avatar_full_width();
 395      }
 396      $html_width = ' width="' . $params['width'] . '"';
 397  
 398      // Set image height.
 399      if ( false !== $params['height'] ) {
 400          // Height has been specified. No modification necessary.
 401      } elseif ( 'thumb' == $params['type'] ) {
 402          $params['height'] = bp_core_avatar_thumb_height();
 403      } else {
 404          $params['height'] = bp_core_avatar_full_height();
 405      }
 406      $html_height = ' height="' . $params['height'] . '"';
 407  
 408      /**
 409       * Filters the classes to be applied to the avatar.
 410       *
 411       * @since 1.6.0
 412       *
 413       * @param array|string $value  Class(es) to be applied to the avatar.
 414       * @param string       $value  ID of the avatar item being requested.
 415       * @param string       $value  Avatar type being requested.
 416       * @param array        $params Array of parameters for the request.
 417       */
 418      $params['class'] = apply_filters( 'bp_core_avatar_class', $params['class'], $params['item_id'], $params['object'], $params );
 419  
 420      // Use an alias to leave the param unchanged.
 421      $avatar_classes = $params['class'];
 422      if ( ! is_array( $avatar_classes ) ) {
 423          $avatar_classes = explode( ' ', $avatar_classes );
 424      }
 425  
 426      // Merge classes.
 427      $avatar_classes = array_merge( $avatar_classes, array(
 428          $params['object'] . '-' . $params['item_id'] . '-avatar',
 429          'avatar-' . $params['width'],
 430      ) );
 431  
 432      // Sanitize each class.
 433      $avatar_classes = array_map( 'sanitize_html_class', $avatar_classes );
 434  
 435      // Populate the class attribute.
 436      $html_class = ' class="' . join( ' ', $avatar_classes ) . ' photo"';
 437  
 438      // Set img URL and DIR based on prepopulated constants.
 439      $avatar_loc        = new stdClass();
 440      $avatar_loc->path  = trailingslashit( bp_core_avatar_upload_path() );
 441      $avatar_loc->url   = trailingslashit( bp_core_avatar_url() );
 442  
 443      $avatar_loc->dir   = trailingslashit( $params['avatar_dir'] );
 444  
 445      /**
 446       * Filters the avatar folder directory URL.
 447       *
 448       * @since 1.1.0
 449       *
 450       * @param string $value Path to the avatar folder URL.
 451       * @param int    $value ID of the avatar item being requested.
 452       * @param string $value Avatar type being requested.
 453       * @param string $value Subdirectory where the requested avatar should be found.
 454       */
 455      $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', ( $avatar_loc->url  . $avatar_loc->dir . $params['item_id'] ), $params['item_id'], $params['object'], $params['avatar_dir'] );
 456  
 457      /**
 458       * Filters the avatar folder directory path.
 459       *
 460       * @since 1.1.0
 461       *
 462       * @param string $value Path to the avatar folder directory.
 463       * @param int    $value ID of the avatar item being requested.
 464       * @param string $value Avatar type being requested.
 465       * @param string $value Subdirectory where the requested avatar should be found.
 466       */
 467      $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', ( $avatar_loc->path . $avatar_loc->dir . $params['item_id'] ), $params['item_id'], $params['object'], $params['avatar_dir'] );
 468  
 469      /**
 470       * Look for uploaded avatar first. Use it if it exists.
 471       * Set the file names to search for, to select the full size
 472       * or thumbnail image.
 473       */
 474      $avatar_size              = ( 'full' == $params['type'] ) ? '-bpfull' : '-bpthumb';
 475      $legacy_user_avatar_name  = ( 'full' == $params['type'] ) ? '-avatar2' : '-avatar1';
 476      $legacy_group_avatar_name = ( 'full' == $params['type'] ) ? '-groupavatar-full' : '-groupavatar-thumb';
 477  
 478      // Check for directory.
 479      if ( file_exists( $avatar_folder_dir ) ) {
 480  
 481          // Open directory.
 482          if ( $av_dir = opendir( $avatar_folder_dir ) ) {
 483  
 484              // Stash files in an array once to check for one that matches.
 485              $avatar_files = array();
 486              while ( false !== ( $avatar_file = readdir( $av_dir ) ) ) {
 487                  // Only add files to the array (skip directories).
 488                  if ( 2 < strlen( $avatar_file ) ) {
 489                      $avatar_files[] = $avatar_file;
 490                  }
 491              }
 492  
 493              // Check for array.
 494              if ( 0 < count( $avatar_files ) ) {
 495  
 496                  // Check for current avatar.
 497                  foreach( $avatar_files as $key => $value ) {
 498                      if ( strpos ( $value, $avatar_size )!== false ) {
 499                          $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
 500                      }
 501                  }
 502  
 503                  // Legacy avatar check.
 504                  if ( !isset( $avatar_url ) ) {
 505                      foreach( $avatar_files as $key => $value ) {
 506                          if ( strpos ( $value, $legacy_user_avatar_name )!== false ) {
 507                              $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
 508                          }
 509                      }
 510  
 511                      // Legacy group avatar check.
 512                      if ( !isset( $avatar_url ) ) {
 513                          foreach( $avatar_files as $key => $value ) {
 514                              if ( strpos ( $value, $legacy_group_avatar_name )!== false ) {
 515                                  $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
 516                              }
 517                          }
 518                      }
 519                  }
 520              }
 521          }
 522  
 523          // Close the avatar directory.
 524          closedir( $av_dir );
 525  
 526          // If we found a locally uploaded avatar.
 527          if ( isset( $avatar_url ) ) {
 528              // Support custom scheme.
 529              $avatar_url = set_url_scheme( $avatar_url, $params['scheme'] );
 530  
 531              // Return it wrapped in an <img> element.
 532              if ( true === $params['html'] ) {
 533  
 534                  /**
 535                   * Filters an avatar URL wrapped in an <img> element.
 536                   *
 537                   * @since 1.1.0
 538                   *
 539                   * @param string $value             Full <img> element for an avatar.
 540                   * @param array  $params            Array of parameters for the request.
 541                   * @param string $value             ID of the item requested.
 542                   * @param string $value             Subdirectory where the requested avatar should be found.
 543                   * @param string $html_css_id       ID attribute for avatar.
 544                   * @param string $html_width        Width attribute for avatar.
 545                   * @param string $html_height       Height attribute for avatar.
 546                   * @param string $avatar_folder_url Avatar URL path.
 547                   * @param string $avatar_folder_dir Avatar DIR path.
 548                   */
 549                  return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '"' . $html_class . $html_css_id  . $html_width . $html_height . $html_alt . $html_title . $extra_attr . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
 550  
 551              // ...or only the URL
 552              } else {
 553  
 554                  /**
 555                   * Filters a locally uploaded avatar URL.
 556                   *
 557                   * @since 1.2.5
 558                   *
 559                   * @param string $avatar_url URL for a locally uploaded avatar.
 560                   * @param array  $params     Array of parameters for the request.
 561                   */
 562                  return apply_filters( 'bp_core_fetch_avatar_url', $avatar_url, $params );
 563              }
 564          }
 565      }
 566  
 567      // By default, Gravatar is not pinged for groups.
 568      if ( null === $params['no_grav'] ) {
 569          $params['no_grav'] = 'group' === $params['object'];
 570      }
 571  
 572      /**
 573       * Filters whether or not to skip Gravatar check.
 574       *
 575       * @since 1.5.0
 576       *
 577       * @param bool  $value  Whether or not to skip Gravatar.
 578       * @param array $params Array of parameters for the avatar request.
 579       */
 580      if ( ! apply_filters( 'bp_core_fetch_avatar_no_grav', $params['no_grav'], $params ) ) {
 581  
 582          // Set gravatar type.
 583          if ( empty( $bp->grav_default->{$params['object']} ) ) {
 584              $default_grav = 'wavatar';
 585          } elseif ( 'mystery' == $bp->grav_default->{$params['object']} ) {
 586  
 587              /**
 588               * Filters the Mystery person avatar src value.
 589               *
 590               * @since 1.2.0
 591               *
 592               * @param string $value Avatar value.
 593               * @param string $value Width to display avatar at.
 594               */
 595              $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $params['width'] );
 596          } else {
 597              $default_grav = $bp->grav_default->{$params['object']};
 598          }
 599  
 600          // Set gravatar object.
 601          if ( empty( $params['email'] ) ) {
 602              if ( 'user' == $params['object'] ) {
 603                  $params['email'] = bp_core_get_user_email( $params['item_id'] );
 604              } elseif ( 'group' == $params['object'] || 'blog' == $params['object'] ) {
 605                  $params['email'] = $params['item_id'] . '-' . $params['object'] . '@' . bp_get_root_domain();
 606              }
 607          }
 608  
 609          /**
 610           * Filters the Gravatar email to use.
 611           *
 612           * @since 1.1.0
 613           *
 614           * @param string $value Email to use in Gravatar request.
 615           * @param string $value ID of the item being requested.
 616           * @param string $value Object type being requested.
 617           */
 618          $params['email'] = apply_filters( 'bp_core_gravatar_email', $params['email'], $params['item_id'], $params['object'] );
 619  
 620          /**
 621           * Filters the Gravatar URL host.
 622           *
 623           * @since 1.0.2
 624           *
 625           * @param string $value Gravatar URL host.
 626           */
 627          $gravatar = apply_filters( 'bp_gravatar_url', '//www.gravatar.com/avatar/' );
 628  
 629          // Append email hash to Gravatar.
 630          $gravatar .=  md5( strtolower( $params['email'] ) );
 631  
 632          // Main Gravatar URL args.
 633          $url_args = array(
 634              's' => $params['width']
 635          );
 636  
 637          // Custom Gravatar URL args.
 638          if ( ! empty( $params['force_default'] ) ) {
 639              $url_args['f'] = 'y';
 640          }
 641          if ( ! empty( $params['rating'] ) ) {
 642              $url_args['r'] = strtolower( $params['rating'] );
 643          }
 644  
 645          /**
 646           * Filters the Gravatar "d" parameter.
 647           *
 648           * @since 2.6.0
 649           *
 650           * @param string $default_grav The avatar default.
 651           * @param array  $params       The avatar's data.
 652           */
 653          $default_grav = apply_filters( 'bp_core_avatar_default', $default_grav, $params );
 654  
 655          // Only set default image if 'Gravatar Logo' is not requested.
 656          if ( 'gravatar_default' !== $default_grav ) {
 657              $url_args['d'] = $default_grav;
 658          }
 659  
 660          // Set up the Gravatar URL.
 661          $gravatar = esc_url( add_query_arg(
 662              rawurlencode_deep( array_filter( $url_args ) ),
 663              $gravatar
 664          ) );
 665  
 666      // No avatar was found, and we've been told not to use a gravatar.
 667      } else {
 668  
 669          /**
 670           * Filters the avatar default when Gravatar is not used.
 671           *
 672           * This is a variable filter dependent on the avatar type being requested.
 673           *
 674           * @since 1.5.0
 675           *
 676           * @param string $value  Default avatar for non-gravatar requests.
 677           * @param array  $params Array of parameters for the avatar request.
 678           */
 679          $gravatar = apply_filters( 'bp_core_default_avatar_' . $params['object'], bp_core_avatar_default( 'local', $params ), $params );
 680      }
 681  
 682      if ( true === $params['html'] ) {
 683  
 684          /** This filter is documented in bp-core/bp-core-avatars.php */
 685          return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '"' . $html_css_id . $html_class . $html_width . $html_height . $html_alt . $html_title . $extra_attr . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
 686      } else {
 687  
 688          /** This filter is documented in bp-core/bp-core-avatars.php */
 689          return apply_filters( 'bp_core_fetch_avatar_url', $gravatar, $params );
 690      }
 691  }
 692  
 693  /**
 694   * Delete an existing avatar.
 695   *
 696   * @since 1.1.0
 697   *
 698   * @param array|string $args {
 699   *     Array of function parameters.
 700   *     @type bool|int    $item_id    ID of the item whose avatar you're deleting.
 701   *                                   Defaults to the current item of type $object.
 702   *     @type string      $object     Object type of the item whose avatar you're
 703   *                                   deleting. 'user', 'group', 'blog', or custom.
 704   *                                   Default: 'user'.
 705   *     @type bool|string $avatar_dir Subdirectory where avatar is located.
 706   *                                   Default: false, which falls back on the default location
 707   *                                   corresponding to the $object.
 708   * }
 709   * @return bool True on success, false on failure.
 710   */
 711  function bp_core_delete_existing_avatar( $args = '' ) {
 712  
 713      $defaults = array(
 714          'item_id'    => false,
 715          'object'     => 'user', // User OR group OR blog OR custom type (if you use filters).
 716          'avatar_dir' => false
 717      );
 718  
 719      $args = wp_parse_args( $args, $defaults );
 720      extract( $args, EXTR_SKIP );
 721  
 722      /**
 723       * Filters whether or not to handle deleting an existing avatar.
 724       *
 725       * If you want to override this function, make sure you return false.
 726       *
 727       * @since 2.5.1
 728       *
 729       * @param bool  $value Whether or not to delete the avatar.
 730       * @param array $args {
 731       *     Array of function parameters.
 732       *
 733       *     @type bool|int    $item_id    ID of the item whose avatar you're deleting.
 734       *                                   Defaults to the current item of type $object.
 735       *     @type string      $object     Object type of the item whose avatar you're
 736       *                                   deleting. 'user', 'group', 'blog', or custom.
 737       *                                   Default: 'user'.
 738       *     @type bool|string $avatar_dir Subdirectory where avatar is located.
 739       *                                   Default: false, which falls back on the default location
 740       *                                   corresponding to the $object.
 741       * }
 742       */
 743      if ( ! apply_filters( 'bp_core_pre_delete_existing_avatar', true, $args ) ) {
 744          return true;
 745      }
 746  
 747      if ( empty( $item_id ) ) {
 748          if ( 'user' == $object )
 749              $item_id = bp_displayed_user_id();
 750          elseif ( 'group' == $object )
 751              $item_id = buddypress()->groups->current_group->id;
 752          elseif ( 'blog' == $object )
 753              $item_id = $current_blog->id;
 754  
 755          /** This filter is documented in bp-core/bp-core-avatars.php */
 756          $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object );
 757  
 758          if ( !$item_id ) return false;
 759      }
 760  
 761      if ( empty( $avatar_dir ) ) {
 762          if ( 'user' == $object )
 763              $avatar_dir = 'avatars';
 764          elseif ( 'group' == $object )
 765              $avatar_dir = 'group-avatars';
 766          elseif ( 'blog' == $object )
 767              $avatar_dir = 'blog-avatars';
 768  
 769          /** This filter is documented in bp-core/bp-core-avatars.php */
 770          $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );
 771  
 772          if ( !$avatar_dir ) return false;
 773      }
 774  
 775      /** This filter is documented in bp-core/bp-core-avatars.php */
 776      $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
 777  
 778      if ( !file_exists( $avatar_folder_dir ) )
 779          return false;
 780  
 781      if ( $av_dir = opendir( $avatar_folder_dir ) ) {
 782          while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
 783              if ( ( preg_match( "/-bpfull/", $avatar_file ) || preg_match( "/-bpthumb/", $avatar_file ) ) && '.' != $avatar_file && '..' != $avatar_file )
 784                  @unlink( $avatar_folder_dir . '/' . $avatar_file );
 785          }
 786      }
 787      closedir($av_dir);
 788  
 789      @rmdir( $avatar_folder_dir );
 790  
 791      /**
 792       * Fires after deleting an existing avatar.
 793       *
 794       * @since 1.1.0
 795       *
 796       * @param array $args Array of arguments used for avatar deletion.
 797       */
 798      do_action( 'bp_core_delete_existing_avatar', $args );
 799  
 800      return true;
 801  }
 802  
 803  /**
 804   * Ajax delete an avatar for a given object and item id.
 805   *
 806   * @since 2.3.0
 807   *
 808   * @return string|null A JSON object containing success data if the avatar was deleted,
 809   *                     error message otherwise.
 810   */
 811  function bp_avatar_ajax_delete() {
 812      if ( ! bp_is_post_request() ) {
 813          wp_send_json_error();
 814      }
 815  
 816      $avatar_data = $_POST;
 817  
 818      if ( empty( $avatar_data['object'] ) || empty( $avatar_data['item_id'] ) ) {
 819          wp_send_json_error();
 820      }
 821  
 822      $nonce = 'bp_delete_avatar_link';
 823      if ( 'group' === $avatar_data['object'] ) {
 824          $nonce = 'bp_group_avatar_delete';
 825      }
 826  
 827      // Check the nonce.
 828      check_admin_referer( $nonce, 'nonce' );
 829  
 830      // Capability check.
 831      if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) {
 832          wp_send_json_error();
 833      }
 834  
 835      // Handle delete.
 836      if ( bp_core_delete_existing_avatar( array( 'item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'] ) ) ) {
 837          $return = array(
 838              'avatar' => esc_url( bp_core_fetch_avatar( array(
 839                  'object'  => $avatar_data['object'],
 840                  'item_id' => $avatar_data['item_id'],
 841                  'html'    => false,
 842                  'type'    => 'full',
 843              ) ) ),
 844              'feedback_code' => 4,
 845              'item_id'       => $avatar_data['item_id'],
 846          );
 847  
 848          wp_send_json_success( $return );
 849      } else {
 850          wp_send_json_error( array(
 851              'feedback_code' => 3,
 852          ) );
 853      }
 854  }
 855  add_action( 'wp_ajax_bp_avatar_delete', 'bp_avatar_ajax_delete' );
 856  
 857  /**
 858   * Handle avatar uploading.
 859   *
 860   * The functions starts off by checking that the file has been uploaded
 861   * properly using bp_core_check_avatar_upload(). It then checks that the file
 862   * size is within limits, and that it has an accepted file extension (jpg, gif,
 863   * png). If everything checks out, crop the image and move it to its real
 864   * location.
 865   *
 866   * @since 1.1.0
 867   *
 868   * @see bp_core_check_avatar_upload()
 869   * @see bp_core_check_avatar_type()
 870   *
 871   * @param array  $file              The appropriate entry the from $_FILES superglobal.
 872   * @param string $upload_dir_filter A filter to be applied to 'upload_dir'.
 873   * @return bool True on success, false on failure.
 874   */
 875  function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) {
 876  
 877      /**
 878       * Filters whether or not to handle uploading.
 879       *
 880       * If you want to override this function, make sure you return false.
 881       *
 882       * @since 1.2.4
 883       *
 884       * @param bool   $value             Whether or not to crop.
 885       * @param array  $file              Appropriate entry from $_FILES superglobal.
 886       * @parma string $upload_dir_filter A filter to be applied to 'upload_dir'.
 887       */
 888      if ( ! apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) ) {
 889          return true;
 890      }
 891  
 892      // Setup some variables.
 893      $bp          = buddypress();
 894      $upload_path = bp_core_avatar_upload_path();
 895  
 896      // Upload the file.
 897      $avatar_attachment = new BP_Attachment_Avatar();
 898      $bp->avatar_admin->original = $avatar_attachment->upload( $file, $upload_dir_filter );
 899  
 900      // In case of an error, stop the process and display a feedback to the user.
 901      if ( ! empty( $bp->avatar_admin->original['error'] ) ) {
 902          bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->original['error'] ), 'error' );
 903          return false;
 904      }
 905  
 906      // The Avatar UI available width.
 907      $ui_available_width = 0;
 908  
 909      // Try to set the ui_available_width using the avatar_admin global.
 910      if ( isset( $bp->avatar_admin->ui_available_width ) ) {
 911          $ui_available_width = $bp->avatar_admin->ui_available_width;
 912      }
 913  
 914      // Maybe resize.
 915      $bp->avatar_admin->resized = $avatar_attachment->shrink( $bp->avatar_admin->original['file'], $ui_available_width );
 916      $bp->avatar_admin->image   = new stdClass();
 917  
 918      // We only want to handle one image after resize.
 919      if ( empty( $bp->avatar_admin->resized ) ) {
 920          $bp->avatar_admin->image->file = $bp->avatar_admin->original['file'];
 921          $bp->avatar_admin->image->dir  = str_replace( $upload_path, '', $bp->avatar_admin->original['file'] );
 922      } else {
 923          $bp->avatar_admin->image->file = $bp->avatar_admin->resized['path'];
 924          $bp->avatar_admin->image->dir  = str_replace( $upload_path, '', $bp->avatar_admin->resized['path'] );
 925          @unlink( $bp->avatar_admin->original['file'] );
 926      }
 927  
 928      // Check for WP_Error on what should be an image.
 929      if ( is_wp_error( $bp->avatar_admin->image->dir ) ) {
 930          bp_core_add_message( sprintf( __( 'Upload failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->image->dir->get_error_message() ), 'error' );
 931          return false;
 932      }
 933  
 934      // If the uploaded image is smaller than the "full" dimensions, throw a warning.
 935      if ( $avatar_attachment->is_too_small( $bp->avatar_admin->image->file ) ) {
 936          bp_core_add_message( sprintf( __( 'You have selected an image that is smaller than recommended. For best results, upload a picture larger than %d x %d pixels.', 'buddypress' ), bp_core_avatar_full_width(), bp_core_avatar_full_height() ), 'error' );
 937      }
 938  
 939      // Set the url value for the image.
 940      $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
 941  
 942      return true;
 943  }
 944  
 945  /**
 946   * Ajax upload an avatar.
 947   *
 948   * @since 2.3.0
 949   *
 950   * @return string|null A JSON object containing success data if the upload succeeded
 951   *                     error message otherwise.
 952   */
 953  function bp_avatar_ajax_upload() {
 954      if ( ! bp_is_post_request() ) {
 955          wp_die();
 956      }
 957  
 958      /**
 959       * Sending the json response will be different if
 960       * the current Plupload runtime is html4.
 961       */
 962      $is_html4 = false;
 963      if ( ! empty( $_POST['html4' ] ) ) {
 964          $is_html4 = true;
 965      }
 966  
 967      // Check the nonce.
 968      check_admin_referer( 'bp-uploader' );
 969  
 970      // Init the BuddyPress parameters.
 971      $bp_params = array();
 972  
 973      // We need it to carry on.
 974      if ( ! empty( $_POST['bp_params' ] ) ) {
 975          $bp_params = $_POST['bp_params' ];
 976      } else {
 977          bp_attachments_json_response( false, $is_html4 );
 978      }
 979  
 980      // We need the object to set the uploads dir filter.
 981      if ( empty( $bp_params['object'] ) ) {
 982          bp_attachments_json_response( false, $is_html4 );
 983      }
 984  
 985      // Capability check.
 986      if ( ! bp_attachments_current_user_can( 'edit_avatar', $bp_params ) ) {
 987          bp_attachments_json_response( false, $is_html4 );
 988      }
 989  
 990      $bp = buddypress();
 991      $bp_params['upload_dir_filter'] = '';
 992      $needs_reset = array();
 993  
 994      if ( 'user' === $bp_params['object'] && bp_is_active( 'xprofile' ) ) {
 995          $bp_params['upload_dir_filter'] = 'xprofile_avatar_upload_dir';
 996  
 997          if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) {
 998              $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user );
 999              $bp->displayed_user->id = $bp_params['item_id'];
1000          }
1001      } elseif ( 'group' === $bp_params['object'] && bp_is_active( 'groups' ) ) {
1002          $bp_params['upload_dir_filter'] = 'groups_avatar_upload_dir';
1003  
1004          if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) {
1005              $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group );
1006              $bp->groups->current_group = groups_get_group( $bp_params['item_id'] );
1007          }
1008      } else {
1009          /**
1010           * Filter here to deal with other components.
1011           *
1012           * @since 2.3.0
1013           *
1014           * @var array $bp_params the BuddyPress Ajax parameters.
1015           */
1016          $bp_params = apply_filters( 'bp_core_avatar_ajax_upload_params', $bp_params );
1017      }
1018  
1019      if ( ! isset( $bp->avatar_admin ) ) {
1020          $bp->avatar_admin = new stdClass();
1021      }
1022  
1023      /**
1024       * The BuddyPress upload parameters is including the Avatar UI Available width,
1025       * add it to the avatar_admin global for a later use.
1026       */
1027      if ( isset( $bp_params['ui_available_width'] ) ) {
1028          $bp->avatar_admin->ui_available_width =  (int) $bp_params['ui_available_width'];
1029      }
1030  
1031      // Upload the avatar.
1032      $avatar = bp_core_avatar_handle_upload( $_FILES, $bp_params['upload_dir_filter'] );
1033  
1034      // Reset objects.
1035      if ( ! empty( $needs_reset ) ) {
1036          if ( ! empty( $needs_reset['component'] ) ) {
1037              $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value'];
1038          } else {
1039              $bp->{$needs_reset['key']} = $needs_reset['value'];
1040          }
1041      }
1042  
1043      // Init the feedback message.
1044      $feedback_message = false;
1045  
1046      if ( ! empty( $bp->template_message ) ) {
1047          $feedback_message = $bp->template_message;
1048  
1049          // Remove template message.
1050          $bp->template_message      = false;
1051          $bp->template_message_type = false;
1052  
1053          @setcookie( 'bp-message', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
1054          @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
1055      }
1056  
1057      if ( empty( $avatar ) ) {
1058          // Default upload error.
1059          $message = __( 'Upload failed.', 'buddypress' );
1060  
1061          // Use the template message if set.
1062          if ( ! empty( $feedback_message ) ) {
1063              $message = $feedback_message;
1064          }
1065  
1066          // Upload error reply.
1067          bp_attachments_json_response( false, $is_html4, array(
1068              'type'    => 'upload_error',
1069              'message' => $message,
1070          ) );
1071      }
1072  
1073      if ( empty( $bp->avatar_admin->image->file ) ) {
1074          bp_attachments_json_response( false, $is_html4 );
1075      }
1076  
1077      $uploaded_image = @getimagesize( $bp->avatar_admin->image->file );
1078  
1079      // Set the name of the file.
1080      $name = $_FILES['file']['name'];
1081      $name_parts = pathinfo( $name );
1082      $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) );
1083  
1084      // Finally return the avatar to the editor.
1085      bp_attachments_json_response( true, $is_html4, array(
1086          'name'      => $name,
1087          'url'       => $bp->avatar_admin->image->url,
1088          'width'     => $uploaded_image[0],
1089          'height'    => $uploaded_image[1],
1090          'feedback'  => $feedback_message,
1091      ) );
1092  }
1093  add_action( 'wp_ajax_bp_avatar_upload', 'bp_avatar_ajax_upload' );
1094  
1095  /**
1096   * Handle avatar webcam capture.
1097   *
1098   * @since 2.3.0
1099   *
1100   * @param string $data    Base64 encoded image.
1101   * @param int    $item_id Item to associate.
1102   * @return bool True on success, false on failure.
1103   */
1104  function bp_avatar_handle_capture( $data = '', $item_id = 0 ) {
1105      if ( empty( $data ) || empty( $item_id ) ) {
1106          return false;
1107      }
1108  
1109      /**
1110       * Filters whether or not to handle avatar webcam capture.
1111       *
1112       * If you want to override this function, make sure you return false.
1113       *
1114       * @since 2.5.1
1115       *
1116       * @param bool   $value   Whether or not to crop.
1117       * @param string $data    Base64 encoded image.
1118       * @param int    $item_id Item to associate.
1119       */
1120      if ( ! apply_filters( 'bp_avatar_pre_handle_capture', true, $data, $item_id ) ) {
1121          return true;
1122      }
1123  
1124      $avatar_dir = bp_core_avatar_upload_path() . '/avatars';
1125  
1126      // It's not a regular upload, we may need to create this folder.
1127      if ( ! file_exists( $avatar_dir ) ) {
1128          if ( ! wp_mkdir_p( $avatar_dir ) ) {
1129              return false;
1130          }
1131      }
1132  
1133      /**
1134       * Filters the Avatar folder directory.
1135       *
1136       * @since 2.3.0
1137       *
1138       * @param string $avatar_dir Directory for storing avatars.
1139       * @param int    $item_id    ID of the item being acted on.
1140       * @param string $value      Avatar type.
1141       * @param string $value      Avatars word.
1142       */
1143      $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', $avatar_dir . '/' . $item_id, $item_id, 'user', 'avatars' );
1144  
1145      // It's not a regular upload, we may need to create this folder.
1146      if( ! is_dir( $avatar_folder_dir ) ) {
1147          if ( ! wp_mkdir_p( $avatar_folder_dir ) ) {
1148              return false;
1149          }
1150      }
1151  
1152      $original_file = $avatar_folder_dir . '/webcam-capture-' . $item_id . '.png';
1153  
1154      if ( file_put_contents( $original_file, $data ) ) {
1155          $avatar_to_crop = str_replace( bp_core_avatar_upload_path(), '', $original_file );
1156  
1157          // Crop to default values.
1158          $crop_args = array( 'item_id' => $item_id, 'original_file' => $avatar_to_crop, 'crop_x' => 0, 'crop_y' => 0 );
1159  
1160          return bp_core_avatar_handle_crop( $crop_args );
1161      } else {
1162          return false;
1163      }
1164  }
1165  
1166  /**
1167   * Crop an uploaded avatar.
1168   *
1169   * @since 1.1.0
1170   *
1171   * @param array|string $args {
1172   *     Array of function parameters.
1173   *
1174   *     @type string      $object        Object type of the item whose avatar you're
1175   *                                      handling. 'user', 'group', 'blog', or custom.
1176   *                                      Default: 'user'.
1177   *     @type string      $avatar_dir    Subdirectory where avatar should be stored.
1178   *                                      Default: 'avatars'.
1179   *     @type bool|int    $item_id       ID of the item that the avatar belongs to.
1180   *     @type bool|string $original_file Absolute path to the original avatar file.
1181   *     @type int         $crop_w        Crop width. Default: the global 'full' avatar width,
1182   *                                      as retrieved by bp_core_avatar_full_width().
1183   *     @type int         $crop_h        Crop height. Default: the global 'full' avatar height,
1184   *                                      as retrieved by bp_core_avatar_full_height().
1185   *     @type int         $crop_x        The horizontal starting point of the crop. Default: 0.
1186   *     @type int         $crop_y        The vertical starting point of the crop. Default: 0.
1187   * }
1188   * @return bool True on success, false on failure.
1189   */
1190  function bp_core_avatar_handle_crop( $args = '' ) {
1191  
1192      $r = wp_parse_args( $args, array(
1193          'object'        => 'user',
1194          'avatar_dir'    => 'avatars',
1195          'item_id'       => false,
1196          'original_file' => false,
1197          'crop_w'        => bp_core_avatar_full_width(),
1198          'crop_h'        => bp_core_avatar_full_height(),
1199          'crop_x'        => 0,
1200          'crop_y'        => 0
1201      ) );
1202  
1203      /**
1204       * Filters whether or not to handle cropping.
1205       *
1206       * If you want to override this function, make sure you return false.
1207       *
1208       * @since 1.2.4
1209       *
1210       * @param bool  $value Whether or not to crop.
1211       * @param array $r     Array of parsed arguments for function.
1212       */
1213      if ( ! apply_filters( 'bp_core_pre_avatar_handle_crop', true, $r ) ) {
1214          return true;
1215      }
1216  
1217      // Crop the file.
1218      $avatar_attachment = new BP_Attachment_Avatar();
1219      $cropped           = $avatar_attachment->crop( $r );
1220  
1221      // Check for errors.
1222      if ( empty( $cropped['full'] ) || empty( $cropped['thumb'] ) || is_wp_error( $cropped['full'] ) || is_wp_error( $cropped['thumb'] ) ) {
1223          return false;
1224      }
1225  
1226      return true;
1227  }
1228  
1229  /**
1230   * Ajax set an avatar for a given object and item id.
1231   *
1232   * @since 2.3.0
1233   *
1234   * @return string|null A JSON object containing success data if the crop/capture succeeded
1235   *                     error message otherwise.
1236   */
1237  function bp_avatar_ajax_set() {
1238      if ( ! bp_is_post_request() ) {
1239          wp_send_json_error();
1240      }
1241  
1242      // Check the nonce.
1243      check_admin_referer( 'bp_avatar_cropstore', 'nonce' );
1244  
1245      $avatar_data = wp_parse_args( $_POST, array(
1246          'crop_w' => bp_core_avatar_full_width(),
1247          'crop_h' => bp_core_avatar_full_height(),
1248          'crop_x' => 0,
1249          'crop_y' => 0
1250      ) );
1251  
1252      if ( empty( $avatar_data['object'] ) || empty( $avatar_data['item_id'] ) || empty( $avatar_data['original_file'] ) ) {
1253          wp_send_json_error();
1254      }
1255  
1256      // Capability check.
1257      if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) {
1258          wp_send_json_error();
1259      }
1260  
1261      if ( ! empty( $avatar_data['type'] ) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object'] ) {
1262          $webcam_avatar = false;
1263  
1264          if ( ! empty( $avatar_data['original_file'] ) ) {
1265              $webcam_avatar = str_replace( array( 'data:image/png;base64,', ' ' ), array( '', '+' ), $avatar_data['original_file'] );
1266              $webcam_avatar = base64_decode( $webcam_avatar );
1267          }
1268  
1269          if ( ! bp_avatar_handle_capture( $webcam_avatar, $avatar_data['item_id'] ) ) {
1270              wp_send_json_error( array(
1271                  'feedback_code' => 1
1272              ) );
1273  
1274          } else {
1275              $return = array(
1276                  'avatar' => esc_url( bp_core_fetch_avatar( array(
1277                      'object'  => $avatar_data['object'],
1278                      'item_id' => $avatar_data['item_id'],
1279                      'html'    => false,
1280                      'type'    => 'full',
1281                  ) ) ),
1282                  'feedback_code' => 2,
1283                  'item_id'       => $avatar_data['item_id'],
1284              );
1285  
1286              /**
1287               * Fires if the new avatar was successfully captured.
1288               *
1289               * @since 1.1.0 Used to inform the avatar was successfully cropped
1290               * @since 2.3.4 Add two new parameters to inform about the user id and
1291               *              about the way the avatar was set (eg: 'crop' or 'camera')
1292               *              Move the action at the right place, once the avatar is set
1293               * @since 2.8.0 Added the `$avatar_data` parameter.
1294               *
1295               * @param string $item_id     Inform about the user id the avatar was set for.
1296               * @param string $type        Inform about the way the avatar was set ('camera').
1297               * @param array  $avatar_data Array of parameters passed to the avatar handler.
1298               */
1299              do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $avatar_data );
1300  
1301              wp_send_json_success( $return );
1302          }
1303  
1304          return;
1305      }
1306  
1307      $original_file = str_replace( bp_core_avatar_url(), '', $avatar_data['original_file'] );
1308  
1309      // Set avatars dir & feedback part.
1310      if ( 'user' === $avatar_data['object'] ) {
1311          $avatar_dir = 'avatars';
1312  
1313      // Defaults to object-avatars dir.
1314      } else {
1315          $avatar_dir = sanitize_key( $avatar_data['object'] ) . '-avatars';
1316      }
1317  
1318      // Crop args.
1319      $r = array(
1320          'item_id'       => $avatar_data['item_id'],
1321          'object'        => $avatar_data['object'],
1322          'avatar_dir'    => $avatar_dir,
1323          'original_file' => $original_file,
1324          'crop_w'        => $avatar_data['crop_w'],
1325          'crop_h'        => $avatar_data['crop_h'],
1326          'crop_x'        => $avatar_data['crop_x'],
1327          'crop_y'        => $avatar_data['crop_y']
1328      );
1329  
1330      // Handle crop.
1331      if ( bp_core_avatar_handle_crop( $r ) ) {
1332          $return = array(
1333              'avatar' => esc_url( bp_core_fetch_avatar( array(
1334                  'object'  => $avatar_data['object'],
1335                  'item_id' => $avatar_data['item_id'],
1336                  'html'    => false,
1337                  'type'    => 'full',
1338              ) ) ),
1339              'feedback_code' => 2,
1340              'item_id'       => $avatar_data['item_id'],
1341          );
1342  
1343          if ( 'user' === $avatar_data['object'] ) {
1344              /** This action is documented in bp-core/bp-core-avatars.php */
1345              do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
1346          } elseif ( 'group' === $avatar_data['object'] ) {
1347              /** This action is documented in bp-groups/bp-groups-screens.php */
1348              do_action( 'groups_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
1349          }
1350  
1351          wp_send_json_success( $return );
1352      } else {
1353          wp_send_json_error( array(
1354              'feedback_code' => 1,
1355          ) );
1356      }
1357  }
1358  add_action( 'wp_ajax_bp_avatar_set', 'bp_avatar_ajax_set' );
1359  
1360  /**
1361   * Filter {@link get_avatar_url()} to use the BuddyPress user avatar URL.
1362   *
1363   * @since 2.9.0
1364   *
1365   * @param  string $retval      The URL of the avatar.
1366   * @param  mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
1367   *                             user email, WP_User object, WP_Post object, or WP_Comment object.
1368   * @param  array  $args        Arguments passed to get_avatar_data(), after processing.
1369   * @return string
1370   */
1371  function bp_core_get_avatar_data_url_filter( $retval, $id_or_email, $args ) {
1372      $user = null;
1373  
1374      // Ugh, hate duplicating code; process the user identifier.
1375      if ( is_numeric( $id_or_email ) ) {
1376          $user = get_user_by( 'id', absint( $id_or_email ) );
1377      } elseif ( $id_or_email instanceof WP_User ) {
1378          // User Object
1379          $user = $id_or_email;
1380      } elseif ( $id_or_email instanceof WP_Post ) {
1381          // Post Object
1382          $user = get_user_by( 'id', (int) $id_or_email->post_author );
1383      } elseif ( $id_or_email instanceof WP_Comment ) {
1384          if ( ! empty( $id_or_email->user_id ) ) {
1385              $user = get_user_by( 'id', (int) $id_or_email->user_id );
1386          }
1387      } elseif ( is_email( $id_or_email ) ) {
1388          $user = get_user_by( 'email', $id_or_email );
1389      }
1390  
1391      // No user, so bail.
1392      if ( false === $user instanceof WP_User ) {
1393          return $retval;
1394      }
1395  
1396      // Set BuddyPress-specific avatar args.
1397      $args['item_id'] = $user->ID;
1398      $args['html']    = false;
1399  
1400      // Use the 'full' type if size is larger than BP's thumb width.
1401      if ( (int) $args['size'] > bp_core_avatar_thumb_width() ) {
1402          $args['type'] = 'full';
1403      }
1404  
1405      // Get the BuddyPress avatar URL.
1406      if ( $bp_avatar = bp_core_fetch_avatar( $args ) ) {
1407          return $bp_avatar;
1408      }
1409  
1410      return $retval;
1411  }
1412  add_filter( 'get_avatar_url', 'bp_core_get_avatar_data_url_filter', 10, 3 );
1413  
1414  /**
1415   * Is the current avatar upload error-free?
1416   *
1417   * @since 1.0.0
1418   *
1419   * @param array $file The $_FILES array.
1420   * @return bool True if no errors are found. False if there are errors.
1421   */
1422  function bp_core_check_avatar_upload( $file ) {
1423      if ( isset( $file['error'] ) && $file['error'] )
1424          return false;
1425  
1426      return true;
1427  }
1428  
1429  /**
1430   * Is the file size of the current avatar upload permitted?
1431   *
1432   * @since 1.0.0
1433   *
1434   * @param array $file The $_FILES array.
1435   * @return bool True if the avatar is under the size limit, otherwise false.
1436   */
1437  function bp_core_check_avatar_size( $file ) {
1438      if ( $file['file']['size'] > bp_core_avatar_original_max_filesize() )
1439          return false;
1440  
1441      return true;
1442  }
1443  
1444  /**
1445   * Get allowed avatar types.
1446   *
1447   * @since 2.3.0
1448   *
1449   * @return array
1450   */
1451  function bp_core_get_allowed_avatar_types() {
1452      $allowed_types = bp_attachments_get_allowed_types( 'avatar' );
1453  
1454      /**
1455       * Filters the list of allowed image types.
1456       *
1457       * @since 2.3.0
1458       *
1459       * @param array $allowed_types List of image types.
1460       */
1461      $avatar_types = (array) apply_filters( 'bp_core_get_allowed_avatar_types', $allowed_types );
1462  
1463      if ( empty( $avatar_types ) ) {
1464          $avatar_types = $allowed_types;
1465      } else {
1466          $avatar_types = array_intersect( $allowed_types, $avatar_types );
1467      }
1468  
1469      return array_values( $avatar_types );
1470  }
1471  
1472  /**
1473   * Get allowed avatar mime types.
1474   *
1475   * @since 2.3.0
1476   *
1477   * @return array
1478   */
1479  function bp_core_get_allowed_avatar_mimes() {
1480      $allowed_types  = bp_core_get_allowed_avatar_types();
1481  
1482      return bp_attachments_get_allowed_mimes( 'avatar', $allowed_types );
1483  }
1484  
1485  /**
1486   * Does the current avatar upload have an allowed file type?
1487   *
1488   * Permitted file types are JPG, GIF and PNG.
1489   *
1490   * @since 1.0.0
1491   *
1492   * @param array $file The $_FILES array.
1493   * @return bool True if the file extension is permitted, otherwise false.
1494   */
1495  function bp_core_check_avatar_type( $file ) {
1496      return bp_attachments_check_filetype( $file['file']['tmp_name'], $file['file']['name'], bp_core_get_allowed_avatar_mimes() );
1497  }
1498  
1499  /**
1500   * Fetch data from the BP root blog's upload directory.
1501   *
1502   * @since 1.8.0
1503   *
1504   * @param string $type The variable we want to return from the $bp->avatars object.
1505   *                     Only 'upload_path' and 'url' are supported. Default: 'upload_path'.
1506   * @return string The avatar upload directory path.
1507   */
1508  function bp_core_get_upload_dir( $type = 'upload_path' ) {
1509      $bp = buddypress();
1510  
1511      switch ( $type ) {
1512          case 'upload_path' :
1513              $constant = 'BP_AVATAR_UPLOAD_PATH';
1514              $key      = 'basedir';
1515  
1516              break;
1517  
1518          case 'url' :
1519              $constant = 'BP_AVATAR_URL';
1520              $key      = 'baseurl';
1521  
1522              break;
1523  
1524          default :
1525              return false;
1526  
1527              break;
1528      }
1529  
1530      // See if the value has already been calculated and stashed in the $bp global.
1531      if ( isset( $bp->avatar->$type ) ) {
1532          $retval = $bp->avatar->$type;
1533      } else {
1534          // If this value has been set in a constant, just use that.
1535          if ( defined( $constant ) ) {
1536              $retval = constant( $constant );
1537          } else {
1538  
1539              // Use cached upload dir data if available.
1540              if ( ! empty( $bp->avatar->upload_dir ) ) {
1541                  $upload_dir = $bp->avatar->upload_dir;
1542  
1543              // No cache, so query for it.
1544              } else {
1545  
1546                  // Get upload directory information from current site.
1547                  $upload_dir = bp_upload_dir();
1548  
1549                  // Stash upload directory data for later use.
1550                  $bp->avatar->upload_dir = $upload_dir;
1551              }
1552  
1553              // Directory does not exist and cannot be created.
1554              if ( ! empty( $upload_dir['error'] ) ) {
1555                  $retval = '';
1556  
1557              } else {
1558                  $retval = $upload_dir[$key];
1559  
1560                  // If $key is 'baseurl', check to see if we're on SSL
1561                  // Workaround for WP13941, WP15928, WP19037.
1562                  if ( $key == 'baseurl' && is_ssl() ) {
1563                      $retval = str_replace( 'http://', 'https://', $retval );
1564                  }
1565              }
1566  
1567          }
1568  
1569          // Stash in $bp for later use.
1570          $bp->avatar->$type = $retval;
1571      }
1572  
1573      return $retval;
1574  }
1575  
1576  /**
1577   * Get the absolute upload path for the WP installation.
1578   *
1579   * @since 1.2.0
1580   *
1581   * @return string Absolute path to WP upload directory.
1582   */
1583  function bp_core_avatar_upload_path() {
1584  
1585      /**
1586       * Filters the absolute upload path for the WP installation.
1587       *
1588       * @since 1.2.0
1589       *
1590       * @param string $value Absolute upload path for the WP installation.
1591       */
1592      return apply_filters( 'bp_core_avatar_upload_path', bp_core_get_upload_dir() );
1593  }
1594  
1595  /**
1596   * Get the raw base URL for root site upload location.
1597   *
1598   * @since 1.2.0
1599   *
1600   * @return string Full URL to current upload location.
1601   */
1602  function bp_core_avatar_url() {
1603  
1604      /**
1605       * Filters the raw base URL for root site upload location.
1606       *
1607       * @since 1.2.0
1608       *
1609       * @param string $value Raw base URL for the root site upload location.
1610       */
1611      return apply_filters( 'bp_core_avatar_url', bp_core_get_upload_dir( 'url' ) );
1612  }
1613  
1614  /**
1615   * Check if a given user ID has an uploaded avatar.
1616   *
1617   * @since 1.0.0
1618   *
1619   * @param int $user_id ID of the user whose avatar is being checked.
1620   * @return bool True if the user has uploaded a local avatar. Otherwise false.
1621   */
1622  function bp_get_user_has_avatar( $user_id = 0 ) {
1623  
1624      if ( empty( $user_id ) )
1625          $user_id = bp_displayed_user_id();
1626  
1627      $retval = false;
1628      if ( bp_core_fetch_avatar( array( 'item_id' => $user_id, 'no_grav' => true, 'html' => false, 'type' => 'full' ) ) != bp_core_avatar_default( 'local' ) )
1629          $retval = true;
1630  
1631      /**
1632       * Filters whether or not a user has an uploaded avatar.
1633       *
1634       * @since 1.6.0
1635       *
1636       * @param bool $retval  Whether or not a user has an uploaded avatar.
1637       * @param int  $user_id ID of the user being checked.
1638       */
1639      return (bool) apply_filters( 'bp_get_user_has_avatar', $retval, $user_id );
1640  }
1641  
1642  /**
1643   * Utility function for fetching an avatar dimension setting.
1644   *
1645   * @since 1.5.0
1646   *
1647   * @param string $type   Dimension type you're fetching dimensions for. 'thumb'
1648   *                       or 'full'. Default: 'thumb'.
1649   * @param string $h_or_w Which dimension is being fetched. 'height' or 'width'.
1650   *                       Default: 'height'.
1651   * @return int|bool $dim The dimension.
1652   */
1653  function bp_core_avatar_dimension( $type = 'thumb', $h_or_w = 'height' ) {
1654      $bp  = buddypress();
1655      $dim = isset( $bp->avatar->{$type}->{$h_or_w} ) ? (int) $bp->avatar->{$type}->{$h_or_w} : false;
1656  
1657      /**
1658       * Filters the avatar dimension setting.
1659       *
1660       * @since 1.5.0
1661       *
1662       * @param int|bool $dim    Dimension setting for the type.
1663       * @param string   $type   The type of avatar whose dimensions are requested. Default 'thumb'.
1664       * @param string   $h_or_w The dimension parameter being requested. Default 'height'.
1665       */
1666      return apply_filters( 'bp_core_avatar_dimension', $dim, $type, $h_or_w );
1667  }
1668  
1669  /**
1670   * Get the 'thumb' avatar width setting.
1671   *
1672   * @since 1.5.0
1673   *
1674   * @return int The 'thumb' width.
1675   */
1676  function bp_core_avatar_thumb_width() {
1677  
1678      /**
1679       * Filters the 'thumb' avatar width setting.
1680       *
1681       * @since 1.5.0
1682       *
1683       * @param int $value Value for the 'thumb' avatar width setting.
1684       */
1685      return apply_filters( 'bp_core_avatar_thumb_width', bp_core_avatar_dimension( 'thumb', 'width' ) );
1686  }
1687  
1688  /**
1689   * Get the 'thumb' avatar height setting.
1690   *
1691   * @since 1.5.0
1692   *
1693   * @return int The 'thumb' height.
1694   */
1695  function bp_core_avatar_thumb_height() {
1696  
1697      /**
1698       * Filters the 'thumb' avatar height setting.
1699       *
1700       * @since 1.5.0
1701       *
1702       * @param int $value Value for the 'thumb' avatar height setting.
1703       */
1704      return apply_filters( 'bp_core_avatar_thumb_height', bp_core_avatar_dimension( 'thumb', 'height' ) );
1705  }
1706  
1707  /**
1708   * Get the 'full' avatar width setting.
1709   *
1710   * @since 1.5.0
1711   *
1712   * @return int The 'full' width.
1713   */
1714  function bp_core_avatar_full_width() {
1715  
1716      /**
1717       * Filters the 'full' avatar width setting.
1718       *
1719       * @since 1.5.0
1720       *
1721       * @param int $value Value for the 'full' avatar width setting.
1722       */
1723      return apply_filters( 'bp_core_avatar_full_width', bp_core_avatar_dimension( 'full', 'width' ) );
1724  }
1725  
1726  /**
1727   * Get the 'full' avatar height setting.
1728   *
1729   * @since 1.5.0
1730   *
1731   * @return int The 'full' height.
1732   */
1733  function bp_core_avatar_full_height() {
1734  
1735      /**
1736       * Filters the 'full' avatar height setting.
1737       *
1738       * @since 1.5.0
1739       *
1740       * @param int $value Value for the 'full' avatar height setting.
1741       */
1742      return apply_filters( 'bp_core_avatar_full_height', bp_core_avatar_dimension( 'full', 'height' ) );
1743  }
1744  
1745  /**
1746   * Get the max width for original avatar uploads.
1747   *
1748   * @since 1.5.0
1749   *
1750   * @return int The max width for original avatar uploads.
1751   */
1752  function bp_core_avatar_original_max_width() {
1753  
1754      /**
1755       * Filters the max width for original avatar uploads.
1756       *
1757       * @since 1.5.0
1758       *
1759       * @param int $value Value for the max width.
1760       */
1761      return apply_filters( 'bp_core_avatar_original_max_width', (int) buddypress()->avatar->original_max_width );
1762  }
1763  
1764  /**
1765   * Get the max filesize for original avatar uploads.
1766   *
1767   * @since 1.5.0
1768   *
1769   * @return int The max filesize for original avatar uploads.
1770   */
1771  function bp_core_avatar_original_max_filesize() {
1772  
1773      /**
1774       * Filters the max filesize for original avatar uploads.
1775       *
1776       * @since 1.5.0
1777       *
1778       * @param int $value Value for the max filesize.
1779       */
1780      return apply_filters( 'bp_core_avatar_original_max_filesize', (int) buddypress()->avatar->original_max_filesize );
1781  }
1782  
1783  /**
1784   * Get the URL of the 'full' default avatar.
1785   *
1786   * @since 1.5.0
1787   * @since 2.6.0 Introduced `$params` and `$object_type` parameters.
1788   *
1789   * @param string $type   'local' if the fallback should be the locally-hosted version
1790   *                       of the mystery person, 'gravatar' if the fallback should be
1791   *                       Gravatar's version. Default: 'gravatar'.
1792   * @param array  $params Parameters passed to bp_core_fetch_avatar().
1793   * @return string The URL of the default avatar.
1794   */
1795  function bp_core_avatar_default( $type = 'gravatar', $params = array() ) {
1796      // Local override.
1797      if ( defined( 'BP_AVATAR_DEFAULT' ) ) {
1798          $avatar = BP_AVATAR_DEFAULT;
1799  
1800      // Use the local default image.
1801      } elseif ( 'local' === $type ) {
1802          $size = '';
1803          if (
1804              ( isset( $params['type'] ) && 'thumb' === $params['type'] && bp_core_avatar_thumb_width() <= 50 ) ||
1805              ( isset( $params['width'] ) && $params['width'] <= 50 )
1806          ) {
1807  
1808              $size = '-50';
1809          }
1810  
1811          $avatar = buddypress()->plugin_url . "bp-core/images/mystery-man{$size}.jpg";
1812  
1813      // Use Gravatar's mystery person as fallback.
1814      } else {
1815          $size = '';
1816          if ( isset( $params['type'] ) && 'thumb' === $params['type'] ) {
1817              $size = bp_core_avatar_thumb_width();
1818          } else {
1819              $size = bp_core_avatar_full_width();
1820          }
1821          $avatar = '//www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&amp;s=' . $size;
1822      }
1823  
1824      /**
1825       * Filters the URL of the 'full' default avatar.
1826       *
1827       * @since 1.5.0
1828       * @since 2.6.0 Added `$params`.
1829       *
1830       * @param string $avatar URL of the default avatar.
1831       * @param array  $params Params provided to bp_core_fetch_avatar().
1832       */
1833      return apply_filters( 'bp_core_avatar_default', $avatar, $params );
1834  }
1835  
1836  /**
1837   * Get the URL of the 'thumb' default avatar.
1838   *
1839   * Uses Gravatar's mystery-person avatar, unless BP_AVATAR_DEFAULT_THUMB has been
1840   * defined.
1841   *
1842   * @since 1.5.0
1843   * @since 2.6.0 Introduced `$object_type` parameter.
1844   *
1845   * @param string $type   'local' if the fallback should be the locally-hosted version
1846   *                       of the mystery person, 'gravatar' if the fallback should be
1847   *                       Gravatar's version. Default: 'gravatar'.
1848   * @param array  $params Parameters passed to bp_core_fetch_avatar().
1849   * @return string The URL of the default avatar thumb.
1850   */
1851  function bp_core_avatar_default_thumb( $type = 'gravatar', $params = array() ) {
1852      // Local override.
1853      if ( defined( 'BP_AVATAR_DEFAULT_THUMB' ) ) {
1854          $avatar = BP_AVATAR_DEFAULT_THUMB;
1855  
1856      // Use the local default image.
1857      } elseif ( 'local' === $type ) {
1858          $avatar = buddypress()->plugin_url . 'bp-core/images/mystery-man-50.jpg';
1859  
1860      // Use Gravatar's mystery person as fallback.
1861      } else {
1862          $avatar = '//www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&amp;s=' . bp_core_avatar_thumb_width();
1863      }
1864  
1865      /**
1866       * Filters the URL of the 'thumb' default avatar.
1867       *
1868       * @since 1.5.0
1869       * @since 2.6.0 Added `$params`.
1870       *
1871       * @param string $avatar URL of the default avatar.
1872       * @param string $params Params provided to bp_core_fetch_avatar().
1873       */
1874      return apply_filters( 'bp_core_avatar_thumb', $avatar, $params );
1875  }
1876  
1877  /**
1878   * Reset the week parameter of the WordPress main query if needed.
1879   *
1880   * When cropping an avatar, a $_POST['w'] var is sent, setting the 'week'
1881   * parameter of the WordPress main query to this posted var. To avoid
1882   * notices, we need to make sure this 'week' query var is reset to 0.
1883   *
1884   * @since 2.2.0
1885   *
1886   * @param WP_Query|null $posts_query The main query object.
1887   */
1888  function bp_core_avatar_reset_query( $posts_query = null ) {
1889      $reset_w = false;
1890  
1891      // Group's avatar edit screen.
1892      if ( bp_is_group_admin_page() ) {
1893          $reset_w = bp_is_group_admin_screen( 'group-avatar' );
1894  
1895      // Group's avatar create screen.
1896      } elseif ( bp_is_group_create() ) {
1897          /**
1898           * We can't use bp_get_groups_current_create_step().
1899           * as it's not set yet
1900           */
1901          $reset_w = 'group-avatar' === bp_action_variable( 1 );
1902  
1903      // User's change avatar screen.
1904      } else {
1905          $reset_w = bp_is_user_change_avatar();
1906      }
1907  
1908      // A user or a group is cropping an avatar.
1909      if ( true === $reset_w && isset( $_POST['avatar-crop-submit'] ) ) {
1910          $posts_query->set( 'w', 0 );
1911      }
1912  }
1913  add_action( 'bp_parse_query', 'bp_core_avatar_reset_query', 10, 1 );
1914  
1915  /**
1916   * Checks whether Avatar UI should be loaded.
1917   *
1918   * @since 2.3.0
1919   *
1920   * @return bool True if Avatar UI should load, false otherwise.
1921   */
1922  function bp_avatar_is_front_edit() {
1923      $retval = false;
1924  
1925      // No need to carry on if the current WordPress version is not supported.
1926      if ( ! bp_attachments_is_wp_version_supported() ) {
1927          return $retval;
1928      }
1929  
1930      if ( bp_is_user_change_avatar() && 'crop-image' !== bp_get_avatar_admin_step() ) {
1931          $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
1932      }
1933  
1934      if ( bp_is_active( 'groups' ) ) {
1935          // Group creation.
1936          if ( bp_is_group_create() && bp_is_group_creation_step( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
1937              $retval = ! bp_disable_group_avatar_uploads();
1938  
1939          // Group Manage.
1940          } elseif ( bp_is_group_admin_page() && bp_is_group_admin_screen( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
1941              $retval = ! bp_disable_group_avatar_uploads();
1942          }
1943      }
1944  
1945      /**
1946       * Use this filter if you need to :
1947       * - Load the avatar UI for a component that is !groups or !user (return true regarding your conditions)
1948       * - Completely disable the avatar UI introduced in 2.3 (eg: __return_false())
1949       *
1950       * @since 2.3.0
1951       *
1952       * @param bool $retval Whether or not to load the Avatar UI.
1953       */
1954      return apply_filters( 'bp_avatar_is_front_edit', $retval );
1955  }
1956  
1957  /**
1958   * Checks whether the Webcam Avatar UI part should be loaded.
1959   *
1960   * @since 2.3.0
1961   *
1962   * @global $is_safari
1963   * @global $is_IE
1964   *
1965   * @return bool True to load the Webcam Avatar UI part. False otherwise.
1966   */
1967  function bp_avatar_use_webcam() {
1968      global $is_safari, $is_IE, $is_chrome;
1969  
1970      /**
1971       * Do not use the webcam feature for mobile devices
1972       * to avoid possible confusions.
1973       */
1974      if ( wp_is_mobile() ) {
1975          return false;
1976      }
1977  
1978      /**
1979       * Bail when the browser does not support getUserMedia.
1980       *
1981       * @see http://caniuse.com/#feat=stream
1982       */
1983      if ( $is_safari || $is_IE || ( $is_chrome && ! is_ssl() ) ) {
1984          return false;
1985      }
1986  
1987      /**
1988       * Use this filter if you need to disable the webcam capture feature
1989       * by returning false.
1990       *
1991       * @since 2.3.0
1992       *
1993       * @param bool $value Whether or not to load Webcam Avatar UI part.
1994       */
1995      return apply_filters( 'bp_avatar_use_webcam', true );
1996  }
1997  
1998  /**
1999   * Template function to load the Avatar UI javascript templates.
2000   *
2001   * @since 2.3.0
2002   */
2003  function bp_avatar_get_templates() {
2004      if ( ! bp_avatar_is_front_edit() ) {
2005          return;
2006      }
2007  
2008      bp_attachments_get_template_part( 'avatars/index' );
2009  }
2010  
2011  /**
2012   * Trick to check if the theme's BuddyPress templates are up to date.
2013   *
2014   * If the "avatar templates" are not including the new template tag, this will
2015   * help users to get the avatar UI.
2016   *
2017   * @since 2.3.0
2018   */
2019  function bp_avatar_template_check() {
2020      if ( ! bp_avatar_is_front_edit() ) {
2021          return;
2022      }
2023  
2024      if ( ! did_action( 'bp_attachments_avatar_check_template' ) ) {
2025          bp_attachments_get_template_part( 'avatars/index' );
2026      }
2027  }


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1