PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.14
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.14
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 / classes / class-cartflows-importer.php

class-cartflows-importer.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.14, at classes/class-cartflows-importer.php

1,570 lines 50.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Admin
4 *
5 * @package CartFlows
6 * @since 1.0.0
7 */
8
9 if ( ! class_exists( 'CartFlows_Importer' ) ) :
10
11 /**
12 * CartFlows Import
13 *
14 * @since 1.0.0
15 */
16 class CartFlows_Importer {
17
18 /**
19 * Instance
20 *
21 * @since 1.0.0
22 * @access private
23 * @var object Class object.
24 */
25 private static $instance;
26
27 /**
28 * Initiator
29 *
30 * @since 1.0.0
31 * @return object initialized object of class.
32 */
33 public static function get_instance() {
34 if ( ! isset( self::$instance ) ) {
35 self::$instance = new self;
36 }
37
38 return self::$instance;
39 }
40
41 /**
42 * Constructor
43 *
44 * @since 1.0.0
45 */
46 public function __construct() {
47 add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
48 add_action( 'wp_ajax_cartflows_load_steps', array( $this, 'load_templates' ) );
49 add_action( 'wp_ajax_cartflows_step_import', array( $this, 'import_step' ) );
50 add_action( 'wp_ajax_cartflows_create_flow', array( $this, 'create_flow' ) );
51 add_action( 'wp_ajax_cartflows_default_flow', array( $this, 'create_default_flow' ) );
52 add_action( 'wp_ajax_cartflows_step_create_blank', array( $this, 'step_create_blank' ) );
53 add_action( 'wp_ajax_cartflows_import_flow_step', array( $this, 'import_flow' ) );
54 add_action( 'admin_footer', array( $this, 'js_templates' ) );
55 add_action( 'cartflows_import_complete', array( $this, 'clear_cache' ) );
56
57 add_filter( 'cartflows_admin_js_localize', array( $this, 'localize_vars' ) );
58
59 add_action( 'wp_ajax_cartflows_activate_plugin', array( $this, 'activate_plugin' ) );
60
61 add_action( 'admin_menu', array( $this, 'add_to_menus' ) );
62 add_action( 'admin_init', array( $this, 'export_json' ) );
63 add_action( 'admin_init', array( $this, 'import_json' ) );
64 add_filter( 'post_row_actions', array( $this, 'export_link' ), 10, 2 );
65 add_action( 'admin_action_cartflows_export_flow', array( $this, 'export_flow' ) );
66 }
67
68 /**
69 * Add the export link to action list for flows row actions
70 *
71 * @since 1.1.4
72 *
73 * @param array $actions Actions array.
74 * @param object $post Post object.
75 *
76 * @return array
77 */
78 function export_link( $actions, $post ) {
79 if ( current_user_can( 'edit_posts' ) && isset( $post ) && CARTFLOWS_FLOW_POST_TYPE === $post->post_type ) {
80 $actions['export'] = '<a href="' . wp_nonce_url( 'admin.php?action=cartflows_export_flow&post=' . $post->ID, basename( __FILE__ ), 'flow_export_nonce' ) . '" title="' . __( 'Export this flow', 'cartflows' ) . '" rel="permalink">' . __( 'Export', 'cartflows' ) . '</a>';
81 }
82 return $actions;
83 }
84
85 /**
86 * Add menus
87 *
88 * @since 1.1.4
89 */
90 function add_to_menus() {
91 add_submenu_page( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE, __( 'Flow Export', 'cartflows' ), __( 'Flow Export', 'cartflows' ), 'export', 'flow_exporter', array( $this, 'exporter_markup' ) );
92 add_submenu_page( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE, __( 'Flow Import', 'cartflows' ), __( 'Flow Import', 'cartflows' ), 'import', 'flow_importer', array( $this, 'importer_markup' ) );
93 }
94
95 /**
96 * Export flow with steps and its meta
97 *
98 * @since 1.1.4
99 */
100 function export_flow() {
101
102 if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'cartflows_export_flow' == $_REQUEST['action'] ) ) ) {
103 wp_die( __( 'No post to export has been supplied!', 'cartflows' ) );
104 }
105
106 if ( ! isset( $_GET['flow_export_nonce'] ) || ! wp_verify_nonce( $_GET['flow_export_nonce'], basename( __FILE__ ) ) ) {
107 return;
108 }
109
110 // Get the original post id.
111 $flow_id = ( isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
112
113 $flows = array();
114 $flows[] = $this->get_flow_export_data( $flow_id );
115 $flows = apply_filters( 'cartflows_export_data', $flows );
116
117 nocache_headers();
118 header( 'Content-Type: application/json; charset=utf-8' );
119 header( 'Content-Disposition: attachment; filename=cartflows-flow-' . $flow_id . '-' . date( 'm-d-Y' ) . '.json' );
120 header( 'Expires: 0' );
121
122 echo json_encode( $flows );
123 exit;
124 }
125
126 /**
127 * Export flow markup
128 *
129 * @since 1.1.4
130 */
131 function exporter_markup() {
132 include_once CARTFLOWS_DIR . 'includes/exporter.php';
133 }
134
135 /**
136 * Import flow markup
137 *
138 * @since 1.1.4
139 */
140 function importer_markup() {
141 include_once CARTFLOWS_DIR . 'includes/importer.php';
142 }
143
144 /**
145 * Export flow
146 *
147 * @since 1.1.4
148 */
149 function export_json() {
150 if ( empty( $_POST['cartflows-action'] ) || 'export' != $_POST['cartflows-action'] ) {
151 return;
152 }
153
154 if ( ! wp_verify_nonce( $_POST['cartflows-action-nonce'], 'cartflows-action-nonce' ) ) {
155 return;
156 }
157
158 if ( ! current_user_can( 'manage_options' ) ) {
159 return;
160 }
161
162 $flows = $this->get_all_flow_export_data();
163 $flows = apply_filters( 'cartflows_export_data', $flows );
164
165 nocache_headers();
166 header( 'Content-Type: application/json; charset=utf-8' );
167 header( 'Content-Disposition: attachment; filename=cartflows-flow-export-' . date( 'm-d-Y' ) . '.json' );
168 header( 'Expires: 0' );
169
170 echo json_encode( $flows );
171 exit;
172 }
173
174 /**
175 * Get flow export data
176 *
177 * @since 1.1.4
178 *
179 * @param integer $flow_id Flow ID.
180 * @return array
181 */
182 function get_flow_export_data( $flow_id ) {
183
184 $export_all = apply_filters( 'cartflows_export_all', false );
185
186 $valid_step_meta_keys = array(
187 '_wp_page_template',
188 '_thumbnail_id',
189 'classic-editor-remember',
190 );
191
192 $new_steps = array();
193 $steps = get_post_meta( $flow_id, 'wcf-steps', true );
194 if ( $steps ) {
195 foreach ( $steps as $key => $step ) {
196
197 // Add step post meta.
198 $new_all_meta = array();
199 $all_meta = get_post_meta( $step['id'] );
200 if ( is_array( $all_meta ) ) {
201
202 if ( $export_all ) {
203 foreach ( $all_meta as $meta_key => $value ) {
204 $new_all_meta[ $meta_key ] = maybe_unserialize( $value[0] );
205 }
206 } else {
207
208 foreach ( $all_meta as $meta_key => $value ) {
209 if ( substr( $meta_key, 0, strlen( 'wcf' ) ) === 'wcf' ) {
210 $new_all_meta[ $meta_key ] = maybe_unserialize( $value[0] );
211 } elseif ( in_array( $meta_key, $valid_step_meta_keys ) ) {
212 $new_all_meta[ $meta_key ] = maybe_unserialize( $value[0] );
213 }
214 }
215 }
216 }
217
218 // Add single step.
219 $step_data_arr = array(
220 'title' => get_the_title( $step['id'] ),
221 'type' => $step['type'],
222 'meta' => $new_all_meta,
223 'post_content' => '',
224 );
225
226 if ( $export_all ) {
227
228 $step_post_obj = get_post( $step['id'] );
229
230 $step_data_arr['post_content'] = $step_post_obj->post_content;
231 }
232
233 $new_steps[] = $step_data_arr;
234 }
235 }
236
237 // Add single flow.
238 return array(
239 'title' => get_the_title( $flow_id ),
240 'steps' => $new_steps,
241 );
242 }
243
244 /**
245 * Get all flow export data
246 *
247 * @since 1.1.4
248 */
249 function get_all_flow_export_data() {
250
251 $query_args = array(
252 'post_type' => CARTFLOWS_FLOW_POST_TYPE,
253
254 // Query performance optimization.
255 'fields' => 'ids',
256 'no_found_rows' => true,
257 'posts_per_page' => -1,
258 );
259
260 $query = new WP_Query( $query_args );
261 $flows = array();
262 if ( $query->posts ) {
263 foreach ( $query->posts as $key => $post_id ) {
264 $flows[] = $this->get_flow_export_data( $post_id );
265 }
266 }
267
268 return $flows;
269 }
270
271 /**
272 * Import our exported file
273 *
274 * @since 1.1.4
275 */
276 function import_json() {
277 if ( empty( $_POST['cartflows-action'] ) || 'import' != $_POST['cartflows-action'] ) {
278 return;
279 }
280
281 if ( ! wp_verify_nonce( $_POST['cartflows-action-nonce'], 'cartflows-action-nonce' ) ) {
282 return;
283 }
284
285 if ( ! current_user_can( 'manage_options' ) ) {
286 return;
287 }
288
289 $filename = $_FILES['file']['name'];
290 $file_info = explode( '.', $filename );
291 $extension = end( $file_info );
292
293 if ( 'json' != $extension ) {
294 wp_die( __( 'Please upload a valid .json file', 'cartflows' ) );
295 }
296
297 $file = $_FILES['file']['tmp_name'];
298
299 if ( empty( $file ) ) {
300 wp_die( __( 'Please upload a file to import', 'cartflows' ) );
301 }
302
303 // Retrieve the settings from the file and convert the JSON object to an array.
304 $flows = json_decode( file_get_contents( $file ), true );
305
306 if ( $flows ) {
307
308 foreach ( $flows as $key => $flow ) {
309
310 $flow_title = $flow['title'];
311 if ( post_exists( $flow['title'] ) ) {
312 $flow_title = $flow['title'] . ' Copy';
313 }
314
315 // Create post object.
316 $new_flow_args = array(
317 'post_type' => CARTFLOWS_FLOW_POST_TYPE,
318 'post_title' => $flow_title,
319 'post_status' => 'draft',
320 );
321
322 // Insert the post into the database.
323 $flow_id = wp_insert_post( $new_flow_args );
324
325 if ( $flow['steps'] ) {
326 foreach ( $flow['steps'] as $key => $step ) {
327
328 $new_step_id = wp_insert_post(
329 array(
330 'post_type' => CARTFLOWS_STEP_POST_TYPE,
331 'post_title' => $step['title'],
332 'post_status' => 'publish',
333 'meta_input' => $step['meta'],
334 'post_content' => isset( $step['post_content'] ) ? $step['post_content'] : '',
335 )
336 );
337
338 // Insert post meta.
339 update_post_meta( $new_step_id, 'wcf-flow-id', $flow_id );
340
341 $step_taxonomy = CARTFLOWS_TAXONOMY_STEP_TYPE;
342 $current_term = term_exists( $step['type'], $step_taxonomy );
343
344 // // Set type object.
345 $data = get_term( $current_term['term_id'], $step_taxonomy );
346 $step_slug = $data->slug;
347 wp_set_object_terms( $new_step_id, $data->slug, $step_taxonomy );
348
349 // Set type.
350 update_post_meta( $new_step_id, 'wcf-step-type', $data->slug );
351
352 // Set flow.
353 wp_set_object_terms( $new_step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
354
355 CartFlows_Importer::get_instance()->set_step_to_flow( $flow_id, $new_step_id, $step['title'], $step_slug );
356
357 if ( isset( $step['post_content'] ) && ! empty( $step['post_content'] ) ) {
358
359 // Download and replace images.
360 $content = $this->get_content( $step['post_content'] );
361
362 // Update post content.
363 wp_update_post(
364 array(
365 'ID' => $new_step_id,
366 'post_content' => $content,
367 )
368 );
369 }
370 }
371 }
372 }
373 }
374
375 add_action( 'admin_notices', array( $this, 'imported_successfully' ) );
376 }
377
378 /**
379 * Download and Replace hotlink images
380 *
381 * @since x.x.x
382 *
383 * @param string $content Mixed post content.
384 * @return array Hotlink image array.
385 */
386 function get_content( $content = '' ) {
387
388 $all_links = wp_extract_urls( $content );
389 $image_links = array();
390 $image_map = array();
391
392 // Not have any link.
393 if ( empty( $all_links ) ) {
394 return $content;
395 }
396
397 foreach ( $all_links as $key => $link ) {
398 if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $link ) ) {
399 $image_links[] = $link;
400 }
401 }
402
403 // Not have any image link.
404 if ( empty( $image_links ) ) {
405 return $content;
406 }
407
408 foreach ( $image_links as $key => $image_url ) {
409
410 // Download remote image.
411 $image = array(
412 'url' => $image_url,
413 'id' => rand( 000, 999 ),
414 );
415 $downloaded_image = CartFlows_Import_Image::get_instance()->import( $image );
416
417 // Old and New image mapping links.
418 $image_map[ $image_url ] = $downloaded_image['url'];
419 }
420
421 // Replace old image links with new image links.
422 foreach ( $image_map as $old_url => $new_url ) {
423 $content = str_replace( $old_url, $new_url, $content );
424 }
425
426 return $content;
427
428 }
429
430 /**
431 * Imported notice
432 *
433 * @since 1.1.4
434 */
435 function imported_successfully() {
436 ?>
437 <div class="notice notice-success">
438 <p><?php _e( 'Successfully imported flows.', 'cartflows' ); ?></p>
439 </div>
440 <?php
441 }
442
443 /**
444 * Clear Cache.
445 *
446 * @since 1.0.0
447 */
448 public function clear_cache() {
449 // Clear 'Elementor' file cache.
450 if ( class_exists( '\Elementor\Plugin' ) ) {
451 Elementor\Plugin::$instance->files_manager->clear_cache();
452 }
453 }
454
455 /**
456 * JS Templates
457 *
458 * @since 1.0.0
459 *
460 * @return void
461 */
462 function js_templates() {
463
464 // Loading Templates.
465 ?>
466 <script type="text/template" id="tmpl-cartflows-step-loading">
467 <div class="template-message-block cartflows-step-loading">
468 <h2>
469 <span class="spinner"></span>
470 <?php _e( 'Loading Steps', 'cartflows' ); ?>
471 </h2>
472 <p class="description"><?php _e( 'Getting steps from the cloud. Please wait for the moment.', 'cartflows' ); ?></p>
473 </div>
474 </script>
475
476 <?php
477 // Search Templates.
478 ?>
479 <script type="text/template" id="tmpl-cartflows-searching-templates">
480 <div class="template-message-block cartflows-searching-templates">
481 <h2>
482 <span class="spinner"></span>
483 <?php _e( 'Searching Template..', 'cartflows' ); ?>
484 </h2>
485 <p class="description"><?php _e( 'Getting templates from the cloud. Please wait for the moment.', 'cartflows' ); ?></p>
486 </div>
487 </script>
488
489 <?php
490 // CartFlows Importing Template.
491 ?>
492 <script type="text/template" id="tmpl-cartflows-step-importing">
493 <div class="template-message-block cartflows-step-importing">
494 <h2><span class="spinner"></span> <?php _e( 'Importing..', 'cartflows' ); ?></h2>
495 </div>
496 </script>
497
498 <?php
499 // CartFlows Imported.
500 ?>
501 <script type="text/template" id="tmpl-cartflows-step-imported">
502 <div class="template-message-block cartflows-step-imported">
503 <h2><span class="dashicons dashicons-yes"></span> <?php _e( 'Imported', 'cartflows' ); ?></h2>
504 <p class="description"><?php _e( 'Thanks for patience', 'cartflows' ); ?> <span class="dashicons dashicons-smiley"></span></p></div>
505 </script>
506
507 <?php
508 // No templates.
509 ?>
510 <script type="text/template" id="tmpl-cartflows-no-steps">
511 <div class="cartflows-no-steps">
512 <div class="template-message-block">
513 <h2><?php _e( 'Coming Soon!', 'cartflows' ); ?></h2>
514 <p class="description"></p>
515 </div>
516 </div>
517 </script>
518
519 <?php
520 // No templates.
521 ?>
522 <script type="text/template" id="tmpl-cartflows-no-flows">
523 <div class="cartflows-no-flows">
524 <div class="template-message-block">
525 <h2><?php _e( 'Coming Soon!', 'cartflows' ); ?></h2>
526 <p class="description"></p>
527 </div>
528 </div>
529 </script>
530
531 <?php
532 // Error handling.
533 ?>
534 <script type="text/template" id="tmpl-templator-error">
535 <div class="notice notice-error"><p>{{ data }}</p></div>
536 </script>
537
538 <?php
539 // Redirect to Elementor.
540 ?>
541 <script type="text/template" id="tmpl-templator-redirect-to-elementor">
542 <div class="template-message-block templator-redirect-to-elementor">
543 <h2><span class="dashicons dashicons-yes"></span> <?php _e( 'Imported', 'cartflows' ); ?></h2>
544 <p class="description"><?php _e( 'Thanks for patience', 'cartflows' ); ?> <span class="dashicons dashicons-smiley"></span><br/><br/><?php _e( 'Redirecting to the Elementor edit window.', 'cartflows' ); ?> </p></div>
545 </script>
546
547 <?php
548 /**
549 * Responsive Buttons
550 */
551 ?>
552 <script type="text/template" id="tmpl-cartflows-responsive-view">
553 <span class="responsive-view">
554 <span class="actions">
555 <a class="desktop" href="#"><span data-view="desktop " class="active dashicons dashicons-desktop"></span></a>
556 <a class="tablet" href="#"><span data-view="tablet" class="dashicons dashicons-tablet"></span></a>
557 <a class="mobile" href="#"><span data-view="mobile" class="dashicons dashicons-smartphone"></span></a>
558 </span>
559 </span>
560 </script>
561
562 <?php
563 // Templates data.
564 ?>
565 <script type="text/template" id="tmpl-cartflows-flows-list">
566
567 <# console.log( data.items.length ) #>
568 <# console.log( data.items ) #>
569 <# if ( data.items.length ) { #>
570 <# for ( key in data.items ) { #>
571 <#
572 var flow_steps = [];
573 if( data.items[ key ].flow_steps ) {
574 flow_steps = data.items[ key ].flow_steps.map(function(value,index) {
575 return value['id'];
576 });
577 }
578 #>
579 <div class="inner">
580 <div class="template">
581 <span class="thumbnail site-preview cartflows-preview-flow-steps" data-flow-steps="{{ JSON.stringify( data.items[ key ].flow_steps ) }}" data-title="{{ data.items[ key ].title.rendered }}">
582 <div class="template-screenshot">
583 <# if( data.items[ key ].featured_image_url ) { #>
584 <img src="{{ data.items[ key ].featured_image_url }}" />
585 <# } else { #>
586 <img src="<?php echo esc_attr( CARTFLOWS_URL ); ?>assets/images/400x400.jpg" />
587 <# } #>
588 </div>
589 <a href="<?php echo CARTFLOWS_TEMPLATES_URL . 'preview/?'; ?>flow={{ data.items[ key ].id }}&title={{{ data.items[ key ].title.rendered }}}" class="preview" target="_blank">Preview <i class="dashicons dashicons-external"></i></a>
590 <# if( data.items[ key ].flow_type && 'pro' === data.items[ key ].flow_type.slug ) { #>
591 <span class="wcf-flow-type pro"><?php _e( 'Pro', 'cartflows' ); ?></span>
592 <# } #>
593 </span>
594 <div class="template-id-container">
595 <h3 class="template-name"> {{{ data.items[ key ].title.rendered }}} </h3>
596 <div class="template-actions">
597
598 <#
599 if( data.items[ key ].page_builder.slug ) {
600 required_plugin_group = data.items[ key ].page_builder.slug;
601 } else {
602 required_plugin_group = '';
603 }
604
605 if( data.items[ key ].page_builder.slug && CartFlowsImportVars.required_plugins[data.items[ key ].page_builder.slug] && CartFlowsImportVars.required_plugins[data.items[ key ].page_builder.slug].button_title ) {
606 import_btn_title = CartFlowsImportVars.required_plugins[ data.items[ key ].page_builder.slug ].button_title;
607 } else {
608 import_btn_title = 'Import';
609 } #>
610
611 <# if( data.items[ key ].licence_status && 'valid' === data.items[ key ].licence_status ) { #>
612 <a data-flow-steps="{{ flow_steps }}" data-required-plugin-group="{{required_plugin_group}}" href="#" class="button button-primary cartflows-step-import" data-template-id="{{ data.items[ key ].id }}">{{ import_btn_title }}</a>
613 <# } else if( CartFlowsImportVars._is_pro_active ) { #>
614 <a target="_blank" href="<?php echo esc_url( admin_url( 'plugins.php?cartflows-license-popup' ) ); ?>" class="button button-primary"><?php _e( 'Activate License', 'cartflows' ); ?></a>
615 <# } else { #>
616 <a target="_blank" href="<?php echo esc_url( CARTFLOWS_DOMAIN_URL ); ?>" class="button button-primary"><?php _e( 'Get Pro', 'cartflows' ); ?></a>
617 <# } #>
618 </div>
619 </div>
620 </div>
621 </div>
622 <# } #>
623 <# } #>
624 </script>
625
626 <?php
627 // Empty Step.
628 ?>
629 <script type="text/template" id="tmpl-cartflows-create-blank-step">
630 <div class="inner">
631 <div class="template">
632 <span class="thumbnail site-preview cartflows-flow-preview">
633 <div class="template-screenshot">
634 <img src="<?php echo esc_attr( CARTFLOWS_URL ); ?>assets/images/start-scratch.jpg" />
635 </div>
636 <div id="wcf_create_notice" class=""><a href="https://cartflows.com/" target="_blank"></a></div>
637 </span>
638 <div class="template-id-container">
639 <h3 class="template-name"> Blank </h3>
640 <div class="template-actions">
641 <a href="#" class="button button-primary cartflows-step-import-blank"><?php _e( 'Create', 'cartflows' ); ?></a>
642 </div>
643 </div>
644 </div>
645 </div>
646 </script>
647
648 <?php
649 // Templates data.
650 ?>
651 <script type="text/template" id="tmpl-cartflows-steps-list">
652 <# if ( data.items.length ) { #>
653 <# for ( key in data.items ) { #>
654 <#
655 var flow_steps = [];
656 if( data.items[ key ].flow_steps ) {
657 flow_steps = data.items[ key ].flow_steps.map(function(value,index) {
658 return value['id'];
659 });
660 }
661 #>
662 <div class="inner">
663 <div class="template">
664 <span class="thumbnail site-preview cartflows-preview-flow-steps" data-flow-steps="{{ JSON.stringify( data.items[ key ].flow_steps ) }}" data-title="{{ data.items[ key ].title.rendered }}">
665 <div class="template-screenshot">
666 <# if( data.items[ key ].featured_image_url ) { #>
667 <img src="{{ data.items[ key ].featured_image_url }}" />
668 <# } else { #>
669 <img src="<?php echo esc_attr( CARTFLOWS_URL ); ?>assets/images/400x400.jpg" />
670 <# } #>
671 </div>
672 <div id="wcf_create_notice" class=""><a href="https://cartflows.com/" target="_blank"></a></div>
673 <a href="<?php echo CARTFLOWS_TEMPLATES_URL . 'preview/?'; ?>step={{ data.items[ key ].id }}&title={{{ data.items[ key ].title.rendered }}}" class="preview" target="_blank">Preview <i class="dashicons dashicons-external"></i></a>
674 <# if( data.items[ key ].flow_type && 'pro' === data.items[ key ].flow_type.slug ) { #>
675 <span class="wcf-flow-type pro"><?php _e( 'Pro', 'cartflows' ); ?></span>
676 <# } #>
677 </span>
678 <div class="template-id-container">
679 <h3 class="template-name"> {{{ data.items[ key ].title.rendered }}} </h3>
680 <div class="template-actions">
681
682 <#
683
684 var step_slug = data.items[ key ].step_type.slug || '';
685 var step_title = data.items[ key ].step_type.name || '';
686 var import_btn_title = 'Import';
687
688 var required_plugin_group = '';
689 if( data.items[ key ].page_builder ) {
690 required_plugin_group = data.items[ key ].page_builder.slug;
691
692 if( data.items[ key ].page_builder.slug && CartFlowsImportVars.required_plugins[data.items[ key ].page_builder.slug] && CartFlowsImportVars.required_plugins[data.items[ key ].page_builder.slug].button_title ) {
693 import_btn_title = CartFlowsImportVars.required_plugins[ data.items[ key ].page_builder.slug ].button_title;
694 }
695 }
696 #>
697
698 <# if( data.items[ key ].licence_status && 'valid' === data.items[ key ].licence_status ) { #>
699 <a data-slug="{{step_slug}}" data-title="{{step_title}}" data-flow-steps="{{ flow_steps }}" data-required-plugin-group="{{required_plugin_group}}" href="#" class="button button-primary cartflows-step-import" data-template-id="{{ data.items[ key ].id }}">{{ import_btn_title }}</a>
700 <# } else if( CartFlowsImportVars._is_pro_active ) { #>
701 <a target="_blank" href="<?php echo esc_url( admin_url( 'plugins.php?cartflows-license-popup' ) ); ?>" class="button button-primary"><?php _e( 'Activate License', 'cartflows' ); ?></a>
702 <# } else { #>
703 <a target="_blank" href="<?php echo esc_url( CARTFLOWS_DOMAIN_URL ); ?>" class="button button-primary"><?php _e( 'Get Pro', 'cartflows' ); ?></a>
704 <# } #>
705 </div>
706 </div>
707 </div>
708 </div>
709 <# } #>
710 <# } #>
711 </script>
712
713 <?php
714 /**
715 * TMPL - Website Unreachable
716 */
717 ?>
718 <script type="text/template" id="tmpl-cartflows-website-unreachable">
719 <div class="postbox cartflows-website-unreachable">
720 <h2><?php _e( 'Under Maintenance..', 'cartflows' ); ?></h2>
721 <p><?php _e( 'If you are seeing this message, most likely our servers are under routine maintenance and we will be back shortly.', 'cartflows' ); ?></p>
722 <p><?php _e( 'In rare case, it is possible your website is having trouble connecting with ours. If you need help, please feel free to get in touch with us from our website..', 'cartflows' ); ?></p>
723 </div>
724 </script>
725
726 <?php
727 /**
728 * TMPL - Filters
729 */
730 ?>
731 <script type="text/template" id="tmpl-cartflows-page-builder-notice">
732 <?php
733 $default_page_builder = Cartflows_Helper::get_common_setting( 'default_page_builder' );
734 $page_builder = Cartflows_Helper::get_required_plugins_for_page_builder( Cartflows_Helper::get_common_setting( 'default_page_builder' ) );
735 $title = $page_builder['title'];
736
737 $plugin_string = '<a href="#" data-slug="' . esc_html( $default_page_builder ) . '" class="wcf-install-plugin">Please click here and activate ' . esc_html( $title ) . '</a>';
738 $theme_status = '';
739 if ( 'divi' === $default_page_builder ) {
740
741 $theme_status = $page_builder['theme-status'];
742 $plugin_status = $page_builder['plugin-status'];
743
744 if ( 'deactivate' === $theme_status || 'install' === $plugin_status ) {
745 $plugin_string = 'Please activate ' . esc_html( $title );
746 } elseif ( ( 'deactivate' === $theme_status || 'not-installed' === $theme_status ) && 'install' === $plugin_status ) {
747 $plugin_string = 'Please install and activate ' . esc_html( $title );
748 }
749 }
750 ?>
751 <div class="wcf-page-builder-message">
752 <p><?php /* translators: %s: Plugin string */ printf( __( '%1$s to see CartFlows templates. If you prefer another page builder tool, you can <a href="%2$s" target="blank">select it here</a>.', 'cartflows' ), $plugin_string, admin_url( 'admin.php?page=' . CARTFLOWS_SETTINGS . '&action=common-settings#form-field-wcf_default_page_builder' ) ); ?></p>
753 <p>If your preferred page builder is not available, feel free to <a href="#" data-slug="canvas" class="wcf-create-from-scratch-link">create your own</a> pages using page builder of your choice as CartFlows works with all major page builders.</p>
754 <p>We plan to add design templates made with more page builder shortly!</p>
755 </div>
756 </script>
757
758 <?php
759 /**
760 * TMPL - Filters
761 */
762 ?>
763 <script type="text/template" id="tmpl-cartflows-term-filters-dropdown">
764 <# if ( data ) { #>
765 <select class="{{ data.args.wrapper_class }} {{ data.args.class }}">
766 <# if ( data.args.show_all ) { #>
767 <option value="all"> <?php _e( 'All', 'cartflows' ); ?> </option>
768 <# } #>
769 <# if ( CartFlowsImportVars.step_type === data.args.remote_slug ) { #>
770 <option value=""> <?php _e( 'Select Step Type', 'cartflows' ); ?> </option>
771 <# } #>
772 <# for ( key in data.items ) { #>
773 <option value='{{ data.items[ key ].id }}' data-group='{{ data.items[ key ].id }}' class="{{ data.items[ key ].name }}" data-slug="{{ data.items[ key ].slug }}" data-title="{{ data.items[ key ].name }}">{{ data.items[ key ].name }}</option>
774 <# } #>
775 </select>
776 <# } #>
777 </script>
778
779 <script type="text/template" id="tmpl-cartflows-term-filters">
780
781 <# if ( data ) { #>
782
783 <?php /* <# if ( CartFlowsImportVars.flow_page_builder === data.args.remote_slug || CartFlowsImportVars.step_page_builder === data.args.remote_slug ) { #> */ ?>
784 <ul class="{{ data.args.wrapper_class }} {{ data.args.class }}">
785
786 <# if ( data.args.show_all ) { #>
787 <li>
788 <a href="#" data-group="all"> All </a>
789 </li>
790 <# } #>
791
792 <# for ( key in data.items ) { #>
793 <li>
794 <a href="#" data-group='{{ data.items[ key ].id }}' class="{{ data.items[ key ].name }}" data-slug="{{ data.items[ key ].slug }}" data-title="{{ data.items[ key ].name }}">{{ data.items[ key ].name }}</a>
795 </li>
796 <# } #>
797
798 </ul>
799
800 <?php
801
802 /*
803 <# } else { #>
804 <select class="{{ data.args.wrapper_class }} {{ data.args.class }}">
805
806 <# if ( data.args.show_all ) { #>
807 <option value="all"> <?php _e( 'All', 'cartflows' ); ?> </option>
808 <# } #>
809
810 <# if ( CartFlowsImportVars.step_type === data.args.remote_slug ) { #>
811 <option value=""> <?php _e( 'Select Step Type', 'cartflows' ); ?> </option>
812 <# } #>
813
814 <# for ( key in data.items ) { #>
815 <option value='{{ data.items[ key ].id }}' data-group='{{ data.items[ key ].id }}' class="{{ data.items[ key ].name }}" data-slug="{{ data.items[ key ].slug }}" data-title="{{ data.items[ key ].name }}">{{ data.items[ key ].name }}</option>
816 <# } #>
817
818 </select>
819 */
820 ?>
821
822 <?php /* <# } #> */ ?>
823
824 <# } #>
825 </script>
826
827 <?php
828 // Step Type.
829 ?>
830 <script type="text/template" id="tmpl-cartflows-step-types">
831 <ul class="wcf-tab nav-tabs">
832 <# if( data.items_count ) { #>
833 <# for( key in data.items ) { #>
834 <# console.log( data.items[ key ].id ) #>
835 <li data-slug="{{data.items[ key ].slug}}" data-title="{{ data.items[ key ].name }}">
836 <a href="#{{{ data.items[ key ].slug }}}">{{{ data.items[ key ].name }}}</a>
837 </li>
838 <# } #>
839 <# } #>
840 </ul>
841 </script>
842
843 <?php
844 // Add to library button.
845 ?>
846 <script type="text/template" id="tmpl-templator-add-to-library">
847 <a class="templator-add-to-library page-title-action cartflows-load-steps-library"><i class="dashicons dashicons-cloud"></i><?php esc_attr_e( 'Import from Cloud', 'cartflows' ); ?></a>
848 </script>
849 <?php
850 }
851
852 /**
853 * Enqueue scripts
854 *
855 * @since 1.0.0
856 *
857 * @hook admin_enqueue_scripts
858 * @param string $hook Current page hook.
859 */
860 function scripts( $hook = '' ) {
861
862 if ( ! self::is_supported_post( get_current_screen()->post_type ) ) {
863 return;
864 }
865
866 wp_enqueue_script( 'cartflows-rest-api', CARTFLOWS_URL . 'assets/js/rest-api.js', array( 'jquery' ), CARTFLOWS_VER, true );
867 wp_enqueue_style( 'cartflows-import', CARTFLOWS_URL . 'assets/css/import.css', null, CARTFLOWS_VER, 'all' );
868 wp_style_add_data( 'cartflows-import', 'rtl', 'replace' );
869 wp_enqueue_script( 'cartflows-import', CARTFLOWS_URL . 'assets/js/import.js', array( 'jquery', 'wp-util', 'cartflows-rest-api', 'updates' ), CARTFLOWS_VER, true );
870
871 $localize_vars = array(
872 '_is_pro_active' => _is_cartflows_pro(),
873
874 // Flow and its rest fields.
875 'flow' => CARTFLOWS_FLOW_POST_TYPE,
876 'flow_fields' => array(
877 'id',
878 'title',
879 'flow_type',
880 'page_builder',
881 'flow_steps',
882 'licence_status',
883 'featured_image_url',
884 'featured_media', // @required for field `featured_image_url`.
885 ),
886
887 // Flow type and rest fields.
888 'flow_type' => CARTFLOWS_TAXONOMY_FLOW_CATEGORY,
889 'flow_type_fields' => array(
890 'id',
891 'name',
892 'slug',
893 ),
894
895 // Flow page builder and rest fields.
896 'flow_page_builder' => CARTFLOWS_TAXONOMY_FLOW_PAGE_BUILDER,
897 'flow_page_builder_fields' => array(
898 'id',
899 'name',
900 'slug',
901 ),
902
903 // Step page builder and rest fields.
904 'step_page_builder' => CARTFLOWS_TAXONOMY_STEP_PAGE_BUILDER,
905 'step_page_builder_fields' => array(
906 'id',
907 'name',
908 'slug',
909 ),
910
911 // Step and its rest fields.
912 'step' => CARTFLOWS_STEP_POST_TYPE,
913 'step_fields' => array(
914 'title',
915 'featured_image_url',
916 'featured_media', // @required for field `featured_image_url`.
917 'id',
918 'flow_type',
919 'step_type',
920 'page_builder',
921 'licence_status',
922 ),
923
924 // Step type and its rest fields.
925 'step_type' => CARTFLOWS_TAXONOMY_STEP_TYPE,
926 'step_type_fields' => array(
927 'id',
928 'name',
929 'slug',
930 ),
931
932 'domain_url' => CARTFLOWS_DOMAIN_URL,
933 'server_url' => CARTFLOWS_TEMPLATES_URL,
934 'server_rest_url' => CARTFLOWS_TEMPLATES_URL . 'wp-json/wp/v2/',
935 'site_url' => site_url(),
936 'import_url' => admin_url( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE . '&page=flow_importer' ),
937 'export_url' => admin_url( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE . '&page=flow_exporter' ),
938 'admin_url' => admin_url(),
939 'licence_args' => CartFlows_API::get_instance()->get_licence_args(),
940 'ajaxurl' => admin_url( 'admin-ajax.php' ),
941 'debug' => ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || isset( $_GET['debug'] ) ) ? true : false,
942
943 'required_plugins' => Cartflows_Helper::get_plugins_groupby_page_builders(),
944
945 'default_page_builder' => Cartflows_Helper::get_common_setting( 'default_page_builder' ),
946 );
947
948 // var_dump(Cartflows_Helper::get_common_setting( 'default_page_builder' ));
949 // wp_die( );
950 // Add thickbox.
951 add_thickbox();
952
953 wp_localize_script( 'cartflows-import', 'CartFlowsImportVars', $localize_vars );
954 wp_localize_script( 'cartflows-rest-api', 'CartFlowsImportVars', $localize_vars );
955 }
956
957 /**
958 * Load Template
959 *
960 * @since 1.0.0
961 *
962 * @hook cartflows_load_steps
963 * @return void
964 */
965 function load_templates() {
966
967 check_ajax_referer( 'cf-load-steps', 'security' );
968
969 $args = ( isset( $_POST['args'] ) ) ? array_map( 'sanitize_text_field', $_POST['args'] ) : array();
970 $templates = CartFlows_API::get_instance()->get_templates( $args );
971
972 if ( $templates['templates_count'] ) {
973 wp_send_json_success( $templates );
974 } else {
975 wp_send_json_error( $templates );
976 }
977 wp_die();
978 }
979
980 /**
981 * Import.
982 *
983 * @since 1.0.0
984 *
985 * @hook wp_ajax_cartflows_import_flow_step
986 * @return void
987 */
988 function import_flow() {
989
990 check_ajax_referer( 'cf-import-flow-step', 'security' );
991
992 $flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : '';
993 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : '';
994
995 wcf()->logger->import_log( '------------------------------------' );
996 wcf()->logger->import_log( 'STARTED! Importing FLOW' );
997 wcf()->logger->import_log( '------------------------------------' );
998 wcf()->logger->import_log( '(✓) Creating new step from remote step [' . $template_id . '] for FLOW ' . get_the_title( $flow_id ) . ' [' . $flow_id . ']' );
999
1000 $response = CartFlows_API::get_instance()->get_template( $template_id );
1001
1002 $post_content = isset( $response['data']['content']->rendered ) ? $response['data']['content']->rendered : '';
1003 if ( 'divi' === Cartflows_Helper::get_common_setting( 'default_page_builder' ) ) {
1004 if ( isset( $response['data']['divi_content'] ) && ! empty( $response['data']['divi_content'] ) ) {
1005 $post_content = $response['data']['divi_content'];
1006 }
1007 }
1008
1009 if ( false === $response['success'] ) {
1010 wcf()->logger->import_log( '(✕) Failed to fetch remote data.' );
1011 wp_send_json_error( $response );
1012 }
1013
1014 wcf()->logger->import_log( '(✓) Successfully getting remote step response ' . json_encode( $response ) );
1015
1016 $new_step_id = wp_insert_post(
1017 array(
1018 'post_type' => CARTFLOWS_STEP_POST_TYPE,
1019 'post_title' => $response['title'],
1020 'post_content' => $post_content,
1021 'post_status' => 'publish',
1022 )
1023 );
1024
1025 if ( is_wp_error( $new_step_id ) ) {
1026 wcf()->logger->import_log( '(✕) Failed to create new step for flow ' . $flow_id );
1027 wp_send_json_error( $new_step_id );
1028 }
1029
1030 if ( 'divi' === Cartflows_Helper::get_common_setting( 'default_page_builder' ) ) {
1031 if ( isset( $response['data']['divi_content'] ) && ! empty( $response['data']['divi_content'] ) ) {
1032 update_post_meta( $new_step_id, 'divi_content', $response['data']['divi_content'] );
1033 }
1034 }
1035
1036 /* Imported Step */
1037 update_post_meta( $new_step_id, 'cartflows_imported_step', 'yes' );
1038
1039 wcf()->logger->import_log( '(✓) Created new step ' . '"' . $response['title'] . '" id ' . $new_step_id );
1040 // insert post meta.
1041 update_post_meta( $new_step_id, 'wcf-flow-id', $flow_id );
1042 wcf()->logger->import_log( '(✓) Added flow ID ' . $flow_id . ' in post meta key wcf-flow-id.' );
1043
1044 /**
1045 * Import & Set type.
1046 */
1047 $term = isset( $response['data']['step_type'] ) ? $response['data']['step_type'] : '';
1048 $term_slug = '';
1049 if ( $term ) {
1050
1051 $taxonomy = CARTFLOWS_TAXONOMY_STEP_TYPE;
1052 $term_exist = term_exists( $term->name, $taxonomy );
1053
1054 if ( empty( $term_exist ) ) {
1055 $terms = array(
1056 array(
1057 'name' => $term->name,
1058 ),
1059 );
1060
1061 Cartflows_Step_Post_Type::get_instance()->add_terms( $taxonomy, $terms );
1062 wcf()->logger->import_log( '(✓) Created new term ' . $term->name );
1063 }
1064
1065 $current_term = term_exists( $term->name, $taxonomy );
1066
1067 // Set type object.
1068 $data = get_term( $current_term['term_id'], $taxonomy );
1069 $term_slug = $data->slug;
1070 $term_name = $data->name;
1071 wp_set_object_terms( $new_step_id, $term_slug, CARTFLOWS_TAXONOMY_STEP_TYPE );
1072 wcf()->logger->import_log( '(✓) Assigned existing term ' . $term_name . ' to the template ' . $new_step_id );
1073
1074 // Set type.
1075 update_post_meta( $new_step_id, 'wcf-step-type', $term_slug );
1076 wcf()->logger->import_log( '(✓) Updated term ' . $term_name . ' to the post meta wcf-step-type.' );
1077 }
1078
1079 // Set flow.
1080 wp_set_object_terms( $new_step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
1081 wcf()->logger->import_log( '(✓) Assigned flow step flow-' . $flow_id );
1082
1083 /**
1084 * Update steps for the current flow.
1085 */
1086 $flow_steps = get_post_meta( $flow_id, 'wcf-steps', true );
1087
1088 if ( ! is_array( $flow_steps ) ) {
1089 $flow_steps = array();
1090 }
1091
1092 $flow_steps[] = array(
1093 'id' => $new_step_id,
1094 'title' => $response['title'],
1095 'type' => $term_slug,
1096 );
1097 update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
1098 wcf()->logger->import_log( '(✓) Updated flow steps post meta key \'wcf-steps\' ' . json_encode( $flow_steps ) );
1099
1100 // Import Post Meta.
1101 self::import_post_meta( $new_step_id, $response );
1102
1103 wcf()->logger->import_log( '(✓) Importing step "' . get_the_title( $new_step_id ) . '" [' . $new_step_id . '] for FLOW "' . get_the_title( $flow_id ) . '" [' . $flow_id . ']' );
1104 wcf()->logger->import_log( '------------------------------------' );
1105 wcf()->logger->import_log( 'COMPLETE! Importing FLOW' );
1106 wcf()->logger->import_log( '------------------------------------' );
1107
1108 do_action( 'cartflows_import_complete' );
1109 wcf()->logger->import_log( '(✓) BATCH STARTED for step ' . $new_step_id . ' for Blog name \'' . get_bloginfo( 'name' ) . '\' (' . get_current_blog_id() . ')' );
1110
1111 // Batch Process.
1112 do_action( 'cartflows_after_template_import', $new_step_id, $response );
1113
1114 /**
1115 * End
1116 */
1117 wp_send_json_success( $new_step_id );
1118 }
1119
1120 /**
1121 * Import Step.
1122 *
1123 * @since 1.0.0
1124 * @hook wp_ajax_cartflows_step_import
1125 *
1126 * @return void
1127 */
1128 function create_default_flow() {
1129
1130 check_ajax_referer( 'cf-default-flow', 'security' );
1131
1132 // Create post object.
1133 $new_flow_post = array(
1134 'post_content' => '',
1135 'post_status' => 'publish',
1136 'post_type' => CARTFLOWS_FLOW_POST_TYPE,
1137 );
1138
1139 // Insert the post into the database.
1140 $flow_id = wp_insert_post( $new_flow_post );
1141
1142 if ( is_wp_error( $flow_id ) ) {
1143 wp_send_json_error( $flow_id->get_error_message() );
1144 }
1145
1146 $flow_steps = array();
1147
1148 $steps_data = array(
1149 'landing' => __( 'Landing Page', 'cartflows' ),
1150 'checkout' => __( 'Checkout Page', 'cartflows' ),
1151 'thankyou' => __( 'Thank You Page', 'cartflows' ),
1152 );
1153
1154 foreach ( $steps_data as $slug => $title ) {
1155
1156 $post_content = '';
1157
1158 switch ( $slug ) {
1159 case 'checkout':
1160 $post_content = '';
1161 break;
1162 case 'thankyou':
1163 $post_content = '';
1164 break;
1165 default:
1166 $post_content = '';
1167 break;
1168 }
1169
1170 $step_id = wp_insert_post(
1171 array(
1172 'post_type' => CARTFLOWS_STEP_POST_TYPE,
1173 'post_title' => $title,
1174 'post_content' => $post_content,
1175 'post_status' => 'publish',
1176 )
1177 );
1178
1179 if ( is_wp_error( $step_id ) ) {
1180 wp_send_json_error( $step_id->get_error_message() );
1181 }
1182
1183 if ( $step_id ) {
1184
1185 $flow_steps[] = array(
1186 'id' => $step_id,
1187 'title' => $title,
1188 'type' => $slug,
1189 );
1190
1191 // insert post meta.
1192 update_post_meta( $step_id, 'wcf-flow-id', $flow_id );
1193 update_post_meta( $step_id, 'wcf-step-type', $slug );
1194
1195 wp_set_object_terms( $step_id, $slug, CARTFLOWS_TAXONOMY_STEP_TYPE );
1196 wp_set_object_terms( $step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
1197 }
1198 }
1199
1200 update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
1201
1202 wp_send_json_success( $flow_id );
1203 }
1204
1205 /**
1206 * Create Flow
1207 *
1208 * @return void
1209 */
1210 function create_flow() {
1211
1212 check_ajax_referer( 'cf-create-flow', 'security' );
1213
1214 // Create post object.
1215 $new_flow_post = array(
1216 'post_content' => '',
1217 'post_status' => 'publish',
1218 'post_type' => CARTFLOWS_FLOW_POST_TYPE,
1219 );
1220
1221 // Insert the post into the database.
1222 $flow_id = wp_insert_post( $new_flow_post );
1223
1224 if ( is_wp_error( $flow_id ) ) {
1225 wp_send_json_error( $flow_id->get_error_message() );
1226 }
1227
1228 /* Imported Flow */
1229 update_post_meta( $flow_id, 'cartflows_imported_flow', 'yes' );
1230
1231 wp_send_json_success( $flow_id );
1232 }
1233
1234 /**
1235 * Create Step
1236 *
1237 * @return void
1238 */
1239 function import_step() {
1240
1241 check_ajax_referer( 'cf-step-import', 'security' );
1242
1243 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : '';
1244 $flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : '';
1245 $step_title = isset( $_POST['step_title'] ) ? sanitize_text_field( $_POST['step_title'] ) : '';
1246 $step_type = isset( $_POST['step_type'] ) ? sanitize_title( $_POST['step_type'] ) : '';
1247 $step_custom_title = isset( $_POST['step_custom_title'] ) ? sanitize_title( $_POST['step_custom_title'] ) : $step_title;
1248
1249 $cartflow_meta = Cartflows_Flow_Meta::get_instance();
1250
1251 $post_id = $cartflow_meta->create_step( $flow_id, $step_type, $step_custom_title );
1252
1253 wcf()->logger->import_log( '------------------------------------' );
1254 wcf()->logger->import_log( 'STARTED! Importing STEP' );
1255 wcf()->logger->import_log( '------------------------------------' );
1256
1257 if ( empty( $template_id ) || empty( $post_id ) ) {
1258 /* translators: %s: template ID */
1259 $data = sprintf( __( 'Invalid template id %1$s or post id %2$s.', 'cartflows' ), $template_id, $post_id );
1260 wcf()->logger->import_log( $data );
1261 wp_send_json_error( $data );
1262 }
1263
1264 wcf()->logger->import_log( 'Remote Step ' . $template_id . ' for local flow "' . get_the_title( $post_id ) . '" [' . $post_id . ']' );
1265
1266 $response = CartFlows_API::get_instance()->get_template( $template_id );
1267
1268 if ( 'divi' === Cartflows_Helper::get_common_setting( 'default_page_builder' ) ) {
1269 if ( isset( $response['data']['divi_content'] ) && ! empty( $response['data']['divi_content'] ) ) {
1270 update_post_meta( $post_id, 'divi_content', $response['data']['divi_content'] );
1271
1272 wp_update_post(
1273 array(
1274 'ID' => $post_id,
1275 'post_content' => $response['data']['divi_content'],
1276 )
1277 );
1278 }
1279 }
1280
1281 /* Imported Step */
1282 update_post_meta( $post_id, 'cartflows_imported_step', 'yes' );
1283
1284 // Import Post Meta.
1285 self::import_post_meta( $post_id, $response );
1286
1287 do_action( 'cartflows_import_complete' );
1288
1289 // Batch Process.
1290 do_action( 'cartflows_after_template_import', $post_id, $response );
1291
1292 wcf()->logger->import_log( '------------------------------------' );
1293 wcf()->logger->import_log( 'COMPLETE! Importing Step' );
1294 wcf()->logger->import_log( '------------------------------------' );
1295
1296 wp_send_json_success( $post_id );
1297 }
1298
1299 /**
1300 * Import Step.
1301 *
1302 * @since 1.0.0
1303 * @hook wp_ajax_cartflows_step_create_blank
1304 *
1305 * @return void
1306 */
1307 function step_create_blank() {
1308
1309 check_ajax_referer( 'cf-step-create-blank', 'security' );
1310
1311 $flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : '';
1312 $step_type = isset( $_POST['step_type'] ) ? sanitize_text_field( $_POST['step_type'] ) : '';
1313 $step_title = isset( $_POST['step_title'] ) ? sanitize_text_field( $_POST['step_title'] ) : '';
1314
1315 if ( empty( $flow_id ) || empty( $step_type ) ) {
1316 /* translators: %s: flow ID */
1317 $data = sprintf( __( 'Invalid flow id %1$s OR step type %2$s.', 'cartflows' ), $flow_id, $step_type );
1318 wcf()->logger->import_log( $data );
1319 wp_send_json_error( $data );
1320 }
1321
1322 wcf()->logger->import_log( '------------------------------------' );
1323 wcf()->logger->import_log( 'STARTED! Creating Blank STEP for Flow ' . $flow_id );
1324
1325 $step_type_title = str_replace( '-', ' ', $step_type );
1326 $step_type_slug = strtolower( str_replace( '-', ' ', $step_type ) );
1327
1328 $new_step_id = wp_insert_post(
1329 array(
1330 'post_type' => CARTFLOWS_STEP_POST_TYPE,
1331 'post_title' => $step_title,
1332 'post_content' => '',
1333 'post_status' => 'publish',
1334 )
1335 );
1336
1337 // insert post meta.
1338 update_post_meta( $new_step_id, 'wcf-flow-id', $flow_id );
1339
1340 $taxonomy = CARTFLOWS_TAXONOMY_STEP_TYPE;
1341 $term_exist = term_exists( $step_type_title, $taxonomy );
1342
1343 if ( empty( $term_exist ) ) {
1344 $terms = array(
1345 array(
1346 'name' => $step_type_title,
1347 ),
1348 );
1349
1350 Cartflows_Step_Post_Type::get_instance()->add_terms( $taxonomy, $terms );
1351 wcf()->logger->import_log( '(✓) Created new term ' . $step_type_title );
1352 }
1353
1354 $current_term = term_exists( $step_type_title, $taxonomy );
1355
1356 // Set type object.
1357 $data = get_term( $current_term['term_id'], $taxonomy );
1358 $step_slug = $data->slug;
1359 wp_set_object_terms( $new_step_id, $data->slug, CARTFLOWS_TAXONOMY_STEP_TYPE );
1360 wcf()->logger->import_log( '(✓) Assigned existing term ' . $step_type_title . ' to the template ' . $new_step_id );
1361
1362 // Set type.
1363 update_post_meta( $new_step_id, 'wcf-step-type', $data->slug );
1364 wcf()->logger->import_log( '(✓) Updated term ' . $data->name . ' to the post meta wcf-step-type.' );
1365
1366 // Set flow.
1367 wp_set_object_terms( $new_step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
1368 wcf()->logger->import_log( '(✓) Assigned flow step flow-' . $flow_id );
1369
1370 CartFlows_Importer::get_instance()->set_step_to_flow( $flow_id, $new_step_id, $step_type_title, $step_slug );
1371
1372 wcf()->logger->import_log( 'COMPLETE! Creating Blank STEP for Flow ' . $flow_id );
1373 wcf()->logger->import_log( '------------------------------------' );
1374
1375 wp_send_json_success( $new_step_id );
1376 }
1377
1378 /**
1379 * Import Post Meta
1380 *
1381 * @since 1.0.0
1382 *
1383 * @param integer $post_id Post ID.
1384 * @param array $response Post meta.
1385 * @return void
1386 */
1387 public static function import_post_meta( $post_id, $response ) {
1388
1389 $metadata = (array) $response['post_meta'];
1390
1391 foreach ( $metadata as $meta_key => $meta_value ) {
1392 $meta_value = isset( $meta_value[0] ) ? $meta_value[0] : '';
1393
1394 if ( $meta_value ) {
1395
1396 if ( is_serialized( $meta_value, true ) ) {
1397 $raw_data = maybe_unserialize( stripslashes( $meta_value ) );
1398 } elseif ( is_array( $meta_value ) ) {
1399 $raw_data = json_decode( stripslashes( $meta_value ), true );
1400 } else {
1401 $raw_data = $meta_value;
1402 }
1403
1404 if ( '_elementor_data' === $meta_key ) {
1405 if ( is_array( $raw_data ) ) {
1406 $raw_data = wp_slash( json_encode( $raw_data ) );
1407 } else {
1408 $raw_data = wp_slash( $raw_data );
1409 }
1410 }
1411 if ( '_elementor_data' !== $meta_key && '_elementor_draft' !== $meta_key && '_fl_builder_data' !== $meta_key && '_fl_builder_draft' !== $meta_key ) {
1412 if ( is_array( $raw_data ) ) {
1413 wcf()->logger->import_log( '(✓) Added post meta ' . $meta_key . ' | ' . json_encode( $raw_data ) );
1414 } else {
1415 if ( ! is_object( $raw_data ) ) {
1416 wcf()->logger->import_log( '(✓) Added post meta ' . $meta_key . ' | ' . $raw_data );
1417 }
1418 }
1419 }
1420
1421 update_post_meta( $post_id, $meta_key, $raw_data );
1422 }
1423 }
1424 }
1425
1426 /**
1427 * Import Template for Elementor
1428 *
1429 * @since 1.0.0
1430 *
1431 * @param integer $post_id Post ID.
1432 * @param array $response Post meta.
1433 * @param array $page_build_data Page build data.
1434 * @return void
1435 */
1436 public static function import_template_elementor( $post_id, $response, $page_build_data ) {
1437 if ( ! is_plugin_active( 'elementor/elementor.php' ) ) {
1438 $data = __( 'Elementor is not activated. Please activate plugin Elementor Page Builder to import the step.', 'cartflows' );
1439 wcf()->logger->import_log( $data );
1440 wp_send_json_error( $data );
1441 }
1442
1443 require_once CARTFLOWS_DIR . 'classes/batch-process/class-cartflows-importer-elementor.php';
1444
1445 wcf()->logger->import_log( '# Started "importing page builder data" for step ' . $post_id );
1446
1447 $obj = new \Elementor\TemplateLibrary\CartFlows_Importer_Elementor();
1448 $obj->import_single_template( $post_id );
1449
1450 wcf()->logger->import_log( '# Complete "importing page builder data" for step ' . $post_id );
1451 }
1452
1453 /**
1454 * Supported post types
1455 *
1456 * @since 1.0.0
1457 *
1458 * @return array Supported post types.
1459 */
1460 public static function supported_post_types() {
1461 return apply_filters(
1462 'cartflows_supported_post_types',
1463 array(
1464 CARTFLOWS_FLOW_POST_TYPE,
1465 )
1466 );
1467 }
1468
1469 /**
1470 * Check supported post type
1471 *
1472 * @since 1.0.0
1473 *
1474 * @param string $post_type Post type.
1475 * @return boolean Supported post type status.
1476 */
1477 public static function is_supported_post( $post_type = '' ) {
1478 if ( in_array( $post_type, self::supported_post_types() ) ) {
1479 return true;
1480 }
1481
1482 return false;
1483 }
1484
1485 /**
1486 * Set steps to the flow
1487 *
1488 * @param integer $flow_id Flow ID.
1489 * @param integer $new_step_id New step ID.
1490 * @param string $step_title Flow Type.
1491 * @param string $step_slug Flow Type.
1492 */
1493 function set_step_to_flow( $flow_id, $new_step_id, $step_title, $step_slug ) {
1494 // Update steps for the current flow.
1495 $flow_steps = get_post_meta( $flow_id, 'wcf-steps', true );
1496
1497 if ( ! is_array( $flow_steps ) ) {
1498 $flow_steps = array();
1499 }
1500
1501 $flow_steps[] = array(
1502 'id' => $new_step_id,
1503 'title' => $step_title,
1504 'type' => $step_slug,
1505 );
1506 update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
1507 wcf()->logger->import_log( '(✓) Updated flow steps post meta key \'wcf-steps\' ' . json_encode( $flow_steps ) );
1508 }
1509
1510 /**
1511 * Localize variables in admin
1512 *
1513 * @param array $vars variables.
1514 */
1515 function localize_vars( $vars ) {
1516
1517 $ajax_actions = array(
1518 'cf_step_import',
1519 'cf_load_steps',
1520 'cf_create_flow',
1521 'cf_default_flow',
1522 'cf_step_create_blank',
1523 'cf_import_flow_step',
1524 );
1525
1526 foreach ( $ajax_actions as $action ) {
1527
1528 $vars[ $action . '_nonce' ] = wp_create_nonce( str_replace( '_', '-', $action ) );
1529 }
1530
1531 return $vars;
1532 }
1533
1534 /**
1535 * Ajax action to activate plugin
1536 */
1537 public function activate_plugin() {
1538
1539 $plugin_init = isset( $_POST['plugin_init'] ) ? sanitize_text_field( $_POST['plugin_init'] ) : '';
1540
1541 $activate = activate_plugin( $plugin_init, '', false, true );
1542
1543 if ( is_wp_error( $activate ) ) {
1544 wp_send_json_error(
1545 array(
1546 'success' => false,
1547 'message' => $activate->get_error_message(),
1548 'init' => $plugin_init,
1549 )
1550 );
1551 }
1552
1553 wp_send_json_success(
1554 array(
1555 'success' => true,
1556 'message' => __( 'Plugin Successfully Activated', 'cartflows' ),
1557 'init' => $plugin_init,
1558 )
1559 );
1560 }
1561
1562 }
1563
1564 /**
1565 * Initialize class object with 'get_instance()' method
1566 */
1567 CartFlows_Importer::get_instance();
1568
1569 endif;
1570