| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace SpringDevs\Subscription\Frontend; |
| 5 |
|
| 6 |
use SpringDevs\Subscription\Illuminate\Action; |
| 7 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 8 |
use SpringDevs\Subscription\Illuminate\Subscription\Subscription; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class ActionController |
| 12 |
* |
| 13 |
* @package SpringDevs\Subscription\Frontend |
| 14 |
*/ |
| 15 |
class ActionController { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize the class |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
add_action( 'before_single_subscrpt_content', array( $this, 'control_action_subscrpt' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Take Subscription Action. |
| 26 |
*/ |
| 27 |
public function control_action_subscrpt() { |
| 28 |
if ( ! ( isset( $_GET['subscrpt_id'] ) && isset( $_GET['action'] ) && isset( $_GET['wpnonce'] ) ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$subscrpt_id = sanitize_text_field( wp_unslash( $_GET['subscrpt_id'] ) ); |
| 33 |
$action = sanitize_text_field( wp_unslash( $_GET['action'] ) ); |
| 34 |
$wpnonce = sanitize_text_field( wp_unslash( $_GET['wpnonce'] ) ); |
| 35 |
|
| 36 |
// Nonce check |
| 37 |
if ( ! wp_verify_nonce( $wpnonce, 'subscrpt_nonce' ) ) { |
| 38 |
$error_notice = __( "You don't have permission to modify this subscription. If you believe this is an error, please contact support.", 'subscription' ); |
| 39 |
wc_add_notice( $error_notice, 'error' ); |
| 40 |
|
| 41 |
$view_subscription_endpoint = Subscription::get_user_endpoint( 'view_subs' ); |
| 42 |
$redirect_url = wc_get_endpoint_url( $view_subscription_endpoint, $subscrpt_id, wc_get_page_permalink( 'myaccount' ) ); |
| 43 |
return wp_safe_redirect( $redirect_url ); |
| 44 |
} |
| 45 |
|
| 46 |
// User check |
| 47 |
$subs_post = get_post( $subscrpt_id ); |
| 48 |
$author_id = $subs_post ? (int) $subs_post->post_author : 0; |
| 49 |
$current_user_id = get_current_user_id(); |
| 50 |
$user_is_admin = current_user_can( 'manage_options' ); |
| 51 |
|
| 52 |
if ( ! $user_is_admin && (int) $author_id !== (int) $current_user_id ) { |
| 53 |
$error_notice = __( "You don't have permission to modify this subscription. If you believe this is an error, please contact support.", 'subscription' ); |
| 54 |
wc_add_notice( $error_notice, 'error' ); |
| 55 |
|
| 56 |
$view_subscription_endpoint = Subscription::get_user_endpoint( 'view_subs' ); |
| 57 |
$redirect_url = wc_get_endpoint_url( $view_subscription_endpoint, $subscrpt_id, wc_get_page_permalink( 'myaccount' ) ); |
| 58 |
return wp_safe_redirect( $redirect_url ); |
| 59 |
} |
| 60 |
|
| 61 |
// Get view subscription endpoint slug. |
| 62 |
$view_subs_endpoint = Subscription::get_user_endpoint( 'view_subs' ); |
| 63 |
|
| 64 |
// Check maximum payment limit for renewal-related actions (including early renewal) |
| 65 |
$renewal_actions = apply_filters( 'subscrpt_renewal_actions', array( 'renew', 'renew-on', 'early-renew' ) ); |
| 66 |
if ( in_array( $action, $renewal_actions, true ) && subscrpt_is_max_payments_reached( $subscrpt_id ) ) { |
| 67 |
wc_add_notice( __( 'This subscription has reached its maximum payment limit and cannot be renewed further.', 'subscription' ), 'error' ); |
| 68 |
wp_safe_redirect( wc_get_endpoint_url( $view_subs_endpoint, $subscrpt_id, wc_get_page_permalink( 'myaccount' ) ) ); |
| 69 |
exit; |
| 70 |
} |
| 71 |
|
| 72 |
if ( 'renew' === $action && ! subscrpt_is_auto_renew_enabled() ) { |
| 73 |
$this->manual_renew_product( $subscrpt_id ); |
| 74 |
} elseif ( 'cancelled' === $action ) { |
| 75 |
$status = get_post_status( $subscrpt_id ); |
| 76 |
$user_cancel = get_post_meta( $subscrpt_id, '_subscrpt_user_cancel', true ); |
| 77 |
if ( 'no' === $user_cancel ) { |
| 78 |
return; |
| 79 |
} elseif ( 'active' === $status ) { |
| 80 |
Action::status( 'pe_cancelled', $subscrpt_id ); |
| 81 |
} else { |
| 82 |
Action::status( $action, $subscrpt_id ); |
| 83 |
} |
| 84 |
} elseif ( 'reactivate' === $action ) { |
| 85 |
Action::status( 'active', $subscrpt_id ); |
| 86 |
} elseif ( 'renew-on' === $action ) { |
| 87 |
update_post_meta( $subscrpt_id, '_subscrpt_auto_renew', 1 ); |
| 88 |
} elseif ( 'renew-off' === $action ) { |
| 89 |
update_post_meta( $subscrpt_id, '_subscrpt_auto_renew', 0 ); |
| 90 |
} elseif ( 'renew' === $action && subscrpt_is_auto_renew_enabled() ) { |
| 91 |
Helper::create_renewal_order( $subscrpt_id ); |
| 92 |
} else { |
| 93 |
// Safety check: If this is any kind of renewal action and limit is reached, block it |
| 94 |
if ( subscrpt_is_max_payments_reached( $subscrpt_id ) && |
| 95 |
( strpos( $action, 'renew' ) !== false || strpos( $action, 'renewal' ) !== false ) ) { |
| 96 |
wc_add_notice( __( 'This subscription has reached its maximum payment limit and cannot be renewed further.', 'subscription' ), 'error' ); |
| 97 |
wp_safe_redirect( wc_get_endpoint_url( $view_subs_endpoint, $subscrpt_id, wc_get_page_permalink( 'myaccount' ) ) ); |
| 98 |
exit; |
| 99 |
} |
| 100 |
|
| 101 |
do_action( 'subscrpt_execute_actions', $subscrpt_id, $action ); |
| 102 |
} |
| 103 |
wp_safe_redirect( wc_get_endpoint_url( $view_subs_endpoint, $subscrpt_id, wc_get_page_permalink( 'myaccount' ) ) ); |
| 104 |
exit; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Manually Renew Subscription. |
| 109 |
* |
| 110 |
* @param Int $subscrpt_id Subscription ID. |
| 111 |
*/ |
| 112 |
public function manual_renew_product( $subscrpt_id ) { |
| 113 |
$product_id = get_post_meta( $subscrpt_id, '_subscrpt_product_id', true ); |
| 114 |
$subscription_variation_id = get_post_meta( $subscrpt_id, '_subscrpt_variation_id', true ); |
| 115 |
|
| 116 |
$variation_id = 0; |
| 117 |
if ( isset( $variation_id ) ) { |
| 118 |
$variation_id = $variation_id; |
| 119 |
} |
| 120 |
|
| 121 |
WC()->cart->empty_cart(); |
| 122 |
|
| 123 |
WC()->cart->add_to_cart( |
| 124 |
$product_id, |
| 125 |
1, |
| 126 |
$variation_id, |
| 127 |
array(), |
| 128 |
array( 'renew_subscrpt' => true ) |
| 129 |
); |
| 130 |
|
| 131 |
wc_add_notice( get_option( 'subscrpt_manual_renew_cart_notice' ), 'success' ); |
| 132 |
$this->redirect( wc_get_cart_url() ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Redirect on URL. |
| 137 |
* |
| 138 |
* @param String $url URL. |
| 139 |
*/ |
| 140 |
public function redirect( $url ) { |
| 141 |
wp_safe_redirect( esc_url( $url ) ); |
| 142 |
exit; |
| 143 |
} |
| 144 |
} |
| 145 |
|