From 936a38255bac54bd251ae908848bafb225573db5 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Thu, 24 Jun 2021 19:12:00 +0000 Subject: [PATCH] Media: Prevent uploading and show an error message when the server doesn't support editing of WebP images, take II. Add new, better error message for it. Props antpb, joedolson, iandunn, azaozz. Fixes #53475. Built from https://develop.svn.wordpress.org/trunk@51227 git-svn-id: http://core.svn.wordpress.org/trunk@50836 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/media.php | 5 +++++ wp-includes/js/plupload/handlers.js | 7 ++++++- wp-includes/js/plupload/handlers.min.js | 2 +- wp-includes/js/plupload/wp-plupload.js | 5 +++++ wp-includes/js/plupload/wp-plupload.min.js | 2 +- wp-includes/media.php | 5 +++++ wp-includes/script-loader.php | 1 + wp-includes/version.php | 2 +- 8 files changed, 25 insertions(+), 4 deletions(-) diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index bdfb65d103..8c27d15c17 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -2198,6 +2198,11 @@ function media_upload_form( $errors = null ) { $plupload_init['multi_selection'] = false; } + // Check if WebP images can be edited. + if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { + $plupload_init['webp_upload_error'] = true; + } + /** * Filters the default Plupload settings. * diff --git a/wp-includes/js/plupload/handlers.js b/wp-includes/js/plupload/handlers.js index 7958e7231d..2560495b76 100644 --- a/wp-includes/js/plupload/handlers.js +++ b/wp-includes/js/plupload/handlers.js @@ -604,7 +604,12 @@ jQuery( document ).ready( function( $ ) { plupload.each( files, function( file ) { if ( file.type === 'image/heic' && up.settings.heic_upload_error ) { // Show error but do not block uploading. - wpQueueError( pluploadL10n.unsupported_image ) + wpQueueError( pluploadL10n.unsupported_image ); + } else if ( file.type === 'image/webp' && up.settings.webp_upload_error ) { + // Disallow uploading of WebP images if the server cannot edit them. + wpQueueError( pluploadL10n.noneditable_image ); + up.removeFile( file ); + return; } fileQueued( file ); diff --git a/wp-includes/js/plupload/handlers.min.js b/wp-includes/js/plupload/handlers.min.js index da6651ff93..ea8deccf13 100644 --- a/wp-includes/js/plupload/handlers.min.js +++ b/wp-includes/js/plupload/handlers.min.js @@ -1 +1 @@ -var uploader,uploader_init,topWin=window.dialogArguments||opener||parent||top;function fileQueued(e){jQuery(".media-blank").remove();var a=jQuery("#media-items").children(),r=post_id||0;1==a.length&&a.removeClass("open").find(".slidetoggle").slideUp(200),jQuery('
').attr("id","media-item-"+e.id).addClass("child-of-"+r).append('
0%
',jQuery('
').text(" "+e.name)).appendTo(jQuery("#media-items")),jQuery("#insert-gallery").prop("disabled",!0)}function uploadStart(){try{void 0!==topWin.tb_remove&&topWin.jQuery("#TB_overlay").unbind("click",topWin.tb_remove)}catch(e){}return!0}function uploadProgress(e,a){var r=jQuery("#media-item-"+a.id);jQuery(".bar",r).width(200*a.loaded/a.size),jQuery(".percent",r).html(a.percent+"%")}function fileUploading(e,a){var r=104857600;rr&&setTimeout(function(){a.status<3&&0===a.loaded&&(wpFileError(a,pluploadL10n.big_upload_failed.replace("%1$s",'').replace("%2$s","")),e.stop(),e.removeFile(a),e.start())},1e4)}function updateMediaForm(){var e=jQuery("#media-items").children();1==e.length?(e.addClass("open").find(".slidetoggle").show(),jQuery(".insert-gallery").hide()):1(\d+)<\/pre>$/,"$1"),/media-upload-error|error-div/.test(a))?r.html(a):(r.find(".percent").html(pluploadL10n.crunching),prepareMediaItem(e,a),updateMediaForm(),post_id&&r.hasClass("child-of-"+post_id)&&jQuery("#attachments-count").text(+jQuery("#attachments-count").text()+1))}function setResize(e){e?window.resize_width&&window.resize_height?uploader.settings.resize={enabled:!0,width:window.resize_width,height:window.resize_height,quality:100}:uploader.settings.multipart_params.image_resize=!0:delete uploader.settings.multipart_params.image_resize}function prepareMediaItem(e,a){var r="undefined"==typeof shortform?1:2,t=jQuery("#media-item-"+e.id);2==r&&2

'+e+"

")}function wpFileError(e,a){itemAjaxError(e.id,a)}function itemAjaxError(e,a){var r=jQuery("#media-item-"+e),t=r.find(".filename").text();r.data("last-err")!=e&&r.html('
'+pluploadL10n.dismiss+""+pluploadL10n.error_uploading.replace("%s",jQuery.trim(t))+" "+a+"
").data("last-err",e)}function deleteSuccess(e){var a;return"-1"==e?itemAjaxError(this.id,"You do not have permission. Has your session expired?"):"0"==e?itemAjaxError(this.id,"Could not be deleted. Has it been deleted already?"):(a=this.id,e=jQuery("#media-item-"+a),(a=jQuery("#type-of-"+a).val())&&jQuery("#"+a+"-counter").text(jQuery("#"+a+"-counter").text()-1),post_id&&e.hasClass("child-of-"+post_id)&&jQuery("#attachments-count").text(jQuery("#attachments-count").text()-1),1==jQuery("form.type-form #media-items").children().length&&0 '+pluploadL10n.deleted+" ").siblings("a.toggle").hide(),jQuery(".filename",e).append(jQuery("a.undo",e).removeClass("hidden")),void jQuery(".menu_order_input",e).hide())}function deleteError(){}function uploadComplete(){jQuery("#insert-gallery").prop("disabled",!1)}function switchUploader(e){e?(deleteUserSetting("uploader"),jQuery(".media-upload-form").removeClass("html-uploader"),"object"==typeof uploader&&uploader.refresh()):(setUserSetting("uploader","1"),jQuery(".media-upload-form").addClass("html-uploader"))}function uploadError(e,a,r,t){var i=104857600;switch(a){case plupload.FAILED:wpFileError(e,pluploadL10n.upload_failed);break;case plupload.FILE_EXTENSION_ERROR:wpFileExtensionError(t,e,pluploadL10n.invalid_filetype);break;case plupload.FILE_SIZE_ERROR:uploadSizeError(t,e);break;case plupload.IMAGE_FORMAT_ERROR:wpFileError(e,pluploadL10n.not_an_image);break;case plupload.IMAGE_MEMORY_ERROR:wpFileError(e,pluploadL10n.image_memory_exceeded);break;case plupload.IMAGE_DIMENSIONS_ERROR:wpFileError(e,pluploadL10n.image_dimensions_exceeded);break;case plupload.GENERIC_ERROR:wpQueueError(pluploadL10n.upload_failed);break;case plupload.IO_ERROR:ii?wpFileError(e,pluploadL10n.big_upload_failed.replace("%1$s",'').replace("%2$s","")):wpQueueError(pluploadL10n.io_error);break;case plupload.HTTP_ERROR:wpQueueError(pluploadL10n.http_error);break;case plupload.INIT_ERROR:jQuery(".media-upload-form").addClass("html-uploader");break;case plupload.SECURITY_ERROR:wpQueueError(pluploadL10n.security_error);break;default:wpFileError(e,pluploadL10n.default_error)}}function uploadSizeError(e,a){var r=pluploadL10n.file_exceeds_size_limit.replace("%s",a.name),r=jQuery("
").attr({id:"media-item-"+a.id,class:"media-item error"}).append(jQuery("

").text(r));jQuery("#media-items").append(r),e.removeFile(a)}function wpFileExtensionError(e,a,r){jQuery("#media-items").append('

'+r+"

"),e.removeFile(a)}function copyAttachmentUploadURLClipboard(){var t;new ClipboardJS(".copy-attachment-url").on("success",function(e){var a=jQuery(e.trigger),r=jQuery(".success",a.closest(".copy-to-clipboard-container"));e.clearSelection(),a.trigger("focus"),clearTimeout(t),r.removeClass("hidden"),t=setTimeout(function(){r.addClass("hidden")},3e3),wp.a11y.speak(pluploadL10n.file_url_copied)})}jQuery(document).ready(function(d){copyAttachmentUploadURLClipboard();var o,l={};d(".media-upload-form").bind("click.uploader",function(e){var a,r=d(e.target);r.is('input[type="radio"]')?(a=r.closest("tr")).hasClass("align")?setUserSetting("align",r.val()):a.hasClass("image-size")&&setUserSetting("imgsize",r.val()):r.is("button.button")?(a=(a=e.target.className||"").match(/url([^ '"]+)/))&&a[1]&&(setUserSetting("urlbutton",a[1]),r.siblings(".urlfield").val(r.data("link-url"))):r.is("a.dismiss")?r.parents(".media-item").fadeOut(200,function(){d(this).remove()}):r.is(".upload-flash-bypass a")||r.is("a.uploader-html")?(d("#media-items, p.submit, span.big-file-warning").css("display","none"),switchUploader(0),e.preventDefault()):r.is(".upload-html-bypass a")?(d("#media-items, p.submit, span.big-file-warning").css("display",""),switchUploader(1),e.preventDefault()):r.is("a.describe-toggle-on")?(r.parent().addClass("open"),r.siblings(".slidetoggle").fadeIn(250,function(){var e,a,r=d(window).scrollTop(),t=d(window).height(),i=d(this).offset().top,o=d(this).height();t&&i&&o&&(a=r+t)<(e=i+o)&&(e-a').attr("id","media-item-"+e.id).addClass("child-of-"+r).append('
0%
',jQuery('
').text(" "+e.name)).appendTo(jQuery("#media-items")),jQuery("#insert-gallery").prop("disabled",!0)}function uploadStart(){try{void 0!==topWin.tb_remove&&topWin.jQuery("#TB_overlay").unbind("click",topWin.tb_remove)}catch(e){}return!0}function uploadProgress(e,a){var r=jQuery("#media-item-"+a.id);jQuery(".bar",r).width(200*a.loaded/a.size),jQuery(".percent",r).html(a.percent+"%")}function fileUploading(e,a){var r=104857600;rr&&setTimeout(function(){a.status<3&&0===a.loaded&&(wpFileError(a,pluploadL10n.big_upload_failed.replace("%1$s",'').replace("%2$s","")),e.stop(),e.removeFile(a),e.start())},1e4)}function updateMediaForm(){var e=jQuery("#media-items").children();1==e.length?(e.addClass("open").find(".slidetoggle").show(),jQuery(".insert-gallery").hide()):1(\d+)<\/pre>$/,"$1"),/media-upload-error|error-div/.test(a))?r.html(a):(r.find(".percent").html(pluploadL10n.crunching),prepareMediaItem(e,a),updateMediaForm(),post_id&&r.hasClass("child-of-"+post_id)&&jQuery("#attachments-count").text(+jQuery("#attachments-count").text()+1))}function setResize(e){e?window.resize_width&&window.resize_height?uploader.settings.resize={enabled:!0,width:window.resize_width,height:window.resize_height,quality:100}:uploader.settings.multipart_params.image_resize=!0:delete uploader.settings.multipart_params.image_resize}function prepareMediaItem(e,a){var r="undefined"==typeof shortform?1:2,i=jQuery("#media-item-"+e.id);2==r&&2

'+e+"

")}function wpFileError(e,a){itemAjaxError(e.id,a)}function itemAjaxError(e,a){var r=jQuery("#media-item-"+e),i=r.find(".filename").text();r.data("last-err")!=e&&r.html('
'+pluploadL10n.dismiss+""+pluploadL10n.error_uploading.replace("%s",jQuery.trim(i))+" "+a+"
").data("last-err",e)}function deleteSuccess(e){var a;return"-1"==e?itemAjaxError(this.id,"You do not have permission. Has your session expired?"):"0"==e?itemAjaxError(this.id,"Could not be deleted. Has it been deleted already?"):(a=this.id,e=jQuery("#media-item-"+a),(a=jQuery("#type-of-"+a).val())&&jQuery("#"+a+"-counter").text(jQuery("#"+a+"-counter").text()-1),post_id&&e.hasClass("child-of-"+post_id)&&jQuery("#attachments-count").text(jQuery("#attachments-count").text()-1),1==jQuery("form.type-form #media-items").children().length&&0 '+pluploadL10n.deleted+" ").siblings("a.toggle").hide(),jQuery(".filename",e).append(jQuery("a.undo",e).removeClass("hidden")),void jQuery(".menu_order_input",e).hide())}function deleteError(){}function uploadComplete(){jQuery("#insert-gallery").prop("disabled",!1)}function switchUploader(e){e?(deleteUserSetting("uploader"),jQuery(".media-upload-form").removeClass("html-uploader"),"object"==typeof uploader&&uploader.refresh()):(setUserSetting("uploader","1"),jQuery(".media-upload-form").addClass("html-uploader"))}function uploadError(e,a,r,i){var t=104857600;switch(a){case plupload.FAILED:wpFileError(e,pluploadL10n.upload_failed);break;case plupload.FILE_EXTENSION_ERROR:wpFileExtensionError(i,e,pluploadL10n.invalid_filetype);break;case plupload.FILE_SIZE_ERROR:uploadSizeError(i,e);break;case plupload.IMAGE_FORMAT_ERROR:wpFileError(e,pluploadL10n.not_an_image);break;case plupload.IMAGE_MEMORY_ERROR:wpFileError(e,pluploadL10n.image_memory_exceeded);break;case plupload.IMAGE_DIMENSIONS_ERROR:wpFileError(e,pluploadL10n.image_dimensions_exceeded);break;case plupload.GENERIC_ERROR:wpQueueError(pluploadL10n.upload_failed);break;case plupload.IO_ERROR:tt?wpFileError(e,pluploadL10n.big_upload_failed.replace("%1$s",'').replace("%2$s","")):wpQueueError(pluploadL10n.io_error);break;case plupload.HTTP_ERROR:wpQueueError(pluploadL10n.http_error);break;case plupload.INIT_ERROR:jQuery(".media-upload-form").addClass("html-uploader");break;case plupload.SECURITY_ERROR:wpQueueError(pluploadL10n.security_error);break;default:wpFileError(e,pluploadL10n.default_error)}}function uploadSizeError(e,a){var r=pluploadL10n.file_exceeds_size_limit.replace("%s",a.name),r=jQuery("
").attr({id:"media-item-"+a.id,class:"media-item error"}).append(jQuery("

").text(r));jQuery("#media-items").append(r),e.removeFile(a)}function wpFileExtensionError(e,a,r){jQuery("#media-items").append('

'+r+"

"),e.removeFile(a)}function copyAttachmentUploadURLClipboard(){var i;new ClipboardJS(".copy-attachment-url").on("success",function(e){var a=jQuery(e.trigger),r=jQuery(".success",a.closest(".copy-to-clipboard-container"));e.clearSelection(),a.trigger("focus"),clearTimeout(i),r.removeClass("hidden"),i=setTimeout(function(){r.addClass("hidden")},3e3),wp.a11y.speak(pluploadL10n.file_url_copied)})}jQuery(document).ready(function(d){copyAttachmentUploadURLClipboard();var o,l={};d(".media-upload-form").bind("click.uploader",function(e){var a,r=d(e.target);r.is('input[type="radio"]')?(a=r.closest("tr")).hasClass("align")?setUserSetting("align",r.val()):a.hasClass("image-size")&&setUserSetting("imgsize",r.val()):r.is("button.button")?(a=(a=e.target.className||"").match(/url([^ '"]+)/))&&a[1]&&(setUserSetting("urlbutton",a[1]),r.siblings(".urlfield").val(r.data("link-url"))):r.is("a.dismiss")?r.parents(".media-item").fadeOut(200,function(){d(this).remove()}):r.is(".upload-flash-bypass a")||r.is("a.uploader-html")?(d("#media-items, p.submit, span.big-file-warning").css("display","none"),switchUploader(0),e.preventDefault()):r.is(".upload-html-bypass a")?(d("#media-items, p.submit, span.big-file-warning").css("display",""),switchUploader(1),e.preventDefault()):r.is("a.describe-toggle-on")?(r.parent().addClass("open"),r.siblings(".slidetoggle").fadeIn(250,function(){var e,a,r=d(window).scrollTop(),i=d(window).height(),t=d(this).offset().top,o=d(this).height();i&&t&&o&&(a=r+i)<(e=t+o)&&(e-a').css({position:"fixed",top:"-1000px",left:"-1000px",height:0,width:0}).attr("id","wp-uploader-browser-"+this.uploader.id).appendTo("body"):a).append(this.browser))}this.uploader.refresh()}}),l.queue=new wp.media.model.Attachments([],{query:!1}),l.errors=new Backbone.Collection,e.Uploader=l)}(wp,jQuery); \ No newline at end of file +window.wp=window.wp||{},function(e,l){var u;"undefined"!=typeof _wpPluploadSettings&&(u=function(e){var n,t,i,p,d=this,r={container:"container",browser:"browse_button",dropzone:"drop_element"},s={};if(this.supports={upload:u.browser.supported},this.supported=this.supports.upload,this.supported){for(t in this.plupload=l.extend(!0,{multipart_params:{}},u.defaults),this.container=document.body,l.extend(!0,this,e),this)"function"==typeof this[t]&&(this[t]=l.proxy(this[t],this));for(t in r)this[t]&&(this[t]=l(this[t]).first(),this[t].length?(this[t].prop("id")||this[t].prop("id","__wp-uploader-id-"+u.uuid++),this.plupload[r[t]]=this[t].prop("id")):delete this[t]);(this.browser&&this.browser.length||this.dropzone&&this.dropzone.length)&&(this.uploader=new plupload.Uploader(this.plupload),delete this.plupload,this.param(this.params||{}),delete this.params,n=function(t,r,a){var e,o;if(r&&r.responseHeaders)if((o=r.responseHeaders.match(/x-wp-upload-attachment-id:\s*(\d+)/i))&&o[1]){if(o=o[1],(e=s[a.id])&&4').css({position:"fixed",top:"-1000px",left:"-1000px",height:0,width:0}).attr("id","wp-uploader-browser-"+this.uploader.id).appendTo("body"):r).append(this.browser))}this.uploader.refresh()}}),u.queue=new wp.media.model.Attachments([],{query:!1}),u.errors=new Backbone.Collection,e.Uploader=u)}(wp,jQuery); \ No newline at end of file diff --git a/wp-includes/media.php b/wp-includes/media.php index 5c73db8f30..5a725ca5f9 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -3858,6 +3858,11 @@ function wp_plupload_default_settings() { $defaults['multi_selection'] = false; } + // Check if WebP images can be edited. + if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { + $defaults['webp_upload_error'] = true; + } + /** * Filters the Plupload default settings. * diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index d1c2450b5d..3227204501 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -875,6 +875,7 @@ function wp_default_scripts( $scripts ) { /* translators: %s: File name. */ 'error_uploading' => __( '“%s” has failed to upload.' ), 'unsupported_image' => __( 'This image cannot be displayed in a web browser. For best results convert it to JPEG before uploading.' ), + 'noneditable_image' => __( 'This image cannot be processed by the web server. Convert it to JPEG or PNG before uploading.' ), 'file_url_copied' => __( 'The file URL has been copied to your clipboard' ), ); diff --git a/wp-includes/version.php b/wp-includes/version.php index e89e2445bd..9b7b1136b9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-beta3-51226'; +$wp_version = '5.8-beta3-51227'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.