| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create funnel |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Modules\Admin\CreateFunnel; |
| 8 |
|
| 9 |
use WPFunnels\Admin\Module\Wpfnl_Admin_Module; |
| 10 |
use WPFunnels\Traits\SingletonTrait; |
| 11 |
use WPFunnels\Wpfnl; |
| 12 |
use WPFunnels\Wpfnl_functions; |
| 13 |
|
| 14 |
class Module extends Wpfnl_Admin_Module |
| 15 |
{ |
| 16 |
use SingletonTrait; |
| 17 |
|
| 18 |
private $builder; |
| 19 |
|
| 20 |
public function get_view() |
| 21 |
{ |
| 22 |
$this->builder = Wpfnl_functions::get_builder_type(); |
| 23 |
if (Wpfnl_functions::is_builder_active($this->builder)) { |
| 24 |
require_once WPFNL_DIR . '/admin/modules/createFunnel/views/view.php'; |
| 25 |
} else { |
| 26 |
require_once WPFNL_DIR . '/admin/modules/createFunnel/views/builder-not-activated.php'; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public function init_ajax() |
| 31 |
{ |
| 32 |
wp_ajax_helper()->handle('create-funnel') |
| 33 |
->with_callback([ $this, 'create_funnel' ]) |
| 34 |
->with_validation($this->get_validation_data()); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Create funnel by ajax request |
| 40 |
* |
| 41 |
* @return array |
| 42 |
* @since 1.0.0 |
| 43 |
*/ |
| 44 |
public function create_funnel( $payload ) |
| 45 |
{ |
| 46 |
// Check if this is a Store Checkout import |
| 47 |
$is_store_checkout = isset($payload['is_store_checkout']) && filter_var($payload['is_store_checkout'], FILTER_VALIDATE_BOOLEAN); |
| 48 |
|
| 49 |
$funnel = Wpfnl::$instance->funnel_store; |
| 50 |
$funnel_id = $funnel->create($payload['funnelName']); |
| 51 |
$funnel_type = ''; |
| 52 |
if ( $funnel_id ) { |
| 53 |
// Mark funnel as Store Checkout if applicable |
| 54 |
if ($is_store_checkout) { |
| 55 |
update_post_meta( $funnel_id, '_wpfnl_funnel_type', 'store_checkout' ); |
| 56 |
$funnel_type = 'store_checkout'; |
| 57 |
} else { |
| 58 |
$general_settings = get_option( '_wpfunnels_general_settings' ); |
| 59 |
if( isset($general_settings['funnel_type']) ){ |
| 60 |
if( 'woocommerce' == $general_settings['funnel_type'] ){ |
| 61 |
$general_settings['funnel_type'] = 'sales'; |
| 62 |
update_option( '_wpfunnels_general_settings', $general_settings ); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
if( 'sales' == $general_settings['funnel_type'] ){ |
| 67 |
// Check if LMS add-on is active AND at least one LMS plugin (LearnDash or CreatorLMS) is active |
| 68 |
if( Wpfnl_functions::is_lms_addon_active() && Wpfnl_functions::is_any_lms_plugin_active() && isset($payload['type']) && 'lms' === $payload['type'] ){ |
| 69 |
update_post_meta( $funnel_id, '_wpfnl_funnel_type', 'lms' ); |
| 70 |
$funnel_type = 'lms'; |
| 71 |
}elseif( Wpfnl_functions::is_wc_active() && isset($payload['type']) && 'wc' === $payload['type'] ){ |
| 72 |
update_post_meta( $funnel_id, '_wpfnl_funnel_type', 'wc' ); |
| 73 |
$funnel_type = 'wc'; |
| 74 |
}elseif( isset($payload['type']) && 'lead' === $payload['type'] ){ |
| 75 |
update_post_meta( $funnel_id, '_wpfnl_funnel_type', 'lead' ); |
| 76 |
$funnel_type = 'lead'; |
| 77 |
} |
| 78 |
|
| 79 |
}else{ |
| 80 |
|
| 81 |
if( isset($payload['type']) && 'lead' === $payload['type'] ){ |
| 82 |
update_post_meta( $funnel_id, '_wpfnl_funnel_type', 'lead' ); |
| 83 |
$funnel_type = 'lead'; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if ($is_store_checkout) { |
| 91 |
// For Store Checkout funnels, always create a fixed checkout → thank you layout |
| 92 |
$steps = $this->get_store_checkout_default_steps(); |
| 93 |
$templateLayout = new \WPFunnels\Admin\Module\Layout\TemplateLayout($funnel, $funnel_id, $funnel_type, $steps); |
| 94 |
$templateLayout->create_step(); |
| 95 |
$templateLayout->save_funnel_data(); |
| 96 |
|
| 97 |
// Persist the checkout step ID so the public-facing override can use it |
| 98 |
require_once WPFNL_DIR . 'includes/core/woocommerce/class-wpfnl-store-checkout-override.php'; |
| 99 |
\WPFunnels\WooCommerce\Wpfnl_Store_Checkout_Override::save_checkout_step_id( $funnel_id ); |
| 100 |
} elseif( !empty($payload['selectedFunnelLayout']['steps']) ){ |
| 101 |
$steps = $payload['selectedFunnelLayout']['steps']; |
| 102 |
$templateLayout = new \WPFunnels\Admin\Module\Layout\TemplateLayout($funnel, $funnel_id, $funnel_type, $steps); |
| 103 |
$templateLayout->create_step(); |
| 104 |
$templateLayout->save_funnel_data(); |
| 105 |
} |
| 106 |
|
| 107 |
$link = add_query_arg( |
| 108 |
[ |
| 109 |
'page' => 'edit_funnel', |
| 110 |
'id' => $funnel_id, |
| 111 |
], |
| 112 |
admin_url('admin.php') |
| 113 |
); |
| 114 |
|
| 115 |
do_action('wpfunnels_after_funnel_created', $funnel_id, $funnel_type); |
| 116 |
|
| 117 |
// Flush rewrite rules so newly created funnel/step permalinks resolve immediately. |
| 118 |
flush_rewrite_rules(); |
| 119 |
|
| 120 |
return [ |
| 121 |
'success' => true, |
| 122 |
'funnelID' => $funnel_id, |
| 123 |
'redirectUrl' => $link, |
| 124 |
]; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* Get the default steps for a Store Checkout funnel (checkout + thank you only). |
| 130 |
* |
| 131 |
* @return array |
| 132 |
* @since 3.5.0 |
| 133 |
*/ |
| 134 |
private function get_store_checkout_default_steps() |
| 135 |
{ |
| 136 |
return [ |
| 137 |
[ |
| 138 |
'name' => __( 'Checkout', 'wpfnl' ), |
| 139 |
'value' => 'checkout', |
| 140 |
'pos_x' => 156, |
| 141 |
'pos_y' => 258, |
| 142 |
], |
| 143 |
[ |
| 144 |
'name' => __( 'Thank You', 'wpfnl' ), |
| 145 |
'value' => 'thankyou', |
| 146 |
'pos_x' => 399, |
| 147 |
'pos_y' => 258, |
| 148 |
], |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* Get Store Checkout funnel if exists |
| 155 |
* |
| 156 |
* @return WP_Post|false |
| 157 |
* @since 3.5.0 |
| 158 |
*/ |
| 159 |
private function get_store_checkout_funnel() |
| 160 |
{ |
| 161 |
$args = array( |
| 162 |
'post_type' => WPFNL_FUNNELS_POST_TYPE, |
| 163 |
'posts_per_page' => 1, |
| 164 |
'post_status' => array( 'publish', 'draft' ), |
| 165 |
'meta_query' => array( |
| 166 |
array( |
| 167 |
'key' => '_wpfnl_funnel_type', |
| 168 |
'value' => 'store_checkout', |
| 169 |
), |
| 170 |
), |
| 171 |
); |
| 172 |
|
| 173 |
$funnels = get_posts( $args ); |
| 174 |
return !empty( $funnels ) ? $funnels[0] : false; |
| 175 |
} |
| 176 |
|
| 177 |
public function get_name() |
| 178 |
{ |
| 179 |
return 'create-funnel'; |
| 180 |
} |
| 181 |
} |
| 182 |
|