simple_local_avatar ) )
echo '' . __('No local avatar is set. Set up your avatar at Gravatar.com.','simple-local-avatars') . '';
else
echo '' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.','simple-local-avatars') . '';
}
?>
avatar_delete( $user_id ); // delete old images if successful
$meta_value = array();
// set the new avatar
if ( is_int( $url_or_media_id ) ) {
$meta_value['media_id'] = $url_or_media_id;
$url_or_media_id = wp_get_attachment_url( $url_or_media_id );
}
$meta_value['full'] = $url_or_media_id;
update_user_meta( $user_id, 'simple_local_avatar', $meta_value ); // save user information (overwriting old)
}
/**
* Save any changes to the user profile
*
* @param int $user_id ID of user being updated
*/
public function edit_user_profile_update( $user_id ) {
// check nonces
if( empty( $_POST['_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) )
return;
// check for uploaded files
if ( ! empty( $_FILES['simple-local-avatar']['name'] ) ) :
// need to be more secure since low privelege users can upload
if ( false !== strpos( $_FILES['simple-local-avatar']['name'], '.php' ) ) {
$this->avatar_upload_error = __('For security reasons, the extension ".php" cannot be in your file name.','simple-local-avatars');
add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ) );
return;
}
// front end (theme my profile etc) support
if ( ! function_exists( 'media_handle_upload' ) )
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// allow developers to override file size upload limit for avatars
add_filter( 'upload_size_limit', array( $this, 'upload_size_limit' ) );
$this->user_id_being_edited = $user_id; // make user_id known to unique_filename_callback function
$avatar_id = media_handle_upload( 'simple-local-avatar', 0, array(), array(
'mimes' => array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
),
'test_form' => false,
'unique_filename_callback' => array( $this, 'unique_filename_callback' )
) );
remove_filter( 'upload_size_limit', array( $this, 'upload_size_limit' ) );
if ( is_wp_error( $avatar_id ) ) { // handle failures.
$this->avatar_upload_error = '' . __( 'There was an error uploading the avatar:', 'simple-local-avatars' ) . ' ' . esc_html( $avatar_id->get_error_message() );
add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ) );
return;
}
$this->assign_new_user_avatar( $avatar_id, $user_id );
endif;
// handle rating
if ( isset( $avatar_id ) || $avatar = get_user_meta( $user_id, 'simple_local_avatar', true ) ) {
if ( empty( $_POST['simple_local_avatar_rating'] ) || ! array_key_exists( $_POST['simple_local_avatar_rating'], $this->avatar_ratings ) )
$_POST['simple_local_avatar_rating'] = key( $this->avatar_ratings );
update_user_meta( $user_id, 'simple_local_avatar_rating', $_POST['simple_local_avatar_rating'] );
}
}
/**
* Allow developers to override the maximum allowable file size for avatar uploads
*
* @param int $bytes WordPress default byte size check
* @return int Maximum byte size
*/
public function upload_size_limit( $bytes ) {
return apply_filters( 'simple_local_avatars_upload_limit', $bytes );
}
/**
* Runs when a user clicks the Remove button for the avatar
*/
public function action_remove_simple_local_avatar() {
if ( ! empty( $_GET['user_id'] ) && ! empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'remove_simple_local_avatar_nonce' ) ) {
$user_id = (int) $_GET['user_id'];
if ( ! current_user_can('edit_user', $user_id) )
wp_die( __('You do not have permission to edit this user.') );
$this->avatar_delete( $user_id ); // delete old images if successful
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
echo get_simple_local_avatar( $user_id );
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
die;
}
/**
* AJAX callback for assigning media ID fetched from media library to user
*/
public function ajax_assign_simple_local_avatar_media() {
// check required information and permissions
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' ) )
die;
$media_id = (int) $_POST['media_id'];
$user_id = (int) $_POST['user_id'];
// ensure the media is real is an image
if ( wp_attachment_is_image( $media_id ) )
$this->assign_new_user_avatar( $media_id, $user_id );
echo get_simple_local_avatar( $user_id );
die;
}
/**
* remove the custom get_avatar hook for the default avatar list output on options-discussion.php
*/
public function avatar_defaults( $avatar_defaults ) {
remove_action( 'get_avatar', array( $this, 'get_avatar' ) );
return $avatar_defaults;
}
/**
* Delete avatars based on a user_id
*
* @param int $user_id
*/
public function avatar_delete( $user_id ) {
$old_avatars = (array) get_user_meta( $user_id, 'simple_local_avatar', true );
if ( empty( $old_avatars ) )
return;
// if it was uploaded media, don't erase the full size or try to erase an the ID
if ( array_key_exists( 'media_id', $old_avatars ) )
unset( $old_avatars['media_id'], $old_avatars['full'] );
if ( ! empty( $old_avatars ) ) {
$upload_path = wp_upload_dir();
foreach ($old_avatars as $old_avatar ) {
// derive the path for the file based on the upload directory
$old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );
if ( file_exists( $old_avatar_path ) )
unlink( $old_avatar_path );
}
}
delete_user_meta( $user_id, 'simple_local_avatar' );
delete_user_meta( $user_id, 'simple_local_avatar_rating' );
}
/**
* Creates a unique, meaningful file name for uploaded avatars.
*
* @param string $dir Path for file
* @param string $name Filename
* @param string $ext File extension (e.g. ".jpg")
* @return string Final filename
*/
public function unique_filename_callback( $dir, $name, $ext ) {
$user = get_user_by( 'id', (int) $this->user_id_being_edited );
$name = $base_name = sanitize_file_name( $user->display_name . '_avatar_' . time() );
// ensure no conflicts with existing file names
$number = 1;
while ( file_exists( $dir . "/$name$ext" ) ) {
$name = $base_name . '_' . $number;
$number++;
}
return $name . $ext;
}
/**
* Adds errors based on avatar upload problems.
*
* @param WP_Error $errors Error messages for user profile screen.
*/
public function user_profile_update_errors( WP_Error $errors ) {
$errors->add( 'avatar_error', $this->avatar_upload_error );
}
/**
* Registers the simple_local_avatar field in the REST API.
*/
public function register_rest_fields() {
register_rest_field( 'user', 'simple_local_avatar', array(
'get_callback' => array( $this, 'get_avatar_rest' ),
'update_callback' => array( $this, 'set_avatar_rest' ),
'schema' => array(
'description' => 'The users simple local avatar',
'type' => 'object',
)
));
}
/**
* Returns the simple_local_avatar meta key for the given user.
*
* @param object $user User object
*/
public function get_avatar_rest( $user ) {
$local_avatar = get_user_meta( $user['id'], 'simple_local_avatar', true );
if ( empty( $local_avatar ) ) {
return;
}
return $local_avatar;
}
/**
* Updates the simple local avatar from a REST request.
*
* Since we are just adding a field to the existing user endpoint
* we don't need to worry about ensuring the calling user has proper permissions.
* Only the user or an administrator would be able to change the avatar.
*
* @param array $input Input submitted via REST request.
* @param object $user The user making the request.
*/
public function set_avatar_rest( $input, $user ) {
$this->assign_new_user_avatar($input['media_id'], $user->ID);
}
}
$simple_local_avatars = new Simple_Local_Avatars;
/**
* more efficient to call simple local avatar directly in theme and avoid gravatar setup
*
* @param int|string|object $id_or_email A user ID, email address, or comment object
* @param int $size Size of the avatar image
* @param string $default URL to a default image to use if no avatar is available
* @param string $alt Alternate text to use in image tag. Defaults to blank
* @return string tag for the user's avatar
*/
function get_simple_local_avatar( $id_or_email, $size = 96, $default = '', $alt = '' ) {
global $simple_local_avatars;
$avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );
if ( empty ( $avatar ) ) {
remove_action( 'get_avatar', array( $simple_local_avatars, 'get_avatar' ) );
$avatar = get_avatar( $id_or_email, $size, $default, $alt );
add_action( 'get_avatar', array( $simple_local_avatars, 'get_avatar' ), 10, 5 );
}
return $avatar;
}
if ( ! function_exists( 'get_avatar' ) && ( $simple_local_avatars_options = get_option('simple_local_avatars') ) && ! empty( $simple_local_avatars_options['only'] ) ) :
/**
* Retrieve the avatar for a user who provided a user ID or email address.
*
* @param int|string|object $id_or_email A user ID, email address, or comment object
* @param int $size Size of the avatar image
* @param string $default URL to a default image to use if no avatar is available
* @param string $alt Alternative text to use in image tag. Defaults to blank
* @return string tag for the user's avatar
*/
function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '' ) {
global $simple_local_avatars;
if ( ! get_option('show_avatars') )
return false;
$safe_alt = empty( $alt ) ? '' : esc_attr( $alt );
if ( !is_numeric($size) )
$size = 96;
if ( ! $avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt ) ) :
if ( empty($default) ) {
$avatar_default = get_option('avatar_default');
if ( empty($avatar_default) )
$default = 'mystery';
else
$default = $avatar_default;
}
$host = is_ssl() ? 'https://secure.gravatar.com' : 'http://0.gravatar.com';
if ( 'mystery' == $default )
$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
elseif ( 'blank' == $default )
$default = includes_url( 'images/blank.gif' );
elseif ( 'gravatar_default' == $default )
$default = "$host/avatar/?s={$size}";
else
$default = "$host/avatar/?d=$default&s={$size}";
$avatar = "";
endif;
return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
}
endif;
/**
* on uninstallation, remove the custom field from the users and delete the local avatars
*/
register_uninstall_hook( __FILE__, 'simple_local_avatars_uninstall' );
function simple_local_avatars_uninstall() {
$simple_local_avatars = new Simple_Local_Avatars;
$users = get_users(array(
'meta_key' => 'simple_local_avatar',
'fields' => 'ids',
));
foreach ( $users as $user_id ):
$simple_local_avatars->avatar_delete( $user_id );
endforeach;
delete_option('simple_local_avatars');
}