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

274 lines 13.1 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 /* Route to the correct AJAX action based on the field type. Rely on the
212 data-field_type attribute (set for both Upload and Avatar fields) so the
213 routing is correct regardless of layout. Fall back to the container check
214 for older markup that doesn't expose the attribute. The back-end user page
215 doesn't wrap fields in .wppb-upload, so the container check alone would
216 wrongly send Upload fields to the avatar handler. */
217 var fieldType = uploadButton.data('field_type');
218 var uploadAction;
219 if ( fieldType === 'Upload' ) {
220 uploadAction = 'wppb_ajax_simple_upload';
221 } else if ( fieldType === 'Avatar' ) {
222 uploadAction = 'wppb_ajax_simple_avatar';
223 } else {
224 uploadAction = uploadButton.closest('.wppb-upload').length > 0 ? 'wppb_ajax_simple_upload' : 'wppb_ajax_simple_avatar';
225 }
226 formData.append('action', uploadAction);
227 formData.append(fieldName, jQuery(e.target).prop('files')[0]);
228 formData.append('nonce', wppb_upload_script_vars.nonce);
229 formData.append('name', fieldName);
230
231 var simpleUploadFields = uploadButton.closest('.wppb-user-forms').find('.wppb_simple_upload');
232
233 simpleUploadFields.each(function() {
234 jQuery( this ).prop( 'disabled', true );
235 });
236
237 var submitAlreadyDisabled = false;
238 var submitButtonText = uploadButton.closest('.wppb-user-forms').find('.submit.button').val();
239 jQuery("input#"+fieldName).closest('.wppb-user-forms').find('.submit.button').val( 'Uploading file...' );
240 if ( jQuery( 'p.form-submit .submit.button' ).prop( 'disabled' ) ) {
241 submitAlreadyDisabled = true;
242 } else {
243 jQuery( 'p.form-submit .submit.button' ).prop( 'disabled', true );
244 }
245
246 jQuery.ajax({
247 url: wppb_upload_script_vars.ajaxUrl,
248 type: 'POST',
249 contentType: false,
250 processData: false,
251 data: formData,
252 success: function(response){
253 var parsed = JSON.parse(response);
254
255 if ( parsed && parsed.errors ) {
256 jQuery("input#"+fieldName).val(null);
257 jQuery('.wppb_simple_upload', jQuery("input#"+fieldName).parent()).val(null);
258 } else {
259 jQuery("input#"+fieldName).val(parsed);
260 }
261
262 if ( !submitAlreadyDisabled ) {
263 jQuery("p.form-submit .submit.button").prop('disabled', false);
264 }
265 simpleUploadFields.each(function() {
266 jQuery( this ).prop( 'disabled', false );
267 });
268 jQuery("input#"+fieldName).closest('.wppb-user-forms').find('.submit.button').val( submitButtonText );
269 },
270 });
271 }
272 };
273 });
274 }