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

264 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(document).ready(function(){
2 validate_simple_upload();
3
4 jQuery(document).on( 'click', '.wppb-rpf-add', function( event ){
5 validate_simple_upload();
6 });
7
8 if( typeof wp.media != "undefined" ){
9 // Uploading files
10 var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
11
12 jQuery(document).on( 'click', '.wppb_upload_button', function( event ){
13 event.preventDefault();
14
15 var set_to_post_id = ''; // Set this
16
17 var file_frame;
18 var uploadInputId = jQuery( this ).data( 'upload_input' );
19 var uploadButton = jQuery( this );
20
21 /* set default tab to upload file */
22 wp.media.controller.Library.prototype.defaults.contentUserSetting = false;
23 wp.media.controller.Library.prototype.defaults.router = false;
24 wp.media.controller.Library.prototype.defaults.searchable = true;
25 wp.media.controller.Library.prototype.defaults.sortable = false;
26 wp.media.controller.Library.prototype.defaults.date = false;
27
28 // If the media frame already exists, reopen it.
29 if ( file_frame ) {
30 // Set the post ID to what we want
31 file_frame.uploader.uploader.param( 'post_id', set_to_post_id );
32 // Open frame
33 file_frame.open();
34 return;
35 } else {
36 // Set the wp.media post id so the uploader grabs the ID we want when initialised
37 wp.media.model.settings.post.id = set_to_post_id;
38 }
39
40 // Create the media frame.
41 file_frame = wp.media.frames.file_frame = wp.media({
42 title: jQuery( this ).data( 'uploader_title' ),
43 button: {
44 text: jQuery( this ).data( 'uploader_button_text' )
45 },
46 multiple: false // Set to true to allow multiple files to be selected
47 });
48
49 /* send the meta_name of the field */
50 file_frame.uploader.options.uploader['params']['wppb_upload'] = 'true';
51 file_frame.uploader.options.uploader['params']['meta_name'] = jQuery( this ).data( 'upload_mn' );
52
53 // When an image is selected, run a callback.
54 file_frame.on( 'select', function() {
55 // We set multiple to false so only get one image from the uploader
56 attachments = file_frame.state().get('selection').toJSON();
57 var attids = [];
58
59 for( var i=0;i < attachments.length; i++ ){
60 // Do something with attachment.id and/or attachment.url here
61 attids.push( attachments[i].id );
62 result = '<div class="upload-field-details" id="'+ uploadInputId +'_info_container" data-attachment_id="'+ attachments[i].id +'">';
63 if( attachments[i].sizes != undefined ){
64 if( attachments[i].sizes.thumbnail != undefined )
65 thumb = attachments[i].sizes.thumbnail;
66 else
67 thumb = attachments[i].sizes.full;
68 thumbnailUrl = thumb.url;
69 }
70 else{
71 thumbnailUrl = attachments[i].icon;
72 }
73
74 result += '<div class="file-thumb">';
75 result += '<a href="'+ attachments[i].url +'" target="_blank" class="wppb-attachment-link">';
76 result += '<img width="80" height="80" src="'+ thumbnailUrl +'"/>';
77 result += '</a>';
78 result += '</div>';
79 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>';
80
81 // if multiple upload false remove previous upload details
82 if( uploadButton.data( 'multiple_upload' ) == false ){
83 jQuery( '.upload-field-details', uploadButton.parent() ).remove();
84 }
85
86 uploadButton.before( result );
87 uploadButton.hide();
88
89 }
90 // turn into comma separated string
91 attids = attids.join(',');
92 jQuery( 'input[id="'+uploadInputId+'"]', uploadButton.parent() ).val( attids );
93
94 // Restore the main post ID
95 wp.media.model.settings.post.id = wp_media_post_id;
96
97 jQuery(document).trigger( 'wppb_file_uploaded' )
98
99 });
100
101 // Finally, open the modal
102 file_frame.open();
103 // remove tabs from the top ( this is done higher in the code when setting router to false )
104 //jQuery('.media-frame-router').remove();
105
106 jQuery('.media-frame-title').append('<style type="text/css">label.setting, .edit-attachment{display:none !important;}</style>');
107 });
108
109 // Restore the main ID when the add media button is pressed
110 jQuery('a.add_media').on('click', function() {
111 wp.media.model.settings.post.id = wp_media_post_id;
112 });
113
114 jQuery(document).on('keypress', '.wppb-remove-upload', function(e){
115 if(e.which == 13) {
116 jQuery(this).trigger('click');
117 }
118 });
119
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);
142
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();
148
149 jQuery(document).trigger( 'wppb_removed_uploaded_file' )
150
151 }
152 });
153 }
154 });
155
156 function validate_simple_upload(){
157 jQuery("input:file").on('change', function(e){
158 //handle simple upload
159 var uploadInputName = jQuery(this).attr('name');
160 var uploadButton = jQuery(this);
161 var oFReader = new FileReader();
162 var attachment = e.target.files[0];
163 oFReader.readAsDataURL(attachment);
164 oFReader.onload = function (oFREvent) {
165 var src = oFREvent.target.result;
166 var error = '';
167 jQuery("#p_" + uploadInputName).text(error);
168 uploadInputId = uploadInputName.split('-').join('_');
169 //Check allowed upload type by wordpress
170 allowed_mime_types = wppb_allowed_wordpress_formats.allowed_wordpress_formats;
171 allowed_by_wordpress = false;
172 for (var key in allowed_mime_types){
173 allowed_type = allowed_mime_types[key];
174 if (attachment.type === allowed_type){
175 allowed_by_wordpress = true;
176 possible_extensions = key.split('|');
177 }
178 }
179 if (allowed_by_wordpress) {
180 //Check allowed extensions
181 allowed_extensions = jQuery("#allowed_extensions_" + uploadInputId).val();
182 if (allowed_extensions !== '') {
183 var allowed = false;
184 allowed_extensions = allowed_extensions.split(',');
185 for (var i = 0; i < allowed_extensions.length; i++) {
186 if (possible_extensions.includes(allowed_extensions[i])) {
187 allowed = true;
188 }
189 }
190 if (allowed == false) {
191 error = wppb_error_messages.upload_type_error_message;
192 }
193 }
194 //Check size limit — prefer per-field limit, fall back to global
195 var perFieldLimit = jQuery("#size_limit_" + uploadInputId).val();
196 var allowed_size_limit = perFieldLimit ? parseFloat(perFieldLimit) : wppb_limit.size_limit;
197 if (attachment.size > allowed_size_limit) {
198 var limit = allowed_size_limit / (1024 * 1024);
199 error = wppb_error_messages.limit_error_message + limit + 'MB.';
200 }
201 }
202 else{
203 error = wppb_error_messages.upload_type_error_message;
204 }
205 if (error !== '') {
206 jQuery("#p_" + uploadInputName).text(error);
207 uploadButton.val('');
208 } else {
209 var fieldName = uploadInputName.replace(/^(simple_upload_)/,'').replace(/-/g,'_');
210 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]);
214 } else {
215 formData.append('action', 'wppb_ajax_simple_avatar');
216 formData.append(fieldName, jQuery(e.target).prop('files')[0]);
217 }
218 formData.append('nonce', wppb_upload_script_vars.nonce);
219 formData.append('name', fieldName);
220
221 var simpleUploadFields = uploadButton.closest('.wppb-user-forms').find('.wppb_simple_upload');
222
223 simpleUploadFields.each(function() {
224 jQuery( this ).prop( 'disabled', true );
225 });
226
227 var submitAlreadyDisabled = false;
228 var submitButtonText = uploadButton.closest('.wppb-user-forms').find('.submit.button').val();
229 jQuery("input#"+fieldName).closest('.wppb-user-forms').find('.submit.button').val( 'Uploading file...' );
230 if ( jQuery( 'p.form-submit .submit.button' ).prop( 'disabled' ) ) {
231 submitAlreadyDisabled = true;
232 } else {
233 jQuery( 'p.form-submit .submit.button' ).prop( 'disabled', true );
234 }
235
236 jQuery.ajax({
237 url: wppb_upload_script_vars.ajaxUrl,
238 type: 'POST',
239 contentType: false,
240 processData: false,
241 data: formData,
242 success: function(response){
243 var parsed = JSON.parse(response);
244
245 if ( parsed && parsed.errors ) {
246 jQuery("input#"+fieldName).val(null);
247 jQuery('.wppb_simple_upload', jQuery("input#"+fieldName).parent()).val(null);
248 } else {
249 jQuery("input#"+fieldName).val(parsed);
250 }
251
252 if ( !submitAlreadyDisabled ) {
253 jQuery("p.form-submit .submit.button").prop('disabled', false);
254 }
255 simpleUploadFields.each(function() {
256 jQuery( this ).prop( 'disabled', false );
257 });
258 jQuery("input#"+fieldName).closest('.wppb-user-forms').find('.submit.button').val( submitButtonText );
259 },
260 });
261 }
262 };
263 });
264 }