[ 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      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 ( ! $params['force_default'] && 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              $url_args['d'] = $params['default'];
 641          }
 642          if ( ! empty( $params['rating'] ) ) {
 643              $url_args['r'] = strtolower( $params['rating'] );
 644          }
 645  
 646          /**
 647           * Filters the Gravatar "d" parameter.
 648           *
 649           * @since 2.6.0
 650           *
 651           * @param string $default_grav The avatar default.
 652           * @param array  $params       The avatar's data.
 653           */
 654          $default_grav = apply_filters( 'bp_core_avatar_default', $default_grav, $params );
 655  
 656          // Only set default image if 'Gravatar Logo' is not requested.
 657          if ( ! $params['force_default'] && 'gravatar_default' !== $default_grav ) {
 658              $url_args['d'] = $default_grav;
 659          }
 660  
 661          // Set up the Gravatar URL.
 662          $gravatar = esc_url( add_query_arg(
 663              rawurlencode_deep( array_filter( $url_args ) ),
 664              $gravatar
 665          ) );
 666  
 667      // No avatar was found, and we've been told not to use a gravatar.
 668      } else {
 669  
 670          /**
 671           * Filters the avatar default when Gravatar is not used.
 672           *
 673           * This is a variable filter dependent on the avatar type being requested.
 674           *
 675           * @since 1.5.0
 676           *
 677           * @param string $value  Default avatar for non-gravatar requests.
 678           * @param array  $params Array of parameters for the avatar request.
 679           */
 680          $gravatar = apply_filters( 'bp_core_default_avatar_' . $params['object'], bp_core_avatar_default( 'local', $params ), $params );
 681      }
 682  
 683      if ( true === $params['html'] ) {
 684  
 685          /** This filter is documented in bp-core/bp-core-avatars.php */
 686          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 );
 687      } else {
 688  
 689          /** This filter is documented in bp-core/bp-core-avatars.php */
 690          return apply_filters( 'bp_core_fetch_avatar_url', $gravatar, $params );
 691      }
 692  }
 693  
 694  /**
 695   * Delete an existing avatar.
 696   *
 697   * @since 1.1.0
 698   *
 699   * @param array|string $args {
 700   *     Array of function parameters.
 701   *     @type bool|int    $item_id    ID of the item whose avatar you're deleting.
 702   *                                   Defaults to the current item of type $object.
 703   *     @type string      $object     Object type of the item whose avatar you're
 704   *                                   deleting. 'user', 'group', 'blog', or custom.
 705   *                                   Default: 'user'.
 706   *     @type bool|string $avatar_dir Subdirectory where avatar is located.
 707   *                                   Default: false, which falls back on the default location
 708   *                                   corresponding to the $object.
 709   * }
 710   * @return bool True on success, false on failure.
 711   */
 712  function bp_core_delete_existing_avatar( $args = '' ) {
 713  
 714      $defaults = array(
 715          'item_id'    => false,
 716          'object'     => 'user', // User OR group OR blog OR custom type (if you use filters).
 717          'avatar_dir' => false
 718      );
 719  
 720      $args = wp_parse_args( $args, $defaults );
 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( $args['item_id'] ) ) {
 748          if ( 'user' === $args['object'] ) {
 749              $args['item_id'] = bp_displayed_user_id();
 750          } elseif ( 'group' === $args['object'] ) {
 751              $args['item_id'] = buddypress()->groups->current_group->id;
 752          } elseif ( 'blog' === $args['object'] ) {
 753              $args['item_id'] = $current_blog->id;
 754          }
 755  
 756          /** This filter is documented in bp-core/bp-core-avatars.php */
 757          $item_id = apply_filters( 'bp_core_avatar_item_id', $args['item_id'], $args['object'] );
 758      } else {
 759          $item_id = $args['item_id'];
 760      }
 761  
 762      if ( $item_id && ( ctype_digit( $item_id ) || is_int( $item_id ) ) ) {
 763          $item_id = (int) $item_id;
 764      } else {
 765          return false;
 766      }
 767  
 768      if ( empty( $args['avatar_dir'] ) ) {
 769          if ( 'user' === $args['object'] ) {
 770              $args['avatar_dir'] = 'avatars';
 771          } elseif ( 'group' === $args['object'] ) {
 772              $args['avatar_dir'] = 'group-avatars';
 773          } elseif ( 'blog' === $args['object'] ) {
 774              $args['avatar_dir'] = 'blog-avatars';
 775          }
 776  
 777          /** This filter is documented in bp-core/bp-core-avatars.php */
 778          $avatar_dir = apply_filters( 'bp_core_avatar_dir', $args['avatar_dir'], $args['object'] );
 779      } else {
 780          $avatar_dir = $args['avatar_dir'];
 781      }
 782  
 783      if ( ! $avatar_dir ) {
 784          return false;
 785      }
 786  
 787      /** This filter is documented in bp-core/bp-core-avatars.php */
 788      $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 );
 789  
 790      if ( ! is_dir( $avatar_folder_dir ) ) {
 791          return false;
 792      }
 793  
 794      if ( $av_dir = opendir( $avatar_folder_dir ) ) {
 795          while ( false !== ( $avatar_file = readdir( $av_dir ) ) ) {
 796              if ( ( preg_match( "/-bpfull/", $avatar_file ) || preg_match( "/-bpthumb/", $avatar_file ) ) && '.' != $avatar_file && '..' != $avatar_file ) {
 797                  @unlink( $avatar_folder_dir . '/' . $avatar_file );
 798              }
 799          }
 800      }
 801      closedir( $av_dir );
 802  
 803      @rmdir( $avatar_folder_dir );
 804  
 805      /**
 806       * Fires after deleting an existing avatar.
 807       *
 808       * @since 1.1.0
 809       *
 810       * @param array $args Array of arguments used for avatar deletion.
 811       */
 812      do_action( 'bp_core_delete_existing_avatar', $args );
 813  
 814      return true;
 815  }
 816  
 817  /**
 818   * Ajax delete an avatar for a given object and item id.
 819   *
 820   * @since 2.3.0
 821   *
 822   * @return string|null A JSON object containing success data if the avatar was deleted,
 823   *                     error message otherwise.
 824   */
 825  function bp_avatar_ajax_delete() {
 826      if ( ! bp_is_post_request() ) {
 827          wp_send_json_error();
 828      }
 829  
 830      $avatar_data = $_POST;
 831  
 832      if ( empty( $avatar_data['object'] ) || empty( $avatar_data['item_id'] ) ) {
 833          wp_send_json_error();
 834      }
 835  
 836      $nonce = 'bp_delete_avatar_link';
 837      if ( 'group' === $avatar_data['object'] ) {
 838          $nonce = 'bp_group_avatar_delete';
 839      }
 840  
 841      // Check the nonce.
 842      check_admin_referer( $nonce, 'nonce' );
 843  
 844      // Capability check.
 845      if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) {
 846          wp_send_json_error();
 847      }
 848  
 849      // Handle delete.
 850      if ( bp_core_delete_existing_avatar( array( 'item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'] ) ) ) {
 851          $return = array(
 852              'avatar' => esc_url( bp_core_fetch_avatar( array(
 853                  'object'  => $avatar_data['object'],
 854                  'item_id' => $avatar_data['item_id'],
 855                  'html'    => false,
 856                  'type'    => 'full',
 857              ) ) ),
 858              'feedback_code' => 4,
 859              'item_id'       => $avatar_data['item_id'],
 860          );
 861  
 862          wp_send_json_success( $return );
 863      } else {
 864          wp_send_json_error( array(
 865              'feedback_code' => 3,
 866          ) );
 867      }
 868  }
 869  add_action( 'wp_ajax_bp_avatar_delete', 'bp_avatar_ajax_delete' );
 870  
 871  /**
 872   * Handle avatar uploading.
 873   *
 874   * The functions starts off by checking that the file has been uploaded
 875   * properly using bp_core_check_avatar_upload(). It then checks that the file
 876   * size is within limits, and that it has an accepted file extension (jpg, gif,
 877   * png). If everything checks out, crop the image and move it to its real
 878   * location.
 879   *
 880   * @since 1.1.0
 881   *
 882   * @see bp_core_check_avatar_upload()
 883   * @see bp_core_check_avatar_type()
 884   *
 885   * @param array  $file              The appropriate entry the from $_FILES superglobal.
 886   * @param string $upload_dir_filter A filter to be applied to 'upload_dir'.
 887   * @return bool True on success, false on failure.
 888   */
 889  function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) {
 890  
 891      /**
 892       * Filters whether or not to handle uploading.
 893       *
 894       * If you want to override this function, make sure you return false.
 895       *
 896       * @since 1.2.4
 897       *
 898       * @param bool   $value             Whether or not to crop.
 899       * @param array  $file              Appropriate entry from $_FILES superglobal.
 900       * @parma string $upload_dir_filter A filter to be applied to 'upload_dir'.
 901       */
 902      if ( ! apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) ) {
 903          return true;
 904      }
 905  
 906      // Setup some variables.
 907      $bp          = buddypress();
 908      $upload_path = bp_core_avatar_upload_path();
 909  
 910      // Upload the file.
 911      $avatar_attachment = new BP_Attachment_Avatar();
 912      $bp->avatar_admin->original = $avatar_attachment->upload( $file, $upload_dir_filter );
 913  
 914      // In case of an error, stop the process and display a feedback to the user.
 915      if ( ! empty( $bp->avatar_admin->original['error'] ) ) {
 916          bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->original['error'] ), 'error' );
 917          return false;
 918      }
 919  
 920      // The Avatar UI available width.
 921      $ui_available_width = 0;
 922  
 923      // Try to set the ui_available_width using the avatar_admin global.
 924      if ( isset( $bp->avatar_admin->ui_available_width ) ) {
 925          $ui_available_width = $bp->avatar_admin->ui_available_width;
 926      }
 927  
 928      // Maybe resize.
 929      $bp->avatar_admin->resized = $avatar_attachment->shrink( $bp->avatar_admin->original['file'], $ui_available_width );
 930      $bp->avatar_admin->image   = new stdClass();
 931  
 932      // We only want to handle one image after resize.
 933      if ( empty( $bp->avatar_admin->resized ) ) {
 934          $bp->avatar_admin->image->file = $bp->avatar_admin->original['file'];
 935          $bp->avatar_admin->image->dir  = str_replace( $upload_path, '', $bp->avatar_admin->original['file'] );
 936      } else {
 937          $bp->avatar_admin->image->file = $bp->avatar_admin->resized['path'];
 938          $bp->avatar_admin->image->dir  = str_replace( $upload_path, '', $bp->avatar_admin->resized['path'] );
 939          @unlink( $bp->avatar_admin->original['file'] );
 940      }
 941  
 942      // Check for WP_Error on what should be an image.
 943      if ( is_wp_error( $bp->avatar_admin->image->dir ) ) {
 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          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' );
 951      }
 952  
 953      // Set the url value for the image.
 954      $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
 955  
 956      return true;
 957  }
 958  
 959  /**
 960   * Ajax upload an avatar.
 961   *
 962   * @since 2.3.0
 963   *
 964   * @return string|null A JSON object containing success data if the upload succeeded
 965   *                     error message otherwise.
 966   */
 967  function bp_avatar_ajax_upload() {
 968      if ( ! bp_is_post_request() ) {
 969          wp_die();
 970      }
 971  
 972      /**
 973       * Sending the json response will be different if
 974       * the current Plupload runtime is html4.
 975       */
 976      $is_html4 = false;
 977      if ( ! empty( $_POST['html4' ] ) ) {
 978          $is_html4 = true;
 979      }
 980  
 981      // Check the nonce.
 982      check_admin_referer( 'bp-uploader' );
 983  
 984      // Init the BuddyPress parameters.
 985      $bp_params = array();
 986  
 987      // We need it to carry on.
 988      if ( ! empty( $_POST['bp_params' ] ) ) {
 989          $bp_params = $_POST['bp_params' ];
 990      } else {
 991          bp_attachments_json_response( false, $is_html4 );
 992      }
 993  
 994      // We need the object to set the uploads dir filter.
 995      if ( empty( $bp_params['object'] ) ) {
 996          bp_attachments_json_response( false, $is_html4 );
 997      }
 998  
 999      // Capability check.
1000      if ( ! bp_attachments_current_user_can( 'edit_avatar', $bp_params ) ) {
1001          bp_attachments_json_response( false, $is_html4 );
1002      }
1003  
1004      $bp = buddypress();
1005      $bp_params['upload_dir_filter'] = '';
1006      $needs_reset = array();
1007  
1008      if ( 'user' === $bp_params['object'] && bp_is_active( 'xprofile' ) ) {
1009          $bp_params['upload_dir_filter'] = 'xprofile_avatar_upload_dir';
1010  
1011          if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) {
1012              $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user );
1013              $bp->displayed_user->id = $bp_params['item_id'];
1014          }
1015      } elseif ( 'group' === $bp_params['object'] && bp_is_active( 'groups' ) ) {
1016          $bp_params['upload_dir_filter'] = 'groups_avatar_upload_dir';
1017  
1018          if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) {
1019              $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group );
1020              $bp->groups->current_group = groups_get_group( $bp_params['item_id'] );
1021          }
1022      } else {
1023          /**
1024           * Filter here to deal with other components.
1025           *
1026           * @since 2.3.0
1027           *
1028           * @var array $bp_params the BuddyPress Ajax parameters.
1029           */
1030          $bp_params = apply_filters( 'bp_core_avatar_ajax_upload_params', $bp_params );
1031      }
1032  
1033      if ( ! isset( $bp->avatar_admin ) ) {
1034          $bp->avatar_admin = new stdClass();
1035      }
1036  
1037      /**
1038       * The BuddyPress upload parameters is including the Avatar UI Available width,
1039       * add it to the avatar_admin global for a later use.
1040       */
1041      if ( isset( $bp_params['ui_available_width'] ) ) {
1042          $bp->avatar_admin->ui_available_width =  (int) $bp_params['ui_available_width'];
1043      }
1044  
1045      // Upload the avatar.
1046      $avatar = bp_core_avatar_handle_upload( $_FILES, $bp_params['upload_dir_filter'] );
1047  
1048      // Reset objects.
1049      if ( ! empty( $needs_reset ) ) {
1050          if ( ! empty( $needs_reset['component'] ) ) {
1051              $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value'];
1052          } else {
1053              $bp->{$needs_reset['key']} = $needs_reset['value'];
1054          }
1055      }
1056  
1057      // Init the feedback message.
1058      $feedback_message = false;
1059  
1060      if ( ! empty( $bp->template_message ) ) {
1061          $feedback_message = $bp->template_message;
1062  
1063          // Remove template message.
1064          $bp->template_message      = false;
1065          $bp->template_message_type = false;
1066  
1067          @setcookie( 'bp-message', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
1068          @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
1069      }
1070  
1071      if ( empty( $avatar ) ) {
1072          // Default upload error.
1073          $message = __( 'Upload failed.', 'buddypress' );
1074  
1075          // Use the template message if set.
1076          if ( ! empty( $feedback_message ) ) {
1077              $message = $feedback_message;
1078          }
1079  
1080          // Upload error reply.
1081          bp_attachments_json_response( false, $is_html4, array(
1082              'type'    => 'upload_error',
1083              'message' => $message,
1084          ) );
1085      }
1086  
1087      if ( empty( $bp->avatar_admin->image->file ) ) {
1088          bp_attachments_json_response( false, $is_html4 );
1089      }
1090  
1091      $uploaded_image = @getimagesize( $bp->avatar_admin->image->file );
1092  
1093      // Set the name of the file.
1094      $name = $_FILES['file']['name'];
1095      $name_parts = pathinfo( $name );
1096      $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) );
1097  
1098      // Finally return the avatar to the editor.
1099      bp_attachments_json_response( true, $is_html4, array(
1100          'name'      => $name,
1101          'url'       => $bp->avatar_admin->image->url,
1102          'width'     => $uploaded_image[0],
1103          'height'    => $uploaded_image[1],
1104          'feedback'  => $feedback_message,
1105      ) );
1106  }
1107  add_action( 'wp_ajax_bp_avatar_upload', 'bp_avatar_ajax_upload' );
1108  
1109  /**
1110   * Handle avatar webcam capture.
1111   *
1112   * @since 2.3.0
1113   *
1114   * @param string $data    Base64 encoded image.
1115   * @param int    $item_id Item to associate.
1116   * @return bool True on success, false on failure.
1117   */
1118  function bp_avatar_handle_capture( $data = '', $item_id = 0 ) {
1119      if ( empty( $data ) || empty( $item_id ) ) {
1120          return false;
1121      }
1122  
1123      /**
1124       * Filters whether or not to handle avatar webcam capture.
1125       *
1126       * If you want to override this function, make sure you return false.
1127       *
1128       * @since 2.5.1
1129       *
1130       * @param bool   $value   Whether or not to crop.
1131       * @param string $data    Base64 encoded image.
1132       * @param int    $item_id Item to associate.
1133       */
1134      if ( ! apply_filters( 'bp_avatar_pre_handle_capture', true, $data, $item_id ) ) {
1135          return true;
1136      }
1137  
1138      $avatar_dir = bp_core_avatar_upload_path() . '/avatars';
1139  
1140      // It's not a regular upload, we may need to create this folder.
1141      if ( ! file_exists( $avatar_dir ) ) {
1142          if ( ! wp_mkdir_p( $avatar_dir ) ) {
1143              return false;
1144          }
1145      }
1146  
1147      /**
1148       * Filters the Avatar folder directory.
1149       *
1150       * @since 2.3.0
1151       *
1152       * @param string $avatar_dir Directory for storing avatars.
1153       * @param int    $item_id    ID of the item being acted on.
1154       * @param string $value      Avatar type.
1155       * @param string $value      Avatars word.
1156       */
1157      $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', $avatar_dir . '/' . $item_id, $item_id, 'user', 'avatars' );
1158  
1159      // It's not a regular upload, we may need to create this folder.
1160      if( ! is_dir( $avatar_folder_dir ) ) {
1161          if ( ! wp_mkdir_p( $avatar_folder_dir ) ) {
1162              return false;
1163          }
1164      }
1165  
1166      $original_file = $avatar_folder_dir . '/webcam-capture-' . $item_id . '.png';
1167  
1168      if ( file_put_contents( $original_file, $data ) ) {
1169          $avatar_to_crop = str_replace( bp_core_avatar_upload_path(), '', $original_file );
1170  
1171          // Crop to default values.
1172          $crop_args = array( 'item_id' => $item_id, 'original_file' => $avatar_to_crop, 'crop_x' => 0, 'crop_y' => 0 );
1173  
1174          return bp_core_avatar_handle_crop( $crop_args );
1175      } else {
1176          return false;
1177      }
1178  }
1179  
1180  /**
1181   * Crop an uploaded avatar.
1182   *
1183   * @since 1.1.0
1184   *
1185   * @param array|string $args {
1186   *     Array of function parameters.
1187   *
1188   *     @type string      $object        Object type of the item whose avatar you're
1189   *                                      handling. 'user', 'group', 'blog', or custom.
1190   *                                      Default: 'user'.
1191   *     @type string      $avatar_dir    Subdirectory where avatar should be stored.
1192   *                                      Default: 'avatars'.
1193   *     @type bool|int    $item_id       ID of the item that the avatar belongs to.
1194   *     @type bool|string $original_file Absolute path to the original avatar file.
1195   *     @type int         $crop_w        Crop width. Default: the global 'full' avatar width,
1196   *                                      as retrieved by bp_core_avatar_full_width().
1197   *     @type int         $crop_h        Crop height. Default: the global 'full' avatar height,
1198   *                                      as retrieved by bp_core_avatar_full_height().
1199   *     @type int         $crop_x        The horizontal starting point of the crop. Default: 0.
1200   *     @type int         $crop_y        The vertical starting point of the crop. Default: 0.
1201   * }
1202   * @return bool True on success, false on failure.
1203   */
1204  function bp_core_avatar_handle_crop( $args = '' ) {
1205  
1206      $r = wp_parse_args( $args, array(
1207          'object'        => 'user',
1208          'avatar_dir'    => 'avatars',
1209          'item_id'       => false,
1210          'original_file' => false,
1211          'crop_w'        => bp_core_avatar_full_width(),
1212          'crop_h'        => bp_core_avatar_full_height(),
1213          'crop_x'        => 0,
1214          'crop_y'        => 0
1215      ) );
1216  
1217      /**
1218       * Filters whether or not to handle cropping.
1219       *
1220       * If you want to override this function, make sure you return false.
1221       *
1222       * @since 1.2.4
1223       *
1224       * @param bool  $value Whether or not to crop.
1225       * @param array $r     Array of parsed arguments for function.
1226       */
1227      if ( ! apply_filters( 'bp_core_pre_avatar_handle_crop', true, $r ) ) {
1228          return true;
1229      }
1230  
1231      // Crop the file.
1232      $avatar_attachment = new BP_Attachment_Avatar();
1233      $cropped           = $avatar_attachment->crop( $r );
1234  
1235      // Check for errors.
1236      if ( empty( $cropped['full'] ) || empty( $cropped['thumb'] ) || is_wp_error( $cropped['full'] ) || is_wp_error( $cropped['thumb'] ) ) {
1237          return false;
1238      }
1239  
1240      return true;
1241  }
1242  
1243  /**
1244   * Ajax set an avatar for a given object and item id.
1245   *
1246   * @since 2.3.0
1247   *
1248   * @return string|null A JSON object containing success data if the crop/capture succeeded
1249   *                     error message otherwise.
1250   */
1251  function bp_avatar_ajax_set() {
1252      if ( ! bp_is_post_request() ) {
1253          wp_send_json_error();
1254      }
1255  
1256      // Check the nonce.
1257      check_admin_referer( 'bp_avatar_cropstore', 'nonce' );
1258  
1259      $avatar_data = wp_parse_args( $_POST, array(
1260          'crop_w' => bp_core_avatar_full_width(),
1261          'crop_h' => bp_core_avatar_full_height(),
1262          'crop_x' => 0,
1263          'crop_y' => 0
1264      ) );
1265  
1266      if ( empty( $avatar_data['object'] ) || empty( $avatar_data['item_id'] ) || empty( $avatar_data['original_file'] ) ) {
1267          wp_send_json_error();
1268      }
1269  
1270      // Capability check.
1271      if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) {
1272          wp_send_json_error();
1273      }
1274  
1275      if ( ! empty( $avatar_data['type'] ) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object'] ) {
1276          $webcam_avatar = false;
1277  
1278          if ( ! empty( $avatar_data['original_file'] ) ) {
1279              $webcam_avatar = str_replace( array( 'data:image/png;base64,', ' ' ), array( '', '+' ), $avatar_data['original_file'] );
1280              $webcam_avatar = base64_decode( $webcam_avatar );
1281          }
1282  
1283          if ( ! bp_avatar_handle_capture( $webcam_avatar, $avatar_data['item_id'] ) ) {
1284              wp_send_json_error( array(
1285                  'feedback_code' => 1
1286              ) );
1287  
1288          } else {
1289              $return = array(
1290                  'avatar' => esc_url( bp_core_fetch_avatar( array(
1291                      'object'  => $avatar_data['object'],
1292                      'item_id' => $avatar_data['item_id'],
1293                      'html'    => false,
1294                      'type'    => 'full',
1295                  ) ) ),
1296                  'feedback_code' => 2,
1297                  'item_id'       => $avatar_data['item_id'],
1298              );
1299  
1300              /**
1301               * Fires if the new avatar was successfully captured.
1302               *
1303               * @since 1.1.0 Used to inform the avatar was successfully cropped
1304               * @since 2.3.4 Add two new parameters to inform about the user id and
1305               *              about the way the avatar was set (eg: 'crop' or 'camera')
1306               *              Move the action at the right place, once the avatar is set
1307               * @since 2.8.0 Added the `$avatar_data` parameter.
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( 'xprofile_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 bp-core/bp-core-avatars.php */
1359              do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
1360          } elseif ( 'group' === $avatar_data['object'] ) {
1361              /** This action is documented in bp-groups/bp-groups-screens.php */
1362              do_action( 'groups_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
1363          }
1364  
1365          wp_send_json_success( $return );
1366      } else {
1367          wp_send_json_error( array(
1368              'feedback_code' => 1,
1369          ) );
1370      }
1371  }
1372  add_action( 'wp_ajax_bp_avatar_set', 'bp_avatar_ajax_set' );
1373  
1374  /**
1375   * Filter {@link get_avatar_url()} to use the BuddyPress user avatar URL.
1376   *
1377   * @since 2.9.0
1378   *
1379   * @param  string $retval      The URL of the avatar.
1380   * @param  mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
1381   *                             user email, WP_User object, WP_Post object, or WP_Comment object.
1382   * @param  array  $args        Arguments passed to get_avatar_data(), after processing.
1383   * @return string
1384   */
1385  function bp_core_get_avatar_data_url_filter( $retval, $id_or_email, $args ) {
1386      $user = null;
1387  
1388      // Ugh, hate duplicating code; process the user identifier.
1389      if ( is_numeric( $id_or_email ) ) {
1390          $user = get_user_by( 'id', absint( $id_or_email ) );
1391      } elseif ( $id_or_email instanceof WP_User ) {
1392          // User Object
1393          $user = $id_or_email;
1394      } elseif ( $id_or_email instanceof WP_Post ) {
1395          // Post Object
1396          $user = get_user_by( 'id', (int) $id_or_email->post_author );
1397      } elseif ( $id_or_email instanceof WP_Comment ) {
1398          if ( ! empty( $id_or_email->user_id ) ) {
1399              $user = get_user_by( 'id', (int) $id_or_email->user_id );
1400          }
1401      } elseif ( is_email( $id_or_email ) ) {
1402          $user = get_user_by( 'email', $id_or_email );
1403      }
1404  
1405      // No user, so bail.
1406      if ( false === $user instanceof WP_User ) {
1407          return $retval;
1408      }
1409  
1410      // Set BuddyPress-specific avatar args.
1411      $args['item_id'] = $user->ID;
1412      $args['html']    = false;
1413  
1414      // Use the 'full' type if size is larger than BP's thumb width.
1415      if ( (int) $args['size'] > bp_core_avatar_thumb_width() ) {
1416          $args['type'] = 'full';
1417      }
1418  
1419      // Get the BuddyPress avatar URL.
1420      if ( $bp_avatar = bp_core_fetch_avatar( $args ) ) {
1421          return $bp_avatar;
1422      }
1423  
1424      return $retval;
1425  }
1426  add_filter( 'get_avatar_url', 'bp_core_get_avatar_data_url_filter', 10, 3 );
1427  
1428  /**
1429   * Is the current avatar upload error-free?
1430   *
1431   * @since 1.0.0
1432   *
1433   * @param array $file The $_FILES array.
1434   * @return bool True if no errors are found. False if there are errors.
1435   */
1436  function bp_core_check_avatar_upload( $file ) {
1437      if ( isset( $file['error'] ) && $file['error'] )
1438          return false;
1439  
1440      return true;
1441  }
1442  
1443  /**
1444   * Is the file size of the current avatar upload permitted?
1445   *
1446   * @since 1.0.0
1447   *
1448   * @param array $file The $_FILES array.
1449   * @return bool True if the avatar is under the size limit, otherwise false.
1450   */
1451  function bp_core_check_avatar_size( $file ) {
1452      if ( $file['file']['size'] > bp_core_avatar_original_max_filesize() )
1453          return false;
1454  
1455      return true;
1456  }
1457  
1458  /**
1459   * Get allowed avatar types.
1460   *
1461   * @since 2.3.0
1462   *
1463   * @return array
1464   */
1465  function bp_core_get_allowed_avatar_types() {
1466      $allowed_types = bp_attachments_get_allowed_types( 'avatar' );
1467  
1468      /**
1469       * Filters the list of allowed image types.
1470       *
1471       * @since 2.3.0
1472       *
1473       * @param array $allowed_types List of image types.
1474       */
1475      $avatar_types = (array) apply_filters( 'bp_core_get_allowed_avatar_types', $allowed_types );
1476  
1477      if ( empty( $avatar_types ) ) {
1478          $avatar_types = $allowed_types;
1479      } else {
1480          $avatar_types = array_intersect( $allowed_types, $avatar_types );
1481      }
1482  
1483      return array_values( $avatar_types );
1484  }
1485  
1486  /**
1487   * Get allowed avatar mime types.
1488   *
1489   * @since 2.3.0
1490   *
1491   * @return array
1492   */
1493  function bp_core_get_allowed_avatar_mimes() {
1494      $allowed_types  = bp_core_get_allowed_avatar_types();
1495  
1496      return bp_attachments_get_allowed_mimes( 'avatar', $allowed_types );
1497  }
1498  
1499  /**
1500   * Does the current avatar upload have an allowed file type?
1501   *
1502   * Permitted file types are JPG, GIF and PNG.
1503   *
1504   * @since 1.0.0
1505   *
1506   * @param array $file The $_FILES array.
1507   * @return bool True if the file extension is permitted, otherwise false.
1508   */
1509  function bp_core_check_avatar_type( $file ) {
1510      return bp_attachments_check_filetype( $file['file']['tmp_name'], $file['file']['name'], bp_core_get_allowed_avatar_mimes() );
1511  }
1512  
1513  /**
1514   * Fetch data from the BP root blog's upload directory.
1515   *
1516   * @since 1.8.0
1517   *
1518   * @param string $type The variable we want to return from the $bp->avatars object.
1519   *                     Only 'upload_path' and 'url' are supported. Default: 'upload_path'.
1520   * @return string The avatar upload directory path.
1521   */
1522  function bp_core_get_upload_dir( $type = 'upload_path' ) {
1523      $bp = buddypress();
1524  
1525      switch ( $type ) {
1526          case 'upload_path' :
1527              $constant = 'BP_AVATAR_UPLOAD_PATH';
1528              $key      = 'basedir';
1529  
1530              break;
1531  
1532          case 'url' :
1533              $constant = 'BP_AVATAR_URL';
1534              $key      = 'baseurl';
1535  
1536              break;
1537  
1538          default :
1539              return false;
1540  
1541              break;
1542      }
1543  
1544      // See if the value has already been calculated and stashed in the $bp global.
1545      if ( isset( $bp->avatar->$type ) ) {
1546          $retval = $bp->avatar->$type;
1547      } else {
1548          // If this value has been set in a constant, just use that.
1549          if ( defined( $constant ) ) {
1550              $retval = constant( $constant );
1551          } else {
1552  
1553              // Use cached upload dir data if available.
1554              if ( ! empty( $bp->avatar->upload_dir ) ) {
1555                  $upload_dir = $bp->avatar->upload_dir;
1556  
1557              // No cache, so query for it.
1558              } else {
1559  
1560                  // Get upload directory information from current site.
1561                  $upload_dir = bp_upload_dir();
1562  
1563                  // Stash upload directory data for later use.
1564                  $bp->avatar->upload_dir = $upload_dir;
1565              }
1566  
1567              // Directory does not exist and cannot be created.
1568              if ( ! empty( $upload_dir['error'] ) ) {
1569                  $retval = '';
1570  
1571              } else {
1572                  $retval = $upload_dir[$key];
1573  
1574                  // If $key is 'baseurl', check to see if we're on SSL
1575                  // Workaround for WP13941, WP15928, WP19037.
1576                  if ( $key == 'baseurl' && is_ssl() ) {
1577                      $retval = str_replace( 'http://', 'https://', $retval );
1578                  }
1579              }
1580  
1581          }
1582  
1583          // Stash in $bp for later use.
1584          $bp->avatar->$type = $retval;
1585      }
1586  
1587      return $retval;
1588  }
1589  
1590  /**
1591   * Get the absolute upload path for the WP installation.
1592   *
1593   * @since 1.2.0
1594   *
1595   * @return string Absolute path to WP upload directory.
1596   */
1597  function bp_core_avatar_upload_path() {
1598  
1599      /**
1600       * Filters the absolute upload path for the WP installation.
1601       *
1602       * @since 1.2.0
1603       *
1604       * @param string $value Absolute upload path for the WP installation.
1605       */
1606      return apply_filters( 'bp_core_avatar_upload_path', bp_core_get_upload_dir() );
1607  }
1608  
1609  /**
1610   * Get the raw base URL for root site upload location.
1611   *
1612   * @since 1.2.0
1613   *
1614   * @return string Full URL to current upload location.
1615   */
1616  function bp_core_avatar_url() {
1617  
1618      /**
1619       * Filters the raw base URL for root site upload location.
1620       *
1621       * @since 1.2.0
1622       *
1623       * @param string $value Raw base URL for the root site upload location.
1624       */
1625      return apply_filters( 'bp_core_avatar_url', bp_core_get_upload_dir( 'url' ) );
1626  }
1627  
1628  /**
1629   * Check if a given user ID has an uploaded avatar.
1630   *
1631   * @since 1.0.0
1632   *
1633   * @param int $user_id ID of the user whose avatar is being checked.
1634   * @return bool True if the user has uploaded a local avatar. Otherwise false.
1635   */
1636  function bp_get_user_has_avatar( $user_id = 0 ) {
1637  
1638      if ( empty( $user_id ) )
1639          $user_id = bp_displayed_user_id();
1640  
1641      $retval = false;
1642      if ( bp_core_fetch_avatar( array( 'item_id' => $user_id, 'no_grav' => true, 'html' => false, 'type' => 'full' ) ) != bp_core_avatar_default( 'local' ) )
1643          $retval = true;
1644  
1645      /**
1646       * Filters whether or not a user has an uploaded avatar.
1647       *
1648       * @since 1.6.0
1649       *
1650       * @param bool $retval  Whether or not a user has an uploaded avatar.
1651       * @param int  $user_id ID of the user being checked.
1652       */
1653      return (bool) apply_filters( 'bp_get_user_has_avatar', $retval, $user_id );
1654  }
1655  
1656  /**
1657   * Utility function for fetching an avatar dimension setting.
1658   *
1659   * @since 1.5.0
1660   *
1661   * @param string $type   Dimension type you're fetching dimensions for. 'thumb'
1662   *                       or 'full'. Default: 'thumb'.
1663   * @param string $h_or_w Which dimension is being fetched. 'height' or 'width'.
1664   *                       Default: 'height'.
1665   * @return int|bool $dim The dimension.
1666   */
1667  function bp_core_avatar_dimension( $type = 'thumb', $h_or_w = 'height' ) {
1668      $bp  = buddypress();
1669      $dim = isset( $bp->avatar->{$type}->{$h_or_w} ) ? (int) $bp->avatar->{$type}->{$h_or_w} : false;
1670  
1671      /**
1672       * Filters the avatar dimension setting.
1673       *
1674       * @since 1.5.0
1675       *
1676       * @param int|bool $dim    Dimension setting for the type.
1677       * @param string   $type   The type of avatar whose dimensions are requested. Default 'thumb'.
1678       * @param string   $h_or_w The dimension parameter being requested. Default 'height'.
1679       */
1680      return apply_filters( 'bp_core_avatar_dimension', $dim, $type, $h_or_w );
1681  }
1682  
1683  /**
1684   * Get the 'thumb' avatar width setting.
1685   *
1686   * @since 1.5.0
1687   *
1688   * @return int The 'thumb' width.
1689   */
1690  function bp_core_avatar_thumb_width() {
1691  
1692      /**
1693       * Filters the 'thumb' avatar width setting.
1694       *
1695       * @since 1.5.0
1696       *
1697       * @param int $value Value for the 'thumb' avatar width setting.
1698       */
1699      return apply_filters( 'bp_core_avatar_thumb_width', bp_core_avatar_dimension( 'thumb', 'width' ) );
1700  }
1701  
1702  /**
1703   * Get the 'thumb' avatar height setting.
1704   *
1705   * @since 1.5.0
1706   *
1707   * @return int The 'thumb' height.
1708   */
1709  function bp_core_avatar_thumb_height() {
1710  
1711      /**
1712       * Filters the 'thumb' avatar height setting.
1713       *
1714       * @since 1.5.0
1715       *
1716       * @param int $value Value for the 'thumb' avatar height setting.
1717       */
1718      return apply_filters( 'bp_core_avatar_thumb_height', bp_core_avatar_dimension( 'thumb', 'height' ) );
1719  }
1720  
1721  /**
1722   * Get the 'full' avatar width setting.
1723   *
1724   * @since 1.5.0
1725   *
1726   * @return int The 'full' width.
1727   */
1728  function bp_core_avatar_full_width() {
1729  
1730      /**
1731       * Filters the 'full' avatar width setting.
1732       *
1733       * @since 1.5.0
1734       *
1735       * @param int $value Value for the 'full' avatar width setting.
1736       */
1737      return apply_filters( 'bp_core_avatar_full_width', bp_core_avatar_dimension( 'full', 'width' ) );
1738  }
1739  
1740  /**
1741   * Get the 'full' avatar height setting.
1742   *
1743   * @since 1.5.0
1744   *
1745   * @return int The 'full' height.
1746   */
1747  function bp_core_avatar_full_height() {
1748  
1749      /**
1750       * Filters the 'full' avatar height setting.
1751       *
1752       * @since 1.5.0
1753       *
1754       * @param int $value Value for the 'full' avatar height setting.
1755       */
1756      return apply_filters( 'bp_core_avatar_full_height', bp_core_avatar_dimension( 'full', 'height' ) );
1757  }
1758  
1759  /**
1760   * Get the max width for original avatar uploads.
1761   *
1762   * @since 1.5.0
1763   *
1764   * @return int The max width for original avatar uploads.
1765   */
1766  function bp_core_avatar_original_max_width() {
1767  
1768      /**
1769       * Filters the max width for original avatar uploads.
1770       *
1771       * @since 1.5.0
1772       *
1773       * @param int $value Value for the max width.
1774       */
1775      return apply_filters( 'bp_core_avatar_original_max_width', (int) buddypress()->avatar->original_max_width );
1776  }
1777  
1778  /**
1779   * Get the max filesize for original avatar uploads.
1780   *
1781   * @since 1.5.0
1782   *
1783   * @return int The max filesize for original avatar uploads.
1784   */
1785  function bp_core_avatar_original_max_filesize() {
1786  
1787      /**
1788       * Filters the max filesize for original avatar uploads.
1789       *
1790       * @since 1.5.0
1791       *
1792       * @param int $value Value for the max filesize.
1793       */
1794      return apply_filters( 'bp_core_avatar_original_max_filesize', (int) buddypress()->avatar->original_max_filesize );
1795  }
1796  
1797  /**
1798   * Get the URL of the 'full' default avatar.
1799   *
1800   * @since 1.5.0
1801   * @since 2.6.0 Introduced `$params` and `$object_type` parameters.
1802   *
1803   * @param string $type   'local' if the fallback should be the locally-hosted version
1804   *                       of the mystery person, 'gravatar' if the fallback should be
1805   *                       Gravatar's version. Default: 'gravatar'.
1806   * @param array  $params Parameters passed to bp_core_fetch_avatar().
1807   * @return string The URL of the default avatar.
1808   */
1809  function bp_core_avatar_default( $type = 'gravatar', $params = array() ) {
1810      // Local override.
1811      if ( defined( 'BP_AVATAR_DEFAULT' ) ) {
1812          $avatar = BP_AVATAR_DEFAULT;
1813  
1814      // Use the local default image.
1815      } elseif ( 'local' === $type ) {
1816          $size = '';
1817          if (
1818              ( isset( $params['type'] ) && 'thumb' === $params['type'] && bp_core_avatar_thumb_width() <= 50 ) ||
1819              ( isset( $params['width'] ) && $params['width'] <= 50 )
1820          ) {
1821  
1822              $size = '-50';
1823          }
1824  
1825          $avatar = buddypress()->plugin_url . "bp-core/images/mystery-man{$size}.jpg";
1826  
1827      // Use Gravatar's mystery person as fallback.
1828      } else {
1829          $size = '';
1830          if ( isset( $params['type'] ) && 'thumb' === $params['type'] ) {
1831              $size = bp_core_avatar_thumb_width();
1832          } else {
1833              $size = bp_core_avatar_full_width();
1834          }
1835          $avatar = '//www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&amp;s=' . $size;
1836      }
1837  
1838      /**
1839       * Filters the URL of the 'full' default avatar.
1840       *
1841       * @since 1.5.0
1842       * @since 2.6.0 Added `$params`.
1843       *
1844       * @param string $avatar URL of the default avatar.
1845       * @param array  $params Params provided to bp_core_fetch_avatar().
1846       */
1847      return apply_filters( 'bp_core_avatar_default', $avatar, $params );
1848  }
1849  
1850  /**
1851   * Get the URL of the 'thumb' default avatar.
1852   *
1853   * Uses Gravatar's mystery-person avatar, unless BP_AVATAR_DEFAULT_THUMB has been
1854   * defined.
1855   *
1856   * @since 1.5.0
1857   * @since 2.6.0 Introduced `$object_type` parameter.
1858   *
1859   * @param string $type   'local' if the fallback should be the locally-hosted version
1860   *                       of the mystery person, 'gravatar' if the fallback should be
1861   *                       Gravatar's version. Default: 'gravatar'.
1862   * @param array  $params Parameters passed to bp_core_fetch_avatar().
1863   * @return string The URL of the default avatar thumb.
1864   */
1865  function bp_core_avatar_default_thumb( $type = 'gravatar', $params = array() ) {
1866      // Local override.
1867      if ( defined( 'BP_AVATAR_DEFAULT_THUMB' ) ) {
1868          $avatar = BP_AVATAR_DEFAULT_THUMB;
1869  
1870      // Use the local default image.
1871      } elseif ( 'local' === $type ) {
1872          $avatar = buddypress()->plugin_url . 'bp-core/images/mystery-man-50.jpg';
1873  
1874      // Use Gravatar's mystery person as fallback.
1875      } else {
1876          $avatar = '//www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&amp;s=' . bp_core_avatar_thumb_width();
1877      }
1878  
1879      /**
1880       * Filters the URL of the 'thumb' default avatar.
1881       *
1882       * @since 1.5.0
1883       * @since 2.6.0 Added `$params`.
1884       *
1885       * @param string $avatar URL of the default avatar.
1886       * @param string $params Params provided to bp_core_fetch_avatar().
1887       */
1888      return apply_filters( 'bp_core_avatar_thumb', $avatar, $params );
1889  }
1890  
1891  /**
1892   * Reset the week parameter of the WordPress main query if needed.
1893   *
1894   * When cropping an avatar, a $_POST['w'] var is sent, setting the 'week'
1895   * parameter of the WordPress main query to this posted var. To avoid
1896   * notices, we need to make sure this 'week' query var is reset to 0.
1897   *
1898   * @since 2.2.0
1899   *
1900   * @param WP_Query|null $posts_query The main query object.
1901   */
1902  function bp_core_avatar_reset_query( $posts_query = null ) {
1903      $reset_w = false;
1904  
1905      // Group's avatar edit screen.
1906      if ( bp_is_group_admin_page() ) {
1907          $reset_w = bp_is_group_admin_screen( 'group-avatar' );
1908  
1909      // Group's avatar create screen.
1910      } elseif ( bp_is_group_create() ) {
1911          /**
1912           * We can't use bp_get_groups_current_create_step().
1913           * as it's not set yet
1914           */
1915          $reset_w = 'group-avatar' === bp_action_variable( 1 );
1916  
1917      // User's change avatar screen.
1918      } else {
1919          $reset_w = bp_is_user_change_avatar();
1920      }
1921  
1922      // A user or a group is cropping an avatar.
1923      if ( true === $reset_w && isset( $_POST['avatar-crop-submit'] ) ) {
1924          $posts_query->set( 'w', 0 );
1925      }
1926  }
1927  add_action( 'bp_parse_query', 'bp_core_avatar_reset_query', 10, 1 );
1928  
1929  /**
1930   * Checks whether Avatar UI should be loaded.
1931   *
1932   * @since 2.3.0
1933   *
1934   * @return bool True if Avatar UI should load, false otherwise.
1935   */
1936  function bp_avatar_is_front_edit() {
1937      $retval = false;
1938  
1939      // No need to carry on if the current WordPress version is not supported.
1940      if ( ! bp_attachments_is_wp_version_supported() ) {
1941          return $retval;
1942      }
1943  
1944      if ( bp_is_user_change_avatar() && 'crop-image' !== bp_get_avatar_admin_step() ) {
1945          $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
1946      }
1947  
1948      if ( bp_is_active( 'groups' ) ) {
1949          // Group creation.
1950          if ( bp_is_group_create() && bp_is_group_creation_step( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
1951              $retval = ! bp_disable_group_avatar_uploads();
1952  
1953          // Group Manage.
1954          } elseif ( bp_is_group_admin_page() && bp_is_group_admin_screen( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
1955              $retval = ! bp_disable_group_avatar_uploads();
1956          }
1957      }
1958  
1959      /**
1960       * Use this filter if you need to :
1961       * - Load the avatar UI for a component that is !groups or !user (return true regarding your conditions)
1962       * - Completely disable the avatar UI introduced in 2.3 (eg: __return_false())
1963       *
1964       * @since 2.3.0
1965       *
1966       * @param bool $retval Whether or not to load the Avatar UI.
1967       */
1968      return apply_filters( 'bp_avatar_is_front_edit', $retval );
1969  }
1970  
1971  /**
1972   * Checks whether the Webcam Avatar UI part should be loaded.
1973   *
1974   * @since 2.3.0
1975   *
1976   * @global $is_safari
1977   * @global $is_IE
1978   *
1979   * @return bool True to load the Webcam Avatar UI part. False otherwise.
1980   */
1981  function bp_avatar_use_webcam() {
1982      global $is_safari, $is_IE, $is_chrome;
1983  
1984      /**
1985       * Do not use the webcam feature for mobile devices
1986       * to avoid possible confusions.
1987       */
1988      if ( wp_is_mobile() ) {
1989          return false;
1990      }
1991  
1992      /**
1993       * Bail when the browser does not support getUserMedia.
1994       *
1995       * @see http://caniuse.com/#feat=stream
1996       */
1997      if ( $is_safari || $is_IE || ( $is_chrome && ! is_ssl() ) ) {
1998          return false;
1999      }
2000  
2001      /**
2002       * Use this filter if you need to disable the webcam capture feature
2003       * by returning false.
2004       *
2005       * @since 2.3.0
2006       *
2007       * @param bool $value Whether or not to load Webcam Avatar UI part.
2008       */
2009      return apply_filters( 'bp_avatar_use_webcam', true );
2010  }
2011  
2012  /**
2013   * Template function to load the Avatar UI javascript templates.
2014   *
2015   * @since 2.3.0
2016   */
2017  function bp_avatar_get_templates() {
2018      if ( ! bp_avatar_is_front_edit() ) {
2019          return;
2020      }
2021  
2022      bp_attachments_get_template_part( 'avatars/index' );
2023  }
2024  
2025  /**
2026   * Trick to check if the theme's BuddyPress templates are up to date.
2027   *
2028   * If the "avatar templates" are not including the new template tag, this will
2029   * help users to get the avatar UI.
2030   *
2031   * @since 2.3.0
2032   */
2033  function bp_avatar_template_check() {
2034      if ( ! bp_avatar_is_front_edit() ) {
2035          return;
2036      }
2037  
2038      if ( ! did_action( 'bp_attachments_avatar_check_template' ) ) {
2039          bp_attachments_get_template_part( 'avatars/index' );
2040      }
2041  }


Generated: Wed Jan 29 01:01:45 2020 Cross-referenced by PHPXref 0.7.1