| 1 |
/** |
| 2 |
* Add support for image uploads. |
| 3 |
* |
| 4 |
* @author Epiphyt |
| 5 |
* @license GPL2 |
| 6 |
* @package epiphyt\Embed_Privacy |
| 7 |
*/ |
| 8 |
jQuery( document ).ready( function( $ ) { |
| 9 |
// remove image |
| 10 |
$( document ).on( 'click', '.embed-privacy-remove-image', function( event ) { |
| 11 |
event.preventDefault(); |
| 12 |
|
| 13 |
var image_item = $( this ).closest( '.embed-privacy-image-item' ); |
| 14 |
|
| 15 |
image_item.find( '.embed-privacy-image-input-container, .embed-privacy-image-set-input-container' ).removeClass( 'embed-privacy-hidden' ); |
| 16 |
image_item.find( '.embed-privacy-image-container, .embed-privacy-image-set-container' ).addClass( 'embed-privacy-hidden' ); |
| 17 |
|
| 18 |
// reset value |
| 19 |
image_item.find( '.embed-privacy-image-input' ).val( '' ); |
| 20 |
image_item.find( '.embed-privacy-upload-input' ).val( '' ); |
| 21 |
// reset type |
| 22 |
image_item.find( '.embed-privacy-upload-input' ).attr( 'type', 'file' ); |
| 23 |
} ); |
| 24 |
|
| 25 |
// upload functionality single image |
| 26 |
$( document ).on( 'click', '.embed-privacy-image-upload', function( event ) { |
| 27 |
event.preventDefault(); |
| 28 |
|
| 29 |
var container = $( this ).closest( '.embed-privacy-image-item' ).find( '.embed-privacy-image-container' ); |
| 30 |
var id_field = $( this ).closest( '.embed-privacy-image-item' ).find( '.embed-privacy-image-input' ); |
| 31 |
var meta_image_frame = wp.media.frames.meta_image_frame = wp.media(); |
| 32 |
var upload_field = $( this ); |
| 33 |
|
| 34 |
meta_image_frame.on( 'select', function() { |
| 35 |
var media_attachment = meta_image_frame.state().get('selection').first().toJSON(); |
| 36 |
|
| 37 |
// clear the image container |
| 38 |
container.find( 'img' ).remove(); |
| 39 |
// add uploaded/selected image |
| 40 |
container.prepend( '<img src="' + ( typeof media_attachment.sizes !== 'undefined' ? typeof media_attachment.sizes.thumbnail !== 'undefined' ? media_attachment.sizes.thumbnail.url : media_attachment.sizes.full.url : media_attachment.url ) + '" alt="">' ); |
| 41 |
// don't hide the container anymore |
| 42 |
container.removeClass( 'embed-privacy-hidden' ); |
| 43 |
// store attachment ID |
| 44 |
id_field.val( media_attachment.id ); |
| 45 |
|
| 46 |
// hide upload field |
| 47 |
upload_field.parent().addClass( 'embed-privacy-hidden' ); |
| 48 |
} ); |
| 49 |
|
| 50 |
meta_image_frame.open(); |
| 51 |
} ); |
| 52 |
} ); |
| 53 |
|