| 1 |
jQuery(document).ready(function(){ |
| 2 |
|
| 3 |
if( typeof wp != "undefined" && typeof wp.media != "undefined" ){ |
| 4 |
|
| 5 |
// Uploading files |
| 6 |
var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id |
| 7 |
|
| 8 |
|
| 9 |
jQuery(document).on( 'click', '.wck_upload_button', function( event ){ |
| 10 |
event.preventDefault(); |
| 11 |
|
| 12 |
var set_to_post_id = jQuery( this ).data( 'post_id' ); // Set this |
| 13 |
|
| 14 |
|
| 15 |
var file_frame; |
| 16 |
var uploadInputId = jQuery( this ).data( 'upload_input' ); |
| 17 |
var uploadInFront = jQuery( this ).data( 'upload_in_backend' ); |
| 18 |
var attachToPost = jQuery( this ).data( 'attach_to_post' ); |
| 19 |
var uploadButton = jQuery( this ); |
| 20 |
|
| 21 |
/* remove set_to_post_id value if we do not want to attach to post */ |
| 22 |
if( attachToPost != true ){ |
| 23 |
set_to_post_id = ''; |
| 24 |
} |
| 25 |
|
| 26 |
/* set default tab to upload file */ |
| 27 |
wp.media.controller.Library.prototype.defaults.contentUserSetting = false; |
| 28 |
if( uploadInFront != true ) |
| 29 |
wp.media.controller.Library.prototype.defaults.router = false; |
| 30 |
wp.media.controller.Library.prototype.defaults.searchable = true; |
| 31 |
wp.media.controller.Library.prototype.defaults.sortable = false; |
| 32 |
|
| 33 |
// If the media frame already exists, reopen it. |
| 34 |
if ( file_frame ) { |
| 35 |
// Set the post ID to what we want |
| 36 |
file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); |
| 37 |
// Open frame |
| 38 |
file_frame.open(); |
| 39 |
return; |
| 40 |
} else { |
| 41 |
// Set the wp.media post id so the uploader grabs the ID we want when initialised |
| 42 |
wp.media.model.settings.post.id = set_to_post_id; |
| 43 |
} |
| 44 |
|
| 45 |
// Create the media frame. |
| 46 |
file_frame = wp.media.frames.file_frame = wp.media({ |
| 47 |
title: jQuery( this ).data( 'uploader_title' ), |
| 48 |
button: { |
| 49 |
text: jQuery( this ).data( 'uploader_button_text' ), |
| 50 |
}, |
| 51 |
multiple: jQuery( this ).data( 'multiple_upload' ) // Set to true to allow multiple files to be selected |
| 52 |
}); |
| 53 |
|
| 54 |
/* restrict allowed file types if we have to */ |
| 55 |
allowedTypes = jQuery( this ).data( 'allowed_types' ); |
| 56 |
if( allowedTypes != undefined ) |
| 57 |
file_frame.uploader.options.uploader['params']['allowed_type'] = allowedTypes; |
| 58 |
|
| 59 |
// When an image is selected, run a callback. |
| 60 |
file_frame.on( 'select', function() { |
| 61 |
// We set multiple to false so only get one image from the uploader |
| 62 |
attachments = file_frame.state().get('selection').toJSON(); |
| 63 |
var attids = []; |
| 64 |
|
| 65 |
for( var i=0;i < attachments.length; i++ ){ |
| 66 |
// Do something with attachment.id and/or attachment.url here |
| 67 |
attids.push( attachments[i].id ); |
| 68 |
result = '<div class="upload-field-details" id="'+ uploadInputId +'_info_container" data-attachment_id="'+ attachments[i].id +'">'; |
| 69 |
if( attachments[i].sizes != undefined ){ |
| 70 |
if( attachments[i].sizes.thumbnail != undefined ) |
| 71 |
thumb = attachments[i].sizes.thumbnail; |
| 72 |
else |
| 73 |
thumb = attachments[i].sizes.full; |
| 74 |
thumbnailUrl = thumb.url; |
| 75 |
} |
| 76 |
else{ |
| 77 |
thumbnailUrl = attachments[i].icon; |
| 78 |
} |
| 79 |
result += '<div class="file-thumb"><img width="80" height="80" src="'+ thumbnailUrl +'"/></div>'; |
| 80 |
result += '<p><span class="file-name">'+attachments[i].filename+'</span><span class="file-type">'+attachments[i].mime +'</span><span class="wck-remove-upload">Remove</span></p></div>'; |
| 81 |
|
| 82 |
/* if multiple upload false remove previous upload details */ |
| 83 |
if( uploadButton.data( 'multiple_upload' ) == false ){ |
| 84 |
jQuery( '.upload-field-details', uploadButton.parent() ).remove(); |
| 85 |
} |
| 86 |
|
| 87 |
uploadButton.before( result ); |
| 88 |
|
| 89 |
} |
| 90 |
/* turn into comma separated string */ |
| 91 |
attids = attids.join(','); |
| 92 |
jQuery( 'input[id="'+uploadInputId+'"]', uploadButton.parent() ).val( attids ); |
| 93 |
|
| 94 |
// Restore the main post ID |
| 95 |
wp.media.model.settings.post.id = wp_media_post_id; |
| 96 |
}); |
| 97 |
|
| 98 |
// Finally, open the modal |
| 99 |
file_frame.open(); |
| 100 |
// remove tabs from the top ( this is done higher in the code when setting router to false ) |
| 101 |
//jQuery('.media-frame-router').remove(); |
| 102 |
|
| 103 |
if( jQuery( this ).data( 'uploader_logged_in' ) == undefined ){ |
| 104 |
jQuery('.media-frame-title').append('<style type="text/css">label.setting{display:none !important;}</style>'); |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
// Restore the main ID when the add media button is pressed |
| 109 |
jQuery('a.add_media').on('click', function() { |
| 110 |
wp.media.model.settings.post.id = wp_media_post_id; |
| 111 |
}); |
| 112 |
|
| 113 |
jQuery(document).on('click', '.wck-remove-upload', function(e){ |
| 114 |
/* update hidden input */ |
| 115 |
removedAttachement = jQuery(this).parent().parent('.upload-field-details').data('attachment_id'); |
| 116 |
upload_input = jQuery(this).parent().parent().parent().children('input[type="hidden"]'); |
| 117 |
uploadAttachemnts = upload_input.val(); |
| 118 |
uploadAttachemntsArray = uploadAttachemnts.split( ',' ); |
| 119 |
newuploadAttachments = []; |
| 120 |
for( var i=0;i < uploadAttachemntsArray.length; i++ ){ |
| 121 |
if( uploadAttachemntsArray[i] != removedAttachement ) |
| 122 |
newuploadAttachments.push(uploadAttachemntsArray[i]); |
| 123 |
} |
| 124 |
newuploadAttachments = newuploadAttachments.join(','); |
| 125 |
upload_input.val(newuploadAttachments); |
| 126 |
|
| 127 |
/* remove the attachment details */ |
| 128 |
jQuery(this).parent().parent('.upload-field-details').remove(); |
| 129 |
}); |
| 130 |
} |
| 131 |
}); |