PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
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 trunk, at includes/funnel/steps.php

207 lines 4.8 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 * @SuppressWarnings(PHPMD.NPathComplexity)
57 */
58 public function init() {
59 if ( empty( $this->funnel->current_step_data ) ) {
60 return;
61 }
62
63 $current_step_type = $this->funnel->current_step_data['type']['key'];
64
65 add_action( 'wp', function () {
66 if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
67 return;
68 }
69
70 if ( ! empty( $this->funnel->funnel_id ) && 'publish' !== get_post_status( $this->funnel->funnel_id ) ) {
71 echo esc_html__( 'There is nothing to view here.', 'sellkit' );
72 die();
73 }
74
75 if ( ! empty( $this->funnel->current_step_data['status'] ) && 'publish' === $this->funnel->current_step_data['status'] ) {
76 return;
77 }
78
79 if ( ! empty( $this->funnel->next_step_data['page_id'] ) ) {
80 wp_safe_redirect( add_query_arg( [ $_GET ], get_permalink( $this->funnel->next_step_data['page_id'] ) ) ); // phpcs:ignore
81 }
82
83 if ( empty( $this->funnel->next_step_data['page_id'] ) ) {
84 echo esc_html__( 'There is nothing to view here.', 'sellkit' );
85 die();
86 }
87 } );
88
89 if ( method_exists( self::$steps[ $current_step_type ], 'do_actions' ) ) {
90 add_action( 'wp', [ self::$steps[ $current_step_type ], 'do_actions' ] );
91 }
92 }
93
94 /**
95 * Create step instances.
96 *
97 * @since 1.1.0
98 * @param string $path Step path.
99 * @return void
100 */
101 public function load_steps( $path = 'includes/funnel/steps' ) {
102 $real_path = trailingslashit( Sellkit::$plugin_dir . $path );
103 $file_paths = glob( $real_path . '*.php' );
104
105 if ( ! empty( $this->funnel->funnel_id ) ) {
106 $this->analytics_updater = new Data_Updater();
107
108 $this->analytics_updater->set_funnel_id( $this->funnel->funnel_id );
109 $this->add_new_visit_log();
110 $this->maybe_add_unique_visit_log();
111 }
112
113 foreach ( $file_paths as $file_path ) {
114 if ( ! file_exists( $file_path ) ) {
115 continue;
116 }
117
118 require_once $file_path;
119
120 $file_name = str_replace( '.php', '', basename( $file_path ) );
121 $step_class = str_replace( '-', ' ', $file_name );
122 $step_class = str_replace( ' ', '_', ucwords( $step_class ) );
123 $step_class = "Sellkit\Funnel\Steps\\{$step_class}";
124
125 if ( ! class_exists( $step_class ) || 'base-step' === $file_name ) {
126 continue;
127 }
128
129 $step = new $step_class();
130
131 self::$steps[ $file_name ] = $step;
132 }
133
134 // load children steps.
135 if ( 'includes/funnel/steps' === $path ) {
136 $this->load_steps( 'includes/funnel/steps/children' );
137 }
138 }
139
140 /**
141 * Adds new visit log.
142 *
143 * @since 1.1.0
144 */
145 private function add_new_visit_log() {
146 add_action( 'init', function () {
147 $this->analytics_updater->add_new_visit();
148 } );
149 }
150
151 /**
152 * Adds new unique visit log.
153 *
154 * @since 1.1.0
155 */
156 private function maybe_add_unique_visit_log() {
157 if ( ! session_id() ) {
158 session_start();
159 }
160
161 $viewed_funnel_ids = isset( $_SESSION['sellkit_viewed_funnels'] ) ? array_map( 'absint', (array) $_SESSION['sellkit_viewed_funnels'] ) : [];
162
163 if ( in_array( absint( $this->funnel->funnel_id ), $viewed_funnel_ids, true ) ) {
164 return;
165 }
166
167 $_SESSION['sellkit_viewed_funnels'] = array_merge( $viewed_funnel_ids, [ absint( $this->funnel->funnel_id ) ] );
168
169 add_action( 'init', function () {
170 $this->analytics_updater->add_new_visit( true );
171 } );
172 }
173
174
175 /**
176 * Adds starting and finishing logs.
177 *
178 * @since 1.1.0
179 */
180 public function add_start_and_finish_logs() {
181 $funnel = sellkit_funnel();
182 $analytics_updater = new Data_Updater();
183
184 if ( empty( $funnel->funnel_id ) && ! empty( Funnel_Homepage::$first_step ) ) {
185 $funnel = new Sellkit_Funnel( Funnel_Homepage::$first_step );
186 }
187
188 $analytics_updater->set_funnel_id( $funnel->funnel_id );
189 $first_key = 0;
190
191 if ( isset( $funnel->funnel_id ) ) {
192 $nodes = get_post_meta( $funnel->funnel_id, 'nodes', true );
193 $first_key = array_key_first( $nodes );
194 }
195
196 if ( isset( $funnel->current_step_data['number'] ) && $first_key === $funnel->current_step_data['number'] ) {
197 $analytics_updater->add_new_start_log();
198 }
199
200 if ( empty( $funnel->next_step_data ) ) {
201 $analytics_updater->add_new_finish_log();
202 }
203 }
204 }
205
206 new Sellkit_Steps();
207