Changeset 2077 for trunk/bp-core/bp-core-avatars.php
- Timestamp:
- 11/02/2009 07:54:21 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/bp-core/bp-core-avatars.php (modified) (26 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-core/bp-core-avatars.php
r2069 r2077 32 32 if ( !defined( 'BP_AVATAR_DEFAULT_THUMB' ) ) 33 33 define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . '/bp-xprofile/images/none-thumbnail.gif' ); 34 34 35 35 function bp_core_fetch_avatar( $args = '' ) { 36 36 global $bp, $current_blog; 37 37 38 38 $defaults = array( 39 39 'item_id' => false, … … 41 41 'type' => 'thumb', 42 42 'avatar_dir' => false, 43 'width' => false, 43 'width' => false, 44 44 'height' => false, 45 45 'class' => 'avatar', … … 50 50 51 51 $params = wp_parse_args( $args, $defaults ); 52 extract( $params, EXTR_SKIP ); 52 extract( $params, EXTR_SKIP ); 53 53 54 54 if ( !$item_id ) { … … 59 59 else if ( 'blog' == $object ) 60 60 $item_id = $current_blog->id; 61 61 62 62 $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object ); 63 63 64 64 if ( !$item_id ) return false; 65 65 } 66 66 67 67 if ( !$avatar_dir ) { 68 68 if ( 'user' == $object ) … … 72 72 else if ( 'blog' == $object ) 73 73 $avatar_dir = 'blog-avatars'; 74 74 75 75 $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object ); 76 77 if ( !$avatar_dir ) return false; 78 } 79 76 77 if ( !$avatar_dir ) return false; 78 } 79 80 80 if ( !$css_id ) 81 $css_id = $object . '-' . $item_id . '-avatar'; 82 81 $css_id = $object . '-' . $item_id . '-avatar'; 82 83 83 if ( $width ) 84 84 $html_width = " width='{$width}'"; 85 85 else 86 86 $html_width = ( 'thumb' == $type ) ? ' width="' . BP_AVATAR_THUMB_WIDTH . '"' : ' width="' . BP_AVATAR_FULL_WIDTH . '"'; 87 87 88 88 if ( $height ) 89 89 $html_height = " height='{$height}'"; 90 90 else 91 91 $html_height = ( 'thumb' == $type ) ? ' height="' . BP_AVATAR_THUMB_HEIGHT . '"' : ' height="' . BP_AVATAR_FULL_HEIGHT . '"'; 92 93 $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', get_blog_option( BP_ROOT_BLOG, 'siteurl' ) . '/' . basename( WP_CONTENT_DIR ) . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 94 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 92 93 $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', get_blog_option( BP_ROOT_BLOG, 'siteurl' ) . '/' . basename( WP_CONTENT_DIR ) . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 94 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 95 95 96 96 /**** … … 116 116 } 117 117 118 /* If no avatars have been uploaded for this item, display a gravatar */ 118 /* If no avatars have been uploaded for this item, display a gravatar */ 119 119 if ( !file_exists( $avatar_url ) && !$no_grav ) { 120 121 120 if ( empty( $bp->grav_default->{$object} ) ) 122 121 $default_grav = 'wavatar'; … … 129 128 else if ( 'full' == $type ) $grav_size = BP_AVATAR_FULL_WIDTH; 130 129 else if ( 'thumb' == $type ) $grav_size = BP_AVATAR_THUMB_WIDTH; 131 130 132 131 if ( 'user' == $object ) { 133 132 $ud = get_userdata( $item_id ); … … 136 135 $grav_email = "{$item_id}-{$object}@{$bp->root_domain}"; 137 136 } 138 139 $grav_email = apply_filters( 'bp_core_gravatar_email', $grav_email, $item_id, $object ); 137 138 $grav_email = apply_filters( 'bp_core_gravatar_email', $grav_email, $item_id, $object ); 140 139 $gravatar = apply_filters( 'bp_gravatar_url', 'http://www.gravatar.com/avatar/' ) . md5( $grav_email ) . '?d=' . $default_grav . '&s=' . $grav_size; 141 140 142 141 return apply_filters( 'bp_core_fetch_avatar', "<img src='{$gravatar}' alt='{$alt}' id='{$css_id}' class='{$class}'{$html_width}{$html_height} />", $params ); 143 144 142 } else if ( !file_exists( $avatar_url ) && $no_grav ) 145 143 return false; 146 147 144 } 148 145 149 146 function bp_core_delete_existing_avatar( $args = '' ) { 150 147 global $bp; 151 148 152 149 $defaults = array( 153 150 'item_id' => false, … … 157 154 158 155 $args = wp_parse_args( $args, $defaults ); 159 extract( $args, EXTR_SKIP ); 160 156 extract( $args, EXTR_SKIP ); 157 161 158 if ( !$item_id ) { 162 159 if ( 'user' == $object ) … … 166 163 else if ( 'blog' == $object ) 167 164 $item_id = $current_blog->id; 168 165 169 166 $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object ); 170 167 171 168 if ( !$item_id ) return false; 172 169 } 173 170 174 171 if ( !$avatar_dir ) { 175 172 if ( 'user' == $object ) … … 179 176 else if ( 'blog' == $object ) 180 177 $avatar_dir = 'blog-avatars'; 181 178 182 179 $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object ); 183 184 if ( !$avatar_dir ) return false; 180 181 if ( !$avatar_dir ) return false; 185 182 } 186 183 … … 193 190 } 194 191 195 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 192 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); 196 193 197 194 if ( !file_exists( $avatar_folder_dir ) ) … … 201 198 while ( false !== ( $avatar_file = readdir($av_dir) ) ) { 202 199 if ( ( preg_match( "/-bpfull/", $avatar_file ) || preg_match( "/-bpthumb/", $avatar_file ) ) && '.' != $avatar_file && '..' != $avatar_file ) 203 @unlink( $avatar_folder_dir . '/' . $avatar_file ); 200 @unlink( $avatar_folder_dir . '/' . $avatar_file ); 204 201 } 205 202 } … … 215 212 function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) { 216 213 global $bp; 217 214 218 215 require_once( ABSPATH . '/wp-admin/includes/image.php' ); 219 216 require_once( ABSPATH . '/wp-admin/includes/file.php' ); 220 217 221 218 $uploadErrors = array( 222 0 => __("There is no error, the file uploaded with success", 'buddypress'), 223 1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(BP_AVATAR_ORIGINAL_MAX_FILESIZE), 219 0 => __("There is no error, the file uploaded with success", 'buddypress'), 220 1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(BP_AVATAR_ORIGINAL_MAX_FILESIZE), 224 221 2 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(BP_AVATAR_ORIGINAL_MAX_FILESIZE), 225 222 3 => __("The uploaded file was only partially uploaded", 'buddypress'), … … 232 229 return false; 233 230 } 234 231 235 232 if ( !bp_core_check_avatar_size( $file ) ) { 236 233 bp_core_add_message( sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format(BP_AVATAR_ORIGINAL_MAX_FILESIZE) ), 'error' ); 237 234 return false; 238 235 } 239 236 240 237 if ( !bp_core_check_avatar_type( $file ) ) { 241 238 bp_core_add_message( __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ), 'error' ); 242 239 return false; 243 240 } 244 241 245 242 // Filter the upload location 246 243 add_filter( 'upload_dir', $upload_dir_filter, 10, 0 ); 247 244 248 245 $bp->avatar_admin->original = wp_handle_upload( $file['file'], array( 'action'=> 'bp_avatar_upload' ) ); 249 246 … … 253 250 return false; 254 251 } 255 252 256 253 // Resize the image down to something manageable and then delete the original 257 254 if ( getimagesize( $bp->avatar_admin->original['file'] ) > BP_AVATAR_ORIGINAL_MAX_WIDTH ) { 258 255 $bp->avatar_admin->resized = wp_create_thumbnail( $bp->avatar_admin->original['file'], BP_AVATAR_ORIGINAL_MAX_WIDTH ); 259 256 } 260 257 261 258 $bp->avatar_admin->image = new stdClass; 262 263 // We only want to handle one image after resize. 259 260 // We only want to handle one image after resize. 264 261 if ( empty( $bp->avatar_admin->resized ) ) 265 262 $bp->avatar_admin->image->dir = $bp->avatar_admin->original['file']; … … 268 265 @unlink( $bp->avatar_admin->original['file'] ); 269 266 } 270 267 271 268 /* Set the url value for the image */ 272 269 $bp->avatar_admin->image->url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $bp->avatar_admin->image->dir ); … … 277 274 function bp_core_avatar_handle_crop( $args = '' ) { 278 275 global $bp; 279 276 280 277 $defaults = array( 281 278 'object' => 'user', … … 297 294 if ( !file_exists( WP_CONTENT_DIR . '/' . $original_file ) ) 298 295 return false; 299 296 300 297 if ( !$item_id ) 301 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . dirname( $original_file ), $item_id, $object, $avatar_dir ); 298 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . dirname( $original_file ), $item_id, $object, $avatar_dir ); 302 299 else 303 300 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir ); … … 305 302 if ( !file_exists( $avatar_folder_dir ) ) 306 303 return false; 307 304 308 305 require_once( ABSPATH . '/wp-admin/includes/image.php' ); 309 306 require_once( ABSPATH . '/wp-admin/includes/file.php' ); … … 311 308 /* Delete the existing avatar files for the object */ 312 309 bp_core_delete_existing_avatar( array( 'object' => $object, 'avatar_path' => $avatar_folder_dir ) ); 313 310 314 311 /* Make sure we at least have a width and height for cropping */ 315 312 if ( !(int)$crop_w ) 316 313 $crop_w = BP_AVATAR_FULL_WIDTH; 317 314 318 315 if ( !(int)$crop_h ) 319 316 $crop_h = BP_AVATAR_FULL_HEIGHT; … … 322 319 $full_filename = wp_hash( $original_file . time() ) . '-bpfull.jpg'; 323 320 $thumb_filename = wp_hash( $original_file . time() ) . '-bpthumb.jpg'; 324 321 325 322 /* Crop the image */ 326 323 $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 ); … … 337 334 if ( is_object ( $id_or_email ) ) 338 335 $id_or_email = $id_or_email->user_id; 339 336 340 337 $bp_avatar = bp_core_fetch_avatar( array( 'no_grav' => true, 'item_id' => $id_or_email, 'width' => $size, 'height' => $size, 'alt' => $alt ) ); 341 338 … … 347 344 if ( $file['error'] ) 348 345 return false; 349 346 350 347 return true; 351 348 } … … 354 351 if ( $file['file']['size'] > BP_AVATAR_ORIGINAL_MAX_FILESIZE ) 355 352 return false; 356 353 357 354 return true; 358 355 } … … 361 358 if ( ( strlen($file['file']['type']) && !preg_match('/(jpe?g|gif|png)$/', $file['file']['type'] ) ) && !preg_match( '/(jpe?g|gif|png)$/', $file['file']['name'] ) ) 362 359 return false; 363 360 364 361 return true; 365 362 }
Note: See TracChangeset
for help on using the changeset viewer.