| 1 |
<?php |
| 2 |
|
| 3 |
use Sellkit\Funnel\Analytics\Data_Updater; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Sellkit_Steps. |
| 9 |
* |
| 10 |
* @since 1.1.0 |
| 11 |
*/ |
| 12 |
class Sellkit_Steps { |
| 13 |
|
| 14 |
/** |
| 15 |
* Funnel Instance |
| 16 |
* |
| 17 |
* @var Sellkit_Funnel Funnel Object. |
| 18 |
* @since 1.1.0 |
| 19 |
*/ |
| 20 |
public $funnel; |
| 21 |
|
| 22 |
/** |
| 23 |
* Step Instance array. |
| 24 |
* |
| 25 |
* @var array Steps instances. |
| 26 |
* @since 1.1.0 |
| 27 |
*/ |
| 28 |
public static $steps; |
| 29 |
|
| 30 |
/** |
| 31 |
* Updates analytics. |
| 32 |
* |
| 33 |
* @var object Analytics updater. |
| 34 |
* @since 1.1.0 |
| 35 |
*/ |
| 36 |
public $analytics_updater; |
| 37 |
|
| 38 |
/** |
| 39 |
* Sellkit_Steps constructor. |
| 40 |
* |
| 41 |
* @since 1.1.0 |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
$this->funnel = Sellkit_Funnel::get_instance(); |
| 45 |
|
| 46 |
$this->load_steps(); |
| 47 |
$this->init(); |
| 48 |
|
| 49 |
add_action( 'template_redirect', [ $this, 'add_start_and_finish_logs' ] ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Initialize steps. |
| 54 |
* |
| 55 |
* @since 1.1.0 |
| 56 |
*/ |
| 57 |
public function init() { |
| 58 |
if ( empty( $this->funnel->current_step_data ) ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$current_step_type = $this->funnel->current_step_data['type']['key']; |
| 63 |
|
| 64 |
add_action( 'wp', function () { |
| 65 |
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! empty( $this->funnel->current_step_data['status'] ) && 'publish' === $this->funnel->current_step_data['status'] ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! empty( $this->funnel->next_step_data['page_id'] ) ) { |
| 74 |
wp_safe_redirect( add_query_arg( [ $_GET ], get_permalink( $this->funnel->next_step_data['page_id'] ) ) ); // phpcs:ignore |
| 75 |
} |
| 76 |
|
| 77 |
if ( empty( $this->funnel->next_step_data['page_id'] ) ) { |
| 78 |
echo esc_html__( 'There is nothing to view here.', 'sellkit' ); |
| 79 |
die(); |
| 80 |
} |
| 81 |
} ); |
| 82 |
|
| 83 |
if ( method_exists( self::$steps[ $current_step_type ], 'do_actions' ) ) { |
| 84 |
add_action( 'wp', [ self::$steps[ $current_step_type ], 'do_actions' ] ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Create step instances. |
| 90 |
* |
| 91 |
* @since 1.1.0 |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function load_steps() { |
| 95 |
$path = trailingslashit( Sellkit::$plugin_dir . 'includes/funnel/steps' ); |
| 96 |
$file_paths = glob( $path . '*.php' ); |
| 97 |
|
| 98 |
if ( ! empty( $this->funnel->funnel_id ) ) { |
| 99 |
$this->analytics_updater = new Data_Updater(); |
| 100 |
|
| 101 |
$this->analytics_updater->set_funnel_id( $this->funnel->funnel_id ); |
| 102 |
$this->add_new_visit_log(); |
| 103 |
$this->maybe_add_unique_visit_log(); |
| 104 |
} |
| 105 |
|
| 106 |
foreach ( $file_paths as $file_path ) { |
| 107 |
if ( ! file_exists( $file_path ) ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
require_once $file_path; |
| 112 |
|
| 113 |
$file_name = str_replace( '.php', '', basename( $file_path ) ); |
| 114 |
$step_class = str_replace( '-', ' ', $file_name ); |
| 115 |
$step_class = str_replace( ' ', '_', ucwords( $step_class ) ); |
| 116 |
$step_class = "Sellkit\Funnel\Steps\\{$step_class}"; |
| 117 |
|
| 118 |
if ( ! class_exists( $step_class ) ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
$step = new $step_class(); |
| 123 |
|
| 124 |
self::$steps[ $file_name ] = $step; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Adds new visit log. |
| 130 |
* |
| 131 |
* @since 1.1.0 |
| 132 |
*/ |
| 133 |
private function add_new_visit_log() { |
| 134 |
add_action( 'init', function () { |
| 135 |
$this->analytics_updater->add_new_visit(); |
| 136 |
} ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Adds new unique visit log. |
| 141 |
* |
| 142 |
* @since 1.1.0 |
| 143 |
*/ |
| 144 |
private function maybe_add_unique_visit_log() { |
| 145 |
if ( ! session_id() ) { |
| 146 |
session_start(); |
| 147 |
} |
| 148 |
|
| 149 |
if ( array_key_exists( 'sellkit_viewed_funnels', $_SESSION ) && in_array( $this->funnel->funnel_id, $_SESSION['sellkit_viewed_funnels'] ) ) { // phpcs:ignore |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
if ( empty( $_SESSION['sellkit_viewed_funnels'] ) ) { |
| 154 |
$_SESSION['sellkit_viewed_funnels'] = []; |
| 155 |
} |
| 156 |
|
| 157 |
$_SESSION['sellkit_viewed_funnels'] = array_merge( $_SESSION['sellkit_viewed_funnels'], [ $this->funnel->funnel_id ] ); |
| 158 |
|
| 159 |
add_action( 'init', function () { |
| 160 |
$this->analytics_updater->add_new_visit( true ); |
| 161 |
} ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Adds starting and finishing logs. |
| 167 |
* |
| 168 |
* @since 1.1.0 |
| 169 |
*/ |
| 170 |
public function add_start_and_finish_logs() { |
| 171 |
$funnel = sellkit_funnel(); |
| 172 |
$analytics_updater = new Data_Updater(); |
| 173 |
|
| 174 |
$analytics_updater->set_funnel_id( $funnel->funnel_id ); |
| 175 |
|
| 176 |
if ( isset( $funnel->current_step_data['number'] ) && 0 === $funnel->current_step_data['number'] ) { |
| 177 |
$analytics_updater->add_new_start_log(); |
| 178 |
} |
| 179 |
|
| 180 |
if ( empty( $funnel->next_step_data ) ) { |
| 181 |
$analytics_updater->add_new_finish_log(); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
new Sellkit_Steps(); |
| 187 |
|