Ticket #21195: 21195.3.diff
File 21195.3.diff, 12.7 KB (added by , 12 years ago) |
---|
-
wp-includes/link-template.php
2435 2435 echo $before, $link, $after; 2436 2436 } 2437 2437 } 2438 2439 /** 2440 * Retrieve the avatar URL for a user, email address, MD5 hash, comment, or post. 2441 * 2442 * @uses apply_filters() 'pre_get_avatar_url' to bypass 2443 * @uses apply_filters() 'get_avatar_url' filters the result 2444 * @uses apply_filters() 'get_avatar_comment_types' filters the comment types for which we can calculate an avatar 2445 * 2446 * @since 3.6 2447 * @param mixed $id_or_email The Gravatar to retrieve: 2448 * (int) {user_id} : Use the email address of the corresponding user 2449 * (string) "{hash}@md5.gravatar.com" : Use the hash directly 2450 * (string) "{email}" 2451 * (object) {User row or WP_User object}: Use the user's email 2452 * (object) {Post row or WP_Post object}: Use the post_author's email 2453 * (object) {Comment row} : Use the comment's user_id or comment_author_email 2454 * @param array $args 2455 * size : (int) Size of the avatar image 2456 * default: (string) URL for the default image or a default type: 2457 * 404 : Return a 404 instead of a default image 2458 * retro : 8bit 2459 * monsterid : Monster 2460 * wavatar : cartoon face 2461 * identicon : the "quilt" 2462 * mystery, mm, mysteryman: The Oyster Man 2463 * blank : A transparent GIF 2464 * gravatar_default : Gravatar Logo 2465 * force_default: (bool) Always show the default image, never the Gravatar 2466 * rating : display avatars up to the given rating: G < PG < R < X. 2467 * scheme : (string) @see set_url_scheme() 2468 * &processed_args : (array) Pass as reference. When the function returns, the value will be the processed/sanitized $args plus a "found_avatar" guess. 2469 * 2470 * @return bool|string URL false on failure 2471 */ 2472 function get_avatar_url( $id_or_email, $args = null ) { 2473 $original_args = $args; 2474 2475 $args = wp_parse_args( $args, array( 2476 'size' => 96, 2477 'default' => get_option( 'avatar_default', 'mystery' ), 2478 'force_default' => false, 2479 'rating' => get_option( 'avatar_rating' ), 2480 'scheme' => null, 2481 'processed_args' => null, // if used, should be a reference 2482 ) ); 2483 2484 if ( is_numeric( $args['size'] ) ) { 2485 $args['size'] = absint( $args['size'] ); 2486 if ( !$args['size'] ) { 2487 $args['size'] = 96; 2488 } 2489 } else { 2490 $args['size'] = 96; 2491 } 2492 2493 if ( empty( $args['default'] ) ) { 2494 $args['default'] = 'mystery'; 2495 } 2496 2497 switch ( $args['default'] ) { 2498 case 'mm' : 2499 case 'mystery' : 2500 case 'mysteryman' : 2501 $args['default'] = 'mm'; 2502 break; 2503 case 'gravatar_default' : 2504 $args['default'] = false; 2505 break; 2506 } 2507 2508 $args['force_default'] = (bool) $args['force_default']; 2509 2510 $args['rating'] = strtolower( $args['rating'] ); 2511 2512 $args['found_avatar'] = false; 2513 2514 $url = apply_filters_ref_array( 'pre_get_avatar_url', array( null, $id_or_email, &$args, $original_args ) ); 2515 if ( !is_null( $url ) ) { 2516 $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) ); 2517 $args['processed_args'] = $args; 2518 unset( $args['processed_args']['processed_args'] ); 2519 return $return; 2520 } 2521 2522 $email_hash = ''; 2523 $user = $email = false; 2524 2525 if ( is_numeric( $id_or_email ) ) { 2526 $user = get_user_by( 'id', absint( $id_or_email ) ); 2527 } elseif ( is_string( $id_or_email ) ) { 2528 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 2529 // md5 hash 2530 list( $email_hash ) = explode( '@', $id_or_email ); 2531 } else { 2532 // email address 2533 $email = $id_or_email; 2534 } 2535 } elseif ( is_object( $id_or_email ) ) { 2536 if ( isset( $id_or_email->comment_ID ) ) { 2537 // Comment Object 2538 2539 // No avatar for pingbacks or trackbacks 2540 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 2541 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 2542 $args['processed_args'] = $args; 2543 unset( $args['processed_args']['processed_args'] ); 2544 return false; 2545 } 2546 2547 if ( ! empty( $id_or_email->user_id ) ) { 2548 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 2549 } 2550 if ( ( !$user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 2551 $email = $id_or_email->comment_author_email; 2552 } 2553 } elseif ( ! empty( $id_or_email->user_login ) ) { 2554 // User Object 2555 $user = $id_or_email; 2556 } elseif ( ! empty( $id_or_email->post_author ) ) { 2557 // Post Object 2558 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 2559 } 2560 } 2561 2562 if ( !$email_hash ) { 2563 if ( $user ) { 2564 $email = $user->user_email; 2565 } 2566 2567 if ( $email ) { 2568 $email_hash = md5( strtolower( trim( $email ) ) ); 2569 } 2570 } 2571 2572 if ( $email_hash ) { 2573 $args['found_avatar'] = true; 2574 } 2575 2576 $url_args = array( 2577 's' => $args['size'], 2578 'd' => $args['default'], 2579 'f' => $args['force_default'] ? 'y' : false, 2580 'r' => $args['rating'], 2581 ); 2582 2583 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', hexdec( $email_hash[0] ) % 3, $email_hash ); 2584 2585 $url = add_query_arg( 2586 rawurlencode_deep( array_filter( $url_args ) ), 2587 set_url_scheme( $url, $args['scheme'] ) 2588 ); 2589 2590 $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) ); 2591 $args['processed_args'] = $args; 2592 unset( $args['processed_args']['processed_args'] ); 2593 return $return; 2594 } -
wp-includes/pluggable.php
1572 1572 1573 1573 if ( !function_exists( 'get_avatar' ) ) : 1574 1574 /** 1575 * Retrieve the avatar for a user who provided a user ID or email address.1575 * Retrieve the avatar img tag for a user, email address, MD5 hash, comment, or post. 1576 1576 * 1577 * @uses apply_filters() 'pre_get_avatar' to bypass 1578 * @uses apply_filters() 'get_avatar' filters the result 1579 * 1577 1580 * @since 2.5 1578 * @param int|string|object $id_or_email A user ID, email address, or comment object 1579 * @param int $size Size of the avatar image 1580 * @param string $default URL to a default image to use if no avatar is available 1581 * @param string $alt Alternative text to use in image tag. Defaults to blank 1582 * @return string <img> tag for the user's avatar 1583 */ 1584 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { 1585 if ( ! get_option('show_avatars') ) 1586 return false; 1581 * @param mixed $id_or_email The Gravatar to retrieve: 1582 * (int) {user_id} : Use the email address of the corresponding user 1583 * (string) "{hash}@md5.gravatar.com" : Use the hash directly 1584 * (string) "{email}" 1585 * (object) {User row or WP_User object}: Use the user's email 1586 * (object) {Post row or WP_Post object}: Use the post_author's email 1587 * (object) {Comment row} : Use the comment's user_id or comment_author_email 1588 * @param array $args 1589 * size : (int) Size of the avatar image 1590 * default: (string) URL for the default image or a default type: 1591 * 404 : Return a 404 instead of a default image @since 3.6 1592 * retro : 8bit 1593 * monsterid : Monster 1594 * wavatar : cartoon face 1595 * identicon : the "quilt" 1596 * mystery, mm, mysteryman: The Oyster Man 1597 * blank : A transparent GIF 1598 * gravatar_default : Gravatar Logo 1599 * force_default: (bool) Always show the default image, never the Gravatar 1600 * rating : display avatars up to the given rating: G < PG < R < X. 1601 * scheme : (string) @see set_url_scheme() 1602 * alt : (string) value for the img element's alt attribute 1603 * class : (array|string) array or sttring of additional classes to add to the img element 1604 * force_display: (bool) Always show the avatar - ignore the show_avatars option 1605 * 1606 * @return bool|string <img> tag for the user's avatar. False on failure. 1607 */ 1608 // Old Parameters: $id_or_email, $size = 96, $default = '', $alt = '' ) 1609 function get_avatar( $id_or_email, $args = null ) { 1610 $defaults = array( 1611 // get_avatar_url() args 1612 'size' => 96, 1613 'default' => get_option( 'avatar_default', 'mystery' ), 1614 'force_default' => false, 1615 'rating' => get_option( 'avatar_rating' ), 1616 'scheme' => null, 1587 1617 1588 if ( false === $alt)1589 $safe_alt = '';1590 else1591 $safe_alt = esc_attr( $alt);1618 'alt' => '', 1619 'class' => null, 1620 'force_display' => false, 1621 ); 1592 1622 1593 if ( !is_numeric($size) ) 1594 $size = '96'; 1623 if ( is_scalar( $args ) ) { 1624 $args = array( 1625 'size' => $args, 1626 ); 1595 1627 1596 $email = ''; 1597 if ( is_numeric($id_or_email) ) { 1598 $id = (int) $id_or_email; 1599 $user = get_userdata($id); 1600 if ( $user ) 1601 $email = $user->user_email; 1602 } elseif ( is_object($id_or_email) ) { 1603 // No avatar for pingbacks or trackbacks 1604 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 1605 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) 1606 return false; 1628 $num_args = func_num_args(); 1629 if ( $num_args > 4 ) { 1630 $num_args = 4; 1631 } 1607 1632 1608 if ( !empty($id_or_email->user_id) ) { 1609 $id = (int) $id_or_email->user_id; 1610 $user = get_userdata($id); 1611 if ( $user) 1612 $email = $user->user_email; 1613 } elseif ( !empty($id_or_email->comment_author_email) ) { 1614 $email = $id_or_email->comment_author_email; 1633 switch ( $num_args ) { 1634 // no breaks 1635 case 4 : 1636 $args['alt'] = func_get_arg( 3 ); 1637 case 3 : 1638 $args['default'] = func_get_arg( 2 ); 1615 1639 } 1616 1640 } else { 1617 $ email = $id_or_email;1641 $args = (array) $args; 1618 1642 } 1619 1643 1620 if ( empty($default) ) { 1621 $avatar_default = get_option('avatar_default'); 1622 if ( empty($avatar_default) ) 1623 $default = 'mystery'; 1624 else 1625 $default = $avatar_default; 1626 } 1644 $original_args = $args; 1627 1645 1628 if ( !empty($email) ) 1629 $email_hash = md5( strtolower( trim( $email ) ) ); 1646 $args = wp_parse_args( $args, $defaults ); 1630 1647 1631 if ( is_ssl() ) { 1632 $host = 'https://secure.gravatar.com'; 1633 } else { 1634 if ( !empty($email) ) 1635 $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) ); 1636 else 1637 $host = 'http://0.gravatar.com'; 1648 $avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) ); 1649 if ( !is_null( $avatar ) ) { 1650 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args ); 1638 1651 } 1639 1652 1640 if ( 'mystery' == $default ) 1641 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') 1642 elseif ( 'blank' == $default ) 1643 $default = includes_url('images/blank.gif'); 1644 elseif ( !empty($email) && 'gravatar_default' == $default ) 1645 $default = ''; 1646 elseif ( 'gravatar_default' == $default ) 1647 $default = "$host/avatar/?s={$size}"; 1648 elseif ( empty($email) ) 1649 $default = "$host/avatar/?d=$default&s={$size}"; 1650 elseif ( strpos($default, 'http://') === 0 ) 1651 $default = add_query_arg( 's', $size, $default ); 1653 if ( !$args['force_display'] && !get_option( 'show_avatars' ) ) { 1654 return false; 1655 } 1652 1656 1653 if ( !empty($email) ) { 1654 $out = "$host/avatar/"; 1655 $out .= $email_hash; 1656 $out .= '?s='.$size; 1657 $out .= '&d=' . urlencode( $default ); 1657 $processed_args = null; 1658 $args['processed_args'] =& $processed_args; 1659 $url = get_avatar_url( $id_or_email, $args ); 1660 if ( !$url || is_wp_error( $url ) ) { 1661 return false; 1662 } 1658 1663 1659 $rating = get_option('avatar_rating'); 1660 if ( !empty( $rating ) ) 1661 $out .= "&r={$rating}"; 1664 $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' ); 1662 1665 1663 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; 1664 } else { 1665 $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />"; 1666 if ( !$processed_args['found_avatar'] || $processed_args['force_default'] ) { 1667 $class[] = ' avatar-default'; 1666 1668 } 1667 1669 1668 return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); 1670 if ( $args['class'] ) { 1671 if ( is_array( $args['class'] ) ) { 1672 $class = array_merge( $class, $args['class'] ); 1673 } else { 1674 $class[] = $args['class']; 1675 } 1676 } 1677 1678 $avatar = sprintf( 1679 '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />', 1680 esc_attr( $processed_args['alt'] ), 1681 esc_url( $url ), 1682 esc_attr( join( ' ', $class ) ), 1683 (int) $processed_args['size'], 1684 (int) $processed_args['size'] 1685 ); 1686 1687 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args ); 1669 1688 } 1670 1689 endif; 1671 1690