<?php
/*
Plugin Name: Gravatar Mirror
Plugin URI: https://wujie.me/replace-the-gravatar-mirror/
Description: Gravatar 国内镜像
Since: 2021.04.09
Version: 2024.05.27
Author: 吴杰
Author URI: https://wujie.me
License: GPL

*/

// 用 gravatar 国内能访问的镜像地址替换官方 gravatar

if ( ! function_exists( 'get_mirror_avatar' ) ) {
    function get_mirror_avatar( $avatar ) {
        // 新 Gravatar 头像源，可自行修改

        $new_gravatar_sever = 'gravatar.loli.net/avatar/';
        //$new_gravatar_sever = 'sdn.geekzu.org/avatar/';  
        //$new_gravatar_sever = 'gravatar.zunhuyun.com/avatar/';  
        //$new_gravatar_sever = 'gravatar.kuibu.net/avatar/';  

        // 如果实在不行，就换cravatar.cn吧 文档：https://cravatar.com/developer/for-wordpress#more-53

        $sources = array(
            'www.gravatar.com/avatar/',
            '0.gravatar.com/avatar/',
            '1.gravatar.com/avatar/',
            '2.gravatar.com/avatar/',
            'secure.gravatar.com/avatar/',
            'cn.gravatar.com/avatar/',
            'gravatar.com/avatar/',
        );
        return str_replace( $sources, $new_gravatar_sever, $avatar );
    }
    add_filter( 'get_avatar', 'get_mirror_avatar' );
}
