Make WordPress Core

Ticket #21195: 21195.5.diff

File 21195.5.diff, 13.1 KB (added by pento, 11 years ago)
  • src/wp-includes/link-template.php

     
    25312531                echo $before, $link, $after;
    25322532        }
    25332533}
     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 */
     2571function 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                $gravatar_server = hexdec( $email_hash[0] ) % 3;
     2674        } else {
     2675                $gravatar_server = rand( 0, 2 );
     2676        }
     2677
     2678        $url_args = array(
     2679                's' => $args['size'],
     2680                'd' => $args['default'],
     2681                'f' => $args['force_default'] ? 'y' : false,
     2682                'r' => $args['rating'],
     2683        );
     2684
     2685        $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
     2686
     2687        $url = add_query_arg(
     2688                rawurlencode_deep( array_filter( $url_args ) ),
     2689                set_url_scheme( $url, $args['scheme'] )
     2690        );
     2691
     2692        $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) );
     2693        $args['processed_args'] = $args;
     2694        unset( $args['processed_args']['processed_args'] );
     2695        return $return;
     2696}
  • src/wp-includes/pluggable.php

     
    16121612
    16131613if ( !function_exists( 'get_avatar' ) ) :
    16141614/**
    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
    16161619 *
    16171620 * @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 int    $size        Size of the avatar image
     1630 * @param string $default     URL for the default image or a default type {
     1631 *     404                    : Return a 404 instead of a default image
     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 * }
     1640 * @param string $alt         Alternative text to use in image tag. Defaults to blank
     1641 * @param array  $args        Extra options to apply to the avatar {
     1642 *     @type bool   $force_default Always show the default image, never the Gravatar
     1643 *     @type string $rating        display avatars up to the given rating: G < PG < R < X.
     1644 *     @type string $scheme        @see set_url_scheme()
     1645 *     @type mixed  $class         array or string of additional classes to add to the img element
     1646 *     @type bool   $force_display Always show the avatar - ignore the show_avatars option
     1647 * }
     1648 *
     1649 * @return bool|string <img> tag for the user's avatar.  False on failure.
     1650 */
     1651function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) {
     1652        $defaults = array(
     1653                // get_avatar_url() args
     1654                'size'          => 96,
     1655                'default'       => get_option( 'avatar_default', 'mystery' ),
     1656                'force_default' => false,
     1657                'rating'        => get_option( 'avatar_rating' ),
     1658                'scheme'        => null,
     1659
     1660                'alt'           => '',
     1661                'class'         => null,
     1662                'force_display' => false,
     1663        );
     1664
     1665        if ( empty( $args ) )
     1666                $args = array();
     1667
     1668        $args['size']    = $size;
     1669        $args['default'] = $default;
     1670        $args['alt']     = $alt;
    16271671
    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;
    1655                 }
    1656         } else {
    1657                 $email = $id_or_email;
     1672        $original_args = $args;
     1673
     1674        $args = wp_parse_args( $args, $defaults );
     1675
     1676        $avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) );
     1677        if ( !is_null( $avatar ) ) {
     1678                return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args );
    16581679        }
    16591680
    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;
     1681        if ( !$args['force_display'] && !get_option( 'show_avatars' ) ) {
     1682                return false;
    16661683        }
    16671684
    1668         if ( !empty($email) )
    1669                 $email_hash = md5( strtolower( trim( $email ) ) );
     1685        $processed_args = null;
     1686        $args['processed_args'] =& $processed_args;
     1687        $url = get_avatar_url( $id_or_email, $args );
     1688        if ( !$url || is_wp_error( $url ) ) {
     1689                return false;
     1690        }
    16701691
    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&amp;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 .= '&amp;d=' . urlencode( $default );
    1698 
    1699                 $rating = get_option('avatar_rating');
    1700                 if ( !empty( $rating ) )
    1701                         $out .= "&amp;r={$rating}";
     1692        $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' );
    17021693
    1703                 $out = str_replace( '&#038;', '&amp;', 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}' />";
     1694        if ( !$processed_args['found_avatar'] || $processed_args['force_default'] ) {
     1695                $class[] = ' avatar-default';
    17071696        }
    17081697
    1709         return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
     1698        if ( $args['class'] ) {
     1699                if ( is_array( $args['class'] ) ) {
     1700                        $class = array_merge( $class, $args['class'] );
     1701                } else {
     1702                        $class[] = $args['class'];
     1703                }
     1704        }
     1705
     1706        $avatar = sprintf(
     1707                '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />',
     1708                esc_attr( $processed_args['alt'] ),
     1709                esc_url( $url ),
     1710                esc_attr( join( ' ', $class ) ),
     1711                (int) $processed_args['size'],
     1712                (int) $processed_args['size']
     1713        );
     1714
     1715        return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args );
    17101716}
    17111717endif;
    17121718