Guest User

Simple Local Avatars 2.1 unofficial

a guest
Oct 2nd, 2015
256
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  Plugin Name: Simple Local Avatars
  4.  Plugin URI: http://10up.com/plugins/simple-local-avatars-wordpress/
  5.  Description: Adds an avatar upload field to user profiles. Generates requested sizes on demand, just like Gravatar! Simple and lightweight. Now with srcset 2x support.
  6.  Version: 2.1
  7.  Author: Jake Goldman, 10up, Ov3rfly
  8.  Author URI: http://10up.com
  9.  License: GPLv2 or later
  10. */
  11.  
  12. /**
  13.  * add field to user profiles
  14.  */
  15.  
  16. class Simple_Local_Avatars {
  17.     private $user_id_being_edited, $avatar_upload_error, $remove_nonce, $avatar_ratings;
  18.     public $options;
  19.  
  20.     /**
  21.      * Set up the hooks and default values
  22.      */
  23.     public function __construct() {
  24.         load_plugin_textdomain( 'simple-local-avatars', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
  25.  
  26.         $this->options = (array) get_option( 'simple_local_avatars' );
  27.         $this->avatar_ratings = array(
  28.             'G' => __('G &#8212; Suitable for all audiences'),
  29.             'PG' => __('PG &#8212; Possibly offensive, usually for audiences 13 and above'),
  30.             'R' => __('R &#8212; Intended for adult audiences above 17'),
  31.             'X' => __('X &#8212; Even more mature than above')
  32.         );
  33.  
  34.         // supplement remote avatars, but not if inside "local only" mode
  35.         if ( empty( $this->options['only'] ) )
  36.             add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );
  37.        
  38.         add_action( 'admin_init', array( $this, 'admin_init' ) );
  39.  
  40.         add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  41.         add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
  42.         add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
  43.        
  44.         add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );
  45.         add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );
  46.         add_action( 'admin_action_remove-simple-local-avatar', array( $this, 'action_remove_simple_local_avatar' ) );
  47.         add_action( 'wp_ajax_assign_simple_local_avatar_media', array( $this, 'ajax_assign_simple_local_avatar_media' ) );
  48.         add_action( 'wp_ajax_remove_simple_local_avatar', array( $this, 'action_remove_simple_local_avatar' ) );
  49.         add_action( 'user_edit_form_tag', array( $this, 'user_edit_form_tag' ) );
  50.        
  51.         add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );
  52.     }
  53.  
  54.     /**
  55.      * Retrieve the local avatar for a user who provided a user ID or email address.
  56.      *
  57.      * @param string $avatar Avatar return by original function
  58.      * @param int|string|object $id_or_email A user ID,  email address, or comment object
  59.      * @param int $size Size of the avatar image
  60.      * @param string $default URL to a default image to use if no avatar is available
  61.      * @param string $alt Alternative text to use in image tag. Defaults to blank
  62.      * @return string <img> tag for the user's avatar
  63.      */
  64.     public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
  65.         if ( is_numeric( $id_or_email ) )
  66.             $user_id = (int) $id_or_email;
  67.         elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )
  68.             $user_id = $user->ID;
  69.         elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) )
  70.             $user_id = (int) $id_or_email->user_id;
  71.        
  72.         if ( empty( $user_id ) )
  73.             return $avatar;
  74.  
  75.         // fetch local avatar from meta and make sure it's properly ste
  76.         $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
  77.         if ( empty( $local_avatars['full'] ) )
  78.             return $avatar;
  79.  
  80.         // check rating
  81.         $avatar_rating = get_user_meta( $user_id, 'simple_local_avatar_rating', true );
  82.         if ( ! empty( $avatar_rating ) && 'G' != $avatar_rating && ( $site_rating = get_option( 'avatar_rating' ) ) ) {
  83.             $ratings = array_keys( $this->avatar_ratings );
  84.             $site_rating_weight = array_search( $site_rating, $ratings );
  85.             $avatar_rating_weight = array_search( $avatar_rating, $ratings );
  86.             if ( false !== $avatar_rating_weight && $avatar_rating_weight > $site_rating_weight )
  87.                 return $avatar;
  88.         }
  89.  
  90.         // handle "real" media
  91.         if ( ! empty( $local_avatars['media_id'] ) ) {
  92.             // has the media been deleted?
  93.             if ( ! $avatar_full_path = get_attached_file( $local_avatars['media_id'] ) ) {
  94.                 // only allowed logged in users to delete bad data to mitigate performance issues
  95.                 if ( is_user_logged_in() )
  96.                     $this->avatar_delete( $user_id );
  97.  
  98.                 return $avatar;
  99.             }
  100.         }
  101.  
  102.         $size = (int) $size;
  103.            
  104.         if ( empty( $alt ) )
  105.             $alt = get_the_author_meta( 'display_name', $user_id );
  106.            
  107.         // generate a new size
  108.         if ( ! array_key_exists( $size, $local_avatars ) ) {
  109.             $local_avatars[$size] = $local_avatars['full']; // just in case of failure elsewhere
  110.  
  111.             // allow automatic rescaling to be turned off
  112.             if ( $allow_dynamic_resizing = apply_filters( 'simple_local_avatars_dynamic_resize', true ) ) :
  113.  
  114.                 $upload_path = wp_upload_dir();
  115.  
  116.                 // get path for image by converting URL, unless its already been set, thanks to using media library approach
  117.                 if ( ! isset( $avatar_full_path ) )
  118.                     $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
  119.  
  120.                 // generate the new size
  121.                 $editor = wp_get_image_editor( $avatar_full_path );
  122.                 if ( ! is_wp_error( $editor ) ) {
  123.                     $resized = $editor->resize( $size, $size, true );
  124.                     if ( ! is_wp_error( $resized ) ) {
  125.                         $dest_file = $editor->generate_filename();
  126.                         $saved = $editor->save( $dest_file );
  127.                         if ( ! is_wp_error( $saved ) )
  128.                             $local_avatars[$size] = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file );
  129.                     }
  130.                 }
  131.  
  132.                 // save updated avatar sizes
  133.                 update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
  134.  
  135.             endif;
  136.         }
  137.  
  138.         // Ov3rfly, generate a new size
  139.         $size_2x = $size * 2;
  140.  
  141.         if ( ! array_key_exists( $size_2x, $local_avatars ) ) {
  142.             $local_avatars[$size_2x] = $local_avatars['full']; // just in case of failure elsewhere
  143.  
  144.             // allow automatic rescaling to be turned off
  145.             if ( $allow_dynamic_resizing = apply_filters( 'simple_local_avatars_dynamic_resize', true ) ) :
  146.  
  147.                 $upload_path = wp_upload_dir();
  148.  
  149.                 // get path for image by converting URL, unless its already been set, thanks to using media library approach
  150.                 if ( ! isset( $avatar_full_path ) )
  151.                     $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
  152.  
  153.                 // generate the new size_2x
  154.                 $editor = wp_get_image_editor( $avatar_full_path );
  155.                 if ( ! is_wp_error( $editor ) ) {
  156.                     $resized = $editor->resize( $size_2x, $size_2x, true );
  157.                     if ( ! is_wp_error( $resized ) ) {
  158.                         $dest_file = $editor->generate_filename();
  159.                         $saved = $editor->save( $dest_file );
  160.                         if ( ! is_wp_error( $saved ) )
  161.                             $local_avatars[$size_2x] = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file );
  162.                     }
  163.                 }
  164.  
  165.                 // save updated avatar sizes
  166.                 update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
  167.  
  168.             endif;
  169.         }
  170.  
  171.         if ( 'http' != substr( $local_avatars[$size], 0, 4 ) )
  172.             $local_avatars[$size] = home_url( $local_avatars[$size] );
  173.  
  174.         // Ov3rfly
  175.         if ( 'http' != substr( $local_avatars[$size_2x], 0, 4 ) )
  176.             $local_avatars[$size_2x] = home_url( $local_avatars[$size_2x] );
  177.        
  178.         // Ov3rfly, fix warning
  179.         // https://wordpress.org/support/topic/plugin-simple-local-avatars-undefined-property-errors-on-admin-side-of-custom-posts-when-plugin-activated
  180.         $author_class = @is_author( $user_id ) ? ' current-author' : '' ;
  181.        
  182.         // Ov3rfly, add srcset
  183.         $avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . esc_url( $local_avatars[$size] ) . "' srcset='" . esc_attr( $local_avatars[$size_2x] ) . " 2x' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
  184.        
  185.         return apply_filters( 'simple_local_avatar', $avatar );
  186.     }
  187.    
  188.     public function admin_init() {
  189.         // upgrade pre 2.0 option
  190.         if ( $old_ops = get_option( 'simple_local_avatars_caps' ) ) {
  191.             if ( ! empty( $old_ops['simple_local_avatars_caps'] ) )
  192.                 update_option( 'simple_local_avatars', array( 'caps' => 1 ) );
  193.  
  194.             delete_option( 'simple_local_avatar_caps' );
  195.         }
  196.  
  197.         register_setting( 'discussion', 'simple_local_avatars', array( $this, 'sanitize_options' ) );
  198.         add_settings_field( 'simple-local-avatars-only', __('Local Avatars Only','simple-local-avatars'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'key' => 'only', 'desc' => 'Only allow local avatars (still uses Gravatar for default avatars)' ) );
  199.         add_settings_field( 'simple-local-avatars-caps', __('Local Upload Permissions','simple-local-avatars'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'key' => 'caps', 'desc' => 'Only allow users with file upload capabilities to upload local avatars (Authors and above)' ) );
  200.     }
  201.  
  202.     /**
  203.      * Add scripts to the profile editing page
  204.      *
  205.      * @param string $hook_suffix Page hook
  206.      */
  207.     public function admin_enqueue_scripts( $hook_suffix ) {
  208.         if ( 'profile.php' != $hook_suffix && 'user-edit.php' != $hook_suffix )
  209.             return;
  210.  
  211.         if ( current_user_can( 'upload_files' ) )
  212.             wp_enqueue_media();
  213.  
  214.         $user_id = ( 'profile.php' == $hook_suffix ) ? get_current_user_id() : (int) $_GET['user_id'];
  215.  
  216.         $this->remove_nonce = wp_create_nonce( 'remove_simple_local_avatar_nonce' );
  217.  
  218.         $script_name_append = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.dev' : '';
  219.         wp_enqueue_script( 'simple-local-avatars', plugins_url( '', __FILE__ ) . '/simple-local-avatars' . $script_name_append . '.js', array('jquery'), false, true );
  220.         wp_localize_script( 'simple-local-avatars', 'i10n_SimpleLocalAvatars', array(
  221.             'user_id'           => $user_id,
  222.             'insertMediaTitle'  => __('Choose an Avatar','simple-local-avatars'),
  223.             'insertIntoPost'    => __('Set as avatar','simple-local-avatars'),
  224.             'deleteNonce'       => $this->remove_nonce,
  225.             'mediaNonce'        => wp_create_nonce( 'assign_simple_local_avatar_nonce' ),
  226.         ) );
  227.     }
  228.  
  229.     /**
  230.      * Sanitize new settings field before saving
  231.      *
  232.      * @param array|string $input Passed input values to sanitize
  233.      * @return array|string Sanitized input fields
  234.      */
  235.     public function sanitize_options( $input ) {
  236.         $new_input['caps'] = empty( $input['caps'] ) ? 0 : 1;
  237.         $new_input['only'] = empty( $input['only'] ) ? 0 : 1;
  238.         return $new_input;
  239.     }
  240.  
  241.     /**
  242.      * Settings field for avatar upload capabilities
  243.      *
  244.      * @param array $args Field arguments
  245.      */
  246.     public function avatar_settings_field( $args ) {
  247.         $args = wp_parse_args( $args, array(
  248.             'key'   => '',
  249.             'desc'  => '',
  250.         ) );
  251.  
  252.         if ( empty( $this->options[$args['key']] ) )
  253.             $this->options[$args['key']] = 0;
  254.        
  255.         echo '
  256.             <label for="simple-local-avatars-' . $args['key'] . '">
  257.                 <input type="checkbox" name="simple_local_avatars[' . $args['key'] . ']" id="simple-local-avatars-' . $args['key'] . '" value="1" ' . checked( $this->options[$args['key']], 1, false ) . ' />
  258.                 ' . __($args['desc'],'simple-local-avatars') . '
  259.             </label>
  260.         ';
  261.     }
  262.  
  263.     /**
  264.      * Output new Avatar fields to user editing / profile screen
  265.      *
  266.      * @param object $profileuser User object
  267.      */
  268.     public function edit_user_profile( $profileuser ) {
  269.     ?>
  270.     <h3><?php _e( 'Avatar','simple-local-avatars' ); ?></h3>
  271.    
  272.     <table class="form-table">
  273.         <tr>
  274.             <th scope="row"><label for="simple-local-avatar"><?php _e('Upload Avatar','simple-local-avatars'); ?></label></th>
  275.             <td style="width: 50px;" id="simple-local-avatar-photo">
  276.                 <?php
  277.                     add_filter( 'pre_option_avatar_rating', '__return_null' );  // ignore ratings here
  278.                     echo get_simple_local_avatar( $profileuser->ID );
  279.                     remove_filter( 'pre_option_avatar_rating', '__return_null' );
  280.                 ?>
  281.             </td>
  282.             <td>
  283.             <?php
  284.                 if ( ! $upload_rights = current_user_can('upload_files') )
  285.                     $upload_rights = empty( $this->options['caps'] );
  286.            
  287.                 if ( $upload_rights ) {
  288.                     do_action( 'simple_local_avatar_notices' );
  289.                     wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
  290.                     $remove_url = add_query_arg(array(
  291.                         'action'    => 'remove-simple-local-avatar',
  292.                         'user_id'   => $profileuser->ID,
  293.                         '_wpnonce'  => $this->remove_nonce,
  294.                     ) );
  295.             ?>
  296.                     <p style="display: inline-block; width: 26em;">
  297.                         <span class="description"><?php _e( 'Choose an image from your computer:' ); ?></span><br />
  298.                         <input type="file" name="simple-local-avatar" id="simple-local-avatar" class="standard-text" />
  299.                         <span class="spinner" id="simple-local-avatar-spinner"></span>
  300.                     </p>
  301.                     <p>
  302.                         <?php if ( current_user_can( 'upload_files' ) && did_action( 'wp_enqueue_media' ) ) : ?><a href="#" class="button hide-if-no-js" id="simple-local-avatar-media"><?php _e( 'Choose from Media Library', 'simple-local-avatars' ); ?></a> &nbsp;<?php endif; ?>
  303.                         <a href="<?php echo $remove_url; ?>" class="button item-delete submitdelete deletion" id="simple-local-avatar-remove"<?php if ( empty( $profileuser->simple_local_avatar ) ) echo ' style="display:none;"'; ?>><?php _e('Delete local avatar','simple-local-avatars'); ?></a>
  304.                     </p>
  305.             <?php
  306.                 } else {
  307.                     if ( empty( $profileuser->simple_local_avatar ) )
  308.                         echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.','simple-local-avatars') . '</span>';
  309.                     else
  310.                         echo '<span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.','simple-local-avatars') . '</span>';
  311.                 }
  312.             ?>
  313.             </td>
  314.         </tr>
  315.         <tr>
  316.             <th scope="row" class="simple-local-avatar-rating-headline"><?php _e('Rating'); ?></th>
  317.             <td colspan="2">
  318.                 <fieldset id="simple-local-avatar-ratings" <?php disabled( empty( $profileuser->simple_local_avatar ) ); ?>>
  319.                     <legend class="screen-reader-text"><span><?php _e('Rating'); ?></span></legend>
  320.                     <?php
  321.                         if ( empty( $profileuser->simple_local_avatar_rating ) || ! array_key_exists( $profileuser->simple_local_avatar_rating, $this->avatar_ratings ) )
  322.                             $profileuser->simple_local_avatar_rating =  'G';
  323.  
  324.                         foreach ( $this->avatar_ratings as $key => $rating ) :
  325.                             echo "\n\t<label><input type='radio' name='simple_local_avatar_rating' value='" . esc_attr( $key ) . "' " . checked( $profileuser->simple_local_avatar_rating, $key, false ) . "/> $rating</label><br />";
  326.                         endforeach;
  327.                     ?>
  328.                     <p class="description"><?php _e( 'If the local avatar is inappropriate for this site, Gravatar will be attempted.' ); ?></p>
  329.                 </fieldset></td>
  330.         </tr>
  331.     </table>
  332.     <?php
  333.     }
  334.  
  335.     /**
  336.      * Ensure that the profile form has proper encoding type
  337.      */
  338.     public function user_edit_form_tag() {
  339.         echo 'enctype="multipart/form-data"';
  340.     }
  341.  
  342.     /**
  343.      * Saves avatar image to a user
  344.      *
  345.      * @param int|string $url_or_media_id Local URL for avatar or ID of attachment
  346.      * @param int $user_id ID of user to assign image to
  347.      */
  348.     private function assign_new_user_avatar( $url_or_media_id, $user_id ) {
  349.         // delete the old avatar
  350.         $this->avatar_delete( $user_id );   // delete old images if successful
  351.  
  352.         $meta_value = array();
  353.  
  354.         // set the new avatar
  355.         if ( is_int( $url_or_media_id ) ) {
  356.             $meta_value['media_id'] = $url_or_media_id;
  357.             $url_or_media_id = wp_get_attachment_url( $url_or_media_id );
  358.         }
  359.  
  360.         $meta_value['full'] = $url_or_media_id;
  361.  
  362.         update_user_meta( $user_id, 'simple_local_avatar', $meta_value );   // save user information (overwriting old)
  363.     }
  364.  
  365.     /**
  366.      * Save any changes to the user profile
  367.      *
  368.      * @param int $user_id ID of user being updated
  369.      */
  370.     public function edit_user_profile_update( $user_id ) {
  371.         // check nonces
  372.         if( empty( $_POST['_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) )
  373.             return;
  374.  
  375.         // check for uploaded files
  376.         if ( ! empty( $_FILES['simple-local-avatar']['name'] ) ) :
  377.  
  378.             // need to be more secure since low privelege users can upload
  379.             if ( false !== strpos( $_FILES['simple-local-avatar']['name'], '.php' ) ) {
  380.                 $this->avatar_upload_error = __('For security reasons, the extension ".php" cannot be in your file name.','simple-local-avatars');
  381.                 add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ) );
  382.                 return;
  383.             }
  384.  
  385.             // front end (theme my profile etc) support
  386.             if ( ! function_exists( 'wp_handle_upload' ) )
  387.                 require_once( ABSPATH . 'wp-admin/includes/file.php' );
  388.  
  389.             // allow developers to override file size upload limit for avatars
  390.             add_filter( 'upload_size_limit', array( $this, 'upload_size_limit' ) );
  391.  
  392.             $this->user_id_being_edited = $user_id; // make user_id known to unique_filename_callback function
  393.             $avatar = wp_handle_upload( $_FILES['simple-local-avatar'], array(
  394.                 'mimes'                     => array(
  395.                     'jpg|jpeg|jpe'  => 'image/jpeg',
  396.                     'gif'           => 'image/gif',
  397.                     'png'           => 'image/png',
  398.                 ),
  399.                 'test_form'                 => false,
  400.                 'unique_filename_callback'  => array( $this, 'unique_filename_callback' )
  401.             ) );
  402.  
  403.             remove_filter( 'upload_size_limit', array( $this, 'upload_size_limit' ) );
  404.  
  405.             if ( empty($avatar['file']) ) {     // handle failures
  406.                 switch ( $avatar['error'] ) {
  407.                     case 'File type does not meet security guidelines. Try another.' :
  408.                         $this->avatar_upload_error = __('Please upload a valid image file for the avatar.','simple-local-avatars');
  409.                         break;
  410.                     default :
  411.                         $this->avatar_upload_error = '<strong>' . __('There was an error uploading the avatar:','simple-local-avatars') . '</strong> ' . esc_html( $avatar['error'] );
  412.                 }
  413.  
  414.                 add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ) );
  415.  
  416.                 return;
  417.             }
  418.  
  419.             $this->assign_new_user_avatar( $avatar['url'], $user_id );
  420.  
  421.         endif;
  422.  
  423.         // handle rating
  424.         if ( isset( $avatar['url'] ) || $avatar = get_user_meta( $user_id, 'simple_local_avatar', true ) ) {
  425.             if ( empty( $_POST['simple_local_avatar_rating'] ) || ! array_key_exists( $_POST['simple_local_avatar_rating'], $this->avatar_ratings ) )
  426.                 $_POST['simple_local_avatar_rating'] = key( $this->avatar_ratings );
  427.  
  428.             update_user_meta( $user_id, 'simple_local_avatar_rating', $_POST['simple_local_avatar_rating'] );
  429.         }
  430.     }
  431.  
  432.     /**
  433.      * Allow developers to override the maximum allowable file size for avatar uploads
  434.      *
  435.      * @param int $bytes WordPress default byte size check
  436.      * @return int Maximum byte size
  437.      */
  438.     public function upload_size_limit( $bytes ) {
  439.         return apply_filters( 'simple_local_avatars_upload_limit', $bytes );
  440.     }
  441.  
  442.     /**
  443.      * Runs when a user clicks the Remove button for the avatar
  444.      */
  445.     public function action_remove_simple_local_avatar() {
  446.         if ( ! empty( $_GET['user_id'] ) &&  ! empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'remove_simple_local_avatar_nonce' ) ) {
  447.             $user_id = (int) $_GET['user_id'];
  448.  
  449.             if ( ! current_user_can('edit_user', $user_id) )
  450.                 wp_die( __('You do not have permission to edit this user.') );
  451.  
  452.             $this->avatar_delete( $user_id );   // delete old images if successful
  453.  
  454.             if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  455.                 echo get_simple_local_avatar( $user_id );
  456.         }
  457.  
  458.         if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  459.             die;
  460.     }
  461.  
  462.     /**
  463.      * AJAX callback for assigning media ID fetched from media library to user
  464.      */
  465.     public function ajax_assign_simple_local_avatar_media() {
  466.         // check required information and permissions
  467.         if ( empty( $_POST['user_id'] ) || empty( $_POST['media_id'] ) || ! current_user_can( 'upload_files' ) || ! current_user_can( 'edit_user', $_POST['user_id'] ) || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'assign_simple_local_avatar_nonce' ) )
  468.             die;
  469.  
  470.         $media_id = (int) $_POST['media_id'];
  471.         $user_id = (int) $_POST['user_id'];
  472.  
  473.         // ensure the media is real is an image
  474.         if ( wp_attachment_is_image( $media_id ) )
  475.             $this->assign_new_user_avatar( $media_id, $user_id );
  476.  
  477.         echo get_simple_local_avatar( $user_id );
  478.  
  479.         die;
  480.     }
  481.    
  482.     /**
  483.      * remove the custom get_avatar hook for the default avatar list output on options-discussion.php
  484.      */
  485.     public function avatar_defaults( $avatar_defaults ) {
  486.         remove_action( 'get_avatar', array( $this, 'get_avatar' ) );
  487.         return $avatar_defaults;
  488.     }
  489.  
  490.     /**
  491.      * Delete avatars based on a user_id
  492.      *
  493.      * @param int $user_id
  494.      */
  495.     public function avatar_delete( $user_id ) {
  496.         $old_avatars = (array) get_user_meta( $user_id, 'simple_local_avatar', true );
  497.  
  498.         if ( empty( $old_avatars ) )
  499.             return;
  500.  
  501.         // if it was uploaded media, don't erase the full size or try to erase an the ID
  502.         if ( array_key_exists( 'media_id', $old_avatars ) )
  503.             unset( $old_avatars['media_id'], $old_avatars['full'] );
  504.  
  505.         if ( ! empty( $old_avatars ) ) {
  506.             $upload_path = wp_upload_dir();
  507.  
  508.             foreach ($old_avatars as $old_avatar ) {
  509.                 // derive the path for the file based on the upload directory
  510.                 $old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );
  511.                 if ( file_exists( $old_avatar_path ) )
  512.                     unlink( $old_avatar_path );
  513.             }
  514.         }
  515.  
  516.         delete_user_meta( $user_id, 'simple_local_avatar' );
  517.         delete_user_meta( $user_id, 'simple_local_avatar_rating' );
  518.     }
  519.  
  520.     /**
  521.      * Creates a unique, meaningful file name for uploaded avatars.
  522.      *
  523.      * @param string $dir Path for file
  524.      * @param string $name Filename
  525.      * @param string $ext File extension (e.g. ".jpg")
  526.      * @return string Final filename
  527.      */
  528.     public function unique_filename_callback( $dir, $name, $ext ) {
  529.         $user = get_user_by( 'id', (int) $this->user_id_being_edited );
  530.         // Ov3rfly
  531.         //$name = $base_name = sanitize_file_name( $user->display_name . '_avatar_' . time() );
  532.         // http://wordpress.org/support/topic/avatar-file-name-should-not-be-based-on-display-name
  533.         $name = $base_name = sanitize_title( $user->display_name . '_avatar_' . time() );
  534.         // ensure no conflicts with existing file names
  535.         $number = 1;
  536.         while ( file_exists( $dir . "/$name$ext" ) ) {
  537.             $name = $base_name . '_' . $number;
  538.             $number++;
  539.         }
  540.                
  541.         return $name . $ext;
  542.     }
  543.  
  544.     /**
  545.      * Adds errors based on avatar upload problems.
  546.      *
  547.      * @param WP_Error $errors Error messages for user profile screen.
  548.      */
  549.     public function user_profile_update_errors( WP_Error $errors ) {
  550.         $errors->add( 'avatar_error', $this->avatar_upload_error );
  551.     }
  552. }
  553.  
  554. $simple_local_avatars = new Simple_Local_Avatars;
  555.  
  556. /**
  557.  * more efficient to call simple local avatar directly in theme and avoid gravatar setup
  558.  *
  559.  * @param int|string|object $id_or_email A user ID,  email address, or comment object
  560.  * @param int $size Size of the avatar image
  561.  * @param string $default URL to a default image to use if no avatar is available
  562.  * @param string $alt Alternate text to use in image tag. Defaults to blank
  563.  * @return string <img> tag for the user's avatar
  564.  */
  565. function get_simple_local_avatar( $id_or_email, $size = 96, $default = '', $alt = '' ) {
  566.     global $simple_local_avatars;
  567.     $avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );
  568.    
  569.     if ( empty ( $avatar ) ) {
  570.         remove_action( 'get_avatar', array( $simple_local_avatars, 'get_avatar' ) );
  571.         $avatar = get_avatar( $id_or_email, $size, $default, $alt );
  572.         add_action( 'get_avatar', array( $simple_local_avatars, 'get_avatar' ) );
  573.     }
  574.    
  575.     return $avatar;
  576. }
  577.  
  578. if ( ! function_exists( 'get_avatar' ) && ( $simple_local_avatars_options = get_option('simple_local_avatars') ) && ! empty( $simple_local_avatars_options['only'] ) ) :
  579.  
  580.     /**
  581.      * Retrieve the avatar for a user who provided a user ID or email address.
  582.      *
  583.      * @param int|string|object $id_or_email A user ID,  email address, or comment object
  584.      * @param int $size Size of the avatar image
  585.      * @param string $default URL to a default image to use if no avatar is available
  586.      * @param string $alt Alternative text to use in image tag. Defaults to blank
  587.      * @return string <img> tag for the user's avatar
  588.      */
  589.     function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '' ) {
  590.         global $simple_local_avatars;
  591.  
  592.         if ( ! get_option('show_avatars') )
  593.             return false;
  594.  
  595.         $safe_alt =  empty( $alt ) ? '' : esc_attr( $alt );
  596.  
  597.         if ( !is_numeric($size) )
  598.             $size = 96;
  599.  
  600.         if ( ! $avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt ) ) :
  601.  
  602.             if ( empty($default) ) {
  603.                 $avatar_default = get_option('avatar_default');
  604.                 if ( empty($avatar_default) )
  605.                     $default = 'mystery';
  606.                 else
  607.                     $default = $avatar_default;
  608.             }
  609.  
  610.             $host = is_ssl() ? 'https://secure.gravatar.com' : 'http://0.gravatar.com';
  611.  
  612.             if ( 'mystery' == $default )
  613.                 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  614.             elseif ( 'blank' == $default )
  615.                 $default = includes_url( 'images/blank.gif' );
  616.             elseif ( 'gravatar_default' == $default )
  617.                 $default = "$host/avatar/?s={$size}";
  618.             else
  619.                 $default = "$host/avatar/?d=$default&amp;s={$size}";
  620.  
  621.             $avatar = "<img alt='{$safe_alt}' src='" . $default . "' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
  622.  
  623.         endif;
  624.  
  625.         return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
  626.     }
  627.  
  628. endif;
  629.  
  630. /**
  631.  * on uninstallation, remove the custom field from the users and delete the local avatars
  632.  */
  633.  
  634. register_uninstall_hook( __FILE__, 'simple_local_avatars_uninstall' );
  635.  
  636. function simple_local_avatars_uninstall() {
  637.     $simple_local_avatars = new Simple_Local_Avatars;
  638.     $users = get_users(array(
  639.         'meta_key'  => 'simple_local_avatar',
  640.         'fields'    => 'ids',
  641.     ));
  642.  
  643.     foreach ( $users as $user_id ):
  644.         $simple_local_avatars->avatar_delete( $user_id );
  645.     endforeach;
  646.    
  647.     delete_option('simple_local_avatars');
  648. }
RAW Paste Data