Ticket #5958: 5958.3.patch
| File 5958.3.patch, 18.5 KB (added by , 12 years ago) |
|---|
-
src/bp-core/bp-core-avatars.php
diff --git src/bp-core/bp-core-avatars.php src/bp-core/bp-core-avatars.php index 7992132..bde4bb9 100644
add_action( 'bp_setup_globals', 'bp_core_set_avatar_globals' ); 174 174 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg. 175 175 */ 176 176 function bp_core_fetch_avatar( $args = '' ) { 177 $bp = buddypress(); 177 178 178 179 // If avatars are disabled for the root site, obey that request and bail 179 if ( ! buddypress()->avatar->show_avatars )180 if ( ! $bp->avatar->show_avatars ) { 180 181 return; 182 } 181 183 182 184 global $current_blog; 183 185 184 $bp = buddypress(); 185 186 // Set a few default variables 187 $def_object = 'user'; 188 $def_type = 'thumb'; 189 $def_class = 'avatar'; 190 191 // Set the default variables array 186 // Set the default variables array and parse it against incoming $args array. 192 187 $params = wp_parse_args( $args, array( 193 188 'item_id' => false, 194 'object' => $def_object,// user/group/blog/custom type (if you use filters)195 'type' => $def_type,// thumb or full189 'object' => 'user', // user/group/blog/custom type (if you use filters) 190 'type' => 'thumb', // thumb or full 196 191 'avatar_dir' => false, // Specify a custom avatar directory for your object 197 192 'width' => false, // Custom width (int) 198 193 'height' => false, // Custom height (int) 199 'class' => $def_class,// Custom <img> class (string)194 'class' => 'avatar', // Custom <img> class (string) 200 195 'css_id' => false, // Custom <img> ID (string) 201 196 'alt' => '', // Custom <img> alt (string) 202 197 'email' => false, // Pass the user email (for gravatar) to prevent querying the DB for it … … function bp_core_fetch_avatar( $args = '' ) { 204 199 'html' => true, // Wrap the return img URL in <img /> 205 200 'title' => '' // Custom <img> title (string) 206 201 ) ); 207 extract( $params, EXTR_SKIP );208 202 209 203 /** Set item_id ***********************************************************/ 210 204 211 if ( empty( $ item_id) ) {205 if ( empty( $params['item_id'] ) ) { 212 206 213 switch ( $ object) {207 switch ( $params['object'] ) { 214 208 215 209 case 'blog' : 216 $ item_id= $current_blog->id;210 $params['item_id'] = $current_blog->id; 217 211 break; 218 212 219 213 case 'group' : 220 214 if ( bp_is_active( 'groups' ) ) { 221 $ item_id= $bp->groups->current_group->id;215 $params['item_id'] = $bp->groups->current_group->id; 222 216 } else { 223 $ item_id= false;217 $params['item_id'] = false; 224 218 } 225 219 226 220 break; 227 221 228 222 case 'user' : 229 223 default : 230 $ item_id= bp_displayed_user_id();224 $params['item_id'] = bp_displayed_user_id(); 231 225 break; 232 226 } 233 227 234 $ item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object, $params );228 $params['item_id'] = apply_filters( 'bp_core_avatar_item_id', $params['item_id'], $params['object'], $params ); 235 229 236 if ( empty( $ item_id) ) {230 if ( empty( $params['item_id'] ) ) { 237 231 return false; 238 232 } 239 233 } 240 234 241 $class = apply_filters( 'bp_core_avatar_class', $class, $item_id, $object, $params );242 243 235 /** Set avatar_dir ********************************************************/ 244 236 245 if ( empty( $ avatar_dir) ) {237 if ( empty( $params['avatar_dir'] ) ) { 246 238 247 switch ( $ object) {239 switch ( $params['object'] ) { 248 240 249 241 case 'blog' : 250 $ avatar_dir= 'blog-avatars';242 $params['avatar_dir'] = 'blog-avatars'; 251 243 break; 252 244 253 245 case 'group' : 254 246 if ( bp_is_active( 'groups' ) ) { 255 $ avatar_dir= 'group-avatars';247 $params['avatar_dir'] = 'group-avatars'; 256 248 } else { 257 $ avatar_dir= false;249 $params['avatar_dir'] = false; 258 250 } 259 251 260 252 break; 261 253 262 254 case 'user' : 263 255 default : 264 $ avatar_dir= 'avatars';256 $params['avatar_dir'] = 'avatars'; 265 257 break; 266 258 } 267 259 268 $ avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object, $params );260 $params['avatar_dir'] = apply_filters( 'bp_core_avatar_dir', $params['avatar_dir'], $params['object'], $params ); 269 261 270 if ( empty( $ avatar_dir) ) {262 if ( empty( $params['avatar_dir'] ) ) { 271 263 return false; 272 264 } 273 265 } 274 266 275 267 /** <img> alt *************************************************************/ 276 268 277 if ( false !== strpos( $ alt, '%s' ) || false !== strpos( $alt, '%1$s' ) ) {269 if ( false !== strpos( $params['alt'], '%s' ) || false !== strpos( $params['alt'], '%1$s' ) ) { 278 270 279 switch ( $ object) {271 switch ( $params['object'] ) { 280 272 281 273 case 'blog' : 282 $item_name = get_blog_option( $ item_id, 'blogname' );274 $item_name = get_blog_option( $params['item_id'], 'blogname' ); 283 275 break; 284 276 285 277 case 'group' : 286 $item_name = bp_get_group_name( groups_get_group( array( 'group_id' => $ item_id) ) );278 $item_name = bp_get_group_name( groups_get_group( array( 'group_id' => $params['item_id'] ) ) ); 287 279 break; 288 280 289 281 case 'user' : 290 282 default : 291 $item_name = bp_core_get_user_displayname( $ item_id);283 $item_name = bp_core_get_user_displayname( $params['item_id'] ); 292 284 break; 293 285 } 294 286 295 $item_name = apply_filters( 'bp_core_avatar_alt', $item_name, $ item_id, $object, $params );296 $ alt = sprintf( $alt, $item_name );287 $item_name = apply_filters( 'bp_core_avatar_alt', $item_name, $params['item_id'], $params['object'], $params ); 288 $params['alt'] = sprintf( $params['alt'], $item_name ); 297 289 } 298 290 299 291 /** Sanity Checks *********************************************************/ 300 292 301 // Get a fallback for the 'alt' parameter 302 if ( empty( $alt ) ) 303 $alt = __( 'Profile Photo', 'buddypress' ); 293 // Get a fallback for the 'alt' parameter, create html output 294 if ( empty( $params['alt'] ) ) { 295 $params['alt'] = __( 'Profile Photo', 'buddypress' ); 296 } 297 $html_alt = ' alt="' . esc_attr( $params['alt'] ) . '"'; 304 298 305 $html_alt = ' alt="' . esc_attr( $alt ) . '"'; 299 // Filter image title and create html string 300 $html_title = ''; 301 $params['title'] = apply_filters( 'bp_core_avatar_title', $params['title'], $params['item_id'], $params['object'], $params ); 306 302 307 // Set title tag, if it's been provided 308 if ( !empty( $title ) ) { 309 $title = " title='" . esc_attr( apply_filters( 'bp_core_avatar_title', $title, $item_id, $object, $params ) ) . "'"; 303 if ( ! empty( $params['title'] ) ) { 304 $html_title = ' title="' . esc_attr( $params['title'] ) . '"'; 310 305 } 311 306 312 // Set CSS ID if passed 313 if ( !empty( $css_id ) ) { 314 $css_id = ' id="' . esc_attr( $css_id ) . '"'; 307 // Set CSS ID and create html string 308 $html_css_id = ''; 309 $params['css_id'] = apply_filters( 'bp_core_css_id', $params['css_id'], $params['item_id'], $params['object'], $params ); 310 311 if ( ! empty( $params['css_id'] ) ) { 312 $html_css_id = ' id="' . esc_attr( $params['css_id'] ) . '"'; 315 313 } 316 314 317 315 // Set image width 318 if ( false !== $ width) {319 $html_width = ' width="' . $width . '"';320 } else if ( 'thumb' == $type) {321 $ html_width = ' width="' . bp_core_avatar_thumb_width() . '"';316 if ( false !== $params['width'] ) { 317 // Width has been specified. No modification necessary. 318 } else if ( 'thumb' == $params['type'] ) { 319 $params['width'] = bp_core_avatar_thumb_width(); 322 320 } else { 323 $ html_width = ' width="' . bp_core_avatar_full_width() . '"';321 $params['width'] = bp_core_avatar_full_width(); 324 322 } 323 $html_width = ' width="' . $params['width'] . '"'; 325 324 326 325 // Set image height 327 if ( false !== $ height) {328 $html_height = ' height="' . $height . '"';329 } else if ( 'thumb' == $type) {330 $ html_height = ' height="' . bp_core_avatar_thumb_height() . '"';326 if ( false !== $params['height'] ) { 327 // Height has been specified. No modification necessary. 328 } else if ( 'thumb' == $params['type'] ) { 329 $params['height'] = bp_core_avatar_thumb_height(); 331 330 } else { 332 $ html_height = ' height="' . bp_core_avatar_full_height() . '"';331 $params['height'] = bp_core_avatar_full_height(); 333 332 } 333 $html_height = ' height="' . $params['height'] . '"'; 334 335 // Create CSS class html string 336 $params['class'] = apply_filters( 'bp_core_avatar_class', $params['class'], $params['item_id'], $params['object'], $params ); 337 $html_class = ' class="' . sanitize_html_class( $params['class'] ) . ' ' . sanitize_html_class( $params['object'] . '-' . $params['item_id'] . '-avatar' ) . ' ' . sanitize_html_class( 'avatar-' . $params['width'] ) . ' photo"'; 334 338 335 339 // Set img URL and DIR based on prepopulated constants 336 340 $avatar_loc = new stdClass(); 337 341 $avatar_loc->path = trailingslashit( bp_core_avatar_upload_path() ); 338 342 $avatar_loc->url = trailingslashit( bp_core_avatar_url() ); 339 343 340 $avatar_loc->dir = trailingslashit( $avatar_dir ); 341 $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', ( $avatar_loc->url . $avatar_loc->dir . $item_id ), $item_id, $object, $avatar_dir ); 342 $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', ( $avatar_loc->path . $avatar_loc->dir . $item_id ), $item_id, $object, $avatar_dir ); 343 344 // Add an identifying class 345 $class .= ' ' . $object . '-' . $item_id . '-avatar ' . sanitize_html_class( "avatar-$width" ) . ' photo'; 344 $avatar_loc->dir = trailingslashit( $params['avatar_dir'] ); 345 $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'] ); 346 $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'] ); 346 347 347 348 /** 348 349 * Look for uploaded avatar first. Use it if it exists. 349 350 * Set the file names to search for, to select the full size 350 351 * or thumbnail image. 351 352 */ 352 $avatar_size = ( 'full' == $ type) ? '-bpfull' : '-bpthumb';353 $legacy_user_avatar_name = ( 'full' == $ type) ? '-avatar2' : '-avatar1';354 $legacy_group_avatar_name = ( 'full' == $ type) ? '-groupavatar-full' : '-groupavatar-thumb';353 $avatar_size = ( 'full' == $params['type'] ) ? '-bpfull' : '-bpthumb'; 354 $legacy_user_avatar_name = ( 'full' == $params['type'] ) ? '-avatar2' : '-avatar1'; 355 $legacy_group_avatar_name = ( 'full' == $params['type'] ) ? '-groupavatar-full' : '-groupavatar-thumb'; 355 356 356 357 // Check for directory 357 358 if ( file_exists( $avatar_folder_dir ) ) { … … function bp_core_fetch_avatar( $args = '' ) { 405 406 if ( isset( $avatar_url ) ) { 406 407 407 408 // Return it wrapped in an <img> element 408 if ( true === $ html) {409 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 );409 if ( true === $params['html'] ) { 410 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '"' . $html_class . $html_css_id . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir ); 410 411 411 412 // ...or only the URL 412 413 } else { … … function bp_core_fetch_avatar( $args = '' ) { 417 418 418 419 // If no avatars could be found, try to display a gravatar 419 420 420 // Skips gravatar check if $no_grav is passed 421 if ( ! apply_filters( 'bp_core_fetch_avatar_no_grav', $no_grav ) ) { 422 423 // Set gravatar size 424 if ( false !== $width ) { 425 $grav_size = $width; 426 } else if ( 'full' == $type ) { 427 $grav_size = bp_core_avatar_full_width(); 428 } else if ( 'thumb' == $type ) { 429 $grav_size = bp_core_avatar_thumb_width(); 430 } 421 // Skips gravatar check if $params['no_grav'] is passed 422 if ( ! apply_filters( 'bp_core_fetch_avatar_no_grav', $params['no_grav'], $params ) ) { 431 423 432 424 // Set gravatar type 433 if ( empty( $bp->grav_default->{$ object} ) ) {425 if ( empty( $bp->grav_default->{$params['object']} ) ) { 434 426 $default_grav = 'wavatar'; 435 } else if ( 'mystery' == $bp->grav_default->{$ object} ) {436 $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $ grav_size);427 } else if ( 'mystery' == $bp->grav_default->{$params['object']} ) { 428 $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $params['width'] ); 437 429 } else { 438 $default_grav = $bp->grav_default->{$ object};430 $default_grav = $bp->grav_default->{$params['object']}; 439 431 } 440 432 441 433 // Set gravatar object 442 if ( empty( $ email) ) {443 if ( 'user' == $ object) {444 $ email = bp_core_get_user_email( $item_id);445 } else if ( 'group' == $ object || 'blog' == $object) {446 $ email = "{$item_id}-{$object}@{bp_get_root_domain()}";434 if ( empty( $params['email'] ) ) { 435 if ( 'user' == $params['object'] ) { 436 $params['email'] = bp_core_get_user_email( $params['item_id'] ); 437 } else if ( 'group' == $params['object'] || 'blog' == $params['object'] ) { 438 $params['email'] = $params['item_id'] . '-' . $params['object'] . '@' . bp_get_root_domain(); 447 439 } 448 440 } 449 441 … … function bp_core_fetch_avatar( $args = '' ) { 454 446 } 455 447 456 448 // Filter gravatar vars 457 $ email = apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object);458 $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $ email ) ) . '?d=' . $default_grav . '&s=' . $grav_size;449 $params['email'] = apply_filters( 'bp_core_gravatar_email', $params['email'], $params['item_id'], $params['object'] ); 450 $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $params['email'] ) ) . '?d=' . $default_grav . '&s=' . $params['width']; 459 451 460 452 // Gravatar rating; http://bit.ly/89QxZA 461 453 $rating = get_option( 'avatar_rating' ); … … function bp_core_fetch_avatar( $args = '' ) { 465 457 466 458 // No avatar was found, and we've been told not to use a gravatar. 467 459 } else { 468 $gravatar = apply_filters( "bp_core_default_avatar_$object", bp_core_avatar_default( 'local' ), $params );460 $gravatar = apply_filters( 'bp_core_default_avatar_' . $params['object'], bp_core_avatar_default( 'local' ), $params ); 469 461 } 470 462 471 if ( true === $ html) {472 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 );463 if ( true === $params['html'] ) { 464 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '"' . $html_css_id . $html_class . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir ); 473 465 } else { 474 466 return apply_filters( 'bp_core_fetch_avatar_url', $gravatar, $params ); 475 467 } -
tests/phpunit/testcases/core/avatars.php
diff --git tests/phpunit/testcases/core/avatars.php tests/phpunit/testcases/core/avatars.php index 8070b7b..316ad26 100644
6 6 class BP_Tests_Avatars extends BP_UnitTestCase { 7 7 protected $old_current_user = 0; 8 8 9 private $params = array(); 10 9 11 public function setUp() { 10 12 parent::setUp(); 11 13 … … class BP_Tests_Avatars extends BP_UnitTestCase { 110 112 public function avatar_cb() { 111 113 return 'foo'; 112 114 } 115 116 /** 117 * @group bp_core_fetch_avatar 118 */ 119 public function test_bp_core_fetch_avatar_parameter_conservation() { 120 // First, run the check with custom parameters, specifying no gravatar. 121 $this->params = array( 122 'item_id' => 1406, 123 'object' => 'custom_object', // user/group/blog/custom type (if you use filters) 124 'type' => 'full', // thumb or full 125 'avatar_dir' => 'custom-dir', // Specify a custom avatar directory for your object 126 'width' => 48, // Custom width (int) 127 'height' => 54, // Custom height (int) 128 'class' => 'custom-class', // Custom <img> class (string) 129 'css_id' => 'custom-css-id', // Custom <img> ID (string) 130 'alt' => 'custom alt', // Custom <img> alt (string) 131 'email' => 'avatar@avatar.org',// Pass the user email (for gravatar) to prevent querying the DB for it 132 'no_grav' => true, // If there is no avatar found, return false instead of a grav? 133 'html' => true, // Wrap the return img URL in <img /> 134 'title' => 'custom-title' // Custom <img> title (string) 135 ); 136 137 // Check to make sure the custom parameters survived the function all the way up to output 138 add_filter( 'bp_core_fetch_avatar', array( $this, 'bp_core_fetch_avatar_filter_check' ), 12, 2 ); 139 $avatar = bp_core_fetch_avatar( $this->params ); 140 141 // Re-run check, allowing gravatars. 142 $this->params['no_grav'] = false; 143 $avatar = bp_core_fetch_avatar( $this->params ); 144 145 remove_filter( 'bp_core_fetch_avatar', array( $this, 'bp_core_fetch_avatar_filter_check' ), 12, 2 ); 146 147 unset( $this->params ); 148 } 149 150 public function bp_core_fetch_avatar_filter_check( $html, $params ) { 151 // Check that the passed parameters match the original custom parameters. 152 $this->assertEmpty( array_merge( array_diff( $params, $this->params ), array_diff( $this->params, $params ) ) ); 153 154 // Check the returned html to see that it matches an expected value. 155 // Get the correct default avatar, based on whether gravatars are allowed. 156 if ( $params['no_grav'] ) { 157 $avatar_url = bp_core_avatar_default( 'local' ); 158 } else { 159 // This test has the slight odor of hokum since it recreates so much code that could be changed at any time. 160 $bp = buddypress(); 161 // Set host based on if using ssl 162 $host = 'http://gravatar.com/avatar/'; 163 if ( is_ssl() ) { 164 $host = 'https://secure.gravatar.com/avatar/'; 165 } 166 // Set expected gravatar type 167 if ( empty( $bp->grav_default->{$this->params['object']} ) ) { 168 $default_grav = 'wavatar'; 169 } else if ( 'mystery' == $bp->grav_default->{$this->params['object']} ) { 170 $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $this->params['width'] ); 171 } else { 172 $default_grav = $bp->grav_default->{$this->params['object']}; 173 } 174 175 $avatar_url = $host . md5( strtolower( $this->params['email'] ) ) . '?d=' . $default_grav . '&s=' . $this->params['width']; 176 177 // Gravatar rating; http://bit.ly/89QxZA 178 $rating = get_option( 'avatar_rating' ); 179 if ( ! empty( $rating ) ) { 180 $avatar_url .= "&r={$rating}"; 181 } 182 } 183 184 $expected_html = '<img src="' . $avatar_url . '" id="' . $this->params['css_id'] . '" class="' . $this->params['class'] . ' ' . $this->params['object'] . '-' . $this->params['item_id'] . '-avatar avatar-' . $this->params['width'] . ' photo" width="' . $this->params['width'] . '" height="' . $this->params['height'] . '" alt="' . $this->params['alt'] . '" title="' . $this->params['title'] . '" />'; 185 186 $this->assertEquals( $html, $expected_html ); 187 } 113 188 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)