Skip to content

Commit

Permalink
Merge pull request #120 from 10up/fix/116-2
Browse files Browse the repository at this point in the history
Fix/116 Rest of the `admin.js` content migrated
  • Loading branch information
faisal-alvi committed May 9, 2022
2 parents 70a1595 + 2e394f9 commit c9e62c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions assets/js/simple-local-avatars.js
Expand Up @@ -211,6 +211,9 @@ jQuery(document).ready(function ($) {
// 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 @@ -266,6 +269,46 @@ jQuery(document).ready(function ($) {
},
} );
}

/**
* Default avatar upload field listener in Settings -> Discussions.
*/
$simpleLocalAvatarDefault.click(function(e) {
e.preventDefault();
var _this = $(this);
var image = wp.media({
title: i10n_SimpleLocalAvatars.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();
}
});

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/class-simple-local-avatars.php
Expand Up @@ -650,14 +650,14 @@ public function admin_enqueue_scripts( $hook_suffix ) {
'i10n_SimpleLocalAvatars',
array(
'user_id' => $user_id,
'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ),
'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ),
'selectCrop' => __( 'Select avatar and Crop', 'simple-local-avatars' ),
'deleteNonce' => $this->remove_nonce,
'cacheNonce' => wp_create_nonce( 'sla_clear_cache_nonce' ),
'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ),
'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ),
'clearCacheError' => esc_html__( 'Something went wrong while clearing cache, please try again.', 'simple-local-avatars' ),
'insertMediaTitle' => esc_html__( 'Choose default avatar', 'simple-local-avatars' ),
'migrateFromWpUserAvatarSuccess' => __( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ),
'migrateFromWpUserAvatarFailure' => __( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ),
'migrateFromWpUserAvatarProgress' => __( 'Migration in progress.', 'simple-local-avatars' ),
Expand Down

0 comments on commit c9e62c9

Please sign in to comment.