PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.17
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.17
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / classes / class-cartflows-frontend.php

class-cartflows-frontend.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.17, at classes/class-cartflows-frontend.php

467 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Frontend.
4 *
5 * @package CartFlows
6 */
7
8 /**
9 * Class Cartflows_Frontend.
10 */
11 class Cartflows_Frontend {
12
13 /**
14 * Member Variable
15 *
16 * @var instance
17 */
18 private static $instance;
19
20 /**
21 * Initiator
22 */
23 public static function get_instance() {
24 if ( ! isset( self::$instance ) ) {
25 self::$instance = new self;
26 }
27 return self::$instance;
28 }
29
30 /**
31 * Constructor
32 */
33 public function __construct() {
34
35 /* Set / Destroy Flow Sessions. Set data */
36 add_action( 'wp', array( $this, 'init_actions' ), 1 );
37
38 add_action( 'init', array( $this, 'debug_data_setting_actions' ) );
39 /* Enqueue global required scripts */
40 add_action( 'wp', array( $this, 'wp_actions' ), 55 );
41
42 /* Modify the checkout order received url to go thank you page in our flow */
43 add_filter( 'woocommerce_get_checkout_order_received_url', array( $this, 'redirect_to_thankyou_page' ), 10, 2 );
44
45 }
46
47 /**
48 * Redirect to thank page if upsell not exists
49 *
50 * @param string $order_recieve_url url.
51 * @param object $order order object.
52 * @since 1.0.0
53 */
54 function redirect_to_thankyou_page( $order_recieve_url, $order ) {
55
56 /* Only for thank you page */
57 wcf()->logger->log( 'Start-' . __CLASS__ . '::' . __FUNCTION__ );
58 wcf()->logger->log( 'Only for thank you page' );
59
60 if ( wcf()->flow->is_thankyou_page_exists( $order ) ) {
61
62 if ( _is_wcf_doing_checkout_ajax() ) {
63
64 $checkout_id = wcf()->utils->get_checkout_id_from_post_data();
65
66 if ( ! $checkout_id ) {
67 $checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() );
68 }
69 } else {
70 $checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() );
71 }
72
73 wcf()->logger->log( 'Checkout ID : ' . $checkout_id );
74
75 if ( $checkout_id ) {
76
77 $thankyou_step_id = wcf()->flow->get_thankyou_page_id( $order );
78
79 if ( $thankyou_step_id ) {
80
81 $order_recieve_url = get_permalink( $thankyou_step_id );
82
83 $order_recieve_url = add_query_arg(
84 array(
85 'wcf-key' => $order->get_order_key(),
86 'wcf-order' => $order->get_id(),
87 ),
88 $order_recieve_url
89 );
90 }
91 }
92 }
93
94 wcf()->logger->log( 'End-' . __CLASS__ . '::' . __FUNCTION__ );
95
96 return $order_recieve_url;
97 }
98
99 /**
100 * Cancel and redirect to checkout
101 *
102 * @param string $return_url url.
103 * @since 1.0.0
104 */
105 function redirect_to_checkout_on_cancel( $return_url ) {
106
107 if ( _is_wcf_doing_checkout_ajax() ) {
108
109 $checkout_id = wcf()->utils->get_checkout_id_from_post_data();
110
111 if ( ! $checkout_id ) {
112 $checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() );
113 }
114 } else {
115 $checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() );
116 }
117
118 if ( $checkout_id ) {
119
120 $return_url = add_query_arg(
121 array(
122 'cancel_order' => 'true',
123 '_wpnonce' => wp_create_nonce( 'woocommerce-cancel_order' ),
124 ),
125 get_permalink( $checkout_id )
126 );
127 }
128
129 return $return_url;
130 }
131
132
133 /**
134 * Remove theme styles.
135 *
136 * @since 1.0.0
137 */
138 function remove_theme_styles() {
139
140 if ( Cartflows_Compatibility::get_instance()->is_compatibility_theme_enabled() ) {
141 return;
142 }
143
144 // get all styles data.
145 global $wp_styles;
146 global $wp_scripts;
147
148 $get_stylesheet = 'themes/' . get_stylesheet() . '/';
149 $get_template = 'themes/' . get_template() . '/';
150
151 $remove_styles = apply_filters( 'cartflows_remove_theme_styles', true );
152
153 if ( $remove_styles ) {
154
155 // loop over all of the registered scripts..
156 foreach ( $wp_styles->registered as $handle => $data ) {
157
158 if ( strpos( $data->src, $get_template ) !== false || strpos( $data->src, $get_stylesheet ) !== false ) {
159
160 // remove it.
161 wp_deregister_style( $handle );
162 wp_dequeue_style( $handle );
163 }
164 }
165 }
166
167 $remove_scripts = apply_filters( 'cartflows_remove_theme_scripts', true );
168
169 if ( $remove_scripts ) {
170
171 // loop over all of the registered scripts.
172 foreach ( $wp_scripts->registered as $handle => $data ) {
173
174 if ( strpos( $data->src, $get_template ) !== false || strpos( $data->src, $get_stylesheet ) !== false ) {
175
176 // remove it.
177 wp_deregister_script( $handle );
178 wp_dequeue_script( $handle );
179 }
180 }
181 }
182
183 }
184
185 /**
186 * Update main order data in transient.
187 *
188 * @param array $woo_styles new styles array.
189 * @since 1.0.0
190 * @return array.
191 */
192 function woo_default_css( $woo_styles ) {
193
194 $woo_styles = array(
195 'woocommerce-layout' => array(
196 'src' => plugins_url( 'assets/css/woocommerce-layout.css', WC_PLUGIN_FILE ),
197 'deps' => '',
198 'version' => WC_VERSION,
199 'media' => 'all',
200 'has_rtl' => true,
201 ),
202 'woocommerce-smallscreen' => array(
203 'src' => plugins_url( 'assets/css/woocommerce-smallscreen.css', WC_PLUGIN_FILE ),
204 'deps' => 'woocommerce-layout',
205 'version' => WC_VERSION,
206 'media' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', '768px' ) . ')',
207 'has_rtl' => true,
208 ),
209 'woocommerce-general' => array(
210 'src' => plugins_url( 'assets/css/woocommerce.css', WC_PLUGIN_FILE ),
211 'deps' => '',
212 'version' => WC_VERSION,
213 'media' => 'all',
214 'has_rtl' => true,
215 ),
216 );
217
218 return $woo_styles;
219 }
220
221 /**
222 * Init Actions.
223 *
224 * @since 1.0.0
225 */
226 function init_actions() {
227
228 $this->set_flow_session();
229 }
230
231 /**
232 * Set flow session.
233 *
234 * @since 1.0.0
235 */
236 function set_flow_session() {
237
238 if ( wcf()->utils->is_step_post_type() ) {
239
240 add_action( 'wp_head', array( $this, 'noindex_flow' ) );
241
242 wcf()->utils->do_not_cache();
243
244 /* Set key to support pixel */
245 if ( isset( $_GET['wcf-key'] ) ) {
246 $_GET['key'] = $_GET['wcf-key'];
247 $_REQUEST['key'] = $_GET['wcf-key'];
248 }
249
250 if ( isset( $_GET['wcf-order'] ) ) {
251 $_GET['order'] = $_GET['wcf-order'];
252 $_REQUEST['order'] = $_GET['wcf-order'];
253 $_GET['order-received'] = $_GET['wcf-order'];
254 $_REQUEST['order-received'] = $_GET['wcf-order'];
255 }
256 }
257 }
258
259 /**
260 * Add noindex, nofollow.
261 *
262 * @since 1.0.0
263 */
264 function noindex_flow() {
265
266 $common = Cartflows_Helper::get_common_settings();
267
268 if ( 'enable' === $common['disallow_indexing'] ) {
269 echo '<meta name="robots" content="noindex,nofollow">';
270 }
271 }
272
273 /**
274 * WP Actions.
275 *
276 * @since 1.0.0
277 */
278 function wp_actions() {
279
280 if ( wcf()->utils->is_step_post_type() ) {
281
282 /* CSS Compatibility for All theme */
283 add_filter( 'woocommerce_enqueue_styles', array( $this, 'woo_default_css' ), 9999 );
284
285 add_action( 'wp_enqueue_scripts', array( $this, 'remove_theme_styles' ), 9999 );
286 add_action( 'wp_enqueue_scripts', array( $this, 'global_flow_scripts' ), 20 );
287
288 /* Load woo templates from plugin */
289 add_filter( 'woocommerce_locate_template', array( $this, 'override_woo_template' ), 20, 3 );
290
291 /* Add version class to body in frontend. */
292 add_filter( 'body_class', array( $this, 'add_cartflows_lite_version_to_body' ) );
293
294 /* Custom Script Option */
295 add_action( 'wp_head', array( $this, 'custom_script_option' ) );
296
297 /* Remove the action applied by the Flatsome theme */
298 if ( Cartflows_Compatibility::get_instance()->is_flatsome_enabled() ) {
299 $this->remove_flatsome_action();
300 }
301 }
302 }
303
304 /**
305 * Debug Data Setting Actions.
306 *
307 * @since 1.1.14
308 */
309 function debug_data_setting_actions() {
310
311 add_filter( 'cartflows_load_min_assets', array( $this, 'allow_load_minify' ) );
312 }
313
314 /**
315 * Get/Set the allow minify option.
316 *
317 * @since 1.1.14
318 */
319 function allow_load_minify() {
320 $debug_data = Cartflows_Helper::get_debug_settings();
321 $allow_minified = $debug_data['allow_minified_files'];
322 $allow_minify = false;
323
324 if ( 'enable' === $allow_minified ) {
325 $allow_minify = true;
326 }
327
328 return $allow_minify;
329 }
330
331 /**
332 * Global flow scripts.
333 *
334 * @since 1.0.0
335 */
336 function global_flow_scripts() {
337
338 global $post;
339
340 $flow = get_post_meta( $post->ID, 'wcf-flow-id', true );
341 $current_step = $post->ID;
342 $next_step_link = '';
343 $compatibility = Cartflows_Compatibility::get_instance();
344
345 if ( _is_wcf_landing_type() ) {
346
347 $next_step_id = wcf()->utils->get_next_step_id( $flow, $current_step );
348 $next_step_link = get_permalink( $next_step_id );
349 }
350
351 $localize = array(
352 'ajax_url' => admin_url( 'admin-ajax.php' ),
353 'is_pb_preview' => $compatibility->is_page_builder_preview(),
354 'current_theme' => $compatibility->get_current_theme(),
355 'current_flow' => $flow,
356 'current_step' => $current_step,
357 'next_step' => $next_step_link,
358 );
359
360 wp_localize_script( 'jquery', 'cartflows', apply_filters( 'global_cartflows_js_localize', $localize ) );
361
362 wp_enqueue_style( 'wcf-frontend-global', wcf()->utils->get_css_url( 'frontend' ), array(), CARTFLOWS_VER );
363
364 wp_enqueue_script(
365 'wcf-frontend-global',
366 wcf()->utils->get_js_url( 'frontend' ),
367 array( 'jquery' ),
368 CARTFLOWS_VER,
369 true
370 );
371 }
372
373 /**
374 * Custom Script in head.
375 *
376 * @since 1.0.0
377 */
378 function custom_script_option() {
379
380 /* Add custom script to header in frontend. */
381 $script = $this->get_custom_script();
382 if ( '' !== $script ) {
383 if ( false === strpos( $script, '<script' ) ) {
384 $script = '<script>' . $script . '</script>';
385 }
386 echo '<!-- Custom CartFlows Script -->';
387 echo $script;
388 echo '<!-- End Custom CartFlows Script -->';
389 }
390 }
391
392 /**
393 * Override woo templates.
394 *
395 * @param string $template new Template full path.
396 * @param string $template_name Template name.
397 * @param string $template_path Template Path.
398 * @since 1.1.5
399 * @return string.
400 */
401 function override_woo_template( $template, $template_name, $template_path ) {
402
403 global $woocommerce;
404
405 $_template = $template;
406
407 $plugin_path = CARTFLOWS_DIR . 'woocommerce/template/';
408
409 if ( file_exists( $plugin_path . $template_name ) ) {
410 $template = $plugin_path . $template_name;
411 }
412
413 if ( ! $template ) {
414 $template = $_template;
415 }
416
417 return $template;
418 }
419
420 /**
421 * Remove the action applied by the Flatsome theme.
422 *
423 * @since 1.1.5
424 * @return void.
425 */
426 function remove_flatsome_action() {
427
428 // Remove action where flatsome dequeued the woocommerce's default styles.
429 remove_action( 'wp_enqueue_scripts', 'flatsome_woocommerce_scripts_styles', 98 );
430 }
431
432 /**
433 * Add version class to body in frontend.
434 *
435 * @since 1.1.5
436 * @param array $classes classes.
437 * @return array $classes classes.
438 */
439 function add_cartflows_lite_version_to_body( $classes ) {
440
441 $classes[] = 'cartflows-' . CARTFLOWS_VER;
442
443 return $classes;
444
445 }
446
447 /**
448 * Get custom script data.
449 *
450 * @since 1.0.0
451 */
452 function get_custom_script() {
453
454 global $post;
455
456 $script = get_post_meta( $post->ID, 'wcf-custom-script', true );
457
458 return $script;
459 }
460 }
461
462 /**
463 * Prepare if class 'Cartflows_Frontend' exist.
464 * Kicking this off by calling 'get_instance()' method
465 */
466 Cartflows_Frontend::get_instance();
467