PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.2
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.2
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.2, at assets/js/import.js

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