PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.5.0
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.5.0
2.7.0 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 All 43 releases
sellkit / includes / funnel / class.php

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

362 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Elementor\Utils as ElementorUtils;
4
5 defined( 'ABSPATH' ) || die();
6
7 /**
8 * Class Sellkit_Funnel
9 *
10 * @since 1.1.0
11 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
12 * @SuppressWarnings(PHPMD.NPathComplexity)
13 */
14 class Sellkit_Funnel {
15
16 /**
17 * Funnel Id.
18 *
19 * @var integer Funnel Id.
20 * @since 1.1.0
21 */
22 public $funnel_id;
23
24 /**
25 * Next step data.
26 *
27 * @var array $next_step_data.
28 * @since 1.1.0
29 */
30 public $next_step_data;
31
32 /**
33 * Next no step data.
34 *
35 * @var array $next_no_step_data.
36 * @since 1.5.0
37 */
38 public $next_no_step_data;
39
40 /**
41 * Ending node step data.
42 *
43 * @var array $end_node_step_data.
44 * @since 1.5.0
45 */
46 public $end_node_step_data;
47
48 /**
49 * The current step data.
50 *
51 * @var array $current_step_data.
52 * @since 1.1.0
53 */
54 public $current_step_data;
55
56 /**
57 * The class instance.
58 *
59 * @var Object Class instance.
60 * @since 1.1.0
61 */
62 public static $instance = null;
63
64 /**
65 * Sellkit_Funnel constructor.
66 *
67 * @param string $page_id Page Id.
68 * @since 1.1.0
69 */
70 public function __construct( $page_id = null ) {
71 $this->order_customization();
72
73 if ( null === $page_id ) {
74 $page_id = self::get_page_id();
75 }
76
77 if ( empty( $page_id ) ) {
78 return;
79 }
80
81 $this->set_funnel_data( $page_id );
82
83 if ( empty( $this->funnel_id ) ) {
84 return;
85 }
86
87 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_funnel_frontend_script' ] );
88 }
89
90 /**
91 * Class Instance.
92 *
93 * @since 1.1.0
94 * @return Sellkit_Funnel|null
95 */
96 public static function get_instance() {
97 if ( null === self::$instance ) {
98 self::$instance = new self();
99 }
100
101 return self::$instance;
102 }
103
104 /**
105 * Gets funnel if the page is step otherwise do nothing.
106 *
107 * @since 1.1.0
108 * @return void
109 * @param string $page_id Page id.
110 */
111 public function set_funnel_data( $page_id ) {
112 $this->current_step_data = get_post_meta( $page_id, 'step_data', true );
113
114 if ( empty( $this->current_step_data['funnel_id'] ) ) {
115 return;
116 }
117
118 $is_nofollow = sellkit_get_option( 'disallow_funnels_from_search_engine' );
119
120 if ( true === (bool) $is_nofollow ) {
121 add_action( 'wp_head', function () {
122 echo '<meta name="robots" content="noindex" />';
123 } );
124 }
125
126 $funnel_data = (array) get_post_meta( $this->current_step_data['funnel_id'], 'nodes', true );
127 $is_flowchart = true;
128
129 if ( empty( $funnel_data ) ) {
130 $funnel_data = get_post_meta( $this->current_step_data['funnel_id'], 'sellkit_steps', true );
131 $is_flowchart = false;
132 }
133
134 $this->funnel_id = $this->current_step_data['funnel_id'];
135
136 if ( false === $is_flowchart ) {
137 $this->next_step_data = ! empty( $funnel_data[ $this->current_step_data['number'] + 1 ] ) ? $funnel_data[ $this->current_step_data['number'] + 1 ] : false;
138 }
139
140 if (
141 true === $is_flowchart &&
142 ! empty( $this->current_step_data['targets'] ) &&
143 ! empty( $funnel_data[ $this->current_step_data['targets'][0]['nodeId'] ] )
144 ) {
145 $this->next_step_data = 'none' !== $funnel_data[ $this->current_step_data['targets'][0]['nodeId'] ] ? $funnel_data[ $this->current_step_data['targets'][0]['nodeId'] ] : false;
146 }
147
148 if ( true === $is_flowchart && ! empty( $this->current_step_data['targets'][1]['nodeId'] ) ) {
149 $this->next_no_step_data = ! empty( $funnel_data[ $this->current_step_data['targets'][1]['nodeId'] ] ) ? $funnel_data[ $this->current_step_data['targets'][1]['nodeId'] ] : false;
150 }
151
152 $end_node_id = $this->get_end_node_id( $funnel_data );
153
154 if ( ! empty( $end_node_id ) ) {
155 $this->end_node_step_data = $funnel_data[ $end_node_id ];
156 }
157 }
158
159 /**
160 * Enqueue funnel frontend scripts.
161 *
162 * @since 1.1.0
163 * @return void
164 */
165 public function enqueue_funnel_frontend_script() {
166 $suffix = defined( 'WP_DEBUG' ) && WP_DEBUG ? '' : '.min';
167
168 wp_enqueue_script(
169 'funnel-frontend',
170 sellkit()->plugin_url() . 'assets/dist/js/funnel-frontend' . $suffix . '.js',
171 [ 'jquery', 'wp-util' ],
172 sellkit()->version(),
173 true
174 );
175
176 wp_localize_script( 'funnel-frontend', 'funnel', $this->get_localize_data() );
177
178 if ( empty( sellkit_get_option( 'google_analytics' ) ) && empty( sellkit_get_option( 'facebook_pixel' ) ) ) {
179 return;
180 }
181
182 wp_enqueue_script(
183 'funnel-settings-variables',
184 sellkit()->plugin_url() . 'assets/dist/js/funnel-settings-variables' . $suffix . '.js',
185 [ 'jquery', 'wp-util' ],
186 sellkit()->version(),
187 true
188 );
189 }
190
191 /**
192 * Get localize data.
193 *
194 * @return array
195 */
196 public function get_localize_data() {
197 if ( ! empty( $this->next_step_data['page_id'] ) ) {
198 $next_step_link = add_query_arg( [
199 'post_type' => Sellkit_Admin_Steps::SELLKIT_STEP_POST_TYPE,
200 'p' => $this->next_step_data['page_id'],
201 ], site_url() );
202 }
203
204 return [
205 'currentStep' => $this->current_step_data,
206 'nextStep' => $this->next_step_data,
207 'nextStepLink' => ! empty( $next_step_link ) ? $next_step_link : '',
208 'siteUrl' => site_url(),
209 ];
210 }
211
212 /**
213 * Gets page id.
214 *
215 * @since 1.1.0
216 * @return false|int
217 * @SuppressWarnings(PHPMD.NPathComplexity)
218 */
219 public static function get_page_id() {
220 $page_id = sellkit_htmlspecialchars( INPUT_POST, 'sellkit_current_page_id' );
221 $preview = sellkit_htmlspecialchars( INPUT_GET, 'preview' );
222
223 if ( ! empty( $preview ) ) {
224 return false;
225 }
226
227 if ( is_numeric( $page_id ) ) {
228 return $page_id;
229 }
230
231 $current_url = esc_url_raw( remove_query_arg( 'order-key' ) );
232
233 if ( empty( strpos( $current_url, 'sellkit_step' ) ) ) {
234 return false;
235 }
236
237 if ( empty( $current_url ) ) {
238 return false;
239 }
240
241 $page_id = sellkit_htmlspecialchars( INPUT_GET, 'p' );
242 $page_slug = sellkit_htmlspecialchars( INPUT_GET, 'sellkit_step' );
243 $page = get_page_by_path( basename( untrailingslashit( $current_url ) ), null, 'sellkit_step' );
244
245 if ( empty( $page ) && ! empty( $page_slug ) ) {
246 $page = get_page_by_path( sanitize_text_field( $page_slug ), null, 'sellkit_step' );
247 }
248
249 if ( empty( $page ) && empty( $page_id ) ) {
250 return false;
251 }
252
253 if ( ! empty( $page_id ) && 'sellkit_step' === get_post_type( $page_id ) ) {
254 return $page_id;
255 }
256
257 if ( empty( $page->ID ) ) {
258 return false;
259 }
260
261 return $page->ID;
262 }
263
264 /**
265 * Customization order pages in admin area.
266 *
267 * @since 1.1.0
268 */
269 public function order_customization() {
270 if ( ! is_admin() ) {
271 return;
272 }
273
274 // Orders filters.
275 add_filter( 'woocommerce_get_formatted_order_total', [ $this, 'add_order_type' ], 10, 2 );
276 add_action( 'woocommerce_admin_order_data_after_order_details', [ $this, 'show_linked_orders' ], 10, 1 );
277 }
278
279 /**
280 * Adding order type to order list table.
281 *
282 * @since 1.1.0
283 * @return string
284 * @param string $formatted_total formatted total price.
285 * @param object $order Order object.
286 */
287 public function add_order_type( $formatted_total, $order ) {
288 $screen = get_current_screen();
289 $is_upsell = $order->get_meta( 'sellkit_main_order_id' );
290
291 if ( ! $screen || 'edit' !== $screen->base || 'shop_order' !== $screen->post_type ) {
292 return $formatted_total;
293 }
294
295 if ( is_numeric( $is_upsell ) ) {
296 $formatted_total .= '<div><span>' . esc_html__( 'Sellkit Upsell', 'sellkit' ) . '</span></div>';
297 }
298
299 return $formatted_total;
300 }
301
302 /**
303 * Show the related between orders.
304 *
305 * @since 1.1.0
306 * @param object $order Order object.
307 */
308 public function show_linked_orders( $order ) {
309 $upsell_order_id = $order->get_meta( 'sellkit_upsell_order_id' );
310 $main_order_id = $order->get_meta( 'sellkit_main_order_id' );
311
312 if ( is_numeric( $main_order_id ) ) {
313 $parent_order = wc_get_order( $main_order_id );
314 $order_number = $parent_order->get_order_number();
315 $parent_order_html = sprintf( '<p class="form-field form-field-wide" style= "margin-top: 20px;"><strong>%s: </strong>', esc_html__( 'SellKit Parent Order', 'sellkit' ) );
316 $parent_order_html .= sprintf( '<span style= "display: block;"><a href="%1s"><strong>#%2s</strong></a></span>', get_edit_post_link( $main_order_id ), esc_attr( $order_number ) );
317 $parent_order_html .= '</p>';
318 echo $parent_order_html;
319 }
320
321 if ( is_numeric( $upsell_order_id ) ) {
322 $upsell_order = wc_get_order( $upsell_order_id );
323 $order_number = $upsell_order->get_order_number();
324 $upsell_order_html = sprintf( '<p class="form-field form-field-wide" style= "margin-top: 20px;"><strong>%s: </strong>', esc_html__( 'SellKit Upsell Order', 'sellkit' ) );
325 $upsell_order_html .= sprintf( '<span style= "display: block;"><a href="%1s"><strong>#%2s</strong></a></span>', get_edit_post_link( $upsell_order_id ), esc_attr( $order_number ) );
326 $upsell_order_html .= '</p>';
327 echo $upsell_order_html;
328 }
329 }
330
331 /**
332 * Gets end node.
333 *
334 * @since 1.5.0
335 * @param array $funnels Funnels array.
336 */
337 public function get_end_node_id( $funnels ) {
338 $funnels = (array) $funnels;
339
340 foreach ( $funnels as $key => $funnel ) {
341 if ( ! empty( $funnel['origin_node'] ) && 'last-node' === $funnel['origin_node'] ) {
342 return $key;
343 }
344 }
345
346 return false;
347 }
348 }
349
350 if ( ! function_exists( 'sellkit_funnel' ) ) {
351 /**
352 * Sellkit funnel wrapper.
353 *
354 * @since 1.1.0
355 * @return Object|Sellkit_Funnel|null
356 */
357 function sellkit_funnel() {
358 return Sellkit_Funnel::get_instance();
359 }
360 }
361
362