目前 WordPress 网站几乎都是使用 Gravatar 全球通头像来关联用户头像的,但是由于 Gravatar 的服务器是在国外,国内经常由于某些 XXX 原因而连接不上,今天就来教大家使用代码将 Gravatar 头像半本地化, 那么什么是半本地化呢?也就是通过用户的邮箱判断用户是否拥有 Gravatar 头像,如果拥有则使用 Gravatar 头像,当用户拥有本地头像且拥有 Gravatar 头像时,则优先使用本地头像。

  1. <?php
  2. class Simple_Local_Avatars {
  3.     private $user_id_being_edited;
  4.     public function __construct() {
  5.         add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );
  6.         add_action( 'admin_init', array( $this, 'admin_init' ) );
  7.         add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
  8.         add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
  9.         add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );
  10.         add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );
  11.         add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );
  12.     }
  13.     public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = false ) {
  14.         if ( is_numeric($id_or_email) )
  15.             $user_id = (int) $id_or_email;
  16.         elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )
  17.             $user_id = $user->ID;
  18.         elseif ( is_object( $id_or_email ) && ! emptyempty( $id_or_email->user_id ) )
  19.             $user_id = (int) $id_or_email->user_id;
  20.         if ( emptyempty( $user_id ) )
  21.             return $avatar;
  22.         $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
  23.         if ( emptyempty( $local_avatars ) || emptyempty( $local_avatars['full'] ) )
  24.             return $avatar;
  25.         $size = (int) $size;
  26.         if ( emptyempty( $alt ) )
  27.             $alt = get_the_author_meta( 'display_name', $user_id );
  28.         // generate a new size
  29.         if ( emptyempty( $local_avatars[$size] ) ) {
  30.             $upload_path = wp_upload_dir();
  31.             $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
  32.             $image_sized = image_resize( $avatar_full_path, $size, $size, true );
  33.             // deal with original being >= to original image (or lack of sizing ability)
  34.             $local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );
  35.             // save updated avatar sizes
  36.             update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
  37.         } elseif ( substr( $local_avatars[$size], 0, 4 ) != 'http' ) {
  38.             $local_avatars[$size] = home_url( $local_avatars[$size] );
  39.         }
  40.         $author_class = is_author( $user_id ) ? ' current-author' : '' ;
  41.         $avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . $local_avatars[$size] . "'  height='{$size}' width='{$size}' />";
  42.         return apply_filters( 'simple_local_avatar', $avatar );
  43.     }
  44.     public function admin_init() {
  45.         //load_plugin_textdomain( 'simple-local-avatars', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
  46.         register_setting( 'discussion', 'simple_local_avatars_caps', array( $this, 'sanitize_options' ) );
  47.         add_settings_field( 'simple-local-avatars-caps', __('Local Avatar Permissions','simple-local-avatars'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars' );
  48.     }
  49.     public function sanitize_options( $input ) {
  50.         $new_input['simple_local_avatars_caps'] = emptyempty( $input['simple_local_avatars_caps'] ) ? 0 : 1;
  51.         return $new_input;
  52.     }
  53.     public function avatar_settings_field( $args ) {
  54.         $options = get_option('simple_local_avatars_caps');
  55.         echo '
  56.             <label for="simple_local_avatars_caps">
  57.                 <input type="checkbox" name="simple_local_avatars_caps" id="simple_local_avatars_caps" value="1" ' . @checked( $options['simple_local_avatars_caps'], 1, false ) . ' />
  58.                 ' . __('仅具有头像上传权限的用户具有设置本地头像权限 (作者及更高等级角色) 。','simple-local-avatars') . '
  59.             </label>
  60.         ';
  61.     }
  62.     public function edit_user_profile( $profileuser ) {
  63.     ?>
  64.     <h3><?php _e( '头像','simple-local-avatars' ); ?></h3>
  65.     <table class="form-table">
  66.         <tr>
  67.             <th><label for="simple-local-avatar"><?php _e('上传头像','simple-local-avatars'); ?></label></th>
  68.             <td style="width: 50px;" valign="top">
  69.                 <?php echo get_avatar( $profileuser->ID ); ?>
  70.             </td>
  71.             <td>
  72.             <?php
  73.                 $options = get_option('simple_local_avatars_caps');
  74.                 if ( emptyempty($options['simple_local_avatars_caps']) || current_user_can('upload_files') ) {
  75.                     do_action( 'simple_local_avatar_notices' );
  76.                     wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
  77.             ?>
  78.                     <input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br />
  79.             <?php
  80.                     if ( emptyempty( $profileuser->simple_local_avatar ) )
  81.                         echo '<span class="description">' . __('尚未设置本地头像,请点击 「浏览」 按钮上传本地头像。','simple-local-avatars') . '</span>';
  82.                     else
  83.                         echo '
  84.                             <input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('移除本地头像','simple-local-avatars') . '<br />
  85.                             <span class="description">' . __('如需要修改本地头像,请重新上传新头像。如需要移除本地头像,请选中上方的 「移除本地头像」 复选框并更新个人资料即可。<br/> 移除本地头像后,将恢复使用 Gravatar 头像。','simple-local-avatars') . '</span>

style="">

>