Ticket #21195: 21195.2.diff
File 21195.2.diff, 13.1 KB (added by , 11 years ago) |
---|
-
src/wp-includes/link-template.php
2531 2531 echo $before, $link, $after; 2532 2532 } 2533 2533 } 2534 2535 /** 2536 * Retrieve the avatar URL for a user, email address, MD5 hash, comment, or post. 2537 * 2538 * @uses apply_filters() 'pre_get_avatar_url' to bypass 2539 * @uses apply_filters() 'get_avatar_url' filters the result 2540 * @uses apply_filters() 'get_avatar_comment_types' filters the comment types for which we can calculate an avatar 2541 * 2542 * @since 3.8.0 2543 * @param mixed $id_or_email The Gravatar to retrieve { 2544 * @type int {user_id} : Use the email address of the corresponding user 2545 * @type string "{hash}@md5.gravatar.com" : Use the hash directly 2546 * @type string "{email}" 2547 * @type object {User row or WP_User object}: Use the user's email 2548 * @type object {Post row or WP_Post object}: Use the post_author's email 2549 * @type object {Comment row} : Use the comment's user_id or comment_author_email 2550 * } 2551 * @param array $args Extra options to apply to the avatar { 2552 * @type int $size Size of the avatar image 2553 * @type string $default URL for the default image or a default type { 2554 * 404 : Return a 404 instead of a default image 2555 * retro : 8bit 2556 * monsterid : Monster 2557 * wavatar : cartoon face 2558 * identicon : the "quilt" 2559 * mystery, mm, mysteryman: The Oyster Man 2560 * blank : A transparent GIF 2561 * gravatar_default : Gravatar Logo 2562 * } 2563 * @type bool $force_default Always show the default image, never the Gravatar 2564 * @type string $rating display avatars up to the given rating: G < PG < R < X. 2565 * @type string $scheme @see set_url_scheme() 2566 * @type array $processed_args Pass as reference. When the function returns, the value will be the processed/sanitized $args plus a "found_avatar" guess. 2567 * } 2568 * 2569 * @return bool|string URL false on failure 2570 */ 2571 function get_avatar_url( $id_or_email, $args = null ) { 2572 $original_args = $args; 2573 2574 $args = wp_parse_args( $args, array( 2575 'size' => 96, 2576 'default' => get_option( 'avatar_default', 'mystery' ), 2577 'force_default' => false, 2578 'rating' => get_option( 'avatar_rating' ), 2579 'scheme' => null, 2580 'processed_args' => null, // if used, should be a reference 2581 ) ); 2582 2583 if ( is_numeric( $args['size'] ) ) { 2584 $args['size'] = absint( $args['size'] ); 2585 if ( !$args['size'] ) { 2586 $args['size'] = 96; 2587 } 2588 } else { 2589 $args['size'] = 96; 2590 } 2591 2592 if ( empty( $args['default'] ) ) { 2593 $args['default'] = 'mystery'; 2594 } 2595 2596 switch ( $args['default'] ) { 2597 case 'mm' : 2598 case 'mystery' : 2599 case 'mysteryman' : 2600 $args['default'] = 'mm'; 2601 break; 2602 case 'gravatar_default' : 2603 $args['default'] = false; 2604 break; 2605 } 2606 2607 $args['force_default'] = (bool) $args['force_default']; 2608 2609 $args['rating'] = strtolower( $args['rating'] ); 2610 2611 $args['found_avatar'] = false; 2612 2613 $url = apply_filters_ref_array( 'pre_get_avatar_url', array( null, $id_or_email, &$args, $original_args ) ); 2614 if ( !is_null( $url ) ) { 2615 $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) ); 2616 $args['processed_args'] = $args; 2617 unset( $args['processed_args']['processed_args'] ); 2618 return $return; 2619 } 2620 2621 $email_hash = ''; 2622 $user = $email = false; 2623 2624 if ( is_numeric( $id_or_email ) ) { 2625 $user = get_user_by( 'id', absint( $id_or_email ) ); 2626 } elseif ( is_string( $id_or_email ) ) { 2627 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 2628 // md5 hash 2629 list( $email_hash ) = explode( '@', $id_or_email ); 2630 } else { 2631 // email address 2632 $email = $id_or_email; 2633 } 2634 } elseif ( is_object( $id_or_email ) ) { 2635 if ( isset( $id_or_email->comment_ID ) ) { 2636 // Comment Object 2637 2638 // No avatar for pingbacks or trackbacks 2639 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 2640 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 2641 $args['processed_args'] = $args; 2642 unset( $args['processed_args']['processed_args'] ); 2643 return false; 2644 } 2645 2646 if ( ! empty( $id_or_email->user_id ) ) { 2647 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 2648 } 2649 if ( ( !$user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 2650 $email = $id_or_email->comment_author_email; 2651 } 2652 } elseif ( ! empty( $id_or_email->user_login ) ) { 2653 // User Object 2654 $user = $id_or_email; 2655 } elseif ( ! empty( $id_or_email->post_author ) ) { 2656 // Post Object 2657 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 2658 } 2659 } 2660 2661 if ( !$email_hash ) { 2662 if ( $user ) { 2663 $email = $user->user_email; 2664 } 2665 2666 if ( $email ) { 2667 $email_hash = md5( strtolower( trim( $email ) ) ); 2668 } 2669 } 2670 2671 if ( $email_hash ) { 2672 $args['found_avatar'] = true; 2673 } 2674 2675 $url_args = array( 2676 's' => $args['size'], 2677 'd' => $args['default'], 2678 'f' => $args['force_default'] ? 'y' : false, 2679 'r' => $args['rating'], 2680 ); 2681 2682 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', hexdec( $email_hash[0] ) % 3, $email_hash ); 2683 2684 $url = add_query_arg( 2685 rawurlencode_deep( array_filter( $url_args ) ), 2686 set_url_scheme( $url, $args['scheme'] ) 2687 ); 2688 2689 $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) ); 2690 $args['processed_args'] = $args; 2691 unset( $args['processed_args']['processed_args'] ); 2692 return $return; 2693 } -
src/wp-includes/pluggable.php
1612 1612 1613 1613 if ( !function_exists( 'get_avatar' ) ) : 1614 1614 /** 1615 * Retrieve the avatar for a user who provided a user ID or email address. 1615 * Retrieve the avatar img tag for a user, email address, MD5 hash, comment, or post. 1616 * 1617 * @uses apply_filters() 'pre_get_avatar' to bypass 1618 * @uses apply_filters() 'get_avatar' filters the result 1616 1619 * 1617 1620 * @since 2.5 1618 * @param int|string|object $id_or_email A user ID, email address, or comment object 1619 * @param int $size Size of the avatar image 1620 * @param string $default URL to a default image to use if no avatar is available 1621 * @param string $alt Alternative text to use in image tag. Defaults to blank 1622 * @return string <img> tag for the user's avatar 1623 */ 1624 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { 1625 if ( ! get_option('show_avatars') ) 1626 return false; 1621 * @param mixed $id_or_email The Gravatar to retrieve { 1622 * @type int {user_id} : Use the email address of the corresponding user 1623 * @type string "{hash}@md5.gravatar.com" : Use the hash directly 1624 * @type string "{email}" 1625 * @type object {User row or WP_User object}: Use the user's email 1626 * @type object {Post row or WP_Post object}: Use the post_author's email 1627 * @type object {Comment row} : Use the comment's user_id or comment_author_email 1628 * } 1629 * @param array $args Extra options to apply to the avatar { 1630 * @type int $size Size of the avatar image 1631 * @type string $default URL for the default image or a default type { 1632 * 404 : Return a 404 instead of a default image 1633 * retro : 8bit 1634 * monsterid : Monster 1635 * wavatar : cartoon face 1636 * identicon : the "quilt" 1637 * mystery, mm, mysteryman: The Oyster Man 1638 * blank : A transparent GIF 1639 * gravatar_default : Gravatar Logo 1640 * } 1641 * @type bool $force_default Always show the default image, never the Gravatar 1642 * @type string $rating display avatars up to the given rating: G < PG < R < X. 1643 * @type string $scheme @see set_url_scheme() 1644 * @type array $processed_args Pass as reference. When the function returns, the value will be the processed/sanitized $args plus a "found_avatar" guess. 1645 * } 1646 * 1647 * @return bool|string <img> tag for the user's avatar. False on failure. 1648 */ 1649 // Old Parameters: $id_or_email, $size = 96, $default = '', $alt = '' ) 1650 function get_avatar( $id_or_email, $args = null ) { 1651 $defaults = array( 1652 // get_avatar_url() args 1653 'size' => 96, 1654 'default' => get_option( 'avatar_default', 'mystery' ), 1655 'force_default' => false, 1656 'rating' => get_option( 'avatar_rating' ), 1657 'scheme' => null, 1658 1659 'alt' => '', 1660 'class' => null, 1661 'force_display' => false, 1662 ); 1663 1664 if ( is_scalar( $args ) ) { 1665 $args = array( 1666 'size' => $args, 1667 ); 1668 1669 $num_args = func_num_args(); 1670 if ( $num_args > 4 ) { 1671 $num_args = 4; 1672 } 1627 1673 1628 if ( false === $alt) 1629 $safe_alt = ''; 1630 else 1631 $safe_alt = esc_attr( $alt ); 1632 1633 if ( !is_numeric($size) ) 1634 $size = '96'; 1635 1636 $email = ''; 1637 if ( is_numeric($id_or_email) ) { 1638 $id = (int) $id_or_email; 1639 $user = get_userdata($id); 1640 if ( $user ) 1641 $email = $user->user_email; 1642 } elseif ( is_object($id_or_email) ) { 1643 // No avatar for pingbacks or trackbacks 1644 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 1645 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) 1646 return false; 1647 1648 if ( !empty($id_or_email->user_id) ) { 1649 $id = (int) $id_or_email->user_id; 1650 $user = get_userdata($id); 1651 if ( $user) 1652 $email = $user->user_email; 1653 } elseif ( !empty($id_or_email->comment_author_email) ) { 1654 $email = $id_or_email->comment_author_email; 1674 switch ( $num_args ) { 1675 // no breaks 1676 case 4 : 1677 $args['alt'] = func_get_arg( 3 ); 1678 case 3 : 1679 $args['default'] = func_get_arg( 2 ); 1655 1680 } 1656 1681 } else { 1657 $ email = $id_or_email;1682 $args = (array) $args; 1658 1683 } 1659 1684 1660 if ( empty($default) ) { 1661 $avatar_default = get_option('avatar_default'); 1662 if ( empty($avatar_default) ) 1663 $default = 'mystery'; 1664 else 1665 $default = $avatar_default; 1685 $original_args = $args; 1686 1687 $args = wp_parse_args( $args, $defaults ); 1688 1689 $avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) ); 1690 if ( !is_null( $avatar ) ) { 1691 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args ); 1666 1692 } 1667 1693 1668 if ( !empty($email) ) 1669 $email_hash = md5( strtolower( trim( $email ) ) ); 1694 if ( !$args['force_display'] && !get_option( 'show_avatars' ) ) { 1695 return false; 1696 } 1670 1697 1671 if ( is_ssl() ) { 1672 $host = 'https://secure.gravatar.com'; 1673 } else { 1674 if ( !empty($email) ) 1675 $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) ); 1676 else 1677 $host = 'http://0.gravatar.com'; 1678 } 1679 1680 if ( 'mystery' == $default ) 1681 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') 1682 elseif ( 'blank' == $default ) 1683 $default = $email ? 'blank' : includes_url( 'images/blank.gif' ); 1684 elseif ( !empty($email) && 'gravatar_default' == $default ) 1685 $default = ''; 1686 elseif ( 'gravatar_default' == $default ) 1687 $default = "$host/avatar/?s={$size}"; 1688 elseif ( empty($email) ) 1689 $default = "$host/avatar/?d=$default&s={$size}"; 1690 elseif ( strpos($default, 'http://') === 0 ) 1691 $default = add_query_arg( 's', $size, $default ); 1692 1693 if ( !empty($email) ) { 1694 $out = "$host/avatar/"; 1695 $out .= $email_hash; 1696 $out .= '?s='.$size; 1697 $out .= '&d=' . urlencode( $default ); 1698 1699 $rating = get_option('avatar_rating'); 1700 if ( !empty( $rating ) ) 1701 $out .= "&r={$rating}"; 1698 $processed_args = null; 1699 $args['processed_args'] =& $processed_args; 1700 $url = get_avatar_url( $id_or_email, $args ); 1701 if ( !$url || is_wp_error( $url ) ) { 1702 return false; 1703 } 1702 1704 1703 $out = str_replace( '&', '&', esc_url( $out ));1704 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; 1705 } else{1706 $ avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";1705 $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' ); 1706 1707 if ( !$processed_args['found_avatar'] || $processed_args['force_default'] ) { 1708 $class[] = ' avatar-default'; 1707 1709 } 1708 1710 1709 return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); 1711 if ( $args['class'] ) { 1712 if ( is_array( $args['class'] ) ) { 1713 $class = array_merge( $class, $args['class'] ); 1714 } else { 1715 $class[] = $args['class']; 1716 } 1717 } 1718 1719 $avatar = sprintf( 1720 '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />', 1721 esc_attr( $processed_args['alt'] ), 1722 esc_url( $url ), 1723 esc_attr( join( ' ', $class ) ), 1724 (int) $processed_args['size'], 1725 (int) $processed_args['size'] 1726 ); 1727 1728 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args ); 1710 1729 } 1711 1730 endif; 1712 1731