2022-01-14 13:13:03 +01:00
|
|
|
(function( $ ) {
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// Tags name
|
|
|
|
|
var tagAttachmentId = 'input[name="' + sua_obj.input_name + '"]';
|
|
|
|
|
var tagAttachmentAvatar = '.sua-attachment-avatar';
|
|
|
|
|
var tagAttachmentDesc = '#sua-attachment-description';
|
|
|
|
|
var tagButtonAdd = '#btn-media-add';
|
|
|
|
|
var tagButtonRemove = '#btn-media-remove';
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// jQuery elements by tags
|
|
|
|
|
var elAttachmentId = $( tagAttachmentId );
|
|
|
|
|
var elAttachmentAvatar = $( tagAttachmentAvatar) ;
|
|
|
|
|
var elAttachmentDesc = $( tagAttachmentDesc );
|
|
|
|
|
var elButtonAdd = $( tagButtonAdd );
|
|
|
|
|
var elButtonRemove = $( tagButtonRemove );
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// WordPress default media sizes
|
|
|
|
|
var WPMediaSizes = [ 'full', 'large', 'medium', 'thumbnail' ];
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// Get default Src and default SrcSet
|
|
|
|
|
var defaultSrc = sua_obj.default_avatar_src;
|
|
|
|
|
var defaultSrcSet = sua_obj.default_avatar_srcset;
|
2022-01-14 13:13:03 +01:00
|
|
|
|
|
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
/*
|
|
|
|
|
* Update attachment
|
|
|
|
|
*
|
|
|
|
|
* @since 3.6
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
function updateAttachment( attachmentSrc = '', attachmentSrcSet = '', attachmentId = null ) {
|
|
|
|
|
|
|
|
|
|
// Change the image attributes
|
|
|
|
|
elAttachmentAvatar.attr({
|
|
|
|
|
'src': attachmentSrc,
|
|
|
|
|
'srcset': attachmentSrcSet
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Set attachment ID value
|
|
|
|
|
elAttachmentId.val( attachmentId === null ? '' : parseInt( attachmentId ) );
|
|
|
|
|
|
|
|
|
|
// Toggle class hidden
|
|
|
|
|
elAttachmentDesc.toggleClass( 'hidden' );
|
|
|
|
|
elButtonRemove.toggleClass( 'hidden' );
|
|
|
|
|
|
2022-01-14 13:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2022-04-02 10:26:41 +02:00
|
|
|
* Init functions
|
2022-01-14 13:13:03 +01:00
|
|
|
*
|
2022-04-02 10:26:41 +02:00
|
|
|
* @since 2.8
|
2022-01-14 13:13:03 +01:00
|
|
|
*/
|
2022-04-02 10:26:41 +02:00
|
|
|
$(function() {
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// Set click functions
|
2022-01-14 13:13:03 +01:00
|
|
|
$(document)
|
2022-04-02 10:26:41 +02:00
|
|
|
.on( 'click', tagButtonAdd, function() {
|
2022-01-14 13:13:03 +01:00
|
|
|
|
|
|
|
|
// Open WordPress Media Library
|
2022-04-02 10:26:41 +02:00
|
|
|
wp.media.editor.open();
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// WP Media Editor function
|
|
|
|
|
wp.media.editor.send.attachment = function( props, attachment ) {
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// Set attachment Src to default URL
|
|
|
|
|
var attachmentSrc = attachment.url;
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// If there is a smaller version I use it
|
|
|
|
|
for ( const WPMediaSize of WPMediaSizes ) {
|
|
|
|
|
if ( typeof attachment.sizes[WPMediaSize] !== 'undefined' && typeof attachment.sizes[WPMediaSize].url !== 'undefined' ) {
|
|
|
|
|
attachmentSrc = attachment.sizes[WPMediaSize].url;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// Update Attachment
|
|
|
|
|
updateAttachment( attachmentSrc, attachmentSrc, attachment.id );
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
}
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
})
|
|
|
|
|
.on( 'click', tagButtonRemove, function() {
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// Update Attachment
|
|
|
|
|
updateAttachment( defaultSrc, defaultSrcSet );
|
2022-01-14 13:13:03 +01:00
|
|
|
|
|
|
|
|
})
|
2022-04-02 10:26:41 +02:00
|
|
|
.on( 'click', tagAttachmentAvatar, function() {
|
2022-01-14 13:13:03 +01:00
|
|
|
|
2022-04-02 10:26:41 +02:00
|
|
|
// Trigger to add button
|
|
|
|
|
elButtonAdd.trigger( 'click' );
|
2022-01-14 13:13:03 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-07 09:44:11 +01:00
|
|
|
})( jQuery );
|