PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.2
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
jetpack / modules / widgets / simple-payments / customizer.js

customizer.js in Jetpack – WP Security, Backup, Speed, & Growth 7.6.2, at modules/widgets/simple-payments/customizer.js

456 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global jQuery, jpSimplePaymentsStrings, confirm, _ */
2 /* eslint no-var: 0, quote-props: 0 */
3
4 ( function( api, wp, $ ) {
5 var $document = $( document );
6
7 $document.ready( function() {
8 $document.on( 'widget-added', function( event, widgetContainer ) {
9 if ( widgetContainer.is( '[id*="jetpack_simple_payments_widget"]' ) ) {
10 initWidget( widgetContainer );
11 }
12 } );
13
14 $document.on( 'widget-synced widget-updated', function( event, widgetContainer ) {
15 //this fires for all widgets, this prevent errors for non SP widgets
16 if ( ! widgetContainer.is( '[id*="jetpack_simple_payments_widget"]' ) ) {
17 return;
18 }
19
20 event.preventDefault();
21
22 syncProductLists();
23
24 var widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' );
25
26 enableFormActions( widgetForm );
27
28 updateProductImage( widgetForm );
29 } );
30 } );
31
32 function initWidget( widgetContainer ) {
33 var widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' );
34
35 //Add New Button
36 widgetForm
37 .find( '.jetpack-simple-payments-add-product' )
38 .on( 'click', showAddNewForm( widgetForm ) );
39 //Edit Button
40 widgetForm
41 .find( '.jetpack-simple-payments-edit-product' )
42 .on( 'click', showEditForm( widgetForm ) );
43 //Select an Image
44 widgetForm
45 .find(
46 '.jetpack-simple-payments-image-fieldset .placeholder, .jetpack-simple-payments-image > img'
47 )
48 .on( 'click', selectImage( widgetForm ) );
49 //Remove Image Button
50 widgetForm
51 .find( '.jetpack-simple-payments-remove-image' )
52 .on( 'click', removeImage( widgetForm ) );
53 //Save Product button
54 widgetForm
55 .find( '.jetpack-simple-payments-save-product' )
56 .on( 'click', saveChanges( widgetForm ) );
57 //Cancel Button
58 widgetForm
59 .find( '.jetpack-simple-payments-cancel-form' )
60 .on( 'click', clearForm( widgetForm ) );
61 //Delete Selected Product
62 widgetForm
63 .find( '.jetpack-simple-payments-delete-product' )
64 .on( 'click', deleteProduct( widgetForm ) );
65 //Input, Select and Checkbox change
66 widgetForm.find( 'select, input, textarea, checkbox' ).on(
67 'change input propertychange',
68 _.debounce( function() {
69 disableFormActions( widgetForm );
70 }, 250 )
71 );
72 }
73
74 function syncProductLists() {
75 var request = wp.ajax.post( 'customize-jetpack-simple-payments-buttons-get', {
76 'customize-jetpack-simple-payments-nonce':
77 api.settings.nonce[ 'customize-jetpack-simple-payments' ],
78 customize_changeset_uuid: api.settings.changeset.uuid,
79 } );
80
81 request.done( function( data ) {
82 var selectedProduct = 0;
83
84 $( document )
85 .find( 'select.jetpack-simple-payments-products' )
86 .each( function( index, select ) {
87 var $select = $( select );
88 selectedProduct = $select.val();
89
90 $select.find( 'option' ).remove();
91 $select.append(
92 $.map( data, function( product ) {
93 return $( '<option>', { value: product.ID, text: product.post_title } );
94 } )
95 );
96 $select.val( selectedProduct );
97 } );
98 } );
99 }
100
101 function showForm( widgetForm ) {
102 //reset validations
103 widgetForm.find( '.invalid' ).removeClass( 'invalid' );
104 //disable widget title and product selector
105 widgetForm
106 .find( '.jetpack-simple-payments-widget-title' )
107 .add( '.jetpack-simple-payments-products' )
108 //disable add and edit buttons
109 .add( '.jetpack-simple-payments-add-product' )
110 .add( '.jetpack-simple-payments-edit-product' )
111 //disable save, delete and cancel until the widget update event is fired
112 .add( '.jetpack-simple-payments-save-product' )
113 .add( '.jetpack-simple-payments-cancel-form' )
114 .add( '.jetpack-simple-payments-delete-product' )
115 .attr( 'disabled', 'disabled' );
116 //show form
117 widgetForm.find( '.jetpack-simple-payments-form' ).show();
118 }
119
120 function hideForm( widgetForm ) {
121 //enable widget title and product selector
122 widgetForm
123 .find( '.jetpack-simple-payments-widget-title' )
124 .add( '.jetpack-simple-payments-products' )
125 .removeAttr( 'disabled' );
126 //hide the form
127 widgetForm.find( '.jetpack-simple-payments-form' ).hide();
128 }
129
130 function changeFormAction( widgetForm, action ) {
131 widgetForm
132 .find( '.jetpack-simple-payments-form-action' )
133 .val( action )
134 .change();
135 }
136
137 function showAddNewForm( widgetForm ) {
138 return function( event ) {
139 event.preventDefault();
140
141 showForm( widgetForm );
142 changeFormAction( widgetForm, 'add' );
143 };
144 }
145
146 function showEditForm( widgetForm ) {
147 return function( event ) {
148 event.preventDefault();
149
150 showForm( widgetForm );
151 changeFormAction( widgetForm, 'edit' );
152 };
153 }
154
155 function clearForm( widgetForm ) {
156 return function( event ) {
157 event.preventDefault();
158
159 hideForm( widgetForm );
160 widgetForm
161 .find( '.jetpack-simple-payments-add-product, .jetpack-simple-payments-edit-product' )
162 .attr( 'disabled', 'disabled' );
163 changeFormAction( widgetForm, 'clear' );
164 };
165 }
166
167 function enableFormActions( widgetForm ) {
168 var isFormVisible = widgetForm.find( '.jetpack-simple-payments-form' ).is( ':visible' );
169 var isProductSelectVisible = widgetForm
170 .find( '.jetpack-simple-payments-products' )
171 .is( ':visible' ); //areProductsVisible ?
172 var isEdit = widgetForm.find( '.jetpack-simple-payments-form-action' ).val() === 'edit';
173
174 if ( isFormVisible ) {
175 widgetForm
176 .find( '.jetpack-simple-payments-save-product' )
177 .add( '.jetpack-simple-payments-cancel-form' )
178 .removeAttr( 'disabled' );
179 } else {
180 widgetForm.find( '.jetpack-simple-payments-add-product' ).removeAttr( 'disabled' );
181 }
182
183 if ( isFormVisible && isEdit ) {
184 widgetForm.find( '.jetpack-simple-payments-delete-product' ).removeAttr( 'disabled' );
185 }
186
187 if ( isProductSelectVisible && ! isFormVisible ) {
188 widgetForm.find( '.jetpack-simple-payments-edit-product' ).removeAttr( 'disabled' );
189 }
190 }
191
192 function disableFormActions( widgetForm ) {
193 widgetForm
194 .find( '.jetpack-simple-payments-add-product' )
195 .add( '.jetpack-simple-payments-edit-product' )
196 .add( '.jetpack-simple-payments-save-product' )
197 .add( '.jetpack-simple-payments-cancel-form' )
198 .add( '.jetpack-simple-payments-delete-product' )
199 .attr( 'disabled', 'disabled' );
200 }
201
202 function selectImage( widgetForm ) {
203 return function( event ) {
204 event.preventDefault();
205
206 var imageContainer = widgetForm.find( '.jetpack-simple-payments-image' );
207
208 var mediaFrame = new wp.media.view.MediaFrame.Select( {
209 title: 'Choose Product Image',
210 multiple: false,
211 library: { type: 'image' },
212 button: { text: 'Choose Image' },
213 } );
214
215 mediaFrame.on( 'select', function() {
216 var selection = mediaFrame
217 .state()
218 .get( 'selection' )
219 .first()
220 .toJSON();
221 //hide placeholder
222 widgetForm.find( '.jetpack-simple-payments-image-fieldset .placeholder' ).hide();
223
224 //load image from media library
225 imageContainer
226 .find( 'img' )
227 .attr( 'src', selection.url )
228 .show();
229
230 //show image and remove button
231 widgetForm.find( '.jetpack-simple-payments-image' ).show();
232
233 //set hidden field for the selective refresh
234 widgetForm
235 .find( '.jetpack-simple-payments-form-image-id' )
236 .val( selection.id )
237 .change();
238 } );
239
240 mediaFrame.open();
241 };
242 }
243
244 function removeImage( widgetForm ) {
245 return function( event ) {
246 event.preventDefault();
247
248 //show placeholder
249 widgetForm.find( '.jetpack-simple-payments-image-fieldset .placeholder' ).show();
250
251 //hide image and remove button
252 widgetForm.find( '.jetpack-simple-payments-image' ).hide();
253
254 //set hidden field for the selective refresh
255 widgetForm
256 .find( '.jetpack-simple-payments-form-image-id' )
257 .val( '' )
258 .change();
259 };
260 }
261
262 function updateProductImage( widgetForm ) {
263 var newImageId = parseInt(
264 widgetForm.find( '.jetpack-simple-payments-form-image-id' ).val(),
265 10
266 );
267 var newImageSrc = widgetForm.find( '.jetpack-simple-payments-form-image-src' ).val();
268
269 var placeholder = widgetForm.find( '.jetpack-simple-payments-image-fieldset .placeholder' );
270 var image = widgetForm.find( '.jetpack-simple-payments-image > img' );
271 var imageControls = widgetForm.find( '.jetpack-simple-payments-image' );
272
273 if ( newImageId && newImageSrc ) {
274 image.attr( 'src', newImageSrc );
275 placeholder.hide();
276 imageControls.show();
277 } else {
278 placeholder.show();
279 image.removeAttr( 'src' );
280 imageControls.hide();
281 }
282 }
283
284 function decimalPlaces( number ) {
285 var parts = number.split( '.' );
286 if ( parts.length > 2 ) {
287 return null;
288 }
289
290 return parts[ 1 ] ? parts[ 1 ].length : 0;
291 }
292
293 function isFormValid( widgetForm ) {
294 widgetForm.find( '.invalid' ).removeClass( 'invalid' );
295
296 var errors = false;
297
298 var postTitle = widgetForm.find( '.jetpack-simple-payments-form-product-title' ).val();
299 if ( ! postTitle ) {
300 widgetForm.find( '.jetpack-simple-payments-form-product-title' ).addClass( 'invalid' );
301 errors = true;
302 }
303
304 var productPrice = widgetForm.find( '.jetpack-simple-payments-form-product-price' ).val();
305 if ( ! productPrice || isNaN( productPrice ) || parseFloat( productPrice ) <= 0 ) {
306 widgetForm.find( '.jetpack-simple-payments-form-product-price' ).addClass( 'invalid' );
307 errors = true;
308 }
309
310 // Japan's Yen is the only supported currency with a zero decimal precision.
311 var precision =
312 widgetForm.find( '.jetpack-simple-payments-form-product-currency' ).val() === 'JPY' ? 0 : 2;
313 var priceDecimalPlaces = decimalPlaces( productPrice );
314 if ( priceDecimalPlaces === null || priceDecimalPlaces > precision ) {
315 widgetForm.find( '.jetpack-simple-payments-form-product-price' ).addClass( 'invalid' );
316 errors = true;
317 }
318
319 var productEmail = widgetForm.find( '.jetpack-simple-payments-form-product-email' ).val();
320 var isProductEmailValid = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(
321 productEmail
322 );
323 if ( ! productEmail || ! isProductEmailValid ) {
324 widgetForm.find( '.jetpack-simple-payments-form-product-email' ).addClass( 'invalid' );
325 errors = true;
326 }
327
328 return ! errors;
329 }
330
331 function saveChanges( widgetForm ) {
332 return function( event ) {
333 event.preventDefault();
334 var productPostId = widgetForm.find( '.jetpack-simple-payments-form-product-id' ).val();
335
336 if ( ! isFormValid( widgetForm ) ) {
337 return;
338 }
339
340 disableFormActions( widgetForm );
341
342 widgetForm.find( '.spinner' ).show();
343
344 var request = wp.ajax.post( 'customize-jetpack-simple-payments-button-save', {
345 'customize-jetpack-simple-payments-nonce':
346 api.settings.nonce[ 'customize-jetpack-simple-payments' ],
347 customize_changeset_uuid: api.settings.changeset.uuid,
348 params: {
349 product_post_id: productPostId,
350 post_title: widgetForm.find( '.jetpack-simple-payments-form-product-title' ).val(),
351 post_content: widgetForm
352 .find( '.jetpack-simple-payments-form-product-description' )
353 .val(),
354 image_id: widgetForm.find( '.jetpack-simple-payments-form-image-id' ).val(),
355 currency: widgetForm.find( '.jetpack-simple-payments-form-product-currency' ).val(),
356 price: widgetForm.find( '.jetpack-simple-payments-form-product-price' ).val(),
357 multiple: widgetForm
358 .find( '.jetpack-simple-payments-form-product-multiple' )
359 .is( ':checked' )
360 ? 1
361 : 0,
362 email: widgetForm.find( '.jetpack-simple-payments-form-product-email' ).val(),
363 },
364 } );
365
366 request.done( function( data ) {
367 var select = widgetForm.find( 'select.jetpack-simple-payments-products' );
368 var productOption = select.find( 'option[value="' + productPostId + '"]' );
369
370 if ( productOption.length > 0 ) {
371 productOption.text( data.product_post_title );
372 } else {
373 select.append(
374 $( '<option>', {
375 value: data.product_post_id,
376 text: data.product_post_title,
377 } )
378 );
379 select.val( data.product_post_id ).change();
380 }
381
382 widgetForm.find( '.jetpack-simple-payments-products-fieldset' ).show();
383 widgetForm.find( '.jetpack-simple-payments-products-warning' ).hide();
384
385 changeFormAction( widgetForm, 'clear' );
386 hideForm( widgetForm );
387 } );
388
389 request.fail( function( data ) {
390 var validCodes = {
391 post_title: 'product-title',
392 price: 'product-price',
393 email: 'product-email',
394 };
395
396 data.forEach( function( item ) {
397 if ( validCodes.hasOwnProperty( item.code ) ) {
398 widgetForm
399 .find( '.jetpack-simple-payments-form-' + validCodes[ item.code ] )
400 .addClass( 'invalid' );
401 }
402 } );
403
404 enableFormActions( widgetForm );
405 } );
406 };
407 }
408
409 function deleteProduct( widgetForm ) {
410 return function( event ) {
411 event.preventDefault();
412
413 if ( ! confirm( jpSimplePaymentsStrings.deleteConfirmation ) ) {
414 return;
415 }
416
417 var formProductId = parseInt(
418 widgetForm.find( '.jetpack-simple-payments-form-product-id' ).val(),
419 10
420 );
421 if ( ! formProductId ) {
422 return;
423 }
424
425 disableFormActions( widgetForm );
426
427 widgetForm.find( '.spinner' ).show();
428
429 var request = wp.ajax.post( 'customize-jetpack-simple-payments-button-delete', {
430 'customize-jetpack-simple-payments-nonce':
431 api.settings.nonce[ 'customize-jetpack-simple-payments' ],
432 customize_changeset_uuid: api.settings.changeset.uuid,
433 params: {
434 product_post_id: formProductId,
435 },
436 } );
437
438 request.done( function() {
439 var productList = widgetForm.find( 'select.jetpack-simple-payments-products' )[ 0 ];
440 productList.remove( productList.selectedIndex );
441 productList.dispatchEvent( new Event( 'change' ) );
442
443 if ( $( productList ).has( 'option' ).length === 0 ) {
444 //hide products select and label
445 widgetForm.find( '.jetpack-simple-payments-products-fieldset' ).hide();
446 //show empty products list warning
447 widgetForm.find( '.jetpack-simple-payments-products-warning' ).show();
448 }
449
450 changeFormAction( widgetForm, 'clear' );
451 hideForm( widgetForm );
452 } );
453 };
454 }
455 } )( wp.customize, wp, jQuery );
456