| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace SpringDevs\Subscription\Frontend; |
| 5 |
|
| 6 |
use SpringDevs\Subscription\Illuminate\Action; |
| 7 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class ActionController |
| 11 |
* |
| 12 |
* @package SpringDevs\Subscription\Frontend |
| 13 |
*/ |
| 14 |
class ActionController { |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize the class |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'before_single_subscrpt_content', array( $this, 'control_action_subscrpt' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Take Subscription Action. |
| 25 |
*/ |
| 26 |
public function control_action_subscrpt() { |
| 27 |
if ( ! ( isset( $_GET['subscrpt_id'] ) && isset( $_GET['action'] ) && isset( $_GET['wpnonce'] ) ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
$subscrpt_id = sanitize_text_field( wp_unslash( $_GET['subscrpt_id'] ) ); |
| 31 |
$action = sanitize_text_field( wp_unslash( $_GET['action'] ) ); |
| 32 |
$wpnonce = sanitize_text_field( wp_unslash( $_GET['wpnonce'] ) ); |
| 33 |
if ( ! wp_verify_nonce( $wpnonce, 'subscrpt_nonce' ) ) { |
| 34 |
wp_die( esc_html( __( 'Sorry !! You cannot permit to access.', 'sdevs_subscrpt' ) ) ); |
| 35 |
} |
| 36 |
if ( 'renew' === $action && ! subscrpt_is_auto_renew_enabled() ) { |
| 37 |
$this->manual_renew_product( $subscrpt_id ); |
| 38 |
} elseif ( 'cancelled' === $action ) { |
| 39 |
$status = get_post_status( $subscrpt_id ); |
| 40 |
$user_cancel = get_post_meta( $subscrpt_id, '_subscrpt_user_cancel', true ); |
| 41 |
if ( 'no' === $user_cancel ) { |
| 42 |
return; |
| 43 |
} elseif ( 'active' === $status ) { |
| 44 |
Action::status( 'pe_cancelled', $subscrpt_id ); |
| 45 |
} else { |
| 46 |
Action::status( $action, $subscrpt_id ); |
| 47 |
} |
| 48 |
} elseif ( 'reactive' === $action ) { |
| 49 |
Action::status( 'active', $subscrpt_id ); |
| 50 |
} elseif ( 'renew-on' === $action ) { |
| 51 |
update_post_meta( $subscrpt_id, '_subscrpt_auto_renew', 1 ); |
| 52 |
} elseif ( 'renew-off' === $action ) { |
| 53 |
update_post_meta( $subscrpt_id, '_subscrpt_auto_renew', 0 ); |
| 54 |
} elseif ( 'renew' === $action && subscrpt_is_auto_renew_enabled() ) { |
| 55 |
Helper::create_renewal_order( $subscrpt_id ); |
| 56 |
} else { |
| 57 |
do_action( 'subscrpt_execute_actions', $subscrpt_id, $action ); |
| 58 |
} |
| 59 |
// phpcs:ignore |
| 60 |
echo ( "<script>location.href = '" . wc_get_endpoint_url( 'view-subscription', $subscrpt_id, wc_get_page_permalink( 'myaccount' ) ) . "';</script>" ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Manually Renew Subscription. |
| 65 |
* |
| 66 |
* @param Int $subscrpt_id Subscription ID. |
| 67 |
*/ |
| 68 |
public function manual_renew_product( $subscrpt_id ) { |
| 69 |
$product_id = get_post_meta( $subscrpt_id, '_subscrpt_product_id', true ); |
| 70 |
$subscription_variation_id = get_post_meta( $subscrpt_id, '_subscrpt_variation_id', true ); |
| 71 |
|
| 72 |
$variation_id = 0; |
| 73 |
if ( isset( $variation_id ) ) { |
| 74 |
$variation_id = $variation_id; |
| 75 |
} |
| 76 |
|
| 77 |
WC()->cart->empty_cart(); |
| 78 |
|
| 79 |
WC()->cart->add_to_cart( |
| 80 |
$product_id, |
| 81 |
1, |
| 82 |
$variation_id, |
| 83 |
array(), |
| 84 |
array( 'renew_subscrpt' => true ) |
| 85 |
); |
| 86 |
|
| 87 |
wc_add_notice( get_option( 'subscrpt_manual_renew_cart_notice' ), 'success' ); |
| 88 |
$this->redirect( wc_get_cart_url() ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Redirect on URL. |
| 93 |
* |
| 94 |
* @param String $url URL. |
| 95 |
*/ |
| 96 |
public function redirect( $url ) { |
| 97 |
?> |
| 98 |
<script> |
| 99 |
window.location.href = '<?php echo esc_url_raw( $url ); ?>'; |
| 100 |
</script> |
| 101 |
<?php |
| 102 |
} |
| 103 |
} |
| 104 |
|