PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.7.2
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.7.2
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / funnel / steps.php

steps.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.7.2, at includes/funnel/steps.php

193 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @param string $path Step path.
93 * @return void
94 */
95 public function load_steps( $path = 'includes/funnel/steps' ) {
96 $real_path = trailingslashit( Sellkit::$plugin_dir . $path );
97 $file_paths = glob( $real_path . '*.php' );
98
99 if ( ! empty( $this->funnel->funnel_id ) ) {
100 $this->analytics_updater = new Data_Updater();
101
102 $this->analytics_updater->set_funnel_id( $this->funnel->funnel_id );
103 $this->add_new_visit_log();
104 $this->maybe_add_unique_visit_log();
105 }
106
107 foreach ( $file_paths as $file_path ) {
108 if ( ! file_exists( $file_path ) ) {
109 continue;
110 }
111
112 require_once $file_path;
113
114 $file_name = str_replace( '.php', '', basename( $file_path ) );
115 $step_class = str_replace( '-', ' ', $file_name );
116 $step_class = str_replace( ' ', '_', ucwords( $step_class ) );
117 $step_class = "Sellkit\Funnel\Steps\\{$step_class}";
118
119 if ( ! class_exists( $step_class ) || 'base-step' === $file_name ) {
120 continue;
121 }
122
123 $step = new $step_class();
124
125 self::$steps[ $file_name ] = $step;
126 }
127
128 // load children steps.
129 if ( 'includes/funnel/steps' === $path ) {
130 $this->load_steps( 'includes/funnel/steps/children' );
131 }
132 }
133
134 /**
135 * Adds new visit log.
136 *
137 * @since 1.1.0
138 */
139 private function add_new_visit_log() {
140 add_action( 'init', function () {
141 $this->analytics_updater->add_new_visit();
142 } );
143 }
144
145 /**
146 * Adds new unique visit log.
147 *
148 * @since 1.1.0
149 */
150 private function maybe_add_unique_visit_log() {
151 if ( ! session_id() ) {
152 session_start();
153 }
154
155 if ( array_key_exists( 'sellkit_viewed_funnels', $_SESSION ) && in_array( $this->funnel->funnel_id, $_SESSION['sellkit_viewed_funnels'] ) ) { // phpcs:ignore
156 return;
157 }
158
159 if ( empty( $_SESSION['sellkit_viewed_funnels'] ) ) {
160 $_SESSION['sellkit_viewed_funnels'] = [];
161 }
162
163 $_SESSION['sellkit_viewed_funnels'] = array_merge( $_SESSION['sellkit_viewed_funnels'], [ $this->funnel->funnel_id ] );
164
165 add_action( 'init', function () {
166 $this->analytics_updater->add_new_visit( true );
167 } );
168 }
169
170
171 /**
172 * Adds starting and finishing logs.
173 *
174 * @since 1.1.0
175 */
176 public function add_start_and_finish_logs() {
177 $funnel = sellkit_funnel();
178 $analytics_updater = new Data_Updater();
179
180 $analytics_updater->set_funnel_id( $funnel->funnel_id );
181
182 if ( isset( $funnel->current_step_data['number'] ) && 0 === $funnel->current_step_data['number'] ) {
183 $analytics_updater->add_new_start_log();
184 }
185
186 if ( empty( $funnel->next_step_data ) ) {
187 $analytics_updater->add_new_finish_log();
188 }
189 }
190 }
191
192 new Sellkit_Steps();
193