PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.3
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.3
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / wizard / assets / js / helper.js

helper.js in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.3, at wizard/assets/js/helper.js

499 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * AJAX Request Queue
3 *
4 * - add()
5 * - remove()
6 * - run()
7 * - stop()
8 *
9 * @since x.x.x
10 */
11
12 const CartFlowsAjaxQueue = ( function () {
13 let requests = [];
14
15 return {
16 /**
17 * Add AJAX request
18 *
19 * @param {string} opt selected opt.
20 * @since x.x.x
21 */
22 add( opt ) {
23 requests.push( opt );
24 },
25
26 /**
27 * Remove AJAX request
28 *
29 * @param {string} opt selected opt.
30 * @since x.x.x
31 */
32 remove( opt ) {
33 if ( jQuery.inArray( opt, requests ) > -1 ) {
34 requests.splice( jQuery.inArray( opt, requests ), 1 );
35 }
36 },
37
38 /**
39 * Run / Process AJAX request
40 *
41 * @since x.x.x
42 */
43 run() {
44 const self = this;
45 let oriSuc;
46
47 if ( requests.length ) {
48 oriSuc = requests[ 0 ].complete;
49
50 requests[ 0 ].complete = function () {
51 if ( typeof oriSuc === 'function' ) {
52 oriSuc();
53 }
54 requests.shift();
55 self.run.apply( self, [] );
56 };
57
58 jQuery.ajax( requests[ 0 ] );
59 } else {
60 self.tid = setTimeout( function () {
61 self.run.apply( self, [] );
62 }, 1000 );
63 }
64 },
65
66 /**
67 * Stop AJAX request
68 *
69 * @since x.x.x
70 */
71 stop() {
72 requests = [];
73 clearTimeout( this.tid );
74 },
75 };
76 } )();
77
78 ( function ( $ ) {
79 const CartFlowsWizard = {
80 remaining_install_plugins: 0,
81 remaining_active_plugins: 0,
82
83 init() {
84 this._bind();
85 },
86
87 /**
88 * Bind
89 */
90 _bind() {
91 //Page builder installation & save option
92 $( document )
93 .on(
94 'click',
95 '.wcf-start-setup',
96 CartFlowsWizard._redirect_next_step
97 )
98 .on(
99 'click',
100 '.install-page-builder-plugins',
101 CartFlowsWizard._install_page_builder_plugin
102 )
103 .on(
104 'click',
105 '.wcf-import-global-flow',
106 CartFlowsWizard._import_store_checkout_template
107 )
108 .on(
109 'click',
110 '.install-required-plugins',
111 CartFlowsWizard._install_required_plugins
112 )
113 .on( 'wp-plugin-installing', CartFlowsWizard._pluginInstalling )
114 .on( 'wp-plugin-install-error', CartFlowsWizard._installError )
115 .on(
116 'wp-plugin-install-success',
117 CartFlowsWizard._installSuccess
118 );
119 },
120
121 /**
122 * Dispatch the event to trigger the step redirect.
123 */
124 _redirect_next_step() {
125 // Rediret to Page builder step from home page.
126 const redirect_page_builder_step_event = new Event(
127 'wcf-redirect-page-builder-step'
128 );
129 document.dispatchEvent( redirect_page_builder_step_event );
130 },
131
132 /**
133 * Install Now
134 *
135 * @param {Object} event event data.
136 */
137 _install_page_builder_plugin( event ) {
138 event.preventDefault();
139
140 let plugin_slug = '';
141
142 document.dispatchEvent(
143 new Event( 'wcf-page-builder-plugins-install-processing' )
144 );
145
146 // Selected page builder
147 const plugin_key =
148 $( '#wcf-selected-page-builder' ).attr(
149 'data-selected-pb'
150 ) || '',
151 plugin_data = cartflows_wizard.page_builders[ plugin_key ],
152 is_installed = plugin_data.install,
153 is_activeted = plugin_data.active,
154 plugin_init = plugin_data.init;
155 plugin_slug = plugin_data.slug;
156
157 // Check plugin status first before saving the page builder option.
158 if ( 'yes' === is_installed && 'no' === is_activeted ) {
159 CartFlowsWizard._activatePlugin(
160 plugin_init,
161 plugin_slug,
162 true
163 );
164 } else if ( 'no' === is_installed ) {
165 CartFlowsWizard._installPlugin( plugin_slug );
166 } else {
167 // Save page builder option and continue.
168 save_page_builder_option( plugin_slug );
169 }
170 },
171
172 _install_required_plugins( event ) {
173 event.preventDefault();
174
175 // Fire an event to change the button state to processing.
176 document.dispatchEvent(
177 new Event( 'wcf-install-require-plugins-processing' )
178 );
179
180 const required_plugins = $( 'form.wcf-install-plugin-form' ).find(
181 'input[type=checkbox]'
182 );
183
184 $.each( required_plugins, function ( index, plugin ) {
185 const is_checked = $( plugin ).is( ':checked' ),
186 status = plugin.getAttribute( 'data-status' );
187
188 if ( is_checked ) {
189 if ( 'not-installed' === status ) {
190 CartFlowsWizard.remaining_install_plugins++;
191 }
192 if ( 'inactive' === status ) {
193 CartFlowsWizard.remaining_active_plugins++;
194 }
195 }
196 } );
197
198 // Have any plugin for install?
199 if ( CartFlowsWizard.remaining_install_plugins ) {
200 CartFlowsWizard._install_all_plugins();
201 } else if ( CartFlowsWizard.remaining_active_plugins ) {
202 CartFlowsWizard._activate_all_plugins();
203 } else if (
204 ! CartFlowsWizard.remaining_active_plugins &&
205 ! CartFlowsWizard.remaining_install_plugins
206 ) {
207 trigger_event();
208 }
209 },
210
211 _installPlugin( plugin_slug ) {
212 if (
213 wp.updates.shouldRequestFilesystemCredentials &&
214 ! wp.updates.ajaxLocked
215 ) {
216 wp.updates.requestFilesystemCredentials( event );
217
218 $document.on( 'credential-modal-cancel', function () {
219 const $message = $( '.install-now.updating-message' );
220
221 $message
222 .removeClass( 'updating-message' )
223 .text( wp.updates.l10n.installNow );
224
225 wp.a11y.speak( wp.updates.l10n.updateCancel, 'polite' );
226 } );
227 }
228
229 wp.updates.queue.push( {
230 action: 'install-plugin', // Required action.
231 data: {
232 slug: plugin_slug,
233 },
234 } );
235
236 // Required to set queue.
237 wp.updates.queueChecker();
238 },
239
240 _activatePlugin( plugin_init, plugin_slug ) {
241 const page_builder_slugs = [
242 'elementor',
243 'beaver-builder-lite-version',
244 'divi',
245 'ultimate-addons-for-gutenberg',
246 ];
247
248 $.ajax( {
249 url: ajaxurl,
250 method: 'POST',
251 data: {
252 action: 'cartflows_wizard_activate_plugin',
253 plugin_slug,
254 plugin_init,
255 security: cartflows_wizard.wizard_activate_plugin_nonce,
256 },
257 } )
258 .done( function ( response ) {
259 if ( response.success ) {
260 console.log( plugin_slug + ' activated' );
261 // trigger_event();
262
263 if (
264 jQuery.inArray( plugin_slug, page_builder_slugs ) >
265 -1
266 ) {
267 save_page_builder_option( plugin_slug );
268 }
269 } else {
270 console.log(
271 'Error: ' + response.data && response.data.message
272 ? response.data.message
273 : 'Plugin not activated'
274 );
275 }
276 } )
277 .fail( function () {
278 console.log( 'activation error' );
279 } );
280 },
281
282 /**
283 * Import the store checkout template.
284 *
285 * @param {Object} event
286 */
287 _import_store_checkout_template( event ) {
288 event.preventDefault();
289 // Selected Template's ID.
290 const store_template_flow =
291 $( '#wcf-selected-store-checkout-template' ).attr(
292 'data-selected-flow-info'
293 ) || '',
294 primary_color = $( 'input[name=primary_color]' ).val(),
295 selected_site_logo = $( '.wcf-selected-image' ).data(
296 'logo-data'
297 );
298
299 // Send the event to react for processing when clicked on the footer button.
300 document.dispatchEvent(
301 new Event( 'wcf-store-checkout-import-text-processing' )
302 );
303
304 const has_error = new CustomEvent(
305 'wcf-store-checkout-import-error',
306 {
307 detail: {
308 is_error: false,
309 errorMsg: '',
310 callToAction: '',
311 },
312 }
313 );
314
315 //Import the requested template via ajax call.
316 $.ajax( {
317 url: ajaxurl,
318 method: 'POST',
319 data: {
320 action: 'cartflows_import_store_checkout',
321 security: cartflows_wizard.import_store_checkout_nonce,
322 flow: store_template_flow,
323 primary_color,
324 site_logo: selected_site_logo,
325 },
326 } )
327 .success( function ( res ) {
328 if ( res.success && true === res.data.success ) {
329 console.log( 'Funnels imported successfully.' );
330
331 document.dispatchEvent(
332 new Event( 'wcf-store-checkout-import-success' )
333 );
334 } else {
335 has_error.detail.is_error = true;
336 has_error.detail.errorMsg = res.data.message;
337 has_error.detail.callToAction = res.data.call_to_action;
338
339 document.dispatchEvent( has_error );
340 }
341 } )
342 .fail( function () {
343 has_error.detail.is_error = true;
344 has_error.detail.errorMsg =
345 cartflows_wizard.template_import_errors.api.title;
346 has_error.detail.callToAction =
347 cartflows_wizard.template_import_errors.api.msg;
348 document.dispatchEvent( has_error );
349 } );
350 },
351
352 /**
353 * Installing Plugin
354 *
355 * @param {Object} event event data.
356 */
357 _pluginInstalling( event ) {
358 event.preventDefault();
359 console.log( 'Installing..' );
360 },
361
362 /**
363 * Install Error
364 *
365 * @param {Object} event event data.
366 */
367 _installError( event ) {
368 event.preventDefault();
369 console.log( 'Install Error!' );
370
371 const redirect_link = $( '.wcf-redirect-link' ).attr( 'value' );
372 if ( '' !== redirect_link ) {
373 trigger_event();
374 }
375 },
376
377 /**
378 * Install Success
379 *
380 * @param {Object} event event data.
381 * @param {Array} args args data.
382 */
383 _installSuccess( event, args ) {
384 event.preventDefault();
385 const plugin_init = args.slug + '/' + args.slug + '.php';
386 const plugin_slug = args.slug;
387
388 // WordPress adds "Activate" button after waiting for 1000ms. So we will run our activation after that.
389 setTimeout( function () {
390 CartFlowsWizard._activatePlugin( plugin_init, plugin_slug );
391
392 if ( CartFlowsWizard.remaining_install_plugins > 0 ) {
393 CartFlowsWizard.remaining_install_plugins--;
394 }
395
396 if ( ! CartFlowsWizard.remaining_install_plugins ) {
397 CartFlowsWizard._activate_all_plugins();
398 }
399 }, 1500 );
400 },
401
402 _install_all_plugins() {
403 const required_plugins = $( 'form.wcf-install-plugin-form' ).find(
404 'input[type=checkbox]'
405 );
406
407 $.each( required_plugins, function ( index, plugin ) {
408 const pluginSlug = plugin.getAttribute( 'data-slug' ),
409 status = plugin.getAttribute( 'data-status' ),
410 is_checked = $( plugin ).is( ':checked' );
411
412 if ( is_checked && 'not-installed' === status ) {
413 CartFlowsWizard._installPlugin( pluginSlug );
414 }
415 } );
416 },
417
418 _activate_all_plugins() {
419 if (
420 ! CartFlowsWizard.remaining_active_plugins &&
421 ! CartFlowsWizard.remaining_install_plugins
422 ) {
423 trigger_event();
424 }
425
426 const required_plugins = $( 'form.wcf-install-plugin-form' ).find(
427 'input[type=checkbox]'
428 );
429
430 // Activate All Plugins.
431 CartFlowsAjaxQueue.stop();
432 CartFlowsAjaxQueue.run();
433
434 $.each( required_plugins, function ( index, plugin ) {
435 const pluginSlug = plugin.getAttribute( 'data-slug' ),
436 status = plugin.getAttribute( 'data-status' ),
437 is_checked = $( plugin ).is( ':checked' ),
438 plugin_init = pluginSlug + '/' + pluginSlug + '.php';
439
440 if ( is_checked && 'inactive' === status ) {
441 CartFlowsAjaxQueue.add( {
442 url: ajaxurl,
443 type: 'POST',
444 data: {
445 action: 'cartflows_wizard_activate_plugin',
446 plugin_slug: pluginSlug,
447 plugin_init,
448 security:
449 cartflows_wizard.wizard_activate_plugin_nonce,
450 },
451 success() {
452 CartFlowsWizard.remaining_active_plugins--;
453
454 if (
455 ! CartFlowsWizard.remaining_active_plugins &&
456 ! CartFlowsWizard.remaining_install_plugins
457 ) {
458 trigger_event();
459 }
460 },
461 } );
462 }
463 } );
464 },
465 };
466
467 function trigger_event() {
468 const custom_event = new Event( 'wcf-plugins-install-success' );
469 document.dispatchEvent( custom_event );
470 }
471
472 function save_page_builder_option( plugin_slug ) {
473 //Save page builder option in global settings.
474 $.ajax( {
475 url: ajaxurl,
476 method: 'POST',
477 data: {
478 action: 'cartflows_page_builder_save_option',
479 security: cartflows_wizard.page_builder_save_option_nonce,
480 page_builder: plugin_slug,
481 },
482 } )
483 .success( function () {
484 console.log( 'Option Saved Successfully.' );
485
486 document.dispatchEvent(
487 new Event( 'wcf-page-builder-plugins-install-success' )
488 );
489 } )
490 .fail( function () {
491 console.log( 'error' );
492 } );
493 }
494
495 $( function () {
496 CartFlowsWizard.init();
497 } );
498 } )( jQuery );
499