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