Changeset 12090
- Timestamp:
- 09/26/2022 10:23:13 PM (3 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory
- Files:
-
- 2 edited
-
assets/js/submit.js (modified) (6 diffs)
-
inc/uploads.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/submit.js
r12087 r12090 1 function photoSubmitLoaded() { 1 /** 2 * Initializes things related to the photo upload form. 3 */ 4 function photoSubmitInit() { 2 5 3 6 // Remove query parameters added by Frontend Uploader. … … 29 32 // Add custom validation for upload file size. 30 33 photo_upload_field.addEventListener( 'change', e => { 31 photoShowFile SizeError( photo_upload_field );34 photoShowFileErrors( photo_upload_field ); 32 35 } ); 33 36 } 34 37 35 // Customize jQuery Validator, if still in use.38 // Disable jQuery Validation, if still in use. 36 39 if ( window.jQuery && window.jQuery.validator ) { 37 // Customize error message for invalid file mimetype. 38 jQuery.validator.messages.accept = PhotoDir.err_invalid_mimetype; 39 } 40 jQuery( photo_upload_form ).validate().settings.ignore = "*"; 41 } 42 43 // Show/remove error when any form field value is changed. 44 photoGetInputFields( photo_upload_form ).forEach( (input, i) => { 45 input.addEventListener( 'change', e => { 46 photoRemoveFieldError( e.target ); 47 } ); 48 } ); 40 49 41 50 // Check validity of form fields on submission. 42 51 photo_upload_form.addEventListener( 'submit', e => { 43 photoShowFileSizeError( photo_upload_field ); 52 e.preventDefault(); 53 54 const photo_upload_form = e.target; 55 const photo_upload_input = document.getElementById( 'ug_photo' ); 56 57 // Clear any server notices from a previous submit. 58 document.querySelector( '.ugc-notice' )?.remove(); 59 60 // Disable submit button. 61 photoSetSubmitButtonDisabled( photo_upload_form, true ); 62 63 // Show errors for fields that aren't the file input. 64 photoShowFieldErrors( photo_upload_form ); 65 66 // Show errors for file input field. 67 photoShowFileErrors( photo_upload_input ); 68 69 // Scroll top of form into view. 70 document.getElementById( 'wporg-photo-upload' ).scrollIntoView( true ); 71 44 72 if ( photo_upload_form.checkValidity() ) { 45 photo_upload_ submit.disabled = true;73 photo_upload_form.submit(); 46 74 } 75 76 return false; 47 77 } ); 48 78 79 } 80 81 /** 82 * Returns the input fields of interest for generic validation. 83 * 84 * Fields that have custom validation should be listed here as exclusions. 85 * 86 * @param {HTMLElement} form - The form. 87 * @returns {HTMLElement[]} Array of input fields. 88 */ 89 function photoGetInputFields( form ) { 90 return form.querySelectorAll( 'input:not([type="hidden"]):not([type="submit"]):not([type="button"]):not([type="file"])'); 91 } 92 93 /** 94 * Sets the 'disabled' attribute for the submit button of a form. 95 * 96 * @param {HTMLElement} form - The form element. 97 * @param {Boolean} state - True if the button should be disabled, else false. 98 */ 99 function photoSetSubmitButtonDisabled( form, state ) { 100 const submitButton = form.querySelector('input[type="submit"]'); 101 102 submitButton.disabled = !!state; 103 } 104 105 /** 106 * Shows errors for invalid input fields and remove previously reported errors 107 * for valid input fields. 108 * 109 * Does not handle input that are: 110 * - hidden 111 * - buttons 112 * - file 113 * 114 * @param {HTMLElement} form - A form. 115 */ 116 function photoShowFieldErrors( form ) { 117 photoGetInputFields( form ).forEach( (input, i) => { 118 photoShowFieldError( input ); 119 } ); 120 } 121 122 /** 123 * Removes an error message for the given input. 124 * 125 * @param {HTMLElement} field - An input field. 126 */ 127 function photoRemoveFieldError( field ) { 128 field.parentNode.querySelectorAll( '.' + PhotoDir.error_class ).forEach( errorEl => errorEl.remove() ); 129 field.parentNode.querySelectorAll( '.processing' ).forEach( errorEl => errorEl.remove() ); 130 } 131 132 /** 133 * Shows the error if the input field is invalid, or removes a previously 134 * reported error if the input field is valid. 135 * 136 * @param {HTMLElement} field - An input field. 137 * @returns {String} Error message, if any. 138 */ 139 function photoShowFieldError( field ) { 140 photoRemoveFieldError( field ); 141 142 let errorMessage = field.validationMessage; 143 const cssClass = PhotoDir.error_class; 144 145 if ( ! errorMessage ) { 146 return ''; 147 } 148 149 // Set custom error messages. 150 if ( field.validity.valueMissing ) { 151 errorMessage = PhotoDir.err_field_required; 152 } 153 154 errorEl = document.createElement( 'span' ); 155 errorEl.setAttribute( 'class', cssClass ); 156 errorEl.innerHTML = errorMessage; 157 field.insertAdjacentElement( 'afterend', errorEl ); 158 159 return ''; 160 } 161 162 /** 163 * Validates a file upload against multiple criteria. 164 * 165 * @param {HTMLElement} field - The HTML file input field element. 166 * @return {Boolean} True if an error was encountered, else false. 167 */ 168 async function photoCheckFileValidations( field ) { 169 let error = false; 170 171 // Check if no file chosen. 172 if ( ! error ) { 173 error = field.validity.valueMissing; 174 } 175 176 // Check for file size error. 177 if ( ! error ) { 178 error = photoCheckFileSize( field ); 179 } 180 181 return error; 49 182 } 50 183 … … 57 190 * existing custom validity message is cleared. 58 191 * 59 * @param {Object} field - The HTML file input field object. 192 * @param {HTMLElement} field - The HTML file input field element. 193 * @return {Boolean} True if file size is invalid, else false. 60 194 */ 61 195 function photoCheckFileSize( field ) { … … 68 202 const file_size = files[0].size; 69 203 70 // Note: If changing the error message for either case, ensure the "// Don't show error message..."71 // regex in `photoShowFileSizeError()` still matches them both.72 204 if ( file_size >= MAX_SIZE ) { 73 205 field.setCustomValidity( PhotoDir.err_file_too_large ); 74 return ;206 return true; 75 207 } else if ( file_size <= MIN_SIZE ) { 76 208 field.setCustomValidity( PhotoDir.err_file_too_small ); 77 return ;209 return true; 78 210 } 79 211 } … … 81 213 // No custom constraint violation. 82 214 field.setCustomValidity(''); 215 return false; 216 } 217 218 /** 219 * Shows error messages for the file upload input. 220 * 221 * @param {HTMLElement} field - The HTML file input field element. 222 */ 223 function photoShowFileErrors( field ) { 224 // Checks custom file input validation. A custom validity error message gets 225 // set on the field if a validation fails. 226 photoCheckFileValidations( field ); 227 photoShowFileError( field ); 83 228 } 84 229 … … 86 231 * Handles the display of the error message for the file upload input. 87 232 * 88 * If the file input field has a file selected, it is checked to see if it is 89 * too large or too small. An appropriate error message is shown in either case. 90 * If no file is selected, or the file is of sufficient size, then no error is 91 * shown and any existing error for the field is cleared. 92 * 93 * This pseudo-mimics and also works around the jQuery Validation handling that 94 * doesn't play well with custom error reporting like this. 95 * 96 * @param {Object} field - The HTML file input field object. 97 */ 98 function photoShowFileSizeError( field ) { 99 // Check field for file size validation errors. 100 photoCheckFileSize( field ); 101 233 * @param {HTMLElement} field - The HTML file input field element. 234 * @return {boolean} True if no error was shown, false if error was shown. 235 */ 236 function photoShowFileError( field ) { 102 237 const errorMessage = field.validationMessage; 103 const errorId = `${field.id}-error`; 104 let errorEl = document.getElementById( errorId ); 105 106 // Don't show error message for any other error. 107 if ( ! errorMessage || ! field.validity.customError || ! / MB\.$/.test( errorMessage ) ) { 108 errorEl?.remove(); 109 return; 110 } 111 112 if ( ! errorEl ) { 113 errorEl = document.createElement( 'label' ); 114 field.after( errorEl ); 115 } 116 117 errorEl.setAttribute( 'id', errorId ); 118 errorEl.setAttribute( 'class', 'custom-error' ); 119 errorEl.setAttribute( 'for', field.id ); 120 errorEl.removeAttribute( 'style' ); 121 errorEl.innerHTML = errorMessage; 238 239 // Remove any existing error message. 240 photoRemoveFieldError( field ); 241 242 // Return if no legitimate custom error to report. 243 if ( ! errorMessage ) { 244 return; 245 } 246 247 photoShowFieldError( field ); 122 248 } 123 249 124 250 document.addEventListener( 'DOMContentLoaded', () => { 125 photoSubmit Loaded();251 photoSubmitInit(); 126 252 } ); -
sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/uploads.php
r12089 r12090 251 251 'PhotoDir', 252 252 [ 253 'error_class' => 'error', 254 255 // Field required. 256 'err_field_required' => __( 'This field is required.', 'wporg-photos' ), 257 253 258 // File extension. 254 259 'err_invalid_mimetype' => __( 'Please select a JPEG image.', 'wporg-photos' ),
Note: See TracChangeset
for help on using the changeset viewer.