PluginProbe
Admin and Site Enhancements (ASE) / 5.2.0
Admin and Site Enhancements (ASE) v5.2.0
9.1.1 9.1.0 9.0.2 9.0.1 9.0.0 8.9.2 8.9.1 8.9.0 8.8.8 8.8.7 8.8.6 8.8.5 8.8.4 8.8.3 8.8.2 8.8.1 8.8.0 8.7.3 8.7.2 8.7.1 8.2.1 8.2.2 8.2.3 8.3.0 8.3.1 All 273 releases
admin-site-enhancements / assets / js / media-replace.js
media-replace.js
73 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function( $ ) {
2 'use strict';
3
4 $(document).ready( function() {
5
6 // Bypass browser cache by appending query parameter to preview image src
7 var imgSrcUrl = $('.wp_attachment_image .thumbnail').attr('src');
8 $('.wp_attachment_image .thumbnail').attr('src', imgSrcUrl + `?v=${new Date().getTime()}`);
9
10 // Place the Replace Media section after the side metaboxes
11 $('#media-replace-div').appendTo('#postbox-container-1');
12
13 // For 'Replace Media' buttom on the edit screen of each media/attachment
14 $('#asenha-media-replace').click( function() {
15 asenha_replace_media();
16 });
17
18 // https://stephanwagner.me/jBox
19 // var mediaReplaceModal = new jBox('Modal', {
20 // content: 'Content here...'
21 // });
22
23 // Open jBox modal
24 // $('.asenha-media-replace').click(function(e) {
25 // e.preventDefault();
26 // mediaReplaceModal.open();
27 // });
28
29 });
30
31 })( jQuery );
32
33 // let asenhaMRVars = asenhaMR;
34
35 function asenha_replace_media() {
36
37 // https://codex.wordpress.org/Javascript_Reference/wp.media
38 // https://github.com/ericandrewlewis/wp-media-javascript-guide
39
40 // Instantiate the media frame
41 mediaFrame = wp.media({
42 title: 'Select New Media File',
43 button: {
44 text: 'Perform Replacement'
45 },
46 multiple: false // Enable/disable multiple select
47 });
48
49 // When an image is selected in the media frame...
50 mediaFrame.on('select', function() {
51
52 // Get media attachment details from the frame state
53 var attachment = mediaFrame.state().get('selection').first().toJSON();
54 console.log( attachment );
55
56 // Send the attachment id to our hidden input
57 jQuery('#new-attachment-id').val(attachment.id);
58
59 if (jQuery("#new-attachment-id").closest('.media-modal').length) {
60 // Do nothing. Media frame is still open.
61 } else {
62 // "Perform Replacement" button has been clicked. Submit the edit form, which includes 'new-attachment-id'
63 jQuery("#new-attachment-id").closest("form").submit();
64 }
65 });
66
67 // Open the media dialog and store it in a variable
68 var mediaFrameEl = jQuery(mediaFrame.open().el);
69
70 // Open the "Upload files" tab on load
71 mediaFrameEl.find('#menu-item-upload').click();
72
73 }