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

wizard.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.0.1, at wizard/ajax/wizard.php

811 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Flows ajax actions.
4 *
5 * @package CartFlows
6 */
7
8 namespace CartflowsAdmin\Wizard\Ajax;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 use CartflowsAdmin\Wizard\Ajax\AjaxBase;
16 use CartflowsAdmin\AdminCore\Ajax\Importer;
17 use CartflowsAdmin\Wizard\Inc\WizardCore;
18
19 /**
20 * Class Steps.
21 */
22 class Wizard extends AjaxBase {
23
24 /**
25 * Instance
26 *
27 * @access private
28 * @var object Class object.
29 * @since 1.0.0
30 */
31 private static $instance;
32
33 /**
34 * Initiator
35 *
36 * @since 1.0.0
37 * @return object initialized object of class.
38 */
39 public static function get_instance() {
40 if ( ! isset( self::$instance ) ) {
41 self::$instance = new self();
42 }
43 return self::$instance;
44 }
45
46 /**
47 * Register ajax events.
48 *
49 * @since 1.0.0
50 * @return void
51 */
52 public function register_ajax_events() {
53
54 $ajax_events = array(
55 'page_builder_save_option',
56 'user_onboarding',
57 'wizard_activate_plugin',
58 'get_global_flow_list',
59 'get_single_store_checkout_flow',
60 'import_store_checkout',
61 'onboarding_completed',
62 'onboarding_exit',
63 );
64
65 $this->init_ajax_events( $ajax_events );
66 }
67
68 /**
69 * Update option.
70 *
71 * @since 1.0.0
72 * @return void
73 */
74 public function onboarding_exit() {
75
76 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
77
78 if ( ! current_user_can( 'manage_options' ) ) {
79 wp_send_json_error( $response_data );
80 }
81
82 /**
83 * Nonce verification
84 */
85 if ( ! check_ajax_referer( 'cartflows_onboarding_exit', 'security', false ) ) {
86 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
87 wp_send_json_error( $response_data );
88 }
89
90 $step = isset( $_POST['current_step'] ) ? sanitize_text_field( wp_unslash( $_POST['current_step'] ) ) : '';
91
92 update_option( 'wcf_setup_skipped', true );
93 update_option( 'wcf_exit_setup_step', $step );
94
95 wp_send_json_success();
96 }
97
98 /**
99 * Update option.
100 *
101 * @since 1.0.0
102 * @return void
103 */
104 public function onboarding_completed() {
105
106 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
107
108 if ( ! current_user_can( 'manage_options' ) ) {
109 wp_send_json_error( $response_data );
110 }
111
112 /**
113 * Nonce verification
114 */
115 if ( ! check_ajax_referer( 'cartflows_onboarding_completed', 'security', false ) ) {
116 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
117 wp_send_json_error( $response_data );
118 }
119
120 update_option( 'wcf_setup_complete', true );
121 delete_option( 'wcf_exit_setup_step' );
122 delete_option( 'wcf_setup_skipped' );
123
124 wp_send_json_success();
125 }
126
127 /**
128 * Save page builder.
129 *
130 * @since 1.0.0
131 * @return void
132 */
133 public function page_builder_save_option() {
134
135 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
136
137 if ( ! current_user_can( 'manage_options' ) ) {
138 wp_send_json_error( $response_data );
139 }
140
141 /**
142 * Nonce verification
143 */
144 if ( ! check_ajax_referer( 'cartflows_page_builder_save_option', 'security', false ) ) {
145 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
146 wp_send_json_error( $response_data );
147 }
148
149 $page_builder = isset( $_POST['page_builder'] ) ? sanitize_text_field( wp_unslash( $_POST['page_builder'] ) ) : '';
150
151 $wcf_settings = get_option( '_cartflows_common', array() );
152
153 if ( false !== strpos( $page_builder, 'beaver-builder' ) ) {
154 $page_builder = 'beaver-builder';
155 }
156
157 if ( false !== strpos( $page_builder, 'gutenberg' ) ) {
158 $page_builder = 'gutenberg';
159 }
160
161 if ( false !== strpos( $page_builder, 'bricks' ) ) {
162 $page_builder = 'bricks-builder';
163 }
164
165 $wcf_settings['default_page_builder'] = $page_builder;
166
167 update_option( '_cartflows_common', $wcf_settings );
168
169 wp_send_json_success(
170 array( 'plugin' => $page_builder )
171 );
172 }
173
174 /**
175 * Optin step.
176 *
177 * @since 1.0.0
178 * @return void
179 */
180 public function user_onboarding() {
181
182 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
183
184 if ( ! current_user_can( 'manage_options' ) ) {
185 wp_send_json_error( $response_data );
186 }
187
188 /**
189 * Nonce verification
190 */
191 if ( ! check_ajax_referer( 'cartflows_user_onboarding', 'security', false ) ) {
192 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
193 wp_send_json_error( $response_data );
194 }
195
196 $target_url = CARTFLOWS_TEMPLATES_URL . 'wp-json/cartflows-server/v1/add-subscriber';
197 $response_body = array();
198
199 $email = isset( $_POST['user_email'] ) ? sanitize_email( wp_unslash( $_POST['user_email'] ) ) : '';
200 $user_name = isset( $_POST['user_fname'] ) ? sanitize_text_field( wp_unslash( $_POST['user_fname'] ) ) : '';
201 $usage_tracking = isset( $_POST['usage_tracking'] ) ? sanitize_text_field( wp_unslash( $_POST['usage_tracking'] ) ) : '';
202
203 if ( empty( $email ) ) {
204 wp_send_json_error(
205 array(
206 'success' => false,
207 'message' => __( 'Please enter your email ID.', 'cartflows' ),
208 )
209 );
210 }
211
212 // Enable the usage tracking for BSF Analytics.
213 update_option( 'cf_usage_optin', $usage_tracking );
214
215 $api_args = array(
216 'timeout' => 90, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
217 'body' => array(
218 'user_email' => $email,
219 'user_fname' => $user_name,
220 'source' => 'cartflows',
221 'add-subscriber' => true,
222 ),
223
224 );
225
226 $response = wp_remote_post( $target_url, $api_args );
227
228 $has_errors = $this->is_api_error( $response );
229
230 if ( $has_errors['error'] ) {
231 wp_send_json_error(
232 array(
233 'success' => false,
234 'message' => $has_errors['error_message'],
235 )
236 );
237 }
238
239 $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
240
241 // Store in option to send the weekly emails.
242 update_site_option( 'cartflows_stats_report_email_ids', $email );
243
244 wp_send_json_success(
245 array(
246 'success' => $response_body['success'],
247 'message' => $response_body['message'] ? $response_body['message'] : '',
248 )
249 );
250 }
251
252 /**
253 * Check is error in the received response.
254 *
255 * @param object $response Received API Response.
256 * @return array $result Error result.
257 */
258 public function is_api_error( $response ) {
259
260 $result = array(
261 'error' => false,
262 'error_message' => __( 'Oops! Something went wrong. Please refresh the page and try again.', 'cartflows' ),
263 'error_code' => 0,
264 );
265
266 if ( is_wp_error( $response ) ) {
267 $result['error'] = true;
268 $result['error_message'] = $response->get_error_message();
269 $result['error_code'] = $response->get_error_code();
270 } elseif ( ! empty( wp_remote_retrieve_response_code( $response ) ) && ! in_array( wp_remote_retrieve_response_code( $response ), array( 200, 201, 204 ), true ) ) {
271 $result['error'] = true;
272 $result['error_message'] = wp_remote_retrieve_response_message( $response );
273 $result['error_code'] = wp_remote_retrieve_response_code( $response );
274 }
275
276 return $result;
277 }
278
279 /**
280 * Update step title.
281 *
282 * @since 1.0.0
283 * @return void
284 */
285 public function wizard_activate_plugin() {
286
287 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
288
289 if ( ! current_user_can( 'manage_options' ) ) {
290 wp_send_json_error( $response_data );
291 }
292
293 /**
294 * Nonce verification
295 */
296 if ( ! check_ajax_referer( 'cartflows_wizard_activate_plugin', 'security', false ) ) {
297 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
298 wp_send_json_error( $response_data );
299 }
300
301 $plugin_init = isset( $_POST['plugin_init'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_init'] ) ) : '';
302 $plugin_slug = isset( $_POST['plugin_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_slug'] ) ) : '';
303
304 $do_sliently = true;
305
306 $exclude_do_silently = array(
307 'woo-cart-abandonment-recovery',
308 );
309
310 if ( in_array( $plugin_slug, $exclude_do_silently, true ) ) {
311 $do_sliently = false;
312 }
313
314 if ( false !== strpos( $plugin_slug, 'beaver-builder' ) ) {
315 $plugin_init = $plugin_slug . '/fl-builder.php';
316 }
317
318 $activate = activate_plugin( $plugin_init, '', false, $do_sliently );
319
320 if ( is_wp_error( $activate ) ) {
321 wp_send_json_error(
322 array(
323 'success' => false,
324 'message' => $activate->get_error_message(),
325 )
326 );
327 }
328
329 wp_send_json_success(
330 array(
331 'success' => true,
332 'message' => $plugin_slug,
333 )
334 );
335 }
336
337 /**
338 * Fetch all the available flows for global checkout.
339 *
340 * @since 1.10.0
341 */
342 public function get_global_flow_list() {
343 $response_data = array( 'message' => $this->get_error_msg( 'permission' ) );
344
345 if ( ! current_user_can( 'manage_options' ) ) {
346 wp_send_json_error( $response_data );
347 }
348
349 /**
350 * Nonce verification
351 */
352 if ( ! check_ajax_referer( 'cartflows_get_global_flow_list', 'security', false ) ) {
353 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
354 wp_send_json_error( $response_data );
355 }
356
357 $page_builder = isset( $_POST['page_builder'] ) ? sanitize_text_field( wp_unslash( $_POST['page_builder'] ) ) : '';
358
359 $page = 1;
360 $flows_list = array();
361
362 if ( empty( $page_builder ) ) {
363 $response_data = array( 'message' => __( 'Please select any of the page builder to display the ready templates.', 'cartflows' ) );
364 wp_send_json_error( $response_data );
365 }
366
367 // Check is the template library updated or not.
368 if ( 'no' === \CartFlows_Batch_Process::get_instance()->get_last_export_checksums( 'store-checkout' ) ) {
369 // Get the cached flows as no changes are made in the template library.
370 $cached_flows = get_site_option( 'cartflows-store-checkout-' . $page_builder . '-flows-and-steps-' . $page );
371
372 $flows_list = ! empty( $cached_flows ) ? $cached_flows : array();
373 }
374
375 // If the cached flow list is empty then make a new request and fetch the templates.
376 if ( empty( $flows_list ) ) {
377 // Fetch new list of templates.
378 $flows_list = $this->import_templates( $page_builder, $page );
379
380 // Update the cheksum after importing the latest templates.
381 \CartFlows_Batch_Process::get_instance()->update_latest_checksums( 'store-checkout' );
382 }
383
384 /**
385 * Redirect to the new step edit screen
386 */
387 $response_data = array(
388 'message' => __( 'Successful!', 'cartflows' ),
389 'flows' => $flows_list,
390 );
391
392 wp_send_json_success( $response_data );
393 }
394
395 /**
396 * Import
397 *
398 * @since 1.0.14
399 * @since 1.6.15 Added page no.
400 * @param string $page_builder page builder slug.
401 * @param integer $page Page number.
402 * @return array
403 */
404 public function import_templates( $page_builder, $page = 1 ) {
405
406 $api_args = array(
407 'timeout' => 30, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
408 );
409
410 $sites_and_pages = array();
411 $site_url = wcf()->get_site_url();
412
413 $query_args = array(
414 'per_page' => 100,
415 'page' => $page,
416 'flow_category' => array(
417 'include_terms' => array( 'store-checkout' ),
418 ),
419 );
420
421 $api_url = add_query_arg( $query_args, $site_url . 'wp-json/cartflows-server/v1/flows-and-steps/' );
422
423 $response = wp_remote_get( $api_url, $api_args );
424
425 if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) {
426
427 $sites_and_pages = json_decode( wp_remote_retrieve_body( $response ), true );
428
429 if ( isset( $sites_and_pages['code'] ) ) {
430 $message = isset( $sites_and_pages['message'] ) ? $sites_and_pages['message'] : '';
431 if ( ! empty( $message ) ) {
432 wcf()->logger->sync_log( 'HTTP Request Error: ' . $message );
433 } else {
434 wcf()->logger->sync_log( 'HTTP Request Error!' );
435 }
436 } elseif ( is_array( $sites_and_pages ) && isset( $sites_and_pages['flows'] ) && ! empty( $sites_and_pages['flows'] ) ) {
437 $option_name = 'cartflows-store-checkout-' . sanitize_key( $page_builder ) . '-flows-and-steps-' . $page;
438 update_site_option( $option_name, $sites_and_pages['flows'] );
439 } else {
440 $sites_and_pages['flows'] = array();
441 }
442 } else {
443 wcf()->logger->sync_log( 'API Error: ' . $response->get_error_message() );
444 }
445
446 return $sites_and_pages['flows'];
447 }
448
449 /**
450 * Get store flow.
451 */
452 public function get_single_store_checkout_flow() {
453
454 $response_data = array( 'message' => $this->get_error_msg( 'permission' ) );
455
456 if ( ! current_user_can( 'manage_options' ) ) {
457 wp_send_json_error( $response_data );
458 }
459
460 /**
461 * Nonce verification
462 */
463 if ( ! check_ajax_referer( 'cartflows_get_single_store_checkout_flow', 'security', false ) ) {
464 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
465 wp_send_json_error( $response_data );
466 }
467
468 $flow_id = isset( $_POST['flow_id'] ) ? absint( $_POST['flow_id'] ) : 0;
469
470 // Get single step Rest API response.
471 $response = \CartFlows_API::get_instance()->get_flow( $flow_id );
472
473 wp_send_json_success( $response['data'] );
474 }
475
476 /**
477 * Import the flow template for the store checkout.
478 * Once the import is done, then set it's checkout page as a Global Checkout.
479 *
480 * @since 1.10.0
481 * @return void
482 */
483 public function import_store_checkout() {
484 $response_data = array( 'message' => $this->get_error_msg( 'permission' ) );
485
486 if ( ! current_user_can( 'manage_options' ) ) {
487 wp_send_json_error( $response_data );
488 }
489
490 /**
491 * Nonce verification
492 */
493 if ( ! check_ajax_referer( 'cartflows_import_store_checkout', 'security', false ) ) {
494 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
495 wp_send_json_error( $response_data );
496 }
497
498 // $_POST['flow'] is the JSON, There is nothing to sanitize JSON as it is data format not data type.
499 $flow = isset( $_POST['flow'] ) ? json_decode( stripslashes( $_POST['flow'] ), true ) : array(); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
500
501 if ( empty( $flow ) ) {
502 $response_data = array( 'message' => __( 'No flow ID found. Please select atleast one flow to import.', 'cartflows' ) );
503 wp_send_json_error( $response_data );
504 }
505
506 // Import the Instant Checkout as a Store Checkour oR just a normal Store Checkout.
507 if ( ! empty( $flow['is_instant_checkout'] ) && (bool) $flow['is_instant_checkout'] ) {
508 $response_data = $this->create_instant_checkout_funnel( $flow );
509 } else {
510 // Get single step Rest API response.
511 $response = \CartFlows_API::get_instance()->get_flow( $flow['ID'] );
512
513 if ( is_wp_error( $response['data'] ) ) {
514
515 /* translators: %1$s: html tag, %2$s: link html start %3$s: link html end */
516 $btn = sprintf( __( 'Request timeout error. Please check if the firewall or any security plugin is blocking the outgoing HTTP/HTTPS requests to templates.cartflows.com or not. %1$sTo resolve this issue, please check this %2$sarticle%3$s.', 'cartflows' ), '<br><br>', '<a target="_blank" href="https://cartflows.com/docs/request-timeout-error-while-importing-the-flow-step-templates/?utm_source=dashboard&utm_medium=free-cartflows&utm_campaign=docs">', '</a>' );
517
518 wp_send_json_error(
519 array(
520 'message' => $response['data']->get_error_message(),
521 'call_to_action' => $btn,
522 'data' => $response,
523 )
524 );
525 }
526
527 // Do license validation if showing Pro flows.
528 $license_status = isset( $response['data']['licence_status'] ) ? $response['data']['licence_status'] : '';
529
530 // If license is invalid then.
531 if ( 'valid' !== $license_status ) {
532
533 $cf_pro_status = WizardCore::get_instance()->get_plugin_status( 'cartflows-pro/cartflows-pro.php' );
534
535 $title = '';
536 $msg = '';
537 if ( 'not-installed' === $cf_pro_status ) {
538 /* translators: %1$s: link html start, %2$s: link html end*/
539 $msg = sprintf( __( 'To import this template, CartFlows Pro Required! %1$sUpgrade to CartFlows Pro%2$s', 'cartflows' ), '<a target="_blank" href="https://cartflows.com/?utm_source=dashboard&utm_medium=free-cartflows&utm_campaign=go-pro">', '</a>' );
540 $title = __( 'CartFlows Pro Required', 'cartflows' );
541 } elseif ( 'inactive' === $cf_pro_status ) {
542 $title = __( 'CartFlows Pro Required', 'cartflows' );
543 /* translators: %1$s: link html start, %2$s: link html end*/
544 $msg = sprintf( __( 'Activate the CartFlows Pro to import the flow! %1$sActivate CartFlows Pro%2$s', 'cartflows' ), '<a target="_blank" href="' . admin_url( 'plugins.php?plugin_status=search&paged=1&s=CartFlows+Pro' ) . '">', '</a>' );
545 } elseif ( 'active' === $cf_pro_status ) {
546 $title = __( 'Invalid License Key', 'cartflows' );
547 /* translators: %1$s: link html start, %2$s: link html end*/
548 $msg = sprintf( __( 'No valid license key found! %1$sActivate license%2$s', 'cartflows' ), '<a target="_blank" href="' . admin_url( 'plugins.php?cartflows-license-popup' ) . '">', '</a>' );
549 }
550
551 wp_send_json_error(
552 array(
553 'message' => $title,
554 'call_to_action' => $msg,
555 'data' => $response,
556 )
557 );
558 }
559 // Do license validation if showing pro flows.
560
561 /**
562 * Create Flow
563 */
564 $new_flow_post = array(
565 'post_title' => isset( $flow['title'] ) ? sanitize_text_field( wp_unslash( $flow['title'] ) ) : '',
566 'post_content' => '',
567 'post_status' => 'publish',
568 'post_type' => CARTFLOWS_FLOW_POST_TYPE,
569 );
570
571 // Insert the post into the database.
572 $new_flow_id = wp_insert_post( $new_flow_post );
573
574 if ( is_wp_error( $new_flow_id ) ) {
575 wp_send_json_error( $new_flow_id->get_error_message() );
576 }
577
578 wcf()->logger->import_log( '✓ Flow Created! Flow ID: ' . $new_flow_id . ' - Remote Flow ID - ' . $flow['ID'] );
579
580 /**
581 * Update Store Checkout flow meta
582 */
583
584 // Set newly created flow as store checkout.
585 update_option( '_cartflows_store_checkout', $new_flow_id );
586
587 // Remove old checkout key if available.
588 delete_option( '_cartflows_old_global_checkout' );
589
590 // reset global checkout on store checkout creation.
591 $common_settings = \Cartflows_Helper::get_common_settings();
592 $common_settings['global_checkout'] = '';
593 $common_settings['override_global_checkout'] = 'disable';
594 update_option( '_cartflows_common', $common_settings );
595
596 /**
597 * Import All Steps
598 */
599 $steps = isset( $flow['steps'] ) ? $flow['steps'] : array();
600
601 $is_checkout_available = false;
602 $flow_steps = array();
603
604 // Get importer function instance to call the import steps function.
605 $importer = new Importer();
606
607 foreach ( $steps as $key => $step ) {
608 // Separate out the Checkout step to set it as Global Checkout.
609 if ( 'checkout' === $step['type'] ) {
610 $is_checkout_available = true;
611 }
612
613 if ( in_array( $step['type'], array( 'upsell', 'downsell' ), true ) && ( ! _is_cartflows_pro() || is_wcf_starter_plan() ) ) {
614 continue;
615 }
616
617 $importer->import_single_step(
618 array(
619 'step' => array(
620 'id' => $step['ID'],
621 'title' => $step['title'],
622 'type' => $step['type'],
623 ),
624 'flow' => array(
625 'id' => $new_flow_id,
626 ),
627 ),
628 'cartflows_import_store_checkout'
629 );
630 }
631
632 /**
633 * This option will be used to display the NPS survey notice.
634 */
635 update_option( '_cartflows_wizard_store_checkout_set', 'yes' );
636
637 /**
638 * Redirect to the new flow edit screen
639 */
640 $response_data = array(
641 'success' => true,
642 'message' => __( 'Successfully imported the Flow!', 'cartflows' ),
643 'items' => $flow,
644 'redirect_url' => admin_url( 'post.php?action=edit&post=' . $new_flow_id ),
645 'new_flow_id' => $new_flow_id,
646 );
647
648 wcf()->logger->import_log( 'COMPLETE! Importing Flow' );
649 }
650
651 wp_send_json_success( $response_data );
652 }
653
654 /**
655 * This function creates the Instant Checkout Funnel.
656 *
657 * @since 2.1.5
658 * @param array $flow The flow which is going to be imported.
659 * @return array Response of the function.
660 */
661 public function create_instant_checkout_funnel( $flow ) {
662
663 wcf()->logger->import_log( 'Importing the Instant Checkout Funnel from onboarding wizard' );
664
665 $response_data = array( 'message' => $this->get_error_msg( 'permission' ) );
666
667 if ( ! current_user_can( 'manage_options' ) ) {
668 wcf()->logger->import_log( 'User don\'t have the permission to do this operation.' );
669 wp_send_json_error( $response_data );
670 }
671
672 /**
673 * Nonce verification
674 */
675 if ( ! check_ajax_referer( 'cartflows_import_store_checkout', 'security', false ) ) {
676 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
677 wcf()->logger->import_log( 'Nonce verificaton is failed.' );
678 wp_send_json_error( $response_data );
679 }
680
681 // Create post object.
682 $new_flow_post = array(
683 'post_title' => isset( $flow['title'] ) ? sanitize_text_field( wp_unslash( $flow['title'] ) ) : '',
684 'post_content' => '',
685 'post_status' => 'publish',
686 'post_type' => CARTFLOWS_FLOW_POST_TYPE,
687 );
688
689 // Insert the post into the database.
690 $flow_id = wp_insert_post( $new_flow_post );
691
692 if ( is_wp_error( $flow_id ) ) {
693 wp_send_json_error( $flow_id->get_error_message() );
694 }
695
696 wcf()->logger->import_log( 'Blank funnel is created with title as: ' . $new_flow_post['post_title'] . ' and ID: ' . $flow_id );
697
698 // If is store checkout update store_checkout options.
699 update_option( '_cartflows_store_checkout', $flow_id );
700
701 // reset global checkout on store checkout creation.
702 $common_settings = \Cartflows_Helper::get_common_settings();
703 $common_settings['global_checkout'] = '';
704 $common_settings['override_global_checkout'] = 'disable';
705
706 update_option( '_cartflows_common', $common_settings );
707
708 wcf()->logger->import_log( 'Funnel is set as Store Checkout' );
709
710 $flow_steps = array();
711
712 wcf()->logger->import_log( 'Is WooCommerce Installed and Activated: ' . wcf()->is_woo_active );
713
714 if ( wcf()->is_woo_active ) {
715 $steps_data = array(
716 'order-form' => array(
717 'title' => __( 'Checkout', 'cartflows' ),
718 'type' => 'checkout',
719 ),
720 'order-confirmation' => array(
721 'title' => __( 'Thank You', 'cartflows' ),
722 'type' => 'thankyou',
723 ),
724 );
725 } else {
726 $steps_data = array(
727 'landing' => array(
728 'title' => __( 'Landing', 'cartflows' ),
729 'type' => 'landing',
730 ),
731 'thankyou' => array(
732 'title' => __( 'Thank You', 'cartflows' ),
733 'type' => 'landing',
734 ),
735 );
736 }
737
738 wcf()->logger->import_log( '====== Creating Steps for the selected funnel: ' . $flow_id . ' ======' );
739
740 foreach ( $steps_data as $slug => $data ) {
741
742 $post_content = '';
743 $step_type = trim( $data['type'] );
744
745 // Create new step.
746 $step_id = wp_insert_post(
747 array(
748 'post_type' => CARTFLOWS_STEP_POST_TYPE,
749 'post_title' => $data['title'],
750 'post_content' => $post_content,
751 'post_status' => 'publish',
752 )
753 );
754
755 // Return the error.
756 if ( is_wp_error( $step_id ) ) {
757 wp_send_json_error( $step_id->get_error_message() );
758 }
759
760 wcf()->logger->import_log( 'New step created: ' . $step_id . ' Step Type: ' . $step_type );
761
762 if ( $step_id ) {
763
764 $flow_steps[] = array(
765 'id' => $step_id,
766 'title' => $data['title'],
767 'type' => $step_type,
768 );
769
770 // Insert post meta.
771 update_post_meta( $step_id, 'wcf-flow-id', $flow_id );
772 update_post_meta( $step_id, 'wcf-step-type', $step_type );
773
774 // Set taxonomies.
775 wp_set_object_terms( $step_id, $step_type, CARTFLOWS_TAXONOMY_STEP_TYPE );
776 wp_set_object_terms( $step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
777
778 update_post_meta( $step_id, '_wp_page_template', 'cartflows-default' );
779
780 wcf()->logger->import_log( 'Required step data set for newly created step: ' . $step_id );
781 }
782 }
783
784 wcf()->logger->import_log( '====== Steps created for the selected funnel: ' . $flow_id . ' ======' );
785
786 update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
787
788 wcf()->logger->import_log( 'Steps in the funnel: ' . $flow_id . ' are set.' );
789
790 // Set the current funnel as instant checkout.
791 update_post_meta( $flow_id, 'instant-layout-style', 'yes' );
792
793 wcf()->logger->import_log( 'The funnel: ' . $flow_id . ' is enabled for instant checkout.' );
794
795 /**
796 * This option will be used to display the NPS survey notice.
797 */
798 update_option( '_cartflows_wizard_store_checkout_set', 'yes' );
799
800 /**
801 * Redirect to the new flow edit screen
802 */
803 return array(
804 'success' => true,
805 'message' => __( 'Successfully created the Funnel!', 'cartflows' ),
806 'redirect_url' => admin_url( 'post.php?action=edit&post=' . $flow_id ),
807 'new_flow_id' => $flow_id,
808 );
809 }
810 }
811