PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
← All changes | front-end/default-fields/upload/upload.js +156 -132 3.9.6 → 4.0.3 View file →
@@ -1,102 +1,4 @@
1 -function validate_simple_upload(){
2 - jQuery("input:file").on('change', function(e){
3 - //handle simple upload
4 - var uploadInputName = jQuery(this).attr('name');
5 - var uploadButton = jQuery(this);
6 - var oFReader = new FileReader();
7 - var attachment = e.target.files[0];
8 - oFReader.readAsDataURL(attachment);
9 - oFReader.onload = function (oFREvent) {
10 - var src = oFREvent.target.result;
11 - var error = '';
12 - jQuery("#p_" + uploadInputName).text(error);
13 - uploadInputId = uploadInputName.split('-').join('_');
14 - //Check allowed upload type by wordpress
15 - allowed_mime_types = wppb_allowed_wordpress_formats.allowed_wordpress_formats;
16 - allowed_by_wordpress = false;
17 - for (var key in allowed_mime_types){
18 - allowed_type = allowed_mime_types[key];
19 - if (attachment.type === allowed_type){
20 - allowed_by_wordpress = true;
21 - possible_extensions = key.split('|');
22 - }
23 - }
24 - if (allowed_by_wordpress) {
25 - //Check allowed extensions
26 - allowed_extensions = jQuery("#allowed_extensions_" + uploadInputId).val();
27 - if (allowed_extensions !== '') {
28 - var allowed = false;
29 - allowed_extensions = allowed_extensions.split(',');
30 - for (var i = 0; i < allowed_extensions.length; i++) {
31 - if (possible_extensions.includes(allowed_extensions[i])) {
32 - allowed = true;
33 - }
34 - }
35 - if (allowed == false) {
36 - error = wppb_error_messages.upload_type_error_message;
37 - }
38 - }
39 - //Check size limit
40 - allowed_size_limit = wppb_limit.size_limit;
41 - if (attachment.size > allowed_size_limit) {
42 - var limit = allowed_size_limit / (1024 * 1024) + 1;
43 - error = wppb_error_messages.limit_error_message + limit + 'MB.';
44 - }
45 - }
46 - else{
47 - error = wppb_error_messages.upload_type_error_message;
48 - }
49 - if (error !== '') {
50 - jQuery("#p_" + uploadInputName).text(error);
51 - uploadButton.val('');
52 - } else {
53 - var fieldName = uploadInputName.replace(/^(simple_upload_)/, '').replace(/(-)/, '_');;
54 - var formData = new FormData();
55 - if (uploadButton.closest('.wppb-upload').length > 0) {
56 - formData.append('action', 'wppb_ajax_simple_upload');
57 - formData.append(fieldName, jQuery(e.target).prop('files')[0]);
58 - } else {
59 - formData.append('action', 'wppb_ajax_simple_avatar');
60 - formData.append(fieldName, jQuery(e.target).prop('files')[0]);
61 - }
62 - formData.append('nonce', wppb_upload_script_vars.nonce);
63 - formData.append('name', fieldName);
64 -
65 - var alreadyDisabled = false;
66 - if ( jQuery( 'p.form-submit .submit.button' ).prop( 'disabled' ) ) {
67 - alreadyDisabled = true;
68 - } else {
69 - jQuery( "p.form-submit .submit.button" ).prop( 'disabled', true );
70 - }
71 -
72 - jQuery.ajax({
73 - url: wppb_upload_script_vars.ajaxUrl,
74 - type: 'POST',
75 - contentType: false,
76 - processData: false,
77 - data: formData,
78 - success: function(response){
79 -
80 - var response = JSON.parse(response)
81 -
82 - if( response.errors ){
83 - jQuery("input#" + fieldName).val(null);
84 - jQuery('.wppb_simple_upload', jQuery("input#" + fieldName).parent()).val(null);
85 - } else {
86 - jQuery("input#" + fieldName).val(response);
87 - }
88 -
89 - if ( !alreadyDisabled ) {
90 - jQuery("p.form-submit .submit.button").prop('disabled', false);
91 - }
92 -
93 - },
94 - });
95 - }
96 - };
97 - });
98 -}
99 1 jQuery(document).ready(function(){
100 2 validate_simple_upload();
101 3
102 4 jQuery(document).on( 'click', '.wppb-rpf-add', function( event ){
@@ -102,9 +4,9 @@
102 4 jQuery(document).on( 'click', '.wppb-rpf-add', function( event ){
103 5 validate_simple_upload();
104 6 });
105 7
106 - if( typeof wp.media != "undefined" ){
8 + if( typeof wp !== "undefined" && typeof wp.media !== "undefined" ){
107 9 // Uploading files
108 10 var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
109 11
110 12 jQuery(document).on( 'click', '.wppb_upload_button', function( event ){
@@ -207,46 +109,168 @@
207 109 // Restore the main ID when the add media button is pressed
208 110 jQuery('a.add_media').on('click', function() {
209 111 wp.media.model.settings.post.id = wp_media_post_id;
210 112 });
113 + }
211 114
212 - jQuery(document).on('keypress', '.wppb-remove-upload', function(e){
213 - if(e.which == 13) {
214 - 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);
215 131 }
216 - });
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);
217 145
218 - jQuery(document).on('click', '.wppb-remove-upload', function(e){
219 - if( confirm( 'Are you sure ?' ) ){
220 - /* update hidden input */
221 - simple_upload_button = jQuery(this).closest('li, td, p.form-row, div.form-row').find('input[type="file"]');
222 - if (simple_upload_button.attr('name')) {
223 - upload_input_id = simple_upload_button.attr('name').split('_').slice(2).join('_');
224 - upload_input_id = upload_input_id.split('-').join('_');
225 - upload_input = jQuery("#" + upload_input_id);
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();
151 +
152 + jQuery(document).trigger( 'wppb_removed_uploaded_file' )
153 +
154 + }
155 + });
156 +});
157 +
158 +function validate_simple_upload(){
159 + jQuery("input:file").on('change', function(e){
160 + //handle simple upload
161 + var uploadInputName = jQuery(this).attr('name');
162 + var uploadButton = jQuery(this);
163 + var oFReader = new FileReader();
164 + var attachment = e.target.files[0];
165 + oFReader.readAsDataURL(attachment);
166 + oFReader.onload = function (oFREvent) {
167 + var src = oFREvent.target.result;
168 + var error = '';
169 + jQuery("#p_" + uploadInputName).text(error);
170 + uploadInputId = uploadInputName.split('-').join('_');
171 + //Check allowed upload type by wordpress
172 + allowed_mime_types = wppb_allowed_wordpress_formats.allowed_wordpress_formats;
173 + allowed_by_wordpress = false;
174 + for (var key in allowed_mime_types){
175 + allowed_type = allowed_mime_types[key];
176 + if (attachment.type === allowed_type){
177 + allowed_by_wordpress = true;
178 + possible_extensions = key.split('|');
226 179 }
227 - else{
228 - upload_input = jQuery(this).closest('li, td, p.form-row, div.form-row').find('input[type="hidden"]');
180 + }
181 + if (allowed_by_wordpress) {
182 + //Check allowed extensions
183 + allowed_extensions = jQuery("#allowed_extensions_" + uploadInputId).val();
184 + if (allowed_extensions !== '') {
185 + var allowed = false;
186 + allowed_extensions = allowed_extensions.split(',');
187 + for (var i = 0; i < allowed_extensions.length; i++) {
188 + if (possible_extensions.includes(allowed_extensions[i])) {
189 + allowed = true;
190 + }
191 + }
192 + if (allowed == false) {
193 + error = wppb_error_messages.upload_type_error_message;
194 + }
229 195 }
230 - removedAttachement = jQuery(this).parent().parent('.upload-field-details').data('attachment_id');
231 - uploadAttachemnts = upload_input.val();
232 - uploadAttachemntsArray = uploadAttachemnts.split(',');
233 - newuploadAttachments = [];
234 - for (var i = 0; i < uploadAttachemntsArray.length; i++) {
235 - if (uploadAttachemntsArray[i] != removedAttachement)
236 - newuploadAttachments.push(uploadAttachemntsArray[i]);
196 + //Check size limit — prefer per-field limit, fall back to global
197 + var perFieldLimit = jQuery("#size_limit_" + uploadInputId).val();
198 + var allowed_size_limit = perFieldLimit ? parseFloat(perFieldLimit) : wppb_limit.size_limit;
199 + if (attachment.size > allowed_size_limit) {
200 + var limit = allowed_size_limit / (1024 * 1024);
201 + error = wppb_error_messages.limit_error_message + limit + 'MB.';
237 202 }
238 - newuploadAttachments = newuploadAttachments.join(',');
239 - upload_input.val(newuploadAttachments);
203 + }
204 + else{
205 + error = wppb_error_messages.upload_type_error_message;
206 + }
207 + if (error !== '') {
208 + jQuery("#p_" + uploadInputName).text(error);
209 + uploadButton.val('');
210 + } else {
211 + var fieldName = uploadInputName.replace(/^(simple_upload_)/,'').replace(/-/g,'_');
212 + var formData = new FormData();
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';
225 + } else {
226 + uploadAction = uploadButton.closest('.wppb-upload').length > 0 ? 'wppb_ajax_simple_upload' : 'wppb_ajax_simple_avatar';
227 + }
228 + formData.append('action', uploadAction);
229 + formData.append(fieldName, jQuery(e.target).prop('files')[0]);
230 + formData.append('nonce', wppb_upload_script_vars.nonce);
231 + formData.append('name', fieldName);
240 232
241 - /* remove the attachment details */
242 - jQuery(this).parent().parent('.upload-field-details').next('a').show();
243 - simple_upload_button.show();
244 - simple_upload_button.val('');
245 - jQuery(this).parent().parent('.upload-field-details').remove();
233 + var simpleUploadFields = uploadButton.closest('.wppb-user-forms').find('.wppb_simple_upload');
246 234
247 - jQuery(document).trigger( 'wppb_removed_uploaded_file' )
248 -
235 + simpleUploadFields.each(function() {
236 + jQuery( this ).prop( 'disabled', true );
237 + });
238 +
239 + var submitAlreadyDisabled = false;
240 + var submitButtonText = uploadButton.closest('.wppb-user-forms').find('.submit.button').val();
241 + jQuery("input#"+fieldName).closest('.wppb-user-forms').find('.submit.button').val( 'Uploading file...' );
242 + if ( jQuery( 'p.form-submit .submit.button' ).prop( 'disabled' ) ) {
243 + submitAlreadyDisabled = true;
244 + } else {
245 + jQuery( 'p.form-submit .submit.button' ).prop( 'disabled', true );
246 + }
247 +
248 + jQuery.ajax({
249 + url: wppb_upload_script_vars.ajaxUrl,
250 + type: 'POST',
251 + contentType: false,
252 + processData: false,
253 + data: formData,
254 + success: function(response){
255 + var parsed = JSON.parse(response);
256 +
257 + if ( parsed && parsed.errors ) {
258 + jQuery("input#"+fieldName).val(null);
259 + jQuery('.wppb_simple_upload', jQuery("input#"+fieldName).parent()).val(null);
260 + } else {
261 + jQuery("input#"+fieldName).val(parsed);
262 + }
263 +
264 + if ( !submitAlreadyDisabled ) {
265 + jQuery("p.form-submit .submit.button").prop('disabled', false);
266 + }
267 + simpleUploadFields.each(function() {
268 + jQuery( this ).prop( 'disabled', false );
269 + });
270 + jQuery("input#"+fieldName).closest('.wppb-user-forms').find('.submit.button').val( submitButtonText );
271 + },
272 + });
249 273 }
250 - });
251 - }
252 -});
274 + };
275 + });
276 +}