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

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