Changeset 3232 for trunk/bp-core/bp-core-avatars.php
- Timestamp:
- 09/06/2010 04:24:57 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-core/bp-core-avatars.php
r2946 r3232 40 40 41 41 if ( !defined( 'BP_AVATAR_DEFAULT' ) ) 42 define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . '/bp- xprofile/images/none.gif' );42 define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . '/bp-core/images/mystery-man.jpg' ); 43 43 44 44 if ( !defined( 'BP_AVATAR_DEFAULT_THUMB' ) ) 45 define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . '/bp-xprofile/images/none-thumbnail.gif' ); 46 } 47 add_action( 'bp_init', 'bp_core_set_avatar_constants' ); 48 45 define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . '/bp-core/images/mystery-man-50.jpg' ); 46 } 47 add_action( 'bp_loaded', 'bp_core_set_avatar_constants', 8 ); 48 49 /** 50 * bp_core_fetch_avatar() 51 * 52 * Fetches an avatar from a BuddyPress object. Supports user/group/blog as 53 * default, but can be extended to include your own custom components too. 54 * 55 * @global object $bp 56 * @global object $current_blog 57 * @param array $args Determine the output of this function 58 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg 59 */ 49 60 function bp_core_fetch_avatar( $args = '' ) { 50 61 global $bp, $current_blog; 51 62 63 // Set a few default variables 64 $def_object = 'user'; 65 $def_type = 'thumb'; 66 $def_class = 'avatar'; 67 $def_alt = __( 'Avatar Image', 'buddypress' ); 68 69 // Set the default variables array 52 70 $defaults = array( 53 'item_id' => false, 54 'object' => 'user', // user OR group OR blog OR custom type (if you use filters) 55 'type' => 'thumb', 56 'avatar_dir' => false, 57 'width' => false, 58 'height' => false, 59 'class' => 'avatar', 60 'css_id' => false, 61 'alt' => __( 'Avatar Image', 'buddypress' ), 62 'email' => false, // Pass the user email (for gravatar) to prevent querying the DB for it 63 'no_grav' => false // If there is no avatar found, return false instead of a grav? 71 'item_id' => false, 72 'object' => $def_object, // user/group/blog/custom type (if you use filters) 73 'type' => $def_type, // thumb or full 74 'avatar_dir' => false, // Specify a custom avatar directory for your object 75 'width' => false, // Custom width (int) 76 'height' => false, // Custom height (int) 77 'class' => $def_class, // Custom <img> class (string) 78 'css_id' => false, // Custom <img> ID (string) 79 'alt' => $def_alt, // Custom <img> alt (string) 80 'email' => false, // Pass the user email (for gravatar) to prevent querying the DB for it 81 'no_grav' => false, // If there is no avatar found, return false instead of a grav? 82 'html' => true // Wrap the return img URL in <img /> 64 83 ); 65 84 85 // Compare defaults to passed and extract 66 86 $params = wp_parse_args( $args, $defaults ); 67 87 extract( $params, EXTR_SKIP ); 68 88 89 // Set item_id if not passed 69 90 if ( !$item_id ) { 70 91 if ( 'user' == $object ) … … 80 101 } 81 102 103 // Set avatar_dir if not passed (uses $object) 82 104 if ( !$avatar_dir ) { 83 105 if ( 'user' == $object ) … … 93 115 } 94 116 95 / * Add an identifying class to each item */117 // Add an identifying class to each item 96 118 $class .= ' ' . $object . '-' . $item_id . '-avatar'; 97 119 98 if ( !empty($css_id) ) 120 // Set CSS ID if passed 121 if ( !empty( $css_id ) ) 99 122 $css_id = " id='{$css_id}'"; 100 123 124 // Set avatar width 101 125 if ( $width ) 102 126 $html_width = " width='{$width}'"; … … 104 128 $html_width = ( 'thumb' == $type ) ? ' width="' . BP_AVATAR_THUMB_WIDTH . '"' : ' width="' . BP_AVATAR_FULL_WIDTH . '"'; 105 129 130 // Set avatar height 106 131 if ( $height ) 107 132 $html_height = " height='{$height}'"; … … 109 134 $html_height = ( 'thumb' == $type ) ? ' height="' . BP_AVATAR_THUMB_HEIGHT . '"' : ' height="' . BP_AVATAR_FULL_HEIGHT . '"'; 110 135 111 $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', str_replace( WP_CONTENT_DIR, BP_AVATAR_URL, BP_AVATAR_UPLOAD_PATH ) . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 136 // Set avatar URL and DIR based on prepopulated constants 137 $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', BP_AVATAR_URL . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 112 138 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 113 139 … … 117 143 * or thumbnail image. 118 144 */ 119 $avatar_ name = ( 'full' == $type ) ? '-bpfull' : '-bpthumb';145 $avatar_size = ( 'full' == $type ) ? '-bpfull' : '-bpthumb'; 120 146 $legacy_user_avatar_name = ( 'full' == $type ) ? '-avatar2' : '-avatar1'; 121 147 $legacy_group_avatar_name = ( 'full' == $type ) ? '-groupavatar-full' : '-groupavatar-thumb'; 122 148 149 // Check for directory 123 150 if ( file_exists( $avatar_folder_dir ) ) { 151 152 // Open directory 124 153 if ( $av_dir = opendir( $avatar_folder_dir ) ) { 154 155 // Stash files in an array once to check for one that matches 156 $avatar_files = array(); 125 157 while ( false !== ( $avatar_file = readdir($av_dir) ) ) { 126 if ( preg_match( "/{$avatar_name}/", $avatar_file ) || preg_match( "/{$legacy_user_avatar_name}/", $avatar_file ) || preg_match( "/{$legacy_group_avatar_name}/", $avatar_file ) ) 127 $avatar_url = $avatar_folder_url . '/' . $avatar_file; 158 // Only add files to the array (skip directories) 159 if ( 2 < strlen( $avatar_file ) ) 160 $avatar_files[] = $avatar_file; 161 } 162 163 // Check for array 164 if ( 0 < count( $avatar_files ) ) { 165 166 // Check for current avatar 167 foreach( $avatar_files as $key => $value ) { 168 if ( strpos ( $value, $avatar_size )!== false ) 169 $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key]; 170 } 171 172 // Legacy avatar check 173 if ( !isset( $avatar_url ) ) { 174 foreach( $avatar_files as $key => $value ) { 175 if ( strpos ( $value, $legacy_user_avatar_name )!== false ) 176 $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key]; 177 } 178 179 // Legacy group avatar check 180 if ( !isset( $avatar_url ) ) { 181 foreach( $avatar_files as $key => $value ) { 182 if ( strpos ( $value, $legacy_group_avatar_name )!== false ) 183 $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key]; 184 } 185 } 186 } 128 187 } 129 188 } 130 closedir($av_dir); 131 132 if ( $avatar_url ) 133 return apply_filters( 'bp_core_fetch_avatar', "<img src='{$avatar_url}' alt='{$alt}' class='{$class}'{$css_id}{$html_width}{$html_height} />", $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir ); 134 } 135 136 /* If no avatars have been uploaded for this item, display a gravatar */ 189 190 // Close the avatar directory 191 closedir( $av_dir ); 192 193 // If we found a locally uploaded avatar 194 if ( $avatar_url ) { 195 196 // Return it wrapped in an <img> element 197 if ( true === $html ) { 198 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir ); 199 200 // ...or only the URL 201 } else { 202 return apply_filters( 'bp_core_fetch_avatar_url', $avatar_url ); 203 } 204 } 205 } 206 207 // If no avatars could be found, try to display a gravatar 208 209 // Skips gravatar check if $no_grav is passed 137 210 if ( !$no_grav ) { 211 212 // Set gravatar size 213 if ( $width ) 214 $grav_size = $width; 215 else if ( 'full' == $type ) 216 $grav_size = BP_AVATAR_FULL_WIDTH; 217 else if ( 'thumb' == $type ) 218 $grav_size = BP_AVATAR_THUMB_WIDTH; 219 220 // Set gravatar type 138 221 if ( empty( $bp->grav_default->{$object} ) ) 139 222 $default_grav = 'wavatar'; 140 223 else if ( 'mystery' == $bp->grav_default->{$object} ) 141 $default_grav = apply_filters( 'bp_core_mysteryman_src', BP_ PLUGIN_URL . '/bp-core/images/mystery-man.jpg');224 $default_grav = apply_filters( 'bp_core_mysteryman_src', BP_AVATAR_DEFAULT, $grav_size ); 142 225 else 143 226 $default_grav = $bp->grav_default->{$object}; 144 227 145 if ( $width ) $grav_size = $width; 146 else if ( 'full' == $type ) $grav_size = BP_AVATAR_FULL_WIDTH; 147 else if ( 'thumb' == $type ) $grav_size = BP_AVATAR_THUMB_WIDTH; 148 228 // Set gravatar object 149 229 if ( empty( $email ) ) { 150 230 if ( 'user' == $object ) { … … 155 235 } 156 236 237 // Set host based on if using ssl 157 238 if ( is_ssl() ) 158 239 $host = 'https://secure.gravatar.com/avatar/'; … … 160 241 $host = 'http://www.gravatar.com/avatar/'; 161 242 162 $email = apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object ); 163 $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( $email ) . '?d=' . $default_grav . '&s=' . $grav_size; 164 165 return apply_filters( 'bp_core_fetch_avatar', "<img src='{$gravatar}' alt='{$alt}' class='{$class}'{$css_id}{$html_width}{$html_height} />", $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir ); 243 // Filter gravatar vars 244 $email = apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object ); 245 $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $email ) ) . '?d=' . $default_grav . '&s=' . $grav_size; 246 247 // Return gravatar wrapped in <img /> 248 if ( true === $html ) 249 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir ); 250 251 // ...or only return the gravatar URL 252 else 253 return apply_filters( 'bp_core_fetch_avatar_url', $gravatar ); 254 166 255 } else { 167 256 return apply_filters( 'bp_core_fetch_avatar', false, $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir ); … … 275 364 } 276 365 277 /* Resize the image down to something manageable and then delete the original */ 278 if ( getimagesize( $bp->avatar_admin->original['file'] ) > BP_AVATAR_ORIGINAL_MAX_WIDTH ) 279 $bp->avatar_admin->resized = wp_create_thumbnail( $bp->avatar_admin->original['file'], BP_AVATAR_ORIGINAL_MAX_WIDTH ); 280 281 $bp->avatar_admin->image = new stdClass; 366 /* Get image size */ 367 $size = @getimagesize( $bp->avatar_admin->original['file'] ); 368 369 /* Check image size and shrink if too large */ 370 if ( $size[0] > BP_AVATAR_ORIGINAL_MAX_WIDTH ) { 371 $thumb = wp_create_thumbnail( $bp->avatar_admin->original['file'], BP_AVATAR_ORIGINAL_MAX_WIDTH ); 372 373 /* Check for thumbnail creation errors */ 374 if ( is_wp_error( $thumb ) ) { 375 bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $thumb->get_error_message() ), 'error' ); 376 return false; 377 } 378 379 /* Thumbnail is good so proceed */ 380 $bp->avatar_admin->resized = $thumb; 381 } 282 382 283 383 /* We only want to handle one image after resize. */ 284 384 if ( empty( $bp->avatar_admin->resized ) ) 285 $bp->avatar_admin->image->dir = $bp->avatar_admin->original['file'];385 $bp->avatar_admin->image->dir = str_replace( BP_AVATAR_UPLOAD_PATH, '', $bp->avatar_admin->original['file'] ); 286 386 else { 287 $bp->avatar_admin->image->dir = $bp->avatar_admin->resized;387 $bp->avatar_admin->image->dir = str_replace( BP_AVATAR_UPLOAD_PATH, '', $bp->avatar_admin->resized ); 288 388 @unlink( $bp->avatar_admin->original['file'] ); 289 389 } 290 390 291 391 /* Set the url value for the image */ 292 $bp->avatar_admin->image->url = str_replace( WP_CONTENT_DIR, BP_AVATAR_URL, $bp->avatar_admin->image->dir );392 $bp->avatar_admin->image->url = BP_AVATAR_URL . $bp->avatar_admin->image->dir; 293 393 294 394 return true; … … 323 423 return false; 324 424 325 if ( !file_exists( WP_CONTENT_DIR . '/' . $original_file ) ) 425 $original_file = BP_AVATAR_UPLOAD_PATH . $original_file; 426 427 if ( !file_exists( $original_file ) ) 326 428 return false; 327 429 328 430 if ( !$item_id ) 329 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR .dirname( $original_file ), $item_id, $object, $avatar_dir );431 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', dirname( $original_file ), $item_id, $object, $avatar_dir ); 330 432 else 331 433 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); … … 352 454 353 455 /* Crop the image */ 354 $full_cropped = wp_crop_image( WP_CONTENT_DIR .$original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_FULL_WIDTH, BP_AVATAR_FULL_HEIGHT, false, $avatar_folder_dir . '/' . $full_filename );355 $thumb_cropped = wp_crop_image( WP_CONTENT_DIR .$original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_THUMB_WIDTH, BP_AVATAR_THUMB_HEIGHT, false, $avatar_folder_dir . '/' . $thumb_filename );456 $full_cropped = wp_crop_image( $original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_FULL_WIDTH, BP_AVATAR_FULL_HEIGHT, false, $avatar_folder_dir . '/' . $full_filename ); 457 $thumb_cropped = wp_crop_image( $original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_THUMB_WIDTH, BP_AVATAR_THUMB_HEIGHT, false, $avatar_folder_dir . '/' . $thumb_filename ); 356 458 357 459 /* Remove the original */ 358 @unlink( WP_CONTENT_DIR .$original_file );460 @unlink( $original_file ); 359 461 360 462 return true; 361 463 } 362 464 363 // Override internal "get_avatar()" function to use our own where possible 465 /** 466 * bp_core_fetch_avatar_filter() 467 * 468 * Attempts to filter get_avatar function and let BuddyPress have a go 469 * at finding an avatar that may have been uploaded locally. 470 * 471 * @global array $authordata 472 * @param string $avatar The result of get_avatar from before-filter 473 * @param int|string|object $user A user ID, email address, or comment object 474 * @param int $size Size of the avatar image (thumb/full) 475 * @param string $default URL to a default image to use if no avatar is available 476 * @param string $alt Alternate text to use in image tag. Defaults to blank 477 * @return <type> 478 */ 364 479 function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt ) { 365 global $authordata; 366 480 global $pagenow; 481 482 // Do not filter if inside WordPress options page 483 if ( 'options-discussion.php' == $pagenow ) 484 return $avatar; 485 486 // If passed an object, assume $user->user_id 367 487 if ( is_object( $user ) ) 368 488 $id = $user->user_id; 489 490 // If passed a number, assume it was a $user_id 369 491 else if ( is_numeric( $user ) ) 370 492 $id = $user; 371 else 372 $id = $authordata->ID; 373 493 494 // If passed a string and that string returns a user, get the $id 495 else if ( is_string( $user ) && ( $user_by_email = get_user_by_email( $user ) ) ) 496 $id = $user_by_email->ID; 497 498 // If somehow $id hasn't been assigned, return the result of get_avatar 374 499 if ( empty( $id ) ) 375 return $avatar; 376 500 return !empty( $avatar ) ? $avatar : $default; 501 502 // Let BuddyPress handle the fetching of the avatar 377 503 $bp_avatar = bp_core_fetch_avatar( array( 'item_id' => $id, 'width' => $size, 'height' => $size, 'alt' => $alt ) ); 378 504 505 // If BuddyPress found an avatar, use it. If not, use the result of get_avatar 379 506 return ( !$bp_avatar ) ? $avatar : $bp_avatar; 380 507 } … … 402 529 } 403 530 531 /** 532 * bp_core_avatar_upload_path() 533 * 534 * Returns the absolute upload path for the WP installation 535 * 536 * @global object $current_blog Current blog information 537 * @uses wp_upload_dir To get upload directory info 538 * @return string Absolute path to WP upload directory 539 */ 404 540 function bp_core_avatar_upload_path() { 405 if ( bp_core_is_multisite() ) 406 $path = ABSPATH . get_blog_option( BP_ROOT_BLOG, 'upload_path' ); 407 else { 408 if ( !$path = get_option( 'upload_path' ) ) 409 $path = WP_CONTENT_DIR . '/uploads'; 410 else 411 $path = ABSPATH . $path; 412 } 413 414 return apply_filters( 'bp_core_avatar_upload_path', $path ); 415 } 416 541 global $current_blog; 542 543 // Get upload directory information from current site 544 $upload_dir = wp_upload_dir(); 545 546 // If multisite, and current blog does not match root blog, make adjustments 547 if ( bp_core_is_multisite() && BP_ROOT_BLOG != $current_blog->blog_id ) 548 $upload_dir['basedir'] = get_blog_option( BP_ROOT_BLOG, 'upload_path' ); 549 550 return apply_filters( 'bp_core_avatar_upload_path', $upload_dir['basedir'] ); 551 } 552 553 /** 554 * bp_core_avatar_url() 555 * 556 * Returns the raw base URL for root site upload location 557 * 558 * @global object $current_blog Current blog information 559 * @uses wp_upload_dir To get upload directory info 560 * @return string Full URL to current upload location 561 */ 417 562 function bp_core_avatar_url() { 418 if ( !bp_core_is_multisite() ) 419 return WP_CONTENT_URL; 420 421 return apply_filters( 'bp_core_avatar_url', get_blog_option( BP_ROOT_BLOG, 'siteurl' ) ); 563 global $current_blog; 564 565 // Get upload directory information from current site 566 $upload_dir = wp_upload_dir(); 567 568 // If multisite, and current blog does not match root blog, make adjustments 569 if ( bp_core_is_multisite() && BP_ROOT_BLOG != $current_blog->blog_id ) 570 $upload_dir['baseurl'] = str_replace( get_blog_option( $current_blog->blog_id, 'home' ) , get_blog_option( BP_ROOT_BLOG, 'home' ), $upload_dir['baseurl'] ); 571 572 return apply_filters( 'bp_core_avatar_url', $upload_dir['baseurl'] ); 422 573 } 423 574
Note: See TracChangeset
for help on using the changeset viewer.