备忘录之Wordpress【四七】缓存 Gravatar 头像(方法二)

折腾了两年的Wordpress,不准备再折腾了,把我所收集和使用的Wordpress代码做个总结吧!

Wordpress缓存 Gravatar 头像(方法二)

Function代码

//缓存Gravatar头像二,by www.yuweiji.com
function wecho_cache_avatar($avatar, $id_or_email, $size, $default, $alt)
{
    $avatar = str_replace(array("cn.gravatar.com", "secure.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), "www.gravatar.com", $avatar);
    $tmp = strpos($avatar, 'http');
    $url = get_avatar_url( $id_or_email, $size ) ;
    $url = str_replace(array("cn.gravatar.com", "secure.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), "www.gravatar.com", $url);
    $avatar2x = get_avatar_url( $id_or_email, ( $size * 2 ) ) ;
    $avatar2x = str_replace(array("cn.gravatar.com", "secure.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), "www.gravatar.com", $avatar2x);
    $g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
    $tmp = strpos($g, 'avatar/') + 7;
    $f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
    $w = home_url();
    $e = ABSPATH .'avatar/'. $size . '*'. $f .'.jpg';
    $e2x = ABSPATH .'avatar/'. ( $size * 2 ) . '*'. $f .'.jpg';
    $t = 1209600;
    if ( (!is_file($e) || (time() - filemtime($e)) > $t) && (!is_file($e2x) || (time() - filemtime($e2x)) > $t ) ) {
        copy(htmlspecialchars_decode($g), $e);
        copy(htmlspecialchars_decode($avatar2x), $e2x);
    } else { $avatar = $w.'/avatar/'. $size . '*'.$f.'.jpg';
        $avatar2x = $w.'/avatar/'. ( $size * 2) . '*'.$f.'.jpg';
        if (filesize($e) < 1000) copy($w.'/avatar/default.jpg', $e);
        if (filesize($e2x) < 1000) copy($w.'/avatar/default.jpg', $e2x);
        $avatar = "<img alt='{$alt}' src='{$avatar}' srcset='{$avatar2x}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    }
    return $avatar;
}
add_filter('get_avatar', 'wecho_cache_avatar',1,5);

温馨提示:如遇到资源失效,或者浏览更多资源,请访问余味记网盘在线查找,感谢您的访问!欢迎关注微信公众号“迷你日志”获取密码和最新资讯!

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注