diff --git a/assets/js/simple-local-avatars.js b/assets/js/simple-local-avatars.js index 7c91d49a..e01eda47 100644 --- a/assets/js/simple-local-avatars.js +++ b/assets/js/simple-local-avatars.js @@ -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 = ''; @@ -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(); + } }); /** diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 8103e210..318d0f0d 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -650,7 +650,6 @@ 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, @@ -658,6 +657,7 @@ public function admin_enqueue_scripts( $hook_suffix ) { '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' ),