| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | jQuery(document).on( 'click', '.wppb-rpf-add', function( event ){ |
| 5 | 5 | validate_simple_upload(); |
| 6 | 6 | }); |
| 7 | 7 | |
| 8 | - if( typeof wp.media != "undefined" ){ | |
| 8 | + if( typeof wp !== "undefined" && typeof wp.media !== "undefined" ){ | |
| 9 | 9 | // Uploading files |
| 10 | 10 | var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id |
| 11 | 11 | |
| 12 | 12 | jQuery(document).on( 'click', '.wppb_upload_button', function( event ){ |
| @@ -109,49 +109,51 @@ | ||
| 109 | 109 | // Restore the main ID when the add media button is pressed |
| 110 | 110 | jQuery('a.add_media').on('click', function() { |
| 111 | 111 | wp.media.model.settings.post.id = wp_media_post_id; |
| 112 | 112 | }); |
| 113 | + } | |
| 113 | 114 | |
| 114 | - jQuery(document).on('keypress', '.wppb-remove-upload', function(e){ | |
| 115 | - if(e.which == 13) { | |
| 116 | - jQuery(this).trigger('click'); | |
| 115 | + // Bind independently of wp.media: users without upload_files get the simple | |
| 116 | + // file input and never load the media gallery scripts. | |
| 117 | + jQuery(document).on('keypress', '.wppb-remove-upload', function(e){ | |
| 118 | + if(e.which == 13) { | |
| 119 | + jQuery(this).trigger('click'); | |
| 120 | + } | |
| 121 | + }); | |
| 122 | + | |
| 123 | + jQuery(document).on('click', '.wppb-remove-upload', function(e){ | |
| 124 | + if( confirm( 'Are you sure ?' ) ){ | |
| 125 | + /* update hidden input */ | |
| 126 | + simple_upload_button = jQuery(this).closest('li, td, p.form-row, div.form-row').find('input[type="file"]'); | |
| 127 | + if (simple_upload_button.attr('name')) { | |
| 128 | + upload_input_id = simple_upload_button.attr('name').split('_').slice(2).join('_'); | |
| 129 | + upload_input_id = upload_input_id.split('-').join('_'); | |
| 130 | + upload_input = jQuery("#" + upload_input_id); | |
| 117 | 131 | } |
| 118 | - }); | |
| 132 | + else{ | |
| 133 | + upload_input = jQuery(this).closest('li, td, p.form-row, div.form-row').find('input[type="hidden"]'); | |
| 134 | + } | |
| 135 | + removedAttachement = jQuery(this).parent().parent('.upload-field-details').data('attachment_id'); | |
| 136 | + uploadAttachemnts = upload_input.val(); | |
| 137 | + uploadAttachemntsArray = uploadAttachemnts.split(','); | |
| 138 | + newuploadAttachments = []; | |
| 139 | + for (var i = 0; i < uploadAttachemntsArray.length; i++) { | |
| 140 | + if (uploadAttachemntsArray[i] != removedAttachement) | |
| 141 | + newuploadAttachments.push(uploadAttachemntsArray[i]); | |
| 142 | + } | |
| 143 | + newuploadAttachments = newuploadAttachments.join(','); | |
| 144 | + upload_input.val(newuploadAttachments); | |
| 119 | 145 | |
| 120 | - jQuery(document).on('click', '.wppb-remove-upload', function(e){ | |
| 121 | - if( confirm( 'Are you sure ?' ) ){ | |
| 122 | - /* update hidden input */ | |
| 123 | - simple_upload_button = jQuery(this).closest('li, td, p.form-row, div.form-row').find('input[type="file"]'); | |
| 124 | - if (simple_upload_button.attr('name')) { | |
| 125 | - upload_input_id = simple_upload_button.attr('name').split('_').slice(2).join('_'); | |
| 126 | - upload_input_id = upload_input_id.split('-').join('_'); | |
| 127 | - upload_input = jQuery("#" + upload_input_id); | |
| 128 | - } | |
| 129 | - else{ | |
| 130 | - upload_input = jQuery(this).closest('li, td, p.form-row, div.form-row').find('input[type="hidden"]'); | |
| 131 | - } | |
| 132 | - removedAttachement = jQuery(this).parent().parent('.upload-field-details').data('attachment_id'); | |
| 133 | - uploadAttachemnts = upload_input.val(); | |
| 134 | - uploadAttachemntsArray = uploadAttachemnts.split(','); | |
| 135 | - newuploadAttachments = []; | |
| 136 | - for (var i = 0; i < uploadAttachemntsArray.length; i++) { | |
| 137 | - if (uploadAttachemntsArray[i] != removedAttachement) | |
| 138 | - newuploadAttachments.push(uploadAttachemntsArray[i]); | |
| 139 | - } | |
| 140 | - newuploadAttachments = newuploadAttachments.join(','); | |
| 141 | - upload_input.val(newuploadAttachments); | |
| 146 | + /* remove the attachment details */ | |
| 147 | + jQuery(this).parent().parent('.upload-field-details').next('a').show(); | |
| 148 | + simple_upload_button.show(); | |
| 149 | + simple_upload_button.val(''); | |
| 150 | + jQuery(this).parent().parent('.upload-field-details').remove(); | |
| 142 | 151 | |
| 143 | - /* remove the attachment details */ | |
| 144 | - jQuery(this).parent().parent('.upload-field-details').next('a').show(); | |
| 145 | - simple_upload_button.show(); | |
| 146 | - simple_upload_button.val(''); | |
| 147 | - jQuery(this).parent().parent('.upload-field-details').remove(); | |
| 152 | + jQuery(document).trigger( 'wppb_removed_uploaded_file' ) | |
| 148 | 153 | |
| 149 | - jQuery(document).trigger( 'wppb_removed_uploaded_file' ) | |
| 150 | - | |
| 151 | - } | |
| 152 | - }); | |
| 153 | - } | |
| 154 | + } | |
| 155 | + }); | |
| 154 | 156 | }); |
| 155 | 157 | |
| 156 | 158 | function validate_simple_upload(){ |
| 157 | 159 | jQuery("input:file").on('change', function(e){ |
| @@ -207,15 +209,25 @@ | ||
| 207 | 209 | uploadButton.val(''); |
| 208 | 210 | } else { |
| 209 | 211 | var fieldName = uploadInputName.replace(/^(simple_upload_)/,'').replace(/-/g,'_'); |
| 210 | 212 | var formData = new FormData(); |
| 211 | - if (uploadButton.closest('.wppb-upload').length > 0) { | |
| 212 | - formData.append('action', 'wppb_ajax_simple_upload'); | |
| 213 | - formData.append(fieldName, jQuery(e.target).prop('files')[0]); | |
| 213 | + /* Route to the correct AJAX action based on the field type. Rely on the | |
| 214 | + data-field_type attribute (set for both Upload and Avatar fields) so the | |
| 215 | + routing is correct regardless of layout. Fall back to the container check | |
| 216 | + for older markup that doesn't expose the attribute. The back-end user page | |
| 217 | + doesn't wrap fields in .wppb-upload, so the container check alone would | |
| 218 | + wrongly send Upload fields to the avatar handler. */ | |
| 219 | + var fieldType = uploadButton.data('field_type'); | |
| 220 | + var uploadAction; | |
| 221 | + if ( fieldType === 'Upload' ) { | |
| 222 | + uploadAction = 'wppb_ajax_simple_upload'; | |
| 223 | + } else if ( fieldType === 'Avatar' ) { | |
| 224 | + uploadAction = 'wppb_ajax_simple_avatar'; | |
| 214 | 225 | } else { |
| 215 | - formData.append('action', 'wppb_ajax_simple_avatar'); | |
| 216 | - formData.append(fieldName, jQuery(e.target).prop('files')[0]); | |
| 226 | + uploadAction = uploadButton.closest('.wppb-upload').length > 0 ? 'wppb_ajax_simple_upload' : 'wppb_ajax_simple_avatar'; | |
| 217 | 227 | } |
| 228 | + formData.append('action', uploadAction); | |
| 229 | + formData.append(fieldName, jQuery(e.target).prop('files')[0]); | |
| 218 | 230 | formData.append('nonce', wppb_upload_script_vars.nonce); |
| 219 | 231 | formData.append('name', fieldName); |
| 220 | 232 | |
| 221 | 233 | var simpleUploadFields = uploadButton.closest('.wppb-user-forms').find('.wppb_simple_upload'); |