PageSourceSearch

https://locusmag.com/wp-content/plugins/checkout-files-upload-wooc…-wc-checkout-files-upload-ajax.js?ver=2.2.6

js locusmag.com collected 2026-09-24 08:37:55 UTC 8,256 bytes, 248 lines download raw bytes

1/**
2 * alg-wc-checkout-files-upload-ajax.js
3 *
4 * @version 2.1.4
5 * @since   1.3.0
6 * @author  Algoritmika Ltd.
7 * @author  WP Wham
8 * @todo    [dev] maybe validate file type with `upload.getType()` (same as `upload.getSize()`)
9 * @todo    [feature] text messages (e.g. `span` or `div`) instead of `alert()`
10 */
11
12(function( $ ) {
13	"use strict";
14	
15	var $container;
16	
17	var queue = [];
18	var activeQueue = [];
19	var maxWorkers = 4;
20	var suppressedErrors = {
21		fail_max_files: false
22	}
23	
24	var processQueue = function() {
25		
26		var key;
27		
28		for ( var i = 0; i < queue.length; i++ ) {
29			if ( activeQueue.length < maxWorkers ) {
30				activeQueue.push( queue.shift() );
31				key = activeQueue.length - 1;
32				activeQueue[key].upload.doUpload( activeQueue[key].fileUploader, key );
33			}
34		}
35		
36		if ( queue.length ) {
37			setTimeout( processQueue, 100 );
38		} else {
39			suppressedErrors.fail_max_files = false;
40		}
41		
42	}
43	
44	var blockCheckout = function() {
45		$container.block({
46			message: null,
47			overlayCSS: {
48				background: '#fff',
49				opacity: 0.6
50			}
51		});
52	};
53	
54	var unblockCheckout = function() {
55		$container.unblock();
56	};
57	
58	var Upload = function ( file ) {
59		this.file = file;
60	};
61	Upload.prototype.getType = function() {
62		return this.file.type;
63	};
64	Upload.prototype.getSize = function() {
65		return this.file.size;
66	};
67	Upload.prototype.getName = function() {
68		return this.file.name;
69	};
70	Upload.prototype.doUpload = function ( fileUploader, queuePosition ) {
71		
72		blockCheckout();
73		
74		var formData = new FormData();
75		formData.append( 'file', this.file, this.getName() );
76		formData.append( 'action', 'alg_ajax_file_upload' );
77		formData.append( 'file_uploader', fileUploader );
78		formData.append( 'order_id', $( '#alg_checkout_files_upload_order_id_' + fileUploader ).val() );
79		formData.append( 'key', $( '#alg_checkout_files_upload_order_key_' + fileUploader ).val() );
80		formData.append( 'nonce', $( '#wpwham-checkout-files-upload-nonce-' + fileUploader ).val() );
81		
82		if ( alg_wc_checkout_files_upload.progress_bar_enabled ) {
83			var progressBarId = 'wpw_cfu_' + Date.now();
84			// grab template div, copy, and populate with stuff needed for this file upload
85			var $progressBarTemplate = $( '#alg-wc-checkout-files-upload-progress-wrapper-' + fileUploader );
86			var $progressBar = $progressBarTemplate.clone();
87			$progressBar.first().attr( 'id', progressBarId );
88			$( '#alg_checkout_files_upload_form_' + fileUploader +
89				' .alg_checkout_files_upload_result_' + fileUploader + ', ' +
90				'#alg_checkout_files_upload_form_' + fileUploader +
91				' .alg-wc-checkout-files-upload-progress-wrapper'
92			).last().after( $progressBar.show() );
93		}
94		
95		$.ajax({
96			type: "POST",
97			url: ajax_object.ajax_url,
98			xhr: function () {
99				var xhr = $.ajaxSettings.xhr();
100				if ( alg_wc_checkout_files_upload.progress_bar_enabled && xhr.upload ) {
101					xhr.upload.fileUploader = fileUploader;
102					xhr.upload.addEventListener( 'progress', function( event ) {
103						if ( event.lengthComputable ) {
104							var percentComplete = ( event.loaded / event.total ) * 100;
105							percentComplete = percentComplete.toFixed( 0 );
106							$( '#' + progressBarId + ' .alg-wc-checkout-files-upload-progress-bar' )
107								.css( 'width', percentComplete + '%' );
108							$( '#' + progressBarId + ' .alg-wc-checkout-files-upload-progress-status' )
109								.text( percentComplete + '%' );
110						}
111					}, false );
112				}
113				return xhr;
114			},
115			success: function (data) {
116				var $template;
117				var $result;
118				var data_decoded = $.parseJSON(data);
119				if ( data_decoded['result'] != 0 ) {
120					// grab template div, copy, and populate with the results of this file upload
121					$template = $( '#alg_checkout_files_upload_result_' + fileUploader );
122					$result = $template.clone();
123					$result.first().removeAttr( 'id' );
124					$result.find( '.alg_checkout_files_upload_result_file_name' )
125						.html( data_decoded['data'] );
126					if ( typeof data_decoded['data_img'] !== 'undefined' && data_decoded['data_img'] > '') {
127						$result.find( '.alg_checkout_files_upload_result_image' )
128							.html( data_decoded['data_img'] );
129					}
130					if ( alg_wc_checkout_files_upload.progress_bar_enabled ) {
131						$result.data( 'progress-bar-id', progressBarId );
132						$( '#' + progressBarId ).before( $result.show() );
133					} else {
134						$( '.alg_checkout_files_upload_result_' + fileUploader ).last()
135							.after( $result.show() );
136					}
137					// if no more file uploads possible, hide button
138					if ( ! $( '#alg_checkout_files_upload_' + fileUploader ).attr( 'multiple' ) ) {
139						$( '#alg_checkout_files_upload_button_' + fileUploader ).hide();
140					}
141				} else {
142					if ( alg_wc_checkout_files_upload.progress_bar_enabled ) {
143						$( '#' + progressBarId ).remove();
144					}
145				}
146				if ( data_decoded['message'] != '' ) {
147					if (
148						typeof data_decoded['error_code'] !== "undefined"
149						&& data_decoded['error_code'] === 'fail_max_files'
150					) {
151						if ( ! suppressedErrors.fail_max_files ) {
152							alert( data_decoded['message'] );
153						}
154						suppressedErrors.fail_max_files = true;
155						queue = [];
156					} else {
157						alert( data_decoded['message'] );
158					}
159				}
160				activeQueue.splice( queuePosition, 1);
161				unblockCheckout();
162			},
163			error: function (error) {
164				activeQueue.splice( queuePosition, 1);
165				unblockCheckout();
166			},
167			async: true,
168			data: formData,
169			cache: false,
170			contentType: false,
171			processData: false,
172			timeout: 60000
173		});
174	};
175
176	$(document).ready(function() {
177	
178		$container = jQuery( 'form.woocommerce-checkout' ).closest( 'div' );
179		
180		$( document ).on( 'click', '.alg_checkout_files_upload_button', function() {
181			var fileUploader = $( this ).data( 'file-uploader' );
182			$( '#alg_checkout_files_upload_' + fileUploader ).click();
183		});
184		
185		$( document ).on( 'change', '.alg_checkout_files_upload_file_input', function() {
186			var files = $( this )[0].files;
187			var fileUploader = $( this ).data( 'file-uploader' );
188			if ( ! files.length ) {
189				return;
190			}
191			for ( var i = 0; i < files.length; i++ ) {
192				var file = files[i];
193				var upload = new Upload( file );
194				var max_file_size = parseInt( alg_wc_checkout_files_upload.max_file_size );
195				if ( max_file_size > 0 && upload.getSize() > max_file_size ) {
196					alert( alg_wc_checkout_files_upload.max_file_size_exceeded_message );
197				} else {
198					queue.push({ 'fileUploader': fileUploader, 'upload': upload });
199				}
200			}
201			processQueue();
202			$( this ).val('');
203		});
204		
205		$( document ).on( 'click', '.alg_checkout_files_upload_result_delete', function( event ) {
206			event.preventDefault();
207			blockCheckout();
208			var $fileContainer = $( this ).closest( 'div' );
209			var fileUploader = $fileContainer.data( 'file-uploader' );
210			var fileKey = $fileContainer.find( '.alg_checkout_files_upload_result_file_name a' )
211				.first().data( 'file-key' );
212			var formData = new FormData();
213			formData.append( 'action', 'alg_ajax_file_delete' );
214			formData.append( 'file_uploader', fileUploader );
215			formData.append( 'file_key', fileKey );
216			formData.append( 'order_id', $( '#alg_checkout_files_upload_order_id_' + fileUploader ).val() );
217			formData.append( 'nonce', $( '#wpwham-checkout-files-upload-nonce-' + fileUploader ).val() );
218			$.ajax({
219				type: "POST",
220				url: ajax_object.ajax_url,
221				success: function ( data ) {
222					var data_decoded = $.parseJSON( data );
223					if ( data_decoded['result'] != 0 ) {
224						if ( alg_wc_checkout_files_upload.progress_bar_enabled ) {
225							$( '#' + $fileContainer.data( 'progress-bar-id' ) ).remove();
226						}
227						$fileContainer.remove();
228						$( '#alg_checkout_files_upload_button_' + fileUploader ).show();
229					}
230					if ( data_decoded['message'] != '' ) {
231						alert( data_decoded['message'] );
232					}
233					unblockCheckout();
234				},
235				error: function (error) {
236					unblockCheckout();
237				},
238				async: true,
239				data: formData,
240				cache: false,
241				contentType: false,
242				processData: false,
243				timeout: 60000
244			});
245		});
246	});
247
248})( jQuery );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.