author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
5 | 1 |
/* global plupload, pluploadL10n, ajaxurl, post_id, wpUploaderInit, deleteUserSetting, setUserSetting, getUserSetting, shortform */ |
0 | 2 |
var topWin = window.dialogArguments || opener || parent || top, uploader, uploader_init; |
3 |
||
16 | 4 |
// Progress and success handlers for media multi uploads. |
5 |
function fileQueued( fileObj ) { |
|
6 |
// Get rid of unused form. |
|
7 |
jQuery( '.media-blank' ).remove(); |
|
0 | 8 |
|
16 | 9 |
var items = jQuery( '#media-items' ).children(), postid = post_id || 0; |
0 | 10 |
|
16 | 11 |
// Collapse a single item. |
0 | 12 |
if ( items.length == 1 ) { |
16 | 13 |
items.removeClass( 'open' ).find( '.slidetoggle' ).slideUp( 200 ); |
0 | 14 |
} |
16 | 15 |
// Create a progress bar containing the filename. |
16 |
jQuery( '<div class="media-item">' ) |
|
0 | 17 |
.attr( 'id', 'media-item-' + fileObj.id ) |
16 | 18 |
.addClass( 'child-of-' + postid ) |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
19 |
.append( jQuery( '<div class="filename original">' ).text( ' ' + fileObj.name ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
20 |
'<div class="progress"><div class="percent">0%</div><div class="bar"></div></div>' ) |
16 | 21 |
.appendTo( jQuery( '#media-items' ) ); |
0 | 22 |
|
16 | 23 |
// Disable submit. |
24 |
jQuery( '#insert-gallery' ).prop( 'disabled', true ); |
|
0 | 25 |
} |
26 |
||
27 |
function uploadStart() { |
|
28 |
try { |
|
29 |
if ( typeof topWin.tb_remove != 'undefined' ) |
|
16 | 30 |
topWin.jQuery( '#TB_overlay' ).unbind( 'click', topWin.tb_remove ); |
31 |
} catch( e ){} |
|
0 | 32 |
|
33 |
return true; |
|
34 |
} |
|
35 |
||
16 | 36 |
function uploadProgress( up, file ) { |
37 |
var item = jQuery( '#media-item-' + file.id ); |
|
0 | 38 |
|
16 | 39 |
jQuery( '.bar', item ).width( ( 200 * file.loaded ) / file.size ); |
40 |
jQuery( '.percent', item ).html( file.percent + '%' ); |
|
0 | 41 |
} |
42 |
||
16 | 43 |
// Check to see if a large file failed to upload. |
5 | 44 |
function fileUploading( up, file ) { |
45 |
var hundredmb = 100 * 1024 * 1024, |
|
46 |
max = parseInt( up.settings.max_file_size, 10 ); |
|
0 | 47 |
|
48 |
if ( max > hundredmb && file.size > hundredmb ) { |
|
5 | 49 |
setTimeout( function() { |
16 | 50 |
if ( file.status < 3 && file.loaded === 0 ) { // Not uploading. |
5 | 51 |
wpFileError( file, pluploadL10n.big_upload_failed.replace( '%1$s', '<a class="uploader-html" href="#">' ).replace( '%2$s', '</a>' ) ); |
16 | 52 |
up.stop(); // Stop the whole queue. |
5 | 53 |
up.removeFile( file ); |
16 | 54 |
up.start(); // Restart the queue. |
0 | 55 |
} |
16 | 56 |
}, 10000 ); // Wait for 10 seconds for the file to start uploading. |
0 | 57 |
} |
58 |
} |
|
59 |
||
60 |
function updateMediaForm() { |
|
16 | 61 |
var items = jQuery( '#media-items' ).children(); |
0 | 62 |
|
16 | 63 |
// Just one file, no need for collapsible part. |
0 | 64 |
if ( items.length == 1 ) { |
16 | 65 |
items.addClass( 'open' ).find( '.slidetoggle' ).show(); |
66 |
jQuery( '.insert-gallery' ).hide(); |
|
0 | 67 |
} else if ( items.length > 1 ) { |
16 | 68 |
items.removeClass( 'open' ); |
5 | 69 |
// Only show Gallery/Playlist buttons when there are at least two files. |
16 | 70 |
jQuery( '.insert-gallery' ).show(); |
0 | 71 |
} |
72 |
||
73 |
// Only show Save buttons when there is at least one file. |
|
16 | 74 |
if ( items.not( '.media-blank' ).length > 0 ) |
75 |
jQuery( '.savebutton' ).show(); |
|
0 | 76 |
else |
16 | 77 |
jQuery( '.savebutton' ).hide(); |
0 | 78 |
} |
79 |
||
16 | 80 |
function uploadSuccess( fileObj, serverData ) { |
81 |
var item = jQuery( '#media-item-' + fileObj.id ); |
|
0 | 82 |
|
16 | 83 |
// On success serverData should be numeric, |
84 |
// fix bug in html4 runtime returning the serverData wrapped in a <pre> tag. |
|
85 |
if ( typeof serverData === 'string' ) { |
|
86 |
serverData = serverData.replace( /^<pre>(\d+)<\/pre>$/, '$1' ); |
|
87 |
||
88 |
// If async-upload returned an error message, place it in the media item div and return. |
|
89 |
if ( /media-upload-error|error-div/.test( serverData ) ) { |
|
90 |
item.html( serverData ); |
|
91 |
return; |
|
92 |
} |
|
0 | 93 |
} |
94 |
||
16 | 95 |
item.find( '.percent' ).html( pluploadL10n.crunching ); |
96 |
||
97 |
prepareMediaItem( fileObj, serverData ); |
|
0 | 98 |
updateMediaForm(); |
99 |
||
100 |
// Increment the counter. |
|
16 | 101 |
if ( post_id && item.hasClass( 'child-of-' + post_id ) ) { |
102 |
jQuery( '#attachments-count' ).text( 1 * jQuery( '#attachments-count' ).text() + 1 ); |
|
103 |
} |
|
0 | 104 |
} |
105 |
||
5 | 106 |
function setResize( arg ) { |
0 | 107 |
if ( arg ) { |
5 | 108 |
if ( window.resize_width && window.resize_height ) { |
109 |
uploader.settings.resize = { |
|
110 |
enabled: true, |
|
111 |
width: window.resize_width, |
|
112 |
height: window.resize_height, |
|
113 |
quality: 100 |
|
114 |
}; |
|
115 |
} else { |
|
0 | 116 |
uploader.settings.multipart_params.image_resize = true; |
5 | 117 |
} |
0 | 118 |
} else { |
5 | 119 |
delete( uploader.settings.multipart_params.image_resize ); |
0 | 120 |
} |
121 |
} |
|
122 |
||
16 | 123 |
function prepareMediaItem( fileObj, serverData ) { |
124 |
var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery( '#media-item-' + fileObj.id ); |
|
0 | 125 |
if ( f == 2 && shortform > 2 ) |
126 |
f = shortform; |
|
127 |
||
128 |
try { |
|
129 |
if ( typeof topWin.tb_remove != 'undefined' ) |
|
16 | 130 |
topWin.jQuery( '#TB_overlay' ).click( topWin.tb_remove ); |
131 |
} catch( e ){} |
|
0 | 132 |
|
16 | 133 |
if ( isNaN( serverData ) || !serverData ) { |
134 |
// Old style: Append the HTML returned by the server -- thumbnail and form inputs. |
|
135 |
item.append( serverData ); |
|
136 |
prepareMediaItemInit( fileObj ); |
|
137 |
} else { |
|
138 |
// New style: server data is just the attachment ID, fetch the thumbnail and form html from the server. |
|
139 |
item.load( 'async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit( fileObj );updateMediaForm();}); |
|
0 | 140 |
} |
141 |
} |
|
142 |
||
16 | 143 |
function prepareMediaItemInit( fileObj ) { |
144 |
var item = jQuery( '#media-item-' + fileObj.id ); |
|
145 |
// Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename. |
|
146 |
jQuery( '.thumbnail', item ).clone().attr( 'class', 'pinkynail toggle' ).prependTo( item ); |
|
0 | 147 |
|
16 | 148 |
// Replace the original filename with the new (unique) one assigned during upload. |
149 |
jQuery( '.filename.original', item ).replaceWith( jQuery( '.filename.new', item ) ); |
|
0 | 150 |
|
16 | 151 |
// Bind Ajax to the new Delete button. |
19 | 152 |
jQuery( 'a.delete', item ).on( 'click', function(){ |
16 | 153 |
// Tell the server to delete it. TODO: Handle exceptions. |
0 | 154 |
jQuery.ajax({ |
155 |
url: ajaxurl, |
|
156 |
type: 'post', |
|
157 |
success: deleteSuccess, |
|
158 |
error: deleteError, |
|
159 |
id: fileObj.id, |
|
160 |
data: { |
|
16 | 161 |
id : this.id.replace(/[^0-9]/g, '' ), |
0 | 162 |
action : 'trash-post', |
16 | 163 |
_ajax_nonce : this.href.replace(/^.*wpnonce=/,'' ) |
0 | 164 |
} |
165 |
}); |
|
166 |
return false; |
|
167 |
}); |
|
168 |
||
16 | 169 |
// Bind Ajax to the new Undo button. |
19 | 170 |
jQuery( 'a.undo', item ).on( 'click', function(){ |
16 | 171 |
// Tell the server to untrash it. TODO: Handle exceptions. |
0 | 172 |
jQuery.ajax({ |
173 |
url: ajaxurl, |
|
174 |
type: 'post', |
|
175 |
id: fileObj.id, |
|
176 |
data: { |
|
16 | 177 |
id : this.id.replace(/[^0-9]/g,'' ), |
0 | 178 |
action: 'untrash-post', |
16 | 179 |
_ajax_nonce: this.href.replace(/^.*wpnonce=/,'' ) |
0 | 180 |
}, |
5 | 181 |
success: function( ){ |
182 |
var type, |
|
16 | 183 |
item = jQuery( '#media-item-' + fileObj.id ); |
0 | 184 |
|
16 | 185 |
if ( type = jQuery( '#type-of-' + fileObj.id ).val() ) |
186 |
jQuery( '#' + type + '-counter' ).text( jQuery( '#' + type + '-counter' ).text()-0+1 ); |
|
0 | 187 |
|
16 | 188 |
if ( post_id && item.hasClass( 'child-of-'+post_id ) ) |
189 |
jQuery( '#attachments-count' ).text( jQuery( '#attachments-count' ).text()-0+1 ); |
|
0 | 190 |
|
16 | 191 |
jQuery( '.filename .trashnotice', item ).remove(); |
192 |
jQuery( '.filename .title', item ).css( 'font-weight','normal' ); |
|
193 |
jQuery( 'a.undo', item ).addClass( 'hidden' ); |
|
194 |
jQuery( '.menu_order_input', item ).show(); |
|
195 |
item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery( this ).css({backgroundColor:''}); } }).removeClass( 'undo' ); |
|
0 | 196 |
} |
197 |
}); |
|
198 |
return false; |
|
199 |
}); |
|
200 |
||
16 | 201 |
// Open this item if it says to start open (e.g. to display an error). |
202 |
jQuery( '#media-item-' + fileObj.id + '.startopen' ).removeClass( 'startopen' ).addClass( 'open' ).find( 'slidetoggle' ).fadeIn(); |
|
0 | 203 |
} |
204 |
||
16 | 205 |
// Generic error message. |
206 |
function wpQueueError( message ) { |
|
207 |
jQuery( '#media-upload-error' ).show().html( '<div class="error"><p>' + message + '</p></div>' ); |
|
0 | 208 |
} |
209 |
||
16 | 210 |
// File-specific error messages. |
211 |
function wpFileError( fileObj, message ) { |
|
212 |
itemAjaxError( fileObj.id, message ); |
|
0 | 213 |
} |
214 |
||
16 | 215 |
function itemAjaxError( id, message ) { |
216 |
var item = jQuery( '#media-item-' + id ), filename = item.find( '.filename' ).text(), last_err = item.data( 'last-err' ); |
|
0 | 217 |
|
16 | 218 |
if ( last_err == id ) // Prevent firing an error for the same file twice. |
0 | 219 |
return; |
220 |
||
16 | 221 |
item.html( '<div class="error-div">' + |
5 | 222 |
'<a class="dismiss" href="#">' + pluploadL10n.dismiss + '</a>' + |
16 | 223 |
'<strong>' + pluploadL10n.error_uploading.replace( '%s', jQuery.trim( filename )) + '</strong> ' + |
5 | 224 |
message + |
16 | 225 |
'</div>' ).data( 'last-err', id ); |
0 | 226 |
} |
227 |
||
16 | 228 |
function deleteSuccess( data ) { |
5 | 229 |
var type, id, item; |
0 | 230 |
if ( data == '-1' ) |
16 | 231 |
return itemAjaxError( this.id, 'You do not have permission. Has your session expired?' ); |
0 | 232 |
|
233 |
if ( data == '0' ) |
|
16 | 234 |
return itemAjaxError( this.id, 'Could not be deleted. Has it been deleted already?' ); |
0 | 235 |
|
5 | 236 |
id = this.id; |
16 | 237 |
item = jQuery( '#media-item-' + id ); |
0 | 238 |
|
239 |
// Decrement the counters. |
|
16 | 240 |
if ( type = jQuery( '#type-of-' + id ).val() ) |
241 |
jQuery( '#' + type + '-counter' ).text( jQuery( '#' + type + '-counter' ).text() - 1 ); |
|
0 | 242 |
|
16 | 243 |
if ( post_id && item.hasClass( 'child-of-'+post_id ) ) |
244 |
jQuery( '#attachments-count' ).text( jQuery( '#attachments-count' ).text() - 1 ); |
|
0 | 245 |
|
16 | 246 |
if ( jQuery( 'form.type-form #media-items' ).children().length == 1 && jQuery( '.hidden', '#media-items' ).length > 0 ) { |
247 |
jQuery( '.toggle' ).toggle(); |
|
248 |
jQuery( '.slidetoggle' ).slideUp( 200 ).siblings().removeClass( 'hidden' ); |
|
0 | 249 |
} |
250 |
||
251 |
// Vanish it. |
|
16 | 252 |
jQuery( '.toggle', item ).toggle(); |
253 |
jQuery( '.slidetoggle', item ).slideUp( 200 ).siblings().removeClass( 'hidden' ); |
|
254 |
item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass( 'undo' ); |
|
0 | 255 |
|
16 | 256 |
jQuery( '.filename:empty', item ).remove(); |
257 |
jQuery( '.filename .title', item ).css( 'font-weight','bold' ); |
|
258 |
jQuery( '.filename', item ).append( '<span class="trashnotice"> ' + pluploadL10n.deleted + ' </span>' ).siblings( 'a.toggle' ).hide(); |
|
259 |
jQuery( '.filename', item ).append( jQuery( 'a.undo', item ).removeClass( 'hidden' ) ); |
|
260 |
jQuery( '.menu_order_input', item ).hide(); |
|
0 | 261 |
|
262 |
return; |
|
263 |
} |
|
264 |
||
5 | 265 |
function deleteError() { |
0 | 266 |
} |
267 |
||
268 |
function uploadComplete() { |
|
16 | 269 |
jQuery( '#insert-gallery' ).prop( 'disabled', false ); |
0 | 270 |
} |
271 |
||
16 | 272 |
function switchUploader( s ) { |
0 | 273 |
if ( s ) { |
16 | 274 |
deleteUserSetting( 'uploader' ); |
275 |
jQuery( '.media-upload-form' ).removeClass( 'html-uploader' ); |
|
0 | 276 |
|
16 | 277 |
if ( typeof( uploader ) == 'object' ) |
0 | 278 |
uploader.refresh(); |
279 |
} else { |
|
16 | 280 |
setUserSetting( 'uploader', '1' ); // 1 == html uploader. |
281 |
jQuery( '.media-upload-form' ).addClass( 'html-uploader' ); |
|
0 | 282 |
} |
283 |
} |
|
284 |
||
16 | 285 |
function uploadError( fileObj, errorCode, message, up ) { |
0 | 286 |
var hundredmb = 100 * 1024 * 1024, max; |
287 |
||
16 | 288 |
switch ( errorCode ) { |
0 | 289 |
case plupload.FAILED: |
16 | 290 |
wpFileError( fileObj, pluploadL10n.upload_failed ); |
0 | 291 |
break; |
292 |
case plupload.FILE_EXTENSION_ERROR: |
|
16 | 293 |
wpFileExtensionError( up, fileObj, pluploadL10n.invalid_filetype ); |
0 | 294 |
break; |
295 |
case plupload.FILE_SIZE_ERROR: |
|
16 | 296 |
uploadSizeError( up, fileObj ); |
0 | 297 |
break; |
298 |
case plupload.IMAGE_FORMAT_ERROR: |
|
16 | 299 |
wpFileError( fileObj, pluploadL10n.not_an_image ); |
0 | 300 |
break; |
301 |
case plupload.IMAGE_MEMORY_ERROR: |
|
16 | 302 |
wpFileError( fileObj, pluploadL10n.image_memory_exceeded ); |
0 | 303 |
break; |
304 |
case plupload.IMAGE_DIMENSIONS_ERROR: |
|
16 | 305 |
wpFileError( fileObj, pluploadL10n.image_dimensions_exceeded ); |
0 | 306 |
break; |
307 |
case plupload.GENERIC_ERROR: |
|
16 | 308 |
wpQueueError( pluploadL10n.upload_failed ); |
0 | 309 |
break; |
310 |
case plupload.IO_ERROR: |
|
16 | 311 |
max = parseInt( up.settings.filters.max_file_size, 10 ); |
0 | 312 |
|
16 | 313 |
if ( max > hundredmb && fileObj.size > hundredmb ) { |
314 |
wpFileError( fileObj, pluploadL10n.big_upload_failed.replace( '%1$s', '<a class="uploader-html" href="#">' ).replace( '%2$s', '</a>' ) ); |
|
315 |
} else { |
|
316 |
wpQueueError( pluploadL10n.io_error ); |
|
317 |
} |
|
318 |
||
0 | 319 |
break; |
320 |
case plupload.HTTP_ERROR: |
|
16 | 321 |
wpQueueError( pluploadL10n.http_error ); |
0 | 322 |
break; |
323 |
case plupload.INIT_ERROR: |
|
16 | 324 |
jQuery( '.media-upload-form' ).addClass( 'html-uploader' ); |
0 | 325 |
break; |
326 |
case plupload.SECURITY_ERROR: |
|
16 | 327 |
wpQueueError( pluploadL10n.security_error ); |
0 | 328 |
break; |
329 |
/* case plupload.UPLOAD_ERROR.UPLOAD_STOPPED: |
|
330 |
case plupload.UPLOAD_ERROR.FILE_CANCELLED: |
|
16 | 331 |
jQuery( '#media-item-' + fileObj.id ).remove(); |
0 | 332 |
break;*/ |
333 |
default: |
|
16 | 334 |
wpFileError( fileObj, pluploadL10n.default_error ); |
0 | 335 |
} |
336 |
} |
|
337 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
function uploadSizeError( up, file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
var message, errorDiv; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
|
16 | 341 |
message = pluploadL10n.file_exceeds_size_limit.replace( '%s', file.name ); |
0 | 342 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
// Construct the error div. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
errorDiv = jQuery( '<div />' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
.attr( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
'id': 'media-item-' + file.id, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
'class': 'media-item error' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
} ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
.append( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
jQuery( '<p />' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
.text( message ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
); |
0 | 353 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
// Append the error. |
16 | 355 |
jQuery( '#media-items' ).append( errorDiv ); |
356 |
up.removeFile( file ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
function wpFileExtensionError( up, file, message ) { |
16 | 360 |
jQuery( '#media-items' ).append( '<div id="media-item-' + file.id + '" class="media-item error"><p>' + message + '</p></div>' ); |
361 |
up.removeFile( file ); |
|
0 | 362 |
} |
363 |
||
18 | 364 |
/** |
365 |
* Copies the attachment URL to the clipboard. |
|
366 |
* |
|
367 |
* @since 5.8.0 |
|
368 |
* |
|
369 |
* @param {MouseEvent} event A click event. |
|
370 |
* |
|
371 |
* @return {void} |
|
372 |
*/ |
|
373 |
function copyAttachmentUploadURLClipboard() { |
|
374 |
var clipboard = new ClipboardJS( '.copy-attachment-url' ), |
|
375 |
successTimeout; |
|
376 |
||
377 |
clipboard.on( 'success', function( event ) { |
|
378 |
var triggerElement = jQuery( event.trigger ), |
|
379 |
successElement = jQuery( '.success', triggerElement.closest( '.copy-to-clipboard-container' ) ); |
|
380 |
||
381 |
// Clear the selection and move focus back to the trigger. |
|
382 |
event.clearSelection(); |
|
383 |
// Show success visual feedback. |
|
384 |
clearTimeout( successTimeout ); |
|
385 |
successElement.removeClass( 'hidden' ); |
|
386 |
// Hide success visual feedback after 3 seconds since last success. |
|
387 |
successTimeout = setTimeout( function() { |
|
388 |
successElement.addClass( 'hidden' ); |
|
389 |
}, 3000 ); |
|
390 |
// Handle success audible feedback. |
|
391 |
wp.a11y.speak( pluploadL10n.file_url_copied ); |
|
392 |
} ); |
|
393 |
} |
|
394 |
||
16 | 395 |
jQuery( document ).ready( function( $ ) { |
18 | 396 |
copyAttachmentUploadURLClipboard(); |
16 | 397 |
var tryAgainCount = {}; |
398 |
var tryAgain; |
|
0 | 399 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
400 |
$( '.media-upload-form' ).on( 'click.uploader', function( e ) { |
16 | 401 |
var target = $( e.target ), tr, c; |
0 | 402 |
|
16 | 403 |
if ( target.is( 'input[type="radio"]' ) ) { // Remember the last used image size and alignment. |
404 |
tr = target.closest( 'tr' ); |
|
0 | 405 |
|
16 | 406 |
if ( tr.hasClass( 'align' ) ) |
407 |
setUserSetting( 'align', target.val() ); |
|
408 |
else if ( tr.hasClass( 'image-size' ) ) |
|
409 |
setUserSetting( 'imgsize', target.val() ); |
|
410 |
||
411 |
} else if ( target.is( 'button.button' ) ) { // Remember the last used image link url. |
|
0 | 412 |
c = e.target.className || ''; |
16 | 413 |
c = c.match( /url([^ '"]+)/ ); |
0 | 414 |
|
415 |
if ( c && c[1] ) { |
|
16 | 416 |
setUserSetting( 'urlbutton', c[1] ); |
417 |
target.siblings( '.urlfield' ).val( target.data( 'link-url' ) ); |
|
0 | 418 |
} |
16 | 419 |
} else if ( target.is( 'a.dismiss' ) ) { |
420 |
target.parents( '.media-item' ).fadeOut( 200, function() { |
|
421 |
$( this ).remove(); |
|
422 |
} ); |
|
423 |
} else if ( target.is( '.upload-flash-bypass a' ) || target.is( 'a.uploader-html' ) ) { // Switch uploader to html4. |
|
424 |
$( '#media-items, p.submit, span.big-file-warning' ).css( 'display', 'none' ); |
|
425 |
switchUploader( 0 ); |
|
0 | 426 |
e.preventDefault(); |
16 | 427 |
} else if ( target.is( '.upload-html-bypass a' ) ) { // Switch uploader to multi-file. |
428 |
$( '#media-items, p.submit, span.big-file-warning' ).css( 'display', '' ); |
|
429 |
switchUploader( 1 ); |
|
0 | 430 |
e.preventDefault(); |
16 | 431 |
} else if ( target.is( 'a.describe-toggle-on' ) ) { // Show. |
432 |
target.parent().addClass( 'open' ); |
|
433 |
target.siblings( '.slidetoggle' ).fadeIn( 250, function() { |
|
434 |
var S = $( window ).scrollTop(), |
|
435 |
H = $( window ).height(), |
|
436 |
top = $( this ).offset().top, |
|
437 |
h = $( this ).height(), |
|
438 |
b, |
|
439 |
B; |
|
0 | 440 |
|
441 |
if ( H && top && h ) { |
|
442 |
b = top + h; |
|
443 |
B = S + H; |
|
444 |
||
445 |
if ( b > B ) { |
|
446 |
if ( b - B < top - S ) |
|
16 | 447 |
window.scrollBy( 0, ( b - B ) + 10 ); |
0 | 448 |
else |
16 | 449 |
window.scrollBy( 0, top - S - 40 ); |
0 | 450 |
} |
451 |
} |
|
16 | 452 |
} ); |
453 |
||
0 | 454 |
e.preventDefault(); |
16 | 455 |
} else if ( target.is( 'a.describe-toggle-off' ) ) { // Hide. |
456 |
target.siblings( '.slidetoggle' ).fadeOut( 250, function() { |
|
457 |
target.parent().removeClass( 'open' ); |
|
458 |
} ); |
|
459 |
||
0 | 460 |
e.preventDefault(); |
461 |
} |
|
462 |
}); |
|
463 |
||
16 | 464 |
// Attempt to create image sub-sizes when an image was uploaded successfully |
465 |
// but the server responded with an HTTP 5xx error. |
|
466 |
tryAgain = function( up, error ) { |
|
467 |
var file = error.file; |
|
468 |
var times; |
|
469 |
var id; |
|
5 | 470 |
|
16 | 471 |
if ( ! error || ! error.responseHeaders ) { |
472 |
wpQueueError( pluploadL10n.http_error_image ); |
|
473 |
return; |
|
474 |
} |
|
5 | 475 |
|
16 | 476 |
id = error.responseHeaders.match( /x-wp-upload-attachment-id:\s*(\d+)/i ); |
477 |
||
478 |
if ( id && id[1] ) { |
|
479 |
id = id[1]; |
|
480 |
} else { |
|
481 |
wpQueueError( pluploadL10n.http_error_image ); |
|
482 |
return; |
|
5 | 483 |
} |
484 |
||
16 | 485 |
times = tryAgainCount[ file.id ]; |
486 |
||
487 |
if ( times && times > 4 ) { |
|
488 |
/* |
|
489 |
* The file may have been uploaded and attachment post created, |
|
490 |
* but post-processing and resizing failed... |
|
491 |
* Do a cleanup then tell the user to scale down the image and upload it again. |
|
492 |
*/ |
|
493 |
$.ajax({ |
|
494 |
type: 'post', |
|
495 |
url: ajaxurl, |
|
496 |
dataType: 'json', |
|
497 |
data: { |
|
498 |
action: 'media-create-image-subsizes', |
|
499 |
_wpnonce: wpUploaderInit.multipart_params._wpnonce, |
|
500 |
attachment_id: id, |
|
501 |
_wp_upload_failed_cleanup: true, |
|
502 |
} |
|
503 |
}); |
|
504 |
||
505 |
if ( error.message && ( error.status < 500 || error.status >= 600 ) ) { |
|
506 |
wpQueueError( error.message ); |
|
507 |
} else { |
|
508 |
wpQueueError( pluploadL10n.http_error_image ); |
|
509 |
} |
|
510 |
||
511 |
return; |
|
512 |
} |
|
513 |
||
514 |
if ( ! times ) { |
|
515 |
tryAgainCount[ file.id ] = 1; |
|
516 |
} else { |
|
517 |
tryAgainCount[ file.id ] = ++times; |
|
518 |
} |
|
0 | 519 |
|
16 | 520 |
// Try to create the missing image sizes. |
521 |
$.ajax({ |
|
522 |
type: 'post', |
|
523 |
url: ajaxurl, |
|
524 |
dataType: 'json', |
|
525 |
data: { |
|
526 |
action: 'media-create-image-subsizes', |
|
527 |
_wpnonce: wpUploaderInit.multipart_params._wpnonce, |
|
528 |
attachment_id: id, |
|
529 |
_legacy_support: 'true', |
|
530 |
} |
|
531 |
}).done( function( response ) { |
|
532 |
var message; |
|
533 |
||
534 |
if ( response.success ) { |
|
535 |
uploadSuccess( file, response.data.id ); |
|
536 |
} else { |
|
537 |
if ( response.data && response.data.message ) { |
|
538 |
message = response.data.message; |
|
539 |
} |
|
540 |
||
541 |
wpQueueError( message || pluploadL10n.http_error_image ); |
|
542 |
} |
|
543 |
}).fail( function( jqXHR ) { |
|
544 |
// If another HTTP 5xx error, try try again... |
|
545 |
if ( jqXHR.status >= 500 && jqXHR.status < 600 ) { |
|
546 |
tryAgain( up, error ); |
|
547 |
return; |
|
548 |
} |
|
549 |
||
550 |
wpQueueError( pluploadL10n.http_error_image ); |
|
551 |
}); |
|
552 |
} |
|
553 |
||
554 |
// Init and set the uploader. |
|
555 |
uploader_init = function() { |
|
556 |
uploader = new plupload.Uploader( wpUploaderInit ); |
|
557 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
558 |
$( '#image_resize' ).on( 'change', function() { |
16 | 559 |
var arg = $( this ).prop( 'checked' ); |
0 | 560 |
|
561 |
setResize( arg ); |
|
562 |
||
563 |
if ( arg ) |
|
16 | 564 |
setUserSetting( 'upload_resize', '1' ); |
0 | 565 |
else |
16 | 566 |
deleteUserSetting( 'upload_resize' ); |
0 | 567 |
}); |
568 |
||
16 | 569 |
uploader.bind( 'Init', function( up ) { |
570 |
var uploaddiv = $( '#plupload-upload-ui' ); |
|
0 | 571 |
|
16 | 572 |
setResize( getUserSetting( 'upload_resize', false ) ); |
0 | 573 |
|
16 | 574 |
if ( up.features.dragdrop && ! $( document.body ).hasClass( 'mobile' ) ) { |
575 |
uploaddiv.addClass( 'drag-drop' ); |
|
576 |
||
577 |
$( '#drag-drop-area' ).on( 'dragover.wp-uploader', function() { // dragenter doesn't fire right :( |
|
578 |
uploaddiv.addClass( 'drag-over' ); |
|
579 |
}).on( 'dragleave.wp-uploader, drop.wp-uploader', function() { |
|
580 |
uploaddiv.removeClass( 'drag-over' ); |
|
0 | 581 |
}); |
582 |
} else { |
|
16 | 583 |
uploaddiv.removeClass( 'drag-drop' ); |
584 |
$( '#drag-drop-area' ).off( '.wp-uploader' ); |
|
0 | 585 |
} |
586 |
||
5 | 587 |
if ( up.runtime === 'html4' ) { |
16 | 588 |
$( '.upload-flash-bypass' ).hide(); |
5 | 589 |
} |
0 | 590 |
}); |
591 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
592 |
uploader.bind( 'postinit', function( up ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
up.refresh(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
|
0 | 596 |
uploader.init(); |
597 |
||
16 | 598 |
uploader.bind( 'FilesAdded', function( up, files ) { |
599 |
$( '#media-upload-error' ).empty(); |
|
0 | 600 |
uploadStart(); |
601 |
||
5 | 602 |
plupload.each( files, function( file ) { |
16 | 603 |
if ( file.type === 'image/heic' && up.settings.heic_upload_error ) { |
604 |
// Show error but do not block uploading. |
|
18 | 605 |
wpQueueError( pluploadL10n.unsupported_image ); |
606 |
} else if ( file.type === 'image/webp' && up.settings.webp_upload_error ) { |
|
607 |
// Disallow uploading of WebP images if the server cannot edit them. |
|
608 |
wpQueueError( pluploadL10n.noneditable_image ); |
|
609 |
up.removeFile( file ); |
|
610 |
return; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
611 |
} else if ( file.type === 'image/avif' && up.settings.avif_upload_error ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
612 |
// Disallow uploading of AVIF images if the server cannot edit them. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
613 |
wpQueueError( pluploadL10n.noneditable_image ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
614 |
up.removeFile( file ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
615 |
return; |
16 | 616 |
} |
617 |
||
5 | 618 |
fileQueued( file ); |
0 | 619 |
}); |
620 |
||
621 |
up.refresh(); |
|
622 |
up.start(); |
|
623 |
}); |
|
624 |
||
16 | 625 |
uploader.bind( 'UploadFile', function( up, file ) { |
626 |
fileUploading( up, file ); |
|
627 |
}); |
|
628 |
||
629 |
uploader.bind( 'UploadProgress', function( up, file ) { |
|
630 |
uploadProgress( up, file ); |
|
0 | 631 |
}); |
632 |
||
16 | 633 |
uploader.bind( 'Error', function( up, error ) { |
634 |
var isImage = error.file && error.file.type && error.file.type.indexOf( 'image/' ) === 0; |
|
635 |
var status = error && error.status; |
|
0 | 636 |
|
16 | 637 |
// If the file is an image and the error is HTTP 5xx try to create sub-sizes again. |
638 |
if ( isImage && status >= 500 && status < 600 ) { |
|
639 |
tryAgain( up, error ); |
|
640 |
return; |
|
641 |
} |
|
642 |
||
643 |
uploadError( error.file, error.code, error.message, up ); |
|
0 | 644 |
up.refresh(); |
645 |
}); |
|
646 |
||
16 | 647 |
uploader.bind( 'FileUploaded', function( up, file, response ) { |
648 |
uploadSuccess( file, response.response ); |
|
0 | 649 |
}); |
650 |
||
16 | 651 |
uploader.bind( 'UploadComplete', function() { |
0 | 652 |
uploadComplete(); |
653 |
}); |
|
5 | 654 |
}; |
655 |
||
16 | 656 |
if ( typeof( wpUploaderInit ) == 'object' ) { |
5 | 657 |
uploader_init(); |
0 | 658 |
} |
659 |
||
660 |
}); |