Advertisement
Guest User

get_avatar() - Simple Local Avatars

a guest
Apr 17th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1.     public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
  2.         if ( is_numeric( $id_or_email ) )
  3.             $user_id = (int) $id_or_email;
  4.         elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )
  5.             $user_id = $user->ID;
  6.         elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) )
  7.             $user_id = (int) $id_or_email->user_id;
  8.        
  9.         if ( empty( $user_id ) )
  10.             return $avatar;
  11.  
  12.         // fetch local avatar from meta and make sure it's properly ste
  13.         $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
  14.         if ( empty( $local_avatars['full'] ) )
  15.             return $avatar;
  16.  
  17.         // check rating
  18.         $avatar_rating = get_user_meta( $user_id, 'simple_local_avatar_rating', true );
  19.         if ( ! empty( $avatar_rating ) && 'G' != $avatar_rating && ( $site_rating = get_option( 'avatar_rating' ) ) ) {
  20.             $ratings = array_keys( $this->avatar_ratings );
  21.             $site_rating_weight = array_search( $site_rating, $ratings );
  22.             $avatar_rating_weight = array_search( $avatar_rating, $ratings );
  23.             if ( false !== $avatar_rating_weight && $avatar_rating_weight > $site_rating_weight )
  24.                 return $avatar;
  25.         }
  26.  
  27.         // handle "real" media
  28.         if ( ! empty( $local_avatars['media_id'] ) ) {
  29.             // has the media been deleted?
  30.             if ( ! $avatar_full_path = get_attached_file( $local_avatars['media_id'] ) ) {
  31.                 // only allowed logged in users to delete bad data to mitigate performance issues
  32.                 if ( is_user_logged_in() )
  33.                     $this->avatar_delete( $user_id );
  34.  
  35.                 return $avatar;
  36.             }
  37.         }
  38.  
  39.         $size = (int) $size;
  40.            
  41.         if ( empty( $alt ) )
  42.             $alt = get_the_author_meta( 'display_name', $user_id );
  43.            
  44.         // generate a new size
  45.         if ( ! array_key_exists( $size, $local_avatars ) ) {
  46.             $local_avatars[$size] = $local_avatars['full']; // just in case of failure elsewhere
  47.  
  48.             // allow automatic rescaling to be turned off
  49.             if ( $allow_dynamic_resizing = apply_filters( 'simple_local_avatars_dynamic_resize', true ) ) :
  50.  
  51.                 $upload_path = wp_upload_dir();
  52.  
  53.                 // get path for image by converting URL, unless its already been set, thanks to using media library approach
  54.                 if ( ! isset( $avatar_full_path ) )
  55.                     $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
  56.  
  57.                 // generate the new size
  58.                 $editor = wp_get_image_editor( $avatar_full_path );
  59.                 if ( ! is_wp_error( $editor ) ) {
  60.                     $resized = $editor->resize( $size, $size, true );
  61.                     if ( ! is_wp_error( $resized ) ) {
  62.                         $dest_file = $editor->generate_filename();
  63.                         $saved = $editor->save( $dest_file );
  64.                         if ( ! is_wp_error( $saved ) )
  65.                             $local_avatars[$size] = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file );
  66.                     }
  67.                 }
  68.  
  69.                 // save updated avatar sizes
  70.                 update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
  71.  
  72.             endif;
  73.         }
  74.  
  75.         if ( 'http' != substr( $local_avatars[$size], 0, 4 ) )
  76.             $local_avatars[$size] = home_url( $local_avatars[$size] );
  77.        
  78.         $author_class = is_author( $user_id ) ? ' current-author' : '' ;
  79.         $avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . esc_url( $local_avatars[$size] ) . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
  80.        
  81.         return apply_filters( 'simple_local_avatar', $avatar );
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement