| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstract class of source base |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\TemplateLibrary; |
| 8 |
|
| 9 |
use WPFunnels\Wpfnl; |
| 10 |
use WPFunnels\Wpfnl_functions; |
| 11 |
|
| 12 |
abstract class Wpfnl_Source_Base |
| 13 |
{ |
| 14 |
abstract public function get_source(); |
| 15 |
|
| 16 |
abstract public function get_funnels($arg = []); |
| 17 |
|
| 18 |
abstract public function get_funnel($template_id); |
| 19 |
|
| 20 |
abstract public function get_data(array $args); |
| 21 |
|
| 22 |
abstract public function import_funnel($args = []); |
| 23 |
|
| 24 |
abstract public function import_step($args = []); |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* After funnel import |
| 29 |
* redirect to new funnel edit url |
| 30 |
* |
| 31 |
* @param $payload |
| 32 |
* |
| 33 |
* @return array |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
public function after_funnel_creation($payload) |
| 37 |
{ |
| 38 |
$funnel = Wpfnl::$instance->funnel_store; |
| 39 |
$funnel->set_id($payload['funnelID']); |
| 40 |
$funnel->set_steps_order(); |
| 41 |
|
| 42 |
// rearrange steps order |
| 43 |
$funnel_data = get_post_meta( $payload['funnelID'], 'funnel_data', true ); |
| 44 |
$steps_order = $this->get_steps_order($funnel_data); |
| 45 |
update_post_meta( $payload['funnelID'], '_steps_order', $steps_order ); |
| 46 |
update_post_meta( $payload['funnelID'], '_steps', $steps_order ); |
| 47 |
Wpfnl_functions::generate_first_step( $payload['funnelID'], $steps_order ); |
| 48 |
|
| 49 |
|
| 50 |
$redirect_link = add_query_arg( |
| 51 |
[ |
| 52 |
'page' => WPFNL_EDIT_FUNNEL_SLUG, |
| 53 |
'id' => $payload['funnelID'], |
| 54 |
'step_id' => $funnel->get_first_step_id(), |
| 55 |
], |
| 56 |
admin_url('admin.php') |
| 57 |
); |
| 58 |
return [ |
| 59 |
'success' => true, |
| 60 |
'redirectLink' => $redirect_link, |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* After funnel import |
| 66 |
* redirect to new funnel edit url |
| 67 |
* |
| 68 |
* @param $payload |
| 69 |
* |
| 70 |
* @return array |
| 71 |
* @since 1.0.0 |
| 72 |
*/ |
| 73 |
public function after_step_creation($payload) |
| 74 |
{ |
| 75 |
$redirect_link = add_query_arg( |
| 76 |
[ |
| 77 |
'page' => WPFNL_EDIT_FUNNEL_SLUG, |
| 78 |
'id' => $payload['funnelID'], |
| 79 |
'step_id' => $payload['stepID'], |
| 80 |
], |
| 81 |
admin_url('admin.php') |
| 82 |
); |
| 83 |
return [ |
| 84 |
'success' => true, |
| 85 |
'redirectLink' => $redirect_link, |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Get steps order |
| 92 |
* |
| 93 |
* @param $funnel_flow_data |
| 94 |
* |
| 95 |
* @return array |
| 96 |
* |
| 97 |
* @since 2.2.6 |
| 98 |
*/ |
| 99 |
private function get_steps_order( $funnel_flow_data ) { |
| 100 |
$drawflow = $funnel_flow_data['drawflow']; |
| 101 |
$nodes = array(); |
| 102 |
$step_order = array(); |
| 103 |
$first_node_id = ''; |
| 104 |
$start_node = array(); |
| 105 |
|
| 106 |
|
| 107 |
if( isset( $drawflow['Home']['data'] ) ) { |
| 108 |
$drawflow_data = $drawflow['Home']['data']; |
| 109 |
|
| 110 |
/** |
| 111 |
* If has only one step, that only step will be the first step, no conditions should be checked. |
| 112 |
* just return the step order |
| 113 |
*/ |
| 114 |
if( 1 === count( $drawflow_data ) ) { |
| 115 |
$node_id = array_keys($drawflow_data)[0]; |
| 116 |
$data = $drawflow_data[$node_id]; |
| 117 |
$step_data = $data['data']; |
| 118 |
$step_id = $step_data['step_id']; |
| 119 |
$step_type = $step_data['step_type']; |
| 120 |
$step_order[] = array( |
| 121 |
'id' => $step_id, |
| 122 |
'step_type' => $step_type, |
| 123 |
'name' => sanitize_text_field( get_the_title( $step_id ) ), |
| 124 |
); |
| 125 |
return $step_order; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* First we will find the first node (the node which has only output connection but no input connection will be considered as first node) and the list of nodes array which has the |
| 131 |
* Step information includes output connection and input connection and it will be stored on $nodes |
| 132 |
*/ |
| 133 |
foreach ( $drawflow_data as $key => $data ) { |
| 134 |
$step_data = $data['data']; |
| 135 |
$step_type = $step_data['step_type']; |
| 136 |
$step_id = $step_type !== 'conditional' ? $step_data['step_id'] : 0; |
| 137 |
if( |
| 138 |
(isset( $data['outputs']['output_1']['connections'] ) && count( $data['outputs']['output_1']['connections'] ) ) || |
| 139 |
(isset( $data['inputs']['input_1']['connections'] ) && count($data['inputs']['input_1']['connections']) ) |
| 140 |
) { |
| 141 |
|
| 142 |
if('conditional' === $step_type) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* A starting node is a node which has only output connection but not any input connection. |
| 148 |
* If the step is landing, then there should not be any input connection for this step. so we will only consider the output connection for landing only. |
| 149 |
* For other step types (checkout, offer, thankyou), we will check if the step has any output connection and no input connection. |
| 150 |
*/ |
| 151 |
if( 'landing' === $step_type ) { |
| 152 |
if ( |
| 153 |
isset($data['outputs']['output_1']['connections']) && count($data['outputs']['output_1']['connections']) && |
| 154 |
(isset( $data['inputs'] ) && count($data['inputs']) == 0 ) |
| 155 |
) { |
| 156 |
$start_node = array( |
| 157 |
'id' => $step_id, |
| 158 |
'step_type' => $step_type, |
| 159 |
'name' => sanitize_text_field(get_the_title($step_id)), |
| 160 |
); |
| 161 |
} |
| 162 |
} else { |
| 163 |
if ( |
| 164 |
isset($data['outputs']['output_1']['connections']) && count($data['outputs']['output_1']['connections']) && |
| 165 |
(isset($data['inputs']['input_1']['connections']) && count($data['inputs']['input_1']['connections']) === 0) |
| 166 |
) { |
| 167 |
$start_node = array( |
| 168 |
'id' => $step_id, |
| 169 |
'step_type' => $step_type, |
| 170 |
'name' => sanitize_text_field(get_the_title($step_id)), |
| 171 |
); |
| 172 |
} else { |
| 173 |
$step_order[] = array( |
| 174 |
'id' => $step_id, |
| 175 |
'step_type' => $step_type, |
| 176 |
'name' => sanitize_text_field(get_the_title($step_id)), |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
$step_order = $this->array_insert($step_order, $start_node, 0); |
| 183 |
} |
| 184 |
return $step_order; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* Array insert element on position |
| 190 |
* |
| 191 |
* @param $original |
| 192 |
* @param $inserted |
| 193 |
* @param int $position |
| 194 |
* |
| 195 |
* @return mixed |
| 196 |
*/ |
| 197 |
private function array_insert(&$original, $inserted, $position) { |
| 198 |
array_splice($original, $position, 0, array($inserted)); |
| 199 |
return $original; |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|