PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.3.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.3.0
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 / modules / optin / classes / class-cartflows-optin-markup.php

class-cartflows-optin-markup.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.3.0, at modules/optin/classes/class-cartflows-optin-markup.php

853 lines 23.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Checkout markup.
4 *
5 * @package CartFlows
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 /**
13 * Checkout Markup
14 *
15 * @since 1.0.0
16 */
17 class Cartflows_Optin_Markup {
18
19 /**
20 * Member Variable
21 *
22 * @var object instance
23 */
24 private static $instance;
25
26 /**
27 * Initiator
28 */
29 public static function get_instance() {
30 if ( ! isset( self::$instance ) ) {
31 self::$instance = new self();
32 }
33 return self::$instance;
34 }
35
36 /**
37 * Constructor
38 */
39 public function __construct() {
40
41 /* Set is checkout flag */
42 add_filter( 'woocommerce_is_checkout', array( $this, 'woo_checkout_flag' ), 9999 );
43
44 add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_optin_fields' ), 10, 2 );
45
46 /* Optin Shortcode */
47 add_shortcode( 'cartflows_optin', array( $this, 'optin_shortcode_markup' ) );
48
49 /* Preconfigured cart data */
50 add_action( 'wp', array( $this, 'preconfigured_cart_data' ), 1 );
51
52 /* Optin shortcode data */
53 add_action( 'wp', array( $this, 'shortcode_load_data' ), 1000 );
54
55 /* Ajax Endpoint */
56 add_filter( 'woocommerce_ajax_get_endpoint', array( $this, 'get_ajax_endpoint' ), 10, 2 );
57 add_filter( 'woocommerce_login_redirect', array( $this, 'after_login_redirect' ), 10, 2 );
58
59 /* Optin Fields */
60 add_filter( 'woocommerce_default_address_fields', array( $this, 'set_optin_default_fields' ), 1000 );
61
62 /** It may required later
63 * add_filter( 'woocommerce_checkout_fields', array( $this, 'set_optin_fields' ) );
64 */
65 add_filter( 'woocommerce_billing_fields', array( $this, 'billing_optin_fields' ), 1000, 2 );
66
67 add_filter( 'woocommerce_checkout_required_field_notice', array( $this, 'change_field_label_in_required_notice' ), 100, 2 );
68
69 add_action( 'init', array( $this, 'remove_woo_actions' ) );
70
71 $this->elementor_editor_compatibility();
72
73 $this->gutenberg_editor_compatibility();
74
75 $this->bb_editor_compatibility();
76 }
77
78 /**
79 * Elementor editor compatibility.
80 */
81 public function elementor_editor_compatibility() {
82
83 add_action(
84 'cartflows_elementor_editor_compatibility',
85 function ( $post_id, $elementor_ajax ) {
86
87 add_action( 'cartflows_elementor_before_optin_shortcode', array( $this, 'before_optin_shortcode_actions' ) );
88 },
89 10,
90 2
91 );
92 }
93
94 /**
95 * Beaver Builder editor compatibility.
96 */
97 public function bb_editor_compatibility() {
98
99 add_action(
100 'cartflows_bb_editor_compatibility',
101 function ( $post_id ) {
102 add_action( 'cartflows_bb_before_optin_shortcode', array( $this, 'before_optin_shortcode_actions' ) );
103 },
104 10,
105 1
106 );
107 }
108
109 /**
110 * Gutenburg editor compatibility.
111 */
112 public function gutenberg_editor_compatibility() {
113
114 add_action( 'cartflows_gutenberg_before_optin_shortcode', array( $this, 'before_optin_shortcode_actions' ) );
115 }
116
117 /**
118 * Remove login and registration actions.
119 */
120 public function remove_woo_actions() {
121
122 if ( _is_wcf_doing_optin_ajax() ) {
123
124 if ( isset( $_POST['billing_email'] ) && ! empty( $_POST['billing_email'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
125
126 if ( email_exists( sanitize_email( wp_unslash( $_POST['billing_email'] ) ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
127 add_filter( 'woocommerce_checkout_registration_required', '__return_false' );
128 add_filter( 'woocommerce_checkout_registration_enabled', '__return_false' );
129 }
130 }
131
132 add_filter( 'woocommerce_checkout_registration_enabled', '__return_false' );
133 }
134
135 add_action( 'cartflows_woo_checkout_update_order_review', array( $this, 'filter_remove_woo_data' ) );
136
137 /**
138 * Remove the duplicate email fields when WooCommerce Payments gateway's WooPay option is enabled.
139 *
140 * @since 2.0.3
141 */
142 if ( class_exists( 'WC_Payments' ) && class_exists( 'WC_Payments_Features' ) && WC_Payments_Features::is_woopay_eligible() ) {
143 remove_action( 'woocommerce_cart_loaded_from_session', array( 'WC_Payments', 'init_woopay' ) );
144 }
145 }
146
147 /**
148 * Filter or remove woo data on update order review ajax call.
149 *
150 * @param string $post_data post data woo.
151 * @return void
152 */
153 public function filter_remove_woo_data( $post_data ) {
154
155 if ( isset( $post_data['_wcf_optin_id'] ) ) {
156 add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );
157 add_filter( 'woocommerce_order_button_text', array( $this, 'place_order_button_text' ), 1 );
158 }
159 }
160
161 /**
162 * Change order button text .
163 *
164 * @param string $button_text place order.
165 * @return string
166 */
167 public function place_order_button_text( $button_text ) {
168
169 $optin_id = get_the_ID();
170
171 if ( ! $optin_id && isset( Cartflows_Woo_Hooks::$ajax_data['_wcf_optin_id'] ) ) {
172 $optin_id = intval( Cartflows_Woo_Hooks::$ajax_data['_wcf_optin_id'] );
173 }
174
175 if ( $optin_id ) {
176
177 $wcf_order_button_text = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-button-text' );
178
179 if ( ! empty( $wcf_order_button_text ) ) {
180 $button_text = $wcf_order_button_text;
181 }
182 }
183
184 return $button_text;
185 }
186
187 /**
188 * Display all WooCommerce notices.
189 *
190 * @since 1.1.5
191 */
192 public function display_woo_notices() {
193
194 if ( null != WC()->session && function_exists( 'woocommerce_output_all_notices' ) ) {
195 woocommerce_output_all_notices();
196 }
197 }
198
199 /**
200 * Check for checkout flag
201 *
202 * @param bool $is_checkout is checkout.
203 *
204 * @return bool
205 */
206 public function woo_checkout_flag( $is_checkout ) {
207
208 if ( ! is_admin() ) {
209
210 if ( _is_wcf_optin_type() ) {
211
212 $is_checkout = true;
213 }
214 }
215
216 return $is_checkout;
217 }
218
219 /**
220 * Render checkout shortcode markup.
221 *
222 * @param array $atts attributes.
223 * @return string
224 */
225 public function optin_shortcode_markup( $atts ) {
226
227 if ( ! function_exists( 'wc_print_notices' ) ) {
228 $notice_out = '<p class="woocommerce-notice">' . __( 'WooCommerce functions do not exist. If you are in an IFrame, please reload it.', 'cartflows' ) . '</p>';
229 $notice_out .= '<button onClick="location.reload()">' . __( 'Click Here to Reload', 'cartflows' ) . '</button>';
230
231 return $notice_out;
232 }
233
234 $atts = shortcode_atts(
235 array(
236 'id' => 0,
237 ),
238 $atts
239 );
240
241 $optin_id = intval( $atts['id'] );
242
243 $show_optin_preview = false;
244
245 if ( is_admin() ) {
246
247 $show_optin_preview = apply_filters( 'cartflows_show_demo_optin_form', false );
248
249 if ( $show_optin_preview && 0 === $optin_id ) {
250
251 if ( isset( $_POST['id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
252 $optin_id = intval( $_POST['id'] ); //phpcs:ignore WordPress.Security.NonceVerification.Missing
253 }
254 }
255 }
256
257 if ( empty( $optin_id ) && ! is_admin() ) {
258
259 if ( ! _is_wcf_optin_type() && ! $show_optin_preview ) {
260
261 return '<h4>' . __( 'Please place shortcode on Optin step-type only.', 'cartflows' ) . '</h4>';
262 }
263
264 global $post;
265 $optin_id = intval( $post->ID );
266
267 }
268
269 $output = '';
270
271 ob_start();
272
273 do_action( 'cartflows_optin_form_before', $optin_id );
274
275 $optin_layout = 'default';
276
277 $template_default = CARTFLOWS_OPTIN_DIR . 'templates/optin-template-simple.php';
278
279 include $template_default;
280
281 $output .= ob_get_clean();
282
283 return $output;
284 }
285
286 /**
287 * Configure Cart Data.
288 *
289 * @since 1.0.0
290 *
291 * @return void
292 */
293 public function preconfigured_cart_data() {
294
295 if ( is_admin() ) {
296 return;
297 }
298
299 global $post;
300
301 if ( _is_wcf_optin_type() ) {
302
303 if ( wp_doing_ajax() ) {
304 return;
305 } else {
306
307 $optin_id = $post->ID;
308
309 do_action( 'cartflows_optin_before_configure_cart', $optin_id );
310
311 $products = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-optin-product' );
312
313 if ( ! is_array( $products ) ) {
314 return;
315 }
316
317 /* Empty the current cart */
318 WC()->cart->empty_cart();
319
320 if ( is_array( $products ) && count( $products ) < 1 ) {
321 wc_add_notice( __( 'No product is selected. Please select a Simple, Virtual and Free product from the meta settings.', 'cartflows' ), 'error' );
322 return;
323 }
324
325 /* Set customer session if not set */
326 if ( ! is_user_logged_in() && WC()->cart->is_empty() ) {
327 WC()->session->set_customer_session_cookie( true );
328 }
329
330 $product_id = reset( $products );
331 $_product = wc_get_product( $product_id );
332
333 if ( ! empty( $_product ) ) {
334
335 if ( $_product->is_type( 'simple' ) && $_product->is_virtual() ) {
336
337 if ( $_product->get_price() > 0 ) {
338 wc_add_notice( __( 'Please update the selected product\'s price to zero (0).', 'cartflows' ), 'error' );
339 } else {
340
341 $quantity = 1;
342
343 WC()->cart->add_to_cart( $product_id, $quantity );
344 }
345 } else {
346
347 wc_add_notice( __( 'Please select a Simple, Virtual and Free product.', 'cartflows' ), 'error' );
348 }
349 } else {
350
351 wc_add_notice( __( 'Please select a Simple, Virtual and Free product.', 'cartflows' ), 'error' );
352 }
353
354 do_action( 'cartflows_optin_after_configure_cart', $optin_id );
355 }
356 }
357 }
358
359 /**
360 * Load shortcode data.
361 *
362 * @return void
363 */
364 public function shortcode_load_data() {
365
366 if ( _is_wcf_optin_type() ) {
367
368 add_action( 'wp_enqueue_scripts', array( $this, 'shortcode_scripts' ), 21 );
369
370 add_action( 'wp_enqueue_scripts', array( $this, 'compatibility_scripts' ), 101 );
371
372 $this->before_optin_shortcode_actions();
373
374 global $post;
375
376 $optin_id = $post->ID;
377
378 do_action( 'cartflows_optin_before_shortcode', $optin_id );
379
380 }
381 }
382
383 /**
384 * Before option shortcode actions.
385 */
386 public function before_optin_shortcode_actions() {
387
388 /* Show notices if cart has errors */
389 add_action( 'woocommerce_cart_has_errors', 'woocommerce_output_all_notices' );
390
391 // Outputting the hidden field in checkout page.
392 add_action( 'woocommerce_after_order_notes', array( $this, 'checkout_shortcode_post_id' ), 99 );
393 add_action( 'woocommerce_login_form_end', array( $this, 'checkout_shortcode_post_id' ), 99 );
394
395 /* Remove unnecessary option */
396 add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
397 add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false' );
398
399 remove_all_actions( 'woocommerce_before_checkout_form' );
400 remove_all_actions( 'woocommerce_checkout_billing' );
401 remove_all_actions( 'woocommerce_checkout_shipping' );
402 remove_all_actions( 'woocommerce_checkout_before_order_review' );
403 remove_all_actions( 'woocommerce_checkout_order_review' );
404 remove_all_actions( 'woocommerce_checkout_after_order_review' );
405 add_filter( 'woocommerce_cart_needs_payment', '__return_false' );
406 add_filter( 'woocommerce_available_payment_gateways', array( $this, 'disable_payment_gateways' ) );
407
408 /* Paypal Expresss remove */
409 if ( function_exists( 'wc_gateway_ppec' ) ) {
410 remove_action( 'wp_enqueue_scripts', array( wc_gateway_ppec()->cart, 'enqueue_scripts' ) );
411 }
412
413 // Hook in actions once.
414 add_action( 'woocommerce_before_checkout_form', 'woocommerce_output_all_notices', 10 );
415 add_action( 'woocommerce_checkout_billing', array( WC()->checkout, 'checkout_form_billing' ) );
416 add_action( 'woocommerce_checkout_shipping', array( WC()->checkout, 'checkout_form_shipping' ) );
417 add_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
418
419 /* Submit Button */
420 add_filter( 'woocommerce_order_button_text', array( $this, 'place_order_button_text' ), 10, 1 );
421
422 add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );
423
424 /* Remove login actions */
425 add_filter( 'woocommerce_checkout_registration_enabled', '__return_false' );
426 add_filter( 'woocommerce_checkout_registration_required', '__return_false' );
427 }
428
429 /**
430 * Disable payment gateways.
431 *
432 * @param array $available_gateways gateways.
433 * @return array
434 */
435 public function disable_payment_gateways( $available_gateways ) {
436
437 if ( ! is_admin() ) {
438
439 $available_gateways = array();
440 }
441
442 return $available_gateways;
443 }
444
445 /**
446 * Render checkout ID hidden field.
447 *
448 * @param array $checkout checkout session data.
449 * @return void
450 */
451 public function checkout_shortcode_post_id( $checkout ) {
452
453 if ( ! _is_wcf_optin_type() ) {
454 return;
455 }
456
457 global $post;
458
459 $optin_id = $post->ID;
460
461 $flow_id = get_post_meta( $optin_id, 'wcf-flow-id', true );
462
463 echo '<input type="hidden" class="input-hidden _wcf_flow_id" name="_wcf_flow_id" value="' . intval( $flow_id ) . '">';
464 echo '<input type="hidden" class="input-hidden _wcf_optin_id" name="_wcf_optin_id" value="' . intval( $optin_id ) . '">';
465 }
466
467 /**
468 * Load shortcode scripts.
469 *
470 * @return void
471 */
472 public function shortcode_scripts() {
473
474 wp_enqueue_style( 'wcf-optin-template', wcf()->utils->get_css_url( 'optin-template' ), '', CARTFLOWS_VER );
475
476 wp_enqueue_script(
477 'wcf-optin-template',
478 wcf()->utils->get_js_url( 'optin-template' ),
479 array( 'jquery' ),
480 CARTFLOWS_VER,
481 true
482 );
483
484 do_action( 'cartflows_optin_scripts' );
485
486 $optin_dynamic_css = apply_filters( 'cartflows_optin_enable_dynamic_css', true );
487
488 if ( $optin_dynamic_css ) {
489
490 global $post;
491
492 $optin_id = $post->ID;
493
494 $style = get_post_meta( $optin_id, 'wcf-dynamic-css', true );
495
496 $css_version = get_post_meta( $optin_id, 'wcf-dynamic-css-version', true );
497
498 if ( empty( $style ) || CARTFLOWS_ASSETS_VERSION !== $css_version ) {
499 $style = $this->generate_style();
500 update_post_meta( $optin_id, 'wcf-dynamic-css', wp_slash( $style ) );
501 update_post_meta( $optin_id, 'wcf-dynamic-css-version', CARTFLOWS_ASSETS_VERSION );
502 }
503
504 /* Load all fonts */
505 CartFlows_Font_Families::render_fonts( $optin_id );
506
507 wp_add_inline_style( 'wcf-optin-template', $style );
508 }
509 }
510
511 /**
512 * Load compatibility scripts.
513 *
514 * @return void
515 */
516 public function compatibility_scripts() {
517
518 global $post;
519
520 $optin_id = $post->ID;
521
522 // Add DIVI Compatibility css if DIVI theme is enabled.
523 if ( Cartflows_Compatibility::get_instance()->is_divi_enabled() ||
524 Cartflows_Compatibility::get_instance()->is_divi_builder_enabled( $optin_id )
525 ) {
526 wp_enqueue_style( 'wcf-optin-template-divi', wcf()->utils->get_css_url( 'optin-template-divi' ), '', CARTFLOWS_VER );
527 }
528 }
529
530 /**
531 * Generate styles.
532 *
533 * @return string
534 */
535 public function generate_style() {
536
537 global $post;
538
539 $optin_id = $post->ID;
540 $r = '';
541 $g = '';
542 $b = '';
543 $output = '';
544
545 /* Global */
546 $primary_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-primary-color' );
547 $base_font_family = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-base-font-family' );
548
549 /* Input Fields */
550 $input_font_family = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-input-font-family' );
551 $input_font_weight = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-input-font-weight' );
552 $field_input_size = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-input-field-size' );
553 $field_tb_padding = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-field-tb-padding' );
554 $field_lr_padding = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-field-lr-padding' );
555 $field_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-field-color' );
556 $field_bg_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-field-bg-color' );
557 $field_border_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-field-border-color' );
558 $field_label_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-field-label-color' );
559
560 if ( 'custom' == $field_input_size ) {
561 $field_input_size = '38px';
562 }
563
564 /* Submit Button */
565 $submit_button_width = '100%';
566 $optin_button_position = '';
567 $button_font_size = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-font-size' );
568 $button_font_family = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-button-font-family' );
569 $button_font_weight = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-button-font-weight' );
570 $submit_button_height = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-button-size' );
571 $submit_tb_padding = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-tb-padding' );
572 $submit_lr_padding = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-lr-padding' );
573 $submit_button_position = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-button-position' );
574 $submit_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-color' );
575 $submit_hover_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-hover-color' );
576 $submit_bg_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-bg-color', $primary_color );
577 $submit_bg_hover_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-bg-hover-color', $primary_color );
578 $submit_border_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-border-color', $primary_color );
579 $submit_border_hover_color = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-submit-border-hover-color', $primary_color );
580
581 if ( 'custom' == $submit_button_height ) {
582 $submit_button_height = '38px';
583 $submit_button_width = 'auto';
584
585 switch ( $submit_button_position ) {
586 case 'left':
587 $optin_button_position = '0 auto 0 0';
588 break;
589 case 'center':
590 $optin_button_position = '0 auto';
591 break;
592 case 'right':
593 $optin_button_position = '0 0 0 auto';
594 break;
595
596 default:
597 $optin_button_position = '0 auto';
598 break;
599 }
600 }
601
602 if ( Cartflows_Compatibility::get_instance()->is_divi_enabled() ||
603 Cartflows_Compatibility::get_instance()->is_divi_builder_enabled( $optin_id ) ) {
604
605 include CARTFLOWS_OPTIN_DIR . 'includes/optin-dynamic-divi-css.php';
606
607 } else {
608 include CARTFLOWS_OPTIN_DIR . 'includes/optin-dynamic-css.php';
609 }
610
611 return $output;
612 }
613
614 /**
615 * Get ajax end points.
616 *
617 * @param string $endpoint_url end point URL.
618 * @param string $request end point request.
619 * @return string
620 */
621 public function get_ajax_endpoint( $endpoint_url, $request ) {
622
623 global $post;
624
625 if ( ! empty( $post ) && ! empty( $_SERVER['REQUEST_URI'] ) ) {
626
627 if ( _is_wcf_optin_type() ) {
628
629 if ( mb_strpos( $endpoint_url, 'checkout', 0, 'utf-8' ) === false ) {
630
631 if ( '' === $request ) {
632 $query_args = array(
633 'wc-ajax' => '%%endpoint%%',
634 );
635 } else {
636 $query_args = array(
637 'wc-ajax' => $request,
638 );
639 }
640
641 $uri = explode( '?', esc_url_raw( $_SERVER['REQUEST_URI'] ), 2 );
642 $uri = $uri[0];
643
644 $endpoint_url = esc_url( add_query_arg( $query_args, $uri ) );
645 }
646 }
647 }
648
649 return $endpoint_url;
650 }
651
652
653 /**
654 * Save checkout fields.
655 *
656 * @param int $order_id order id.
657 * @param array $posted posted data.
658 * @return void
659 */
660 public function save_optin_fields( $order_id, $posted ) {
661 // We are calling this function on WooCommerce action where nonce is verified.
662 //phpcs:disable WordPress.Security.NonceVerification.Missing
663 if ( isset( $_POST['_wcf_optin_id'] ) ) {
664 $optin_id = intval( $_POST['_wcf_optin_id'] );
665 $flow_id = isset( $_POST['_wcf_flow_id'] ) ? intval( wp_unslash( $_POST['_wcf_flow_id'] ) ) : 0;
666 }
667
668 if ( ! empty( $flow_id ) && ! empty( $optin_id ) ) {
669
670 $order = wc_get_order( $order_id );
671 if ( CARTFLOWS_STEP_POST_TYPE === get_post_type( $optin_id ) ) {
672 $order->update_meta_data( '_wcf_optin_id', $optin_id );
673 }
674
675 if ( CARTFLOWS_FLOW_POST_TYPE === get_post_type( $flow_id ) ) {
676 $order->update_meta_data( '_wcf_flow_id', $flow_id );
677 }
678
679 $order->save();
680 }
681 //phpcs:enable WordPress.Security.NonceVerification.Missing
682 }
683
684 /**
685 * Redirect users to our checkout if hidden param
686 *
687 * @param string $redirect redirect url.
688 * @param object $user user.
689 * @return string
690 */
691 public function after_login_redirect( $redirect, $user ) {
692 // We are calling this function on WooCommerce action where nonce is verified.
693 //phpcs:disable WordPress.Security.NonceVerification.Missing
694 if ( isset( $_POST['_wcf_optin_id'] ) ) {
695
696 $optin_id = intval( $_POST['_wcf_optin_id'] );
697
698 // Security: Validate that the post ID is a CartFlows step to prevent open redirect.
699 if ( $optin_id > 0 && wcf()->utils->is_step_post_type( get_post_type( $optin_id ) ) ) {
700 $redirect = get_permalink( $optin_id );
701 }
702 }
703 //phpcs:enable WordPress.Security.NonceVerification.Missing
704
705 return $redirect;
706 }
707
708
709 /**
710 * Add custom class to the fields to change the UI to three column.
711 *
712 * @param array $fields fields.
713 */
714 public function set_optin_default_fields( $fields ) {
715
716 if ( _is_wcf_optin_type() ) {
717
718 global $post;
719
720 $optin_id = $post->ID;
721 } elseif ( _is_wcf_doing_optin_ajax() && wcf()->utils->get_optin_id_from_post_data() ) {
722
723
724 $optin_id = wcf()->utils->get_optin_id_from_post_data();
725 } else {
726 return $fields;
727 }
728
729 $first_name = $fields['first_name'];
730 $last_name = $fields['last_name'];
731
732 /* Make fields required */
733 $first_name['required'] = true;
734 $last_name['required'] = true;
735
736 $fields = array(
737 'first_name' => $first_name,
738 'last_name' => $last_name,
739 );
740
741 return apply_filters( 'cartflows_optin_default_fields', $fields, $optin_id );
742 }
743
744 /**
745 * Add custom class to the fields to change the UI to three column.
746 *
747 * @param array $fields fields.
748 */
749 public function set_optin_fields( $fields ) {
750
751 if ( _is_wcf_optin_type() ) {
752
753 global $post;
754
755 $optin_id = $post->ID;
756 } elseif ( _is_wcf_doing_optin_ajax() && wcf()->utils->get_optin_id_from_post_data() ) {
757
758
759 $optin_id = wcf()->utils->get_optin_id_from_post_data();
760 } else {
761 return $fields;
762 }
763
764 $billing_first_name = $fields['billing']['billing_first_name'];
765 $billing_last_name = $fields['billing']['billing_last_name'];
766 $billing_email = $fields['billing']['billing_email'];
767
768 $fields['billing'] = array(
769 'billing_first_name' => $billing_first_name,
770 'billing_last_name' => $billing_last_name,
771 'billing_email' => $billing_email,
772 );
773
774 return apply_filters( 'cartflows_optin_fields', $fields, $optin_id );
775 }
776
777 /**
778 * Billing field customization.
779 *
780 * @param array $fields fields data.
781 * @param string $country country name.
782 * @return array
783 */
784 public function billing_optin_fields( $fields, $country ) {
785 //phpcs:disable WordPress.Security.NonceVerification.Missing
786 $show_optin_preview = false;
787
788 if ( is_admin() ) {
789 $show_optin_preview = apply_filters( 'cartflows_show_demo_optin_form', false );
790 }
791
792 if ( _is_wcf_optin_type() || $show_optin_preview ) {
793
794 global $post;
795
796 if ( $post ) {
797 $optin_id = $post->ID;
798 } elseif ( is_admin() && isset( $_POST['id'] ) ) {
799
800 $optin_id = intval( $_POST['id'] );
801 }
802 } elseif ( _is_wcf_doing_optin_ajax() && wcf()->utils->get_optin_id_from_post_data() ) {
803
804 $optin_id = wcf()->utils->get_optin_id_from_post_data();
805 } else {
806 return $fields;
807 }
808
809 if ( is_wc_endpoint_url( 'edit-address' ) ) {
810 return $fields;
811 }
812
813 $billing_first_name = $fields['billing_first_name'];
814 $billing_last_name = $fields['billing_last_name'];
815 $billing_email = $fields['billing_email'];
816
817 /* Make fields required */
818 $billing_first_name['required'] = true;
819 $billing_last_name['required'] = true;
820
821 $fields = array(
822 'billing_first_name' => $billing_first_name,
823 'billing_last_name' => $billing_last_name,
824 'billing_email' => $billing_email,
825 );
826 return apply_filters( 'cartflows_billing_optin_fields', $fields, $country, $optin_id );
827 //phpcs:enable WordPress.Security.NonceVerification.Missing
828 }
829
830
831 /**
832 * Replace billing label.
833 *
834 * @param string $notice Notice.
835 * @param string $field_label Field name.
836 * @return string
837 */
838 public function change_field_label_in_required_notice( $notice, $field_label ) {
839
840 if ( _is_wcf_doing_optin_ajax() ) {
841
842 $notice = str_replace( 'Billing ', '', $notice );
843 }
844
845 return $notice;
846 }
847 }
848
849 /**
850 * Kicking this off by calling 'get_instance()' method
851 */
852 Cartflows_Optin_Markup::get_instance();
853