codemirror
3 years ago
datatables
3 years ago
admin-page.js
3 years ago
content-order-sort.js
3 years ago
custom-admin-menu.js
3 years ago
hide-admin-notices.js
3 years ago
jBox.all.min.js
3 years ago
jquery.jsticky.mod.js
3 years ago
jquery.jsticky.mod.min.js
3 years ago
jquery.mjs.nestedSortable.js
3 years ago
jquery.ui.touch-punch.min.js
3 years ago
js.cookie.min.js
3 years ago
media-replace.js
3 years ago
multiple-user-roles.js
3 years ago
public.js
3 years ago
toggle-hidden-menu.js
3 years ago
media-replace.js
73 lines
| 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 | } |