Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set default avatar #96

Merged
merged 4 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Cache the button.
const $clearCacheBtn = $( '#clear_cache_btn' );
const $clearCacheMessage = $( '#clear_cache_message' );
const $simpleLocalAvatarDefault = $('#simple-local-avatar-default');
const $simpleLocalAvatarFileUrl = $('#simple-local-avatar-file-url');
const $simpleLocalAvatarFileId = $('#simple-local-avatar-file-id');

// Spinner button.
const spinnerButton = '<span class="spinner is-active" style="margin-left:5px;margin-right:0;"></span>';
Expand Down Expand Up @@ -58,4 +61,44 @@
},
} );
}

/**
* Default avatar upload field listener in Settings -> Discussions.
*/
$simpleLocalAvatarDefault.click(function(e) {
e.preventDefault();
var _this = $(this);
var image = wp.media({
title: slaAdmin.insertMediaTitle,
multiple: false,
library : {
type : 'image',
}
}).open()
.on('select', function(e){
// This will return the selected image from the Media Uploader, the result is an object
var uploaded_image = image.state().get('selection').first();
uploaded_image = uploaded_image.toJSON();
var avatar_preview = uploaded_image?.sizes?.thumbnail?.url;
if ( 'undefined' === typeof avatar_preview ) {
avatar_preview = uploaded_image.url;
}
var simpleDefaultAvatarImg = _this.parent().find('img.avatar');
simpleDefaultAvatarImg.show();
simpleDefaultAvatarImg.attr( 'src', avatar_preview );
simpleDefaultAvatarImg.attr( 'srcset', avatar_preview );
$simpleLocalAvatarFileUrl.val(avatar_preview);
$simpleLocalAvatarFileId.val(uploaded_image.id);
});
});

if ( $simpleLocalAvatarFileUrl.length && $simpleLocalAvatarFileUrl.val() !== '' ) {
var $simpleDefaultAvatarImg = $simpleLocalAvatarFileUrl.parent().find('img.avatar');
$simpleDefaultAvatarImg.attr('src', $simpleLocalAvatarFileUrl.val());
$simpleDefaultAvatarImg.attr('srcset', $simpleLocalAvatarFileUrl.val());
}

if ( '' === $simpleLocalAvatarFileId.val() ) {
$simpleLocalAvatarFileId.parent().find('img.avatar').hide();
}
} )( jQuery );
59 changes: 55 additions & 4 deletions includes/class-simple-local-avatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function add_hooks() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
add_action( 'wp_ajax_sla_clear_user_cache', array( $this, 'sla_clear_user_cache' ) );

add_filter( 'avatar_defaults', array( $this, 'add_avatar_default_field' ) );
add_action( 'wpmu_new_blog', array( $this, 'set_defaults' ) );
}

Expand Down Expand Up @@ -344,6 +345,12 @@ public function get_default_avatar_url( $size ) {
$default = includes_url( 'images/blank.gif' );
} elseif ( 'gravatar_default' === $default ) {
$default = "$host/avatar/?s={$size}";
} elseif ( 'simple_local_avatar' === $default ) {
$default = "$host/avatar/?d=$default&amp;s={$size}";
$default_avatar_id = get_option( 'simple_local_avatar_default', '' );
if ( ! empty( $default_avatar_id ) ) {
$default = wp_get_attachment_image_url( $default_avatar_id );
}
} else {
$default = "$host/avatar/?d=$default&amp;s={$size}";
}
Expand Down Expand Up @@ -431,6 +438,9 @@ public function admin_init() {
'desc' => esc_html__( 'Clear cache of stored avatars', 'simple-local-avatars' ),
)
);

// Save default avatar file.
$this->save_default_avatar_file_id();
}

/**
Expand Down Expand Up @@ -1131,8 +1141,9 @@ public function admin_scripts() {
'sla_admin',
'slaAdmin',
[
'nonce' => wp_create_nonce( 'sla_clear_cache_nonce' ),
'error' => esc_html__( 'Something went wrong while clearing cache, please try again.', 'simple-local-avatars' ),
'nonce' => wp_create_nonce( 'sla_clear_cache_nonce' ),
'error' => esc_html__( 'Something went wrong while clearing cache, please try again.', 'simple-local-avatars' ),
'insertMediaTitle' => esc_html__( 'Choose default avatar', 'simple-local-avatars' ),
]
);
}
Expand Down Expand Up @@ -1222,8 +1233,49 @@ private function clear_user_avatar_cache( $local_avatars, $user_id, $media_id )
}

/**
* Migrate the user's avatar data from WP User Avatar/ProfilePress
* Add default avatar upload file field.
*
* @param array $defaults Default options for avatar.
*
* @return array Default options of avatar.
*/
public function add_avatar_default_field( $defaults ) {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
$default_avatar_file_url = '';
$default_avatar_file_id = get_option( 'simple_local_avatar_default', '' );
if ( ! empty( $default_avatar_file_id ) ) {
$default_avatar_file_url = wp_get_attachment_image_url( $default_avatar_file_id );
}
ob_start();
?>
<input type="hidden" name="simple-local-avatar-file-id" id="simple-local-avatar-file-id" value="<?php echo ! empty( $default_avatar_file_id ) ? esc_attr( $default_avatar_file_id ) : ''; ?>"/>
<input type="hidden" name="simple-local-avatar-file-url" id="simple-local-avatar-file-url" value="<?php echo ! empty( $default_avatar_file_url ) ? esc_url( $default_avatar_file_url ) : ''; ?>"/>
<input type="button" name="simple-local-avatar" id="simple-local-avatar-default" class="button-secondary" value="<?php esc_attr_e( 'Choose Default Avatar', 'simple-local-avatar' ); ?>"/>
<?php
$defaults['simple_local_avatar'] = ob_get_clean();

return $defaults;
}

/**
* Save default avatar attachment id in option.
*/
private function save_default_avatar_file_id() {
global $pagenow;

$file_id = filter_input( INPUT_POST, 'simple-local-avatar-file-id', FILTER_SANITIZE_NUMBER_INT );

// check for uploaded files
if ( 'options.php' === $pagenow && ! empty( $file_id ) ) {
update_option( 'simple_local_avatar_default', $file_id );
}
}

/**
* Migrate the user's avatar data from WP User Avatar/ProfilePress
*
* This function creates a new option in the wp_options table to store the processed user IDs
* so that we can run this command multiple times without processing the same user over and over again.
*
Expand Down Expand Up @@ -1308,7 +1360,6 @@ public function migrate_from_wp_user_avatar() {
}

return $count;

}

/**
Expand Down