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


Generated: Thu Jul 9 01:01:31 2020 Cross-referenced by PHPXref 0.7.1