| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dyanamic Flow Actions |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Initialization |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class Cartflows_Wd_Flow_Actions { |
| 14 |
|
| 15 |
/** |
| 16 |
* Member Variable |
| 17 |
* |
| 18 |
* @var instance |
| 19 |
*/ |
| 20 |
private static $instance; |
| 21 |
|
| 22 |
/** |
| 23 |
* Initiator |
| 24 |
*/ |
| 25 |
public static function get_instance() { |
| 26 |
if ( ! isset( self::$instance ) ) { |
| 27 |
self::$instance = new self(); |
| 28 |
} |
| 29 |
return self::$instance; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
|
| 37 |
add_filter( 'cartflows_skip_configure_cart', array( $this, 'skip_cf_cart_configuration' ), 10, 2 ); |
| 38 |
|
| 39 |
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_cf_hidden_fields' ) ); |
| 40 |
add_filter( 'woocommerce_add_to_cart_redirect', array( $this, 'redirect_to_next_flow_step' ), 10, 2 ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Skip Configure Cart Data. |
| 45 |
* |
| 46 |
* @param bool $skip_cart is skip cart. |
| 47 |
* @param int $checkout_id checkout id.. |
| 48 |
*/ |
| 49 |
public function skip_cf_cart_configuration( $skip_cart, $checkout_id ) { |
| 50 |
|
| 51 |
if ( isset( $_GET['cf-redirect'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 52 |
$skip_cart = true; |
| 53 |
} |
| 54 |
|
| 55 |
return $skip_cart; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Add hidden fields. |
| 60 |
*/ |
| 61 |
public function add_cf_hidden_fields() { |
| 62 |
|
| 63 |
global $post, $wcf_step; |
| 64 |
|
| 65 |
if ( $post && $wcf_step ) { |
| 66 |
|
| 67 |
$flow_id = $wcf_step->get_flow_id(); |
| 68 |
$step_id = $wcf_step->get_step_id(); |
| 69 |
|
| 70 |
echo '<input type="hidden" name="wcf-flow-id" value="' . absint( $flow_id ) . '"/>'; |
| 71 |
echo '<input type="hidden" name="wcf-step-id" value="' . absint( $step_id ) . '"/>'; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Redirect to flow next step. |
| 77 |
* |
| 78 |
* @param string $redirect_url next step URL. |
| 79 |
* @param string $product Product adding in cart. |
| 80 |
*/ |
| 81 |
public function redirect_to_next_flow_step( $redirect_url, $product ) { |
| 82 |
|
| 83 |
if ( isset( $_REQUEST['wcf-step-id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 84 |
|
| 85 |
$step_id = intval( $_REQUEST['wcf-step-id'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 86 |
|
| 87 |
if ( CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) { |
| 88 |
return $redirect_url; |
| 89 |
} |
| 90 |
|
| 91 |
$wcf_step_obj = wcf_get_step( $step_id ); |
| 92 |
$next_step_id = $wcf_step_obj->get_direct_next_step_id(); |
| 93 |
|
| 94 |
if ( $next_step_id ) { |
| 95 |
|
| 96 |
$redirect_url = add_query_arg( |
| 97 |
array( |
| 98 |
'cf-redirect' => true, |
| 99 |
), |
| 100 |
get_permalink( $next_step_id ) |
| 101 |
); |
| 102 |
|
| 103 |
wc_clear_notices(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return $redirect_url; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Kicking this off by calling 'get_instance()' method |
| 113 |
*/ |
| 114 |
Cartflows_Wd_Flow_Actions::get_instance(); |
| 115 |
|