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