PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.6
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.6
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
profile-builder / front-end / default-fields / upload / upload.js

upload.js in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.6, at front-end/default-fields/upload/upload.js

253 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 jQuery(document).ready(function(){
100 validate_simple_upload();
101
102 jQuery(document).on( 'click', '.wppb-rpf-add', function( event ){
103 validate_simple_upload();
104 });
105
106 if( typeof wp.media != "undefined" ){
107 // Uploading files
108 var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
109
110 jQuery(document).on( 'click', '.wppb_upload_button', function( event ){
111 event.preventDefault();
112
113 var set_to_post_id = ''; // Set this
114
115 var file_frame;
116 var uploadInputId = jQuery( this ).data( 'upload_input' );
117 var uploadButton = jQuery( this );
118
119 /* set default tab to upload file */
120 wp.media.controller.Library.prototype.defaults.contentUserSetting = false;
121 wp.media.controller.Library.prototype.defaults.router = false;
122 wp.media.controller.Library.prototype.defaults.searchable = true;
123 wp.media.controller.Library.prototype.defaults.sortable = false;
124 wp.media.controller.Library.prototype.defaults.date = false;
125
126 // If the media frame already exists, reopen it.
127 if ( file_frame ) {
128 // Set the post ID to what we want
129 file_frame.uploader.uploader.param( 'post_id', set_to_post_id );
130 // Open frame
131 file_frame.open();
132 return;
133 } else {
134 // Set the wp.media post id so the uploader grabs the ID we want when initialised
135 wp.media.model.settings.post.id = set_to_post_id;
136 }
137
138 // Create the media frame.
139 file_frame = wp.media.frames.file_frame = wp.media({
140 title: jQuery( this ).data( 'uploader_title' ),
141 button: {
142 text: jQuery( this ).data( 'uploader_button_text' )
143 },
144 multiple: false // Set to true to allow multiple files to be selected
145 });
146
147 /* send the meta_name of the field */
148 file_frame.uploader.options.uploader['params']['wppb_upload'] = 'true';
149 file_frame.uploader.options.uploader['params']['meta_name'] = jQuery( this ).data( 'upload_mn' );
150
151 // When an image is selected, run a callback.
152 file_frame.on( 'select', function() {
153 // We set multiple to false so only get one image from the uploader
154 attachments = file_frame.state().get('selection').toJSON();
155 var attids = [];
156
157 for( var i=0;i < attachments.length; i++ ){
158 // Do something with attachment.id and/or attachment.url here
159 attids.push( attachments[i].id );
160 result = '<div class="upload-field-details" id="'+ uploadInputId +'_info_container" data-attachment_id="'+ attachments[i].id +'">';
161 if( attachments[i].sizes != undefined ){
162 if( attachments[i].sizes.thumbnail != undefined )
163 thumb = attachments[i].sizes.thumbnail;
164 else
165 thumb = attachments[i].sizes.full;
166 thumbnailUrl = thumb.url;
167 }
168 else{
169 thumbnailUrl = attachments[i].icon;
170 }
171
172 result += '<div class="file-thumb">';
173 result += '<a href="'+ attachments[i].url +'" target="_blank" class="wppb-attachment-link">';
174 result += '<img width="80" height="80" src="'+ thumbnailUrl +'"/>';
175 result += '</a>';
176 result += '</div>';
177 result += '<p><span class="file-name">'+attachments[i].filename+'</span><span class="file-type">'+attachments[i].mime +'</span><span class="wppb-remove-upload" tabindex="0">'+wppb_upload_script_vars.remove_link_text+'</span></p></div>';
178
179 // if multiple upload false remove previous upload details
180 if( uploadButton.data( 'multiple_upload' ) == false ){
181 jQuery( '.upload-field-details', uploadButton.parent() ).remove();
182 }
183
184 uploadButton.before( result );
185 uploadButton.hide();
186
187 }
188 // turn into comma separated string
189 attids = attids.join(',');
190 jQuery( 'input[id="'+uploadInputId+'"]', uploadButton.parent() ).val( attids );
191
192 // Restore the main post ID
193 wp.media.model.settings.post.id = wp_media_post_id;
194
195 jQuery(document).trigger( 'wppb_file_uploaded' )
196
197 });
198
199 // Finally, open the modal
200 file_frame.open();
201 // remove tabs from the top ( this is done higher in the code when setting router to false )
202 //jQuery('.media-frame-router').remove();
203
204 jQuery('.media-frame-title').append('<style type="text/css">label.setting, .edit-attachment{display:none !important;}</style>');
205 });
206
207 // Restore the main ID when the add media button is pressed
208 jQuery('a.add_media').on('click', function() {
209 wp.media.model.settings.post.id = wp_media_post_id;
210 });
211
212 jQuery(document).on('keypress', '.wppb-remove-upload', function(e){
213 if(e.which == 13) {
214 jQuery(this).trigger('click');
215 }
216 });
217
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);
226 }
227 else{
228 upload_input = jQuery(this).closest('li, td, p.form-row, div.form-row').find('input[type="hidden"]');
229 }
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]);
237 }
238 newuploadAttachments = newuploadAttachments.join(',');
239 upload_input.val(newuploadAttachments);
240
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();
246
247 jQuery(document).trigger( 'wppb_removed_uploaded_file' )
248
249 }
250 });
251 }
252 });
253