PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.0
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 / assets / js / import.js

import.js in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.0, at assets/js/import.js

1,224 lines 34.2 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 1.0.0
10 */
11
12 var CartFlowsAjaxQueue = (function() {
13
14 var requests = [];
15
16 return {
17
18 /**
19 * Add AJAX request
20 *
21 * @since 1.0.0
22 */
23 add: function(opt) {
24 requests.push(opt);
25 },
26
27 /**
28 * Remove AJAX request
29 *
30 * @since 1.0.0
31 */
32 remove: function(opt) {
33 if( jQuery.inArray(opt, requests) > -1 )
34 requests.splice($.inArray(opt, requests), 1);
35 },
36
37 /**
38 * Run / Process AJAX request
39 *
40 * @since 1.0.0
41 */
42 run: function() {
43 var self = this,
44 oriSuc;
45
46 if( requests.length ) {
47 oriSuc = requests[0].complete;
48
49 requests[0].complete = function() {
50 if( typeof(oriSuc) === 'function' ) oriSuc();
51 requests.shift();
52 self.run.apply(self, []);
53 };
54
55 jQuery.ajax(requests[0]);
56
57 } else {
58
59 self.tid = setTimeout(function() {
60 self.run.apply(self, []);
61 }, 1000);
62 }
63 },
64
65 /**
66 * Stop AJAX request
67 *
68 * @since 1.0.0
69 */
70 stop: function() {
71
72 requests = [];
73 clearTimeout(this.tid);
74 }
75 };
76
77 }());
78
79
80 (function($){
81
82 CartFlowsImport = {
83
84 doc : $( document ),
85 wrap : $( '.wcf-flow-steps-data-wrap' ),
86 inner : $( '.wcf-flow-steps-data-wrap-importer' ),
87 post_id : $( '#post_ID' ).val(),
88
89 _ref : null,
90
91 _api_step_type : {},
92 _api_params : {},
93
94 all_steps : 0,
95 remaining_steps : 0,
96
97 /**
98 * Init
99 */
100 init: function()
101 {
102 this._bind();
103
104 if( $('.wcf-step-wrap').length ) {
105 this._set_category_slugs();
106 }
107
108 if( $('.post-type-cartflows_flow').hasClass('edit-php') ) {
109 this._cache_remote_flows();
110 }
111 if( $('.post-type-cartflows_flow').hasClass('post-php') ) {
112 this._cache_remote_steps();
113 }
114
115 if( $('.post-type-cartflows_flow').hasClass('edit-php') && null !== this._getParamFromURL('add-new-flow') ) {
116 this._render_remote_flows();
117 }
118
119 if( $('.post-type-cartflows_flow').hasClass('post-php') && null !== this._getParamFromURL('add-new-step') ) {
120 this._render_remote_steps();
121 }
122
123 if( this._getParamFromURL('highlight-step-id') ) {
124 var selector = $('.wcf-step-wrap[data-id="'+this._getParamFromURL('highlight-step-id')+'"]');
125 if( selector.length ) {
126 $('html, body').animate({
127 scrollTop: selector.offset().top
128 }, 1500);
129 }
130 }
131 },
132
133 /**
134 * Get URL param.
135 */
136 _getParamFromURL: function(name, url)
137 {
138 if (!url) url = window.location.href;
139 name = name.replace(/[\[\]]/g, "\\$&");
140 var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
141 results = regex.exec(url);
142 if (!results) return null;
143 if (!results[2]) return '';
144 return decodeURIComponent(results[2].replace(/\+/g, " "));
145 },
146
147 _set_category_slugs: function() {
148
149 // API Request.
150 var api_params = {
151 _fields : CartFlowsImportVars.step_type_fields.toString(),
152 };
153
154 var api_post = {
155 slug : CartFlowsImportVars.step_type + '?' + decodeURIComponent( $.param( api_params ) ),
156 };
157
158 CartFlowsAPI._api_request( api_post, function( data ) {
159 if( parseInt( data.items_count ) && $.isEmptyObject( CartFlowsImport._api_step_type ) ) {
160 CartFlowsImport._api_step_type = data.items;
161 }
162 } );
163 },
164
165 /**
166 * Binds events
167 */
168 _bind: function()
169 {
170 var self = CartFlowsImport;
171
172 self.doc.on('click', '.cartflows-flow-import-blank', self._create_default_flow);
173 self.doc.on('click', '#wcf-remote-flow-importer .wcf-page-builder-links a', self._show_page_bulider_for_flow);
174 // self.doc.on('click', '#wcf-remote-flow-importer .filter-links a', self._filterFlowClick );
175 self.doc.on('click', '#wcf-remote-step-importer .wcf-page-builder-links a', self._show_page_bulider_for_step);
176 self.doc.on('click', '#wcf-remote-step-importer .step-type-filter-links a', self._filterStepClick );
177
178 self.doc.on('click' , '.cartflows-step-import-blank:not(.get-pro)', self._create_blank_step);
179 self.doc.on('click' , '#wcf-remote-step-importer .cartflows-step-import', self._import_step);
180 self.doc.on('click' , '#wcf-remote-flow-importer .cartflows-step-import', self._import_flow);
181
182 self.doc.on('click' , '.cartflows-preview-flow-step', self._preview_individual);
183
184 self.doc.on( 'add_template_to_page-fail', self._add_template_to_page_fail);
185
186 // self.doc.on( 'trigger-on-templates-load', self._on_templates_load );
187
188 $( 'body' ).on('thickbox:iframe:loaded', self._previewLoaded );
189
190 // Event's for API request.
191 // $( document ).on('keyup input', '#wcf-remote-flow-importer .wcf-flow-search-input', self._remote_flow_search );
192 $( document ).on('keyup input', '#wcf-remote-step-importer .wcf-flow-search-input', self._remote_step_search );
193
194 $( document ).on('click', '.actions a', self._previewResponsive );
195
196 $( document ).on( 'click', '.page-title-action', self._render_remote_flows );
197 $( document ).on( 'click', '.wcf-trigger-popup', self._render_remote_steps );
198
199 $( document ).on( 'click', '.wcf-templates-popup-overlay', self._close_template_popup );
200 $( document ).on( 'click', '.wcf-popup-close-wrap .close-icon', self._close_template_popup );
201
202 // $( document ).on( 'click', '.wcf-tab > li > a', self._switch_step_tab );
203 },
204
205 _switch_step_tab: function( event ) {
206
207 event.preventDefault();//stop browser to take action for clicked anchor
208
209 //get displaying tab content jQuery selector
210 var active_tab_selector = $('.wcf-tab > li.active > a').attr('href');
211
212 //find actived navigation and remove 'active' css
213 var actived_nav = $('.wcf-tab > li.active');
214 actived_nav.removeClass('active');
215
216 //add 'active' css into clicked navigation
217 $(this).parents('li').addClass('active');
218
219 //hide displaying tab content
220 $(active_tab_selector).removeClass('active');
221 $(active_tab_selector).addClass('hide');
222
223 //show target tab content
224 var target_tab_selector = $(this).attr('href');
225 $(target_tab_selector).removeClass('hide');
226 $(target_tab_selector).addClass('active');
227 },
228
229 /**
230 * Search Site.
231 *
232 * Prepare Before API Request:
233 * - Remove Inline Height
234 * - Added 'hide-me' class to hide the 'No more sites!' string.
235 * - Added 'loading-content' for body.
236 * - Show spinner.
237 */
238 _remote_step_search: function( event ) {
239
240 event.preventDefault();//stop browser to take action for clicked anchor
241
242 // Remove all filter classes.
243 $('.step-type-filter-links').find('a').removeClass('current');
244 $('.step-type-filter-links').find('li:first-child a').addClass('current');
245
246 window.clearTimeout(CartFlowsImport._ref);
247 CartFlowsImport._ref = window.setTimeout(function () {
248 CartFlowsImport._ref = null;
249 CartFlowsImport._showSteps();
250 }, 500);
251
252 },
253
254 /**
255 * Responsive On Click.
256 */
257 _previewResponsive: function( event ) {
258
259 event.preventDefault();
260
261 var icon = $(this).find('.dashicons');
262
263 var viewClass = icon.attr('data-view') || '';
264
265 $('#TB_window').removeClass( 'desktop tablet mobile' );
266 $('#TB_window').addClass( viewClass );
267
268 $('.actions .dashicons').removeClass('active');
269 icon.addClass('active');
270
271 $('#TB_iframeContent').removeClass();
272 $('#TB_iframeContent').addClass( viewClass );
273
274 },
275
276 /**
277 * On Filter Clicked
278 */
279 _filterStepClick: function( event ) {
280
281 event.preventDefault();
282
283 $(this).parents('.step-type-filter-links').find('a').removeClass('current');
284 $(this).addClass('current');
285
286 $step_type = $(this).data('slug');
287
288 if ( 'upsell' === $step_type || 'downsell' === $step_type ) {
289 $( '.wcf-template-notice' ).show();
290
291 }else{
292 $( '.wcf-template-notice' ).hide();
293 }
294
295 CartFlowsImport._showSteps();
296
297 if( '' == CartFlowsImportVars._is_pro_active && ( 'upsell' == $step_type || 'downsell' == $step_type ) ) {
298 console.log( 'here' );
299 $('.cartflows-step-import-blank').text( 'Get Pro' );
300 $('.cartflows-step-import-blank').attr( 'href', CartFlowsImportVars.domain_url );
301 $('.cartflows-step-import-blank').attr( 'target', '_blank' );
302 $('.cartflows-step-import-blank').addClass('get-pro');
303 // // $('#wcf-remote-step-list').find('.cartflows-step-import-blank').attr( 'class', 'button button-primary' );
304 } else {
305 $('.cartflows-step-import-blank').text( 'Create' );
306 $('.cartflows-step-import-blank').removeClass( 'get-pro' );
307 $('.cartflows-step-import-blank').removeAttr( 'target' );
308 }
309
310 },
311
312 _showSteps: function() {
313
314 // Add Params for API request.
315 var api_params = {
316 licence_args : CartFlowsImportVars.licence_args,
317 per_page : 15,
318 _fields : CartFlowsImportVars.step_fields.toString(),
319 };
320
321 // Type.
322 var type = $('#wcf-remote-step-filters .step-type-filter-links').find('.current').data('group') || '';
323 var step_type = $('#wcf-remote-step-filters .step-type-filter-links').find('.current').data('slug') || '';
324 if( '' !== type && 'all' !== type ) {
325 api_params[ CartFlowsImportVars.step_type ] = type;
326 }
327
328 // API Request.
329 var api_post = {
330 slug : CartFlowsImportVars.step + '?' + decodeURIComponent( $.param( api_params ) ),
331 };
332
333 CartFlowsAPI._api_request( api_post, function( data ) {
334
335 data.current_step_type = step_type;
336
337 var template = wp.template('cartflows-steps-list');
338 // if( parseInt( data.items_count ) ) {
339 $('#wcf-remote-step-list').html( template( data ) );
340 $('#wcf-remote-step-list').prepend( wp.template('cartflows-create-blank-step') );
341 // } else {
342 // $('#wcf-remote-step-list').html( wp.template('cartflows-no-steps') );
343 // }
344 } );
345 },
346
347 _apiAddParam_per_page: function() {
348 CartFlowsImport._api_params['per_page'] = 15;
349 },
350
351 // Add Params for API request.
352 _apiAddParam_licence_args: function() {
353 CartFlowsImport._api_params['licence_args'] = CartFlowsImportVars.licence_args;
354 },
355
356 // Add 'search'
357 _apiAddParam_search: function() {
358 var search_val = $('.wcf-flow-search-input').val() || '';
359 if( '' !== search_val ) {
360 CartFlowsImport._api_params['search'] = search_val;
361 }
362 },
363
364 _close_popup: function() {
365 $('#cartflows-steps').fadeOut();
366 $('body').removeClass('cartflows-popup-is-open');
367 },
368
369 _post_auto_save: function() {
370 var post_title = $( '#title' );
371 var post_prompt_text = $( '#title-prompt-text' );
372 var self = CartFlowsImport;
373
374 if ( ! post_title.val() ) {
375 post_title.val( 'CartFlows #' + self.post_id );
376 if( post_prompt_text.length ) {
377 post_prompt_text.remove();
378 }
379 }
380
381 if ( wp.autosave ) {
382 wp.autosave.server.triggerSave();
383 }
384 },
385
386 _cache_remote_flows: function() {
387
388 // Add Params for API request.
389 var api_params = {
390 licence_args : CartFlowsImportVars.licence_args,
391 hide_empty : true,
392 _fields : CartFlowsImportVars.flow_type_fields.toString(),
393 };
394
395 // API Request.
396 var api_post = {
397 slug : CartFlowsImportVars.flow_type + '?'+decodeURIComponent( $.param( api_params ) ),
398 wrapper_class : 'flow-type-filter-links filter-links',
399 show_all : false,
400 };
401
402 CartFlowsAPI._api_request( api_post, function( data ) {
403 var template = wp.template('cartflows-term-filters');
404 $('#cartflows-flow-funnel-type').html(template( data ));
405 $('#cartflows-flow-funnel-type').find('li:first a').addClass('current');
406 } );
407
408 // Add Params for API request.
409 var api_params = {
410 licence_args : CartFlowsImportVars.licence_args,
411 _fields : CartFlowsImportVars.flow_fields.toString(),
412 };
413
414 // API Request.
415 var api_post = {
416 slug : CartFlowsImportVars.flow + '?' + decodeURIComponent( $.param( api_params ) ),
417 };
418
419 CartFlowsAPI._api_request( api_post, function( data ) {
420 var template = wp.template('cartflows-flows-list');
421 // if( parseInt( data.items_count ) ) {
422 $('#wcf-remote-flow-list').html( template( data ) );
423
424 $('#wcf-remote-flow-list').prepend( wp.template('cartflows-create-blank-flow') );
425
426
427
428 // } else {
429 // $('#wcf-remote-flow-list').html( wp.template('cartflows-no-flows') );
430 // }
431 } );
432
433 },
434
435 _render_remote_flows: function( event ) {
436
437 if( event ) {
438 event.preventDefault();
439 }
440
441 $("#wcf-remote-flow-importer").addClass('open');
442
443 $("html").addClass('wcf-popup-open');
444
445 $("#wcf-remote-flow-importer .spinner").css( 'visibility', 'hidden' );
446 },
447
448 _cache_remote_steps: function() {
449
450 // Disable the button until caching the data.
451 $( 'html' ).addClass('wcf-steps-loading');
452
453 // Add Params for API request.
454 var api_params = {
455 licence_args : CartFlowsImportVars.licence_args,
456 _fields : CartFlowsImportVars.step_type_fields.toString(),
457 };
458
459 // API Request.
460 var api_post = {
461 slug : CartFlowsImportVars.step_type + '?'+decodeURIComponent( $.param( api_params ) ),
462 wrapper_class : 'step-type-filter-links filter-links',
463 show_all : false,
464 };
465
466 CartFlowsAPI._api_request( api_post, function( data ) {
467
468 var step_type_response_data = data;
469 var step_types_count = data.items_count;
470
471 // Send other request for JS caching.
472 if( data.items ) {
473 for ( key in data.items ) {
474
475 // Add Params for API request.
476 var api_params = {
477 licence_args : CartFlowsImportVars.licence_args,
478 per_page : 15,
479 _fields : CartFlowsImportVars.step_fields.toString(),
480 };
481
482 api_params[ CartFlowsImportVars.step_type ] = data.items[ key ].id;
483
484 // API Request.
485 var api_post = {
486 slug : CartFlowsImportVars.step + '?' + decodeURIComponent( $.param( api_params ) ),
487 };
488
489 CartFlowsAPI._api_request( api_post, function( data ) {
490 var template = wp.template('cartflows-steps-list');
491 if( parseInt( data.items_count ) ) {
492 $('#wcf-remote-step-list').html( template( data ) );
493 $('#wcf-remote-step-list').prepend( wp.template('cartflows-create-blank-step') );
494 } else {
495 $('#wcf-remote-step-list').html( wp.template('cartflows-no-steps') );
496 }
497
498 step_types_count--;
499
500 if( 0 == step_types_count ) {
501
502 var template = wp.template('cartflows-term-filters');
503 $('#wcf-remote-step-filters').html(template( step_type_response_data ));
504 $('#wcf-remote-step-filters').find('li:first a').addClass('current');
505
506 $('#wcf-remote-content').find( '.spinner' ).remove();
507
508 CartFlowsImport._showSteps();
509
510 $( 'html' ).removeClass('wcf-steps-loading');
511 }
512 });
513 }
514 }
515 } );
516 },
517
518 _render_remote_steps: function( event ) {
519
520 if( event ) {
521 event.preventDefault();
522 }
523
524 $("#wcf-remote-step-importer").addClass('open');
525
526 $("html").addClass('wcf-popup-open');
527
528 if( $( 'html' ).hasClass('wcf-steps-loading') ) {
529 $('#wcf-remote-content').append( '<span class="spinner is-active"></span>' );
530 } else {
531 CartFlowsImport._showSteps();
532 }
533
534 },
535
536 _categorize_data: function( items ) {
537
538 var categorised_data = [];
539
540 $.each( items, function( index, value ) {
541
542 var step_type = value.step_type.slug;
543
544 if( 'undefined' == typeof categorised_data[step_type] ) {
545 categorised_data[step_type] = [];
546 }
547
548 categorised_data[step_type].push( value );
549
550 });
551
552 return categorised_data;
553
554 },
555
556 _close_template_popup: function( event ) {
557 if ( $( event.target ).hasClass('wcf-templates-popup-overlay')
558 || $( event.target ).hasClass('close-icon') ) {
559
560 // New step creation/importing is in process..
561 if( ! $( '.wcf-templates-popup-overlay' ).hasClass('request-process') ) {
562 $("html").removeClass('wcf-popup-open');
563 $(".wcf-templates-popup-overlay").removeClass( 'open' );
564 }
565 }
566 },
567
568 _ajax: function( data, callback, trigger ) {
569
570 var self = CartFlowsImport;
571
572 $.ajax({
573 url : ajaxurl,
574 type : 'POST',
575 data : data,
576 })
577 .done(function( request, status, XHR )
578 {
579 var data = {
580 request : request,
581 status : status,
582 XHR : XHR,
583 };
584
585 if( callback && typeof callback == "function"){
586 callback( data );
587 }
588
589 self.doc.trigger( trigger + '-done', [request, status, XHR] );
590 })
591 .fail(function( jqXHR, textStatus )
592 {
593 self.doc.trigger( trigger + '-fail', [jqXHR, textStatus] );
594 })
595 .always(function()
596 {
597 self.doc.trigger( trigger + '-always' );
598 });
599
600 },
601
602 _load_templates: function( args, trigger_name ) {
603 var self = CartFlowsImport;
604
605 if( $('body').hasClass('processing') ) {
606 return;
607 }
608
609 $('body').addClass('processing');
610
611 $.ajax({
612 url : ajaxurl,
613 type : 'POST',
614 data : {
615 action : 'cartflows_load_steps',
616 security: cartflows_admin.cf_load_steps_nonce,
617 args : args,
618 },
619 })
620 .done(function( items, status, XHR )
621 {
622 self.doc.trigger( trigger_name, [items] );
623 $('body').removeClass('processing');
624 })
625 .fail(function( jqXHR, textStatus )
626 {
627 })
628 .always(function()
629 {
630 });
631 },
632
633 _empty: function(data)
634 {
635 if(typeof(data) == 'number' || typeof(data) == 'boolean')
636 {
637 return false;
638 }
639 if(typeof(data) == 'undefined' || data === null)
640 {
641 return true;
642 }
643 if(typeof(data.length) != 'undefined')
644 {
645 return data.length == 0;
646 }
647 var count = 0;
648 for(var i in data)
649 {
650 if(data.hasOwnProperty(i))
651 {
652 count ++;
653 }
654 }
655 return count == 0;
656 },
657
658 _preview_individual: function()
659 {
660 var id = $(this).data('id') || '';
661 var href = $(this).data('href') || '';
662
663 // Active.
664 $('.cartflows-preview-flow-step').removeClass('active');
665 $('.cartflows-preview-flow-step[data-id="'+id+'"]').addClass('active');
666
667 $('#TB_window').addClass('cartflows-thickbox-loading');
668
669 $('#TB_iframeContent').removeAttr('onload');
670 $('#TB_iframeContent').removeAttr('src');
671 $('#TB_iframeContent').attr('src', href );
672 $('#TB_iframeContent').attr('onload', 'CartFlowsImport.showIframe()' )
673 },
674 showIframe: function() {
675 $("#TB_load").remove();
676 $("#TB_window").css({'visibility':'visible'});
677 // $("#TB_window").css({'visibility':'visible'}).trigger( 'thickbox:iframe:loaded' );
678 $('#TB_window').removeClass('cartflows-thickbox-loading');
679 },
680
681 /**
682 * Remove thickbox loading class
683 *
684 * @param object event Event object.
685 * @return void.
686 */
687 _previewLoaded: function( event ) {
688 event.preventDefault();
689 $('#TB_window').removeClass('cartflows-thickbox-loading');
690 },
691
692 _show_page_bulider_for_step: function( event ) {
693 event.preventDefault();
694 var id = $(this).data( 'id' ) || '';
695 var title = $(this).text() || '';
696
697 $(this).parents('.wcf-page-builder-links').find('a').removeClass('current');
698 $(this).addClass('current');
699
700 if( 'elementor' == id ) {
701 $('#wcf-upcoming-page-builders').hide().html('');
702 $('#wcf-remote-step-list').show();
703 } else {
704 $('#wcf-upcoming-page-builders').show().html('<p class="description">Templates for '+title+' are coming soon.</p>');
705 $('#wcf-upcoming-page-builders').show().prepend( wp.template('cartflows-create-blank-step') );
706 $('#wcf-remote-step-list').hide();
707 }
708 },
709
710 _show_page_bulider_for_flow: function( event ) {
711 event.preventDefault();
712 var id = $(this).data( 'id' ) || '';
713 var title = $(this).text() || '';
714
715 $(this).parents('.wcf-page-builder-links').find('a').removeClass('current');
716 $(this).addClass('current');
717
718 if( 'elementor' == id ) {
719 $('#wcf-upcoming-page-builders').hide().html('');
720 $('#wcf-remote-flow-list').show();
721 } else {
722 $('#wcf-upcoming-page-builders').show().html('<p class="description">Templates for '+title+' are coming soon.</p>');
723 $('#wcf-upcoming-page-builders').show().prepend( wp.template('cartflows-create-blank-flow') );
724 $('#wcf-remote-flow-list').hide();
725 }
726 },
727
728 _create_default_flow: function( event )
729 {
730 event.preventDefault();
731 var self = CartFlowsImport;
732 var btn = $( this );
733
734 if( btn.hasClass('updating-message') ) {
735 return;
736 }
737
738 $('#wcf-remote-flow-importer').addClass('request-process');
739
740 btn.addClass('updating-message').text( 'Creating Flow..' );
741 btn.parents('.template').addClass('importing');
742
743 var data = {
744 action : 'cartflows_default_flow',
745 security: cartflows_admin.cf_default_flow_nonce
746 };
747
748 // Import Template AJAX.
749 self._ajax( data, function( data ) {
750
751 if( data.request.success ) {
752
753
754 var flow_id = data.request.data; // $( 'form#post #post_ID').val();
755 // Created.
756 setTimeout(function() {
757 btn.removeClass('updating-message').text('Flow Created! Redirecting..');
758 window.location = CartFlowsImportVars.admin_url + 'post.php?post='+flow_id+'&action=edit';
759 }, 3000);
760
761 }
762 });
763
764 },
765
766 _activate_plugin: function( plugin_slug ) {
767
768 var self = CartFlowsImport;
769
770 var data = {
771 action : 'cartflows_activate_plugin',
772 plugin_slug : plugin_slug
773 };
774
775 self._ajax( data, function( data ) {
776 });
777 },
778
779 _process_import_flow: function( response )
780 {
781
782 var self = CartFlowsImport;
783 var btn = $( '#wcf-remote-flow-importer .cartflows-step-import.updating-message' );
784
785 var plugin_slug = '';
786 plugin = response.slug;
787
788 switch ( plugin ) {
789 case 'elementor':
790 plugin_slug = 'elementor/elementor.php';
791 break;
792 }
793
794 self._activate_plugin( plugin_slug );
795
796 btn.text( 'Creating Flow..' );
797
798 var flow_steps_string = btn.data('flow-steps') || '';
799 var flow_steps = ( '' !== flow_steps_string ) ? JSON.parse("[" + flow_steps_string + "]") : [];
800
801 var data = {
802 action : 'cartflows_create_flow',
803 security: cartflows_admin.cf_create_flow_nonce
804 };
805
806 // Import Template AJAX.
807 self._ajax( data, function( data ) {
808
809 if( data.request.success ) {
810
811 var flow_id = data.request.data; // $( 'form#post #post_ID').val();
812
813 if( flow_steps ) {
814
815 // Activate ALl Plugins.
816 CartFlowsAjaxQueue.stop();
817 CartFlowsAjaxQueue.run();
818
819 CartFlowsImport.all_steps = flow_steps.length;
820
821 // Importing.
822 btn.addClass('updating-message').text('Importing Step 1 of ' + CartFlowsImport.all_steps );
823
824 $.each(flow_steps, function(index, template_id) {
825
826 CartFlowsAjaxQueue.add({
827 url: CartFlowsImportVars.ajaxurl,
828 type: 'POST',
829 data: {
830 action : 'cartflows_import_flow_step',
831 flow_id : flow_id,
832 template_id : template_id,
833 security : cartflows_admin.cf_import_flow_step_nonce
834 },
835 success: function( result ) {
836 CartFlowsImport.remaining_steps = CartFlowsImport.remaining_steps + 1;
837
838 if( CartFlowsImport.remaining_steps === CartFlowsImport.all_steps ) {
839
840 // Imported.
841 btn.addClass('updating-message').text('Imported Step ' + CartFlowsImport.remaining_steps + ' of ' + CartFlowsImport.all_steps );
842
843 setTimeout(function() {
844 btn.removeClass('updating-message').text('All Imported! Redirecting..');
845 window.location = CartFlowsImportVars.admin_url + 'post.php?post='+flow_id+'&action=edit';
846 }, 3000);
847
848 } else {
849 // Importing.
850 btn.addClass('updating-message').text('Importing Step ' + CartFlowsImport.remaining_steps + ' of ' + CartFlowsImport.all_steps );
851 }
852 },
853 error: function( result ) {
854 CartFlowsImport.remaining_steps = CartFlowsImport.remaining_steps + 1;
855
856 template = btn.parents('.template.importing');
857 template.find('.preview')
858 .addClass('notice notice-warning')
859 .removeClass('preview')
860 .text( result.statusText );
861
862 if( CartFlowsImport.remaining_steps === CartFlowsImport.all_steps ) {
863
864 // Imported.
865 btn.addClass('updating-message').text('Failed ' + CartFlowsImport.remaining_steps + ' of ' + CartFlowsImport.all_steps );
866
867 setTimeout(function() {
868 btn.removeClass('updating-message button-primary').addClass('disabled');
869 // location.reload();
870 }, 3000);
871
872 } else {
873 // Importing.
874 btn.addClass('updating-message').text('Failed ' + CartFlowsImport.remaining_steps + ' of ' + CartFlowsImport.all_steps );
875 }
876 },
877 fail: function( result ) {
878
879 CartFlowsImport.remaining_steps = CartFlowsImport.remaining_steps + 1;
880
881 if( CartFlowsImport.remaining_steps === CartFlowsImport.all_steps ) {
882
883 // Imported.
884 btn.addClass('updating-message').text('Imported ' + CartFlowsImport.remaining_steps + ' of ' + CartFlowsImport.all_steps );
885
886 setTimeout(function() {
887 btn.removeClass('updating-message').text('All Step Imported! Reloading..');
888 location.reload();
889 }, 3000);
890
891 } else {
892 // Importing.
893 btn.addClass('updating-message').text('Importing ' + CartFlowsImport.remaining_steps + ' of ' + CartFlowsImport.all_steps );
894 }
895 }
896 });
897 });
898 }
899 }
900 });
901 },
902
903 _import_flow: function()
904 {
905 var btn = $( this );
906 var plugins = btn.data('required');
907
908 if( btn.hasClass('updating-message') ) {
909 return;
910 }
911
912 $('#wcf-remote-flow-importer').addClass('request-process');
913
914 if( ! $.isEmptyObject( plugins ) ) {
915
916 btn.addClass('updating-message');
917 btn.parents('.template').addClass('importing');
918
919 if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.ajaxLocked ) {
920 wp.updates.requestFilesystemCredentials( event );
921 }
922
923 $.each( plugins, function( plugin_slug, plugin ) {
924
925 var is_installed = plugin.install;
926 var is_active = plugin.active;
927
928 if( 'no' == is_installed ) {
929
930 btn.text( 'Installing ' + plugin_slug + '...' );
931
932 wp.updates.installPlugin({
933 slug: plugin_slug,
934 success: CartFlowsImport._process_import_flow,
935 error: CartFlowsImport._handle_error
936 });
937
938 } else {
939
940 if( 'no' == is_active ) {
941 btn.text( 'Activating ' + plugin_slug + '...' );
942 }
943
944 var data = {
945 slug: plugin_slug
946 };
947
948 CartFlowsImport._process_import_flow( data );
949 }
950 });
951 }
952 },
953
954 _handle_error: function( response )
955 {
956 var btn = $( '.cartflows-step-import.updating-message' );
957
958 btn.addClass('updating-message').text( response.errorMessage );
959 },
960
961 _create_blank_step: function( event )
962 {
963 event.preventDefault();
964
965 var btn = $(this),
966 self = CartFlowsImport,
967 flow_id = $("#post_ID").val(),
968 step_slug = $('.step-type-filter-links .current').data('slug') || '',
969 step_title = $('.step-type-filter-links .current').data('title') || '',
970 step_type = step_slug,
971 step_count = $('.wcf-step-wrap[data-term-slug="'+step_slug+'"]').length || 1;
972 step_custom_title = `${step_title} ${step_count+1}`;
973
974
975 if ( ! CartFlowsImportVars._is_pro_active && ( 'upsell' === step_type || 'downsell' === step_type )) {
976 return;
977 }
978
979 btn.parents('.template').addClass('importing');
980
981 if ( ! CartFlowsImportVars._is_pro_active ) {
982
983 if ( 'checkout' === step_type ) {
984 var checkout_steps = $( '.wcf-step-wrap[data-term-slug="checkout"]');
985
986 if ( 0 < checkout_steps.length ) {
987
988 var parent_template = btn.parents('.template.importing');
989
990 btn.removeClass('importing updating-message')
991 .text('Import Failed!');
992
993 var notice_wrap = parent_template.find('#wcf_create_notice').show();
994
995 notice_wrap.find('a')
996 .addClass('notice notice-warning ')
997 .text ( 'Upgrade to Pro for adding more than one Checkout step' );
998 return;
999 }
1000 }
1001 }
1002
1003 $('#wcf-remote-step-importer').addClass('request-process');
1004
1005 $('.cartflows-step-import').addClass('disabled');
1006 btn.addClass('importing updating-message').text('Creating..');
1007
1008
1009 // Process Import Page.
1010 if( $( 'body' ).hasClass( 'post-type-cartflows_flow' ) ) {
1011 var data = {
1012 action : 'cartflows_step_create_blank',
1013 flow_id : flow_id,
1014 step_type : step_type,
1015 step_title : step_custom_title,
1016 security : cartflows_admin.cf_step_create_blank_nonce
1017 };
1018
1019
1020
1021 // Import Template AJAX.
1022 self._ajax( data, function( data ) {
1023
1024 var self = CartFlowsImport;
1025 var template = btn.parents('.template.importing');
1026
1027 if( data.request.success ) {
1028 btn.text('Created. Reloading..');
1029 setTimeout(function() {
1030 window.location.href = window.location.href + '&highlight-step-id=' + data.request.data;
1031 }, 3000);
1032
1033 } else {
1034 btn.removeClass('importing updating-message')
1035 .text('Creating Failed!');
1036
1037 template.find('.cartflows-step-preview').append( "<div class='preview'></div>" );
1038
1039 template.find('.preview')
1040 .addClass('notice notice-warning')
1041 .removeClass('preview')
1042 .text( data.request.data );
1043 }
1044 } );
1045 }
1046 },
1047
1048 _process_import_step: function( response ) {
1049
1050 var step_slug = $('.step-type-filter-links .current').data('slug') || '',
1051 step_title = $('.step-type-filter-links .current').data('title') || '',
1052 step_count = $('.wcf-step-wrap[data-term-slug="'+step_slug+'"]').length || 1;
1053 step_custom_title = `${step_title} ${step_count+1}`,
1054 btn = $('#wcf-remote-step-importer .cartflows-step-import.updating-message'),
1055 self = CartFlowsImport;
1056
1057 if ( ! CartFlowsImportVars._is_pro_active ) {
1058
1059 if ( 'checkout' === step_slug ) {
1060
1061 var checkout_steps = $( '.wcf-step-wrap[data-term-slug="checkout"]');
1062
1063 if ( 0 < checkout_steps.length ) {
1064
1065 var parent_template = btn.parents('.template.importing');
1066
1067 btn.removeClass('importing updating-message')
1068 .text('Import Failed!');
1069
1070 parent_template.find('.preview').hide();
1071 var notice_wrap = parent_template.find('#wcf_create_notice')
1072 .show();
1073
1074 notice_wrap.find('a')
1075 .addClass('notice notice-warning ')
1076 .text ( 'Upgrade to Pro for adding more than one Checkout step' );
1077
1078 $('#wcf-remote-step-importer').removeClass('request-process');
1079 return;
1080 }
1081 }
1082 }
1083
1084 var plugin_slug = '';
1085 plugin = response.slug;
1086
1087 switch ( plugin ) {
1088 case 'elementor':
1089 plugin_slug = 'elementor/elementor.php';
1090 break;
1091 }
1092
1093 self._activate_plugin( plugin_slug );
1094
1095 var template_id = btn.data('template-id') || '';
1096 var flow_id = $("#post_ID").val();
1097 var step_type = step_slug;
1098 var self = CartFlowsImport;
1099
1100 $('.cartflows-step-import-blank').addClass('disabled');
1101 $('.cartflows-step-import').addClass('disabled');
1102 btn.addClass('importing updating-message').text('Importing..');
1103
1104 // Process Import Page.
1105 if( $( 'body' ).hasClass( 'post-type-cartflows_flow' ) )
1106 {
1107 var data = {
1108 action : 'cartflows_step_import',
1109 flow_id : flow_id,
1110 template_id : template_id,
1111 step_title : step_custom_title,
1112 step_type : step_type,
1113 security : cartflows_admin.cf_step_import_nonce
1114 };
1115
1116 // Import Template AJAX.
1117 self._ajax( data, function( data ) {
1118
1119 var self = CartFlowsImport;
1120 var template = btn.parents('.template.importing');
1121
1122 if( data.request.success ) {
1123 btn.text('Imported. Reloading..');
1124 setTimeout(function() {
1125 window.location.href = window.location.href + '&highlight-step-id=' + data.request.data;
1126 }, 3000);
1127
1128 } else {
1129
1130 $( '.wcf-templates-popup-overlay' ).removeClass('request-process');
1131
1132 btn.removeClass('importing updating-message')
1133 .text('Import Failed!');
1134
1135 template.find('.preview')
1136 .addClass('notice notice-warning')
1137 .removeClass('preview')
1138 .text( data.request.data );
1139 }
1140
1141 }, 'add_template_to_page' );
1142 }
1143 },
1144
1145 _import_step: function( event )
1146 {
1147 event.preventDefault();
1148 var btn = $(this);
1149 var plugins = btn.data('required');
1150
1151 if( btn.hasClass('updating-message') ) {
1152 return;
1153 }
1154
1155 btn.addClass('updating-message');
1156
1157 if( ! $.isEmptyObject( plugins ) ) {
1158
1159 if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.ajaxLocked ) {
1160 wp.updates.requestFilesystemCredentials( event );
1161 }
1162
1163 $('#wcf-remote-step-importer').addClass('request-process');
1164
1165 $.each( plugins, function( plugin_slug, plugin ) {
1166
1167 var is_installed = plugin.install;
1168 var is_active = plugin.active;
1169
1170 btn.parents('.template').addClass('importing');
1171
1172 if( 'no' == is_installed ) {
1173
1174 btn.addClass('updating-message').text( 'Installing ' + plugin_slug + '...' );
1175
1176 wp.updates.installPlugin({
1177 slug: plugin_slug,
1178 success: CartFlowsImport._process_import_step,
1179 error: CartFlowsImport._handle_error
1180 });
1181
1182 } else {
1183
1184 if( 'no' == is_active ) {
1185 btn.addClass('updating-message').text( 'Activating ' + plugin_slug + '...' );
1186 }
1187
1188 var data = {
1189 slug: plugin_slug
1190 };
1191
1192 CartFlowsImport._process_import_step( data );
1193 }
1194 });
1195 }
1196
1197 },
1198
1199 _add_template_to_page_fail: function( event, jqXHR, textStatus ) {
1200 event.preventDefault();
1201 var self = CartFlowsImport,
1202 template = $('.wcf-flow-steps-data-wrap-importer .template.importing');
1203
1204 template.find('.cartflows-step-import')
1205 .removeClass('importing updating-message')
1206 .text('Import Failed!');
1207
1208
1209 template.find('.preview')
1210 .addClass('notice notice-warning')
1211 .removeClass('preview')
1212 .text( jqXHR.responseText );
1213 }
1214
1215 };
1216
1217 /**
1218 * Initialization
1219 */
1220 $(function(){
1221 CartFlowsImport.init();
1222 });
1223
1224 })(jQuery);