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 / class.php

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

384 lines 9.4 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 $structure = get_option( 'permalink_structure' );
223
224 if ( ! empty( $preview ) ) {
225 return false;
226 }
227
228 if ( is_numeric( $page_id ) ) {
229 return $page_id;
230 }
231
232 $current_url = esc_url_raw( remove_query_arg( 'order-key' ) );
233
234 if ( ! empty( $structure ) ) {
235 $current_url = self::get_base_url( $current_url );
236 }
237
238 if ( empty( strpos( $current_url, 'sellkit_step' ) ) ) {
239 return false;
240 }
241
242 if ( empty( $current_url ) ) {
243 return false;
244 }
245
246 $page_id = sellkit_htmlspecialchars( INPUT_GET, 'p' );
247 $page_slug = sellkit_htmlspecialchars( INPUT_GET, 'sellkit_step' );
248 $page = get_page_by_path( basename( untrailingslashit( $current_url ) ), null, 'sellkit_step' );
249
250 if ( empty( $page ) && ! empty( $page_slug ) ) {
251 $page = get_page_by_path( sanitize_text_field( $page_slug ), null, 'sellkit_step' );
252 }
253
254 if ( empty( $page ) && empty( $page_id ) ) {
255 return false;
256 }
257
258 if ( ! empty( $page_id ) && 'sellkit_step' === get_post_type( $page_id ) ) {
259 return $page_id;
260 }
261
262 if ( empty( $page->ID ) ) {
263 return false;
264 }
265
266 return $page->ID;
267 }
268
269 /**
270 * Gets main url.
271 *
272 * @since 1.5.4
273 * @param string $url Url.
274 * @return string
275 */
276 private static function get_base_url( $url ) {
277 $parsed = wp_parse_url( $url );
278
279 if ( empty( $parsed['query'] ) ) {
280 return $url;
281 }
282
283 return explode( '?', $url )[0];
284 }
285
286 /**
287 * Customization order pages in admin area.
288 *
289 * @since 1.1.0
290 */
291 public function order_customization() {
292 if ( ! is_admin() ) {
293 return;
294 }
295
296 // Orders filters.
297 add_filter( 'woocommerce_get_formatted_order_total', [ $this, 'add_order_type' ], 10, 2 );
298 add_action( 'woocommerce_admin_order_data_after_order_details', [ $this, 'show_linked_orders' ], 10, 1 );
299 }
300
301 /**
302 * Adding order type to order list table.
303 *
304 * @since 1.1.0
305 * @return string
306 * @param string $formatted_total formatted total price.
307 * @param object $order Order object.
308 */
309 public function add_order_type( $formatted_total, $order ) {
310 $screen = get_current_screen();
311 $is_upsell = $order->get_meta( 'sellkit_main_order_id' );
312
313 if ( ! $screen || 'edit' !== $screen->base || 'shop_order' !== $screen->post_type ) {
314 return $formatted_total;
315 }
316
317 if ( is_numeric( $is_upsell ) ) {
318 $formatted_total .= '<div><span>' . esc_html__( 'Sellkit Upsell', 'sellkit' ) . '</span></div>';
319 }
320
321 return $formatted_total;
322 }
323
324 /**
325 * Show the related between orders.
326 *
327 * @since 1.1.0
328 * @param object $order Order object.
329 */
330 public function show_linked_orders( $order ) {
331 $upsell_order_id = $order->get_meta( 'sellkit_upsell_order_id' );
332 $main_order_id = $order->get_meta( 'sellkit_main_order_id' );
333
334 if ( is_numeric( $main_order_id ) ) {
335 $parent_order = wc_get_order( $main_order_id );
336 $order_number = $parent_order->get_order_number();
337 $parent_order_html = sprintf( '<p class="form-field form-field-wide" style= "margin-top: 20px;"><strong>%s: </strong>', esc_html__( 'SellKit Parent Order', 'sellkit' ) );
338 $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 ) );
339 $parent_order_html .= '</p>';
340 echo $parent_order_html;
341 }
342
343 if ( is_numeric( $upsell_order_id ) ) {
344 $upsell_order = wc_get_order( $upsell_order_id );
345 $order_number = $upsell_order->get_order_number();
346 $upsell_order_html = sprintf( '<p class="form-field form-field-wide" style= "margin-top: 20px;"><strong>%s: </strong>', esc_html__( 'SellKit Upsell Order', 'sellkit' ) );
347 $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 ) );
348 $upsell_order_html .= '</p>';
349 echo $upsell_order_html;
350 }
351 }
352
353 /**
354 * Gets end node.
355 *
356 * @since 1.5.0
357 * @param array $funnels Funnels array.
358 */
359 public function get_end_node_id( $funnels ) {
360 $funnels = (array) $funnels;
361
362 foreach ( $funnels as $key => $funnel ) {
363 if ( ! empty( $funnel['origin_node'] ) && 'last-node' === $funnel['origin_node'] ) {
364 return $key;
365 }
366 }
367
368 return false;
369 }
370 }
371
372 if ( ! function_exists( 'sellkit_funnel' ) ) {
373 /**
374 * Sellkit funnel wrapper.
375 *
376 * @since 1.1.0
377 * @return Object|Sellkit_Funnel|null
378 */
379 function sellkit_funnel() {
380 return Sellkit_Funnel::get_instance();
381 }
382 }
383
384