| @@ -15,16 +15,23 @@ | ||
| 15 | 15 | public function __construct() { |
| 16 | 16 | add_action( 'subscrpt_subscription_expired', array( $this, 'after_subscription_expired' ) ); |
| 17 | 17 | add_filter( 'subscrpt_renewal_item_meta', array( $this, 'filter_renewal_item_meta' ), 10, 2 ); |
| 18 | 18 | add_filter( 'subscrpt_renewal_product_args', array( $this, 'filter_renewal_product_args' ), 10, 3 ); |
| 19 | + | |
| 20 | + // Grace period hooks. | |
| 21 | + add_action( 'subscrpt_subscription_expired', [ $this, 'maybe_trigger_grace_start_hook' ] ); | |
| 22 | + add_action( 'subscrpt_scheduled_grace_end', [ $this, 'trigger_grace_end_hook' ] ); | |
| 23 | + | |
| 24 | + // Clear grace period on subscription re-activation. | |
| 25 | + add_action( 'subscrpt_subscription_activated', [ $this, 'clear_grace_period_schedules' ] ); | |
| 19 | 26 | } |
| 20 | 27 | |
| 21 | 28 | /** |
| 22 | 29 | * Filter renewal product args. |
| 23 | 30 | * |
| 24 | - * @param array $product_args product args. | |
| 25 | - * @param \WC_Product $product Product Object. | |
| 26 | - * @param \WC_Order_Item $order_item Order Item Object. | |
| 31 | + * @param array $product_args product args. | |
| 32 | + * @param \WC_Product $product Product Object. | |
| 33 | + * @param \WC_Order_Item_Product $order_item Order Item Object. | |
| 27 | 34 | * |
| 28 | 35 | * @return array |
| 29 | 36 | */ |
| 30 | 37 | public function filter_renewal_product_args( $product_args, $product, $order_item ) { |
| @@ -33,9 +40,9 @@ | ||
| 33 | 40 | } |
| 34 | 41 | |
| 35 | 42 | if ( ! $product ) { |
| 36 | 43 | if ( ! is_admin() ) { |
| 37 | - wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' ); | |
| 44 | + wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'subscription' ), 'error' ); | |
| 38 | 45 | } |
| 39 | 46 | return false; |
| 40 | 47 | } |
| 41 | 48 | |
| @@ -42,9 +49,9 @@ | ||
| 42 | 49 | if ( $product->is_type( 'variable' ) ) { |
| 43 | 50 | $product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) ); |
| 44 | 51 | if ( ! $product ) { |
| 45 | 52 | if ( ! is_admin() ) { |
| 46 | - wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' ); | |
| 53 | + wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'subscription' ), 'error' ); | |
| 47 | 54 | } |
| 48 | 55 | return false; |
| 49 | 56 | } |
| 50 | 57 | } |
| @@ -93,9 +100,83 @@ | ||
| 93 | 100 | * |
| 94 | 101 | * @param int $subscription_id Subscription ID. |
| 95 | 102 | */ |
| 96 | 103 | public function after_subscription_expired( $subscription_id ) { |
| 104 | + // Check if maximum payment limit has been reached | |
| 105 | + if ( subscrpt_is_max_payments_reached( $subscription_id ) ) { | |
| 106 | + error_log( "WPS: Maximum payment limit reached for subscription #{$subscription_id}. Auto-renewal cancelled." ); | |
| 107 | + return; | |
| 108 | + } | |
| 109 | + | |
| 97 | 110 | if ( subscrpt_is_auto_renew_enabled() ) { |
| 111 | + // Change subscription status to pending if it was a trial subscription. | |
| 112 | + $trial = get_post_meta( $subscription_id, '_subscrpt_trial', true ); | |
| 113 | + if ( ! empty( $trial ) ) { | |
| 114 | + Action::status( 'pending', $subscription_id ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + // Create renewal order. | |
| 98 | 118 | Helper::create_renewal_order( $subscription_id ); |
| 99 | 119 | } |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Maybe run grace period hook. | |
| 124 | + * | |
| 125 | + * @param int $subscription_id Subscription ID. | |
| 126 | + */ | |
| 127 | + public function maybe_trigger_grace_start_hook( $subscription_id ) { | |
| 128 | + $default_grace_period = get_option( 'subscrpt_default_payment_grace_period', '7' ); | |
| 129 | + if ( (int) $default_grace_period <= 0 ) { | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + | |
| 133 | + $subscription_data = Helper::get_subscription_data( $subscription_id ); | |
| 134 | + $next_datetime = ! empty( $subscription_data['next_date'] ) ? strtotime( $subscription_data['next_date'] ) : 0; | |
| 135 | + $grace_end_datetime = $next_datetime + ( (int) $default_grace_period * DAY_IN_SECONDS ); | |
| 136 | + | |
| 137 | + // If grace period already ended. | |
| 138 | + if ( time() >= $grace_end_datetime ) { | |
| 139 | + return; | |
| 140 | + } | |
| 141 | + | |
| 142 | + // Grace period started. | |
| 143 | + do_action( 'subscrpt_grace_period_started', $subscription_id ); | |
| 144 | + | |
| 145 | + subscrpt_write_log( "Subscription #{$subscription_id} grace period started." ); | |
| 146 | + | |
| 147 | + // Set hook to run when grace period ends. | |
| 148 | + $hook = 'subscrpt_scheduled_grace_end'; | |
| 149 | + if ( function_exists( 'as_schedule_single_action' ) ) { | |
| 150 | + as_unschedule_action( $hook, [ 'subscription_id' => $subscription_id ] ); | |
| 151 | + as_schedule_single_action( $grace_end_datetime, $hook, [ 'subscription_id' => $subscription_id ], 'WPSubscription' ); | |
| 152 | + } else { | |
| 153 | + wp_clear_scheduled_hook( $hook, [ $subscription_id ] ); | |
| 154 | + wp_schedule_single_event( $grace_end_datetime, $hook, [ $subscription_id ] ); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Trigger grace end hook. | |
| 160 | + * | |
| 161 | + * @param int $subscription_id Subscription ID. | |
| 162 | + */ | |
| 163 | + public function trigger_grace_end_hook( $subscription_id ) { | |
| 164 | + subscrpt_write_log( "Subscription #{$subscription_id} grace period ended." ); | |
| 165 | + | |
| 166 | + // Grace period ended. | |
| 167 | + do_action( 'subscrpt_grace_period_ended', $subscription_id ); | |
| 168 | + } | |
| 169 | + | |
| 170 | + /** | |
| 171 | + * Clear grace period schedules. | |
| 172 | + * | |
| 173 | + * @param int $subscription_id Subscription ID. | |
| 174 | + */ | |
| 175 | + public function clear_grace_period_schedules( $subscription_id ) { | |
| 176 | + $hook = 'subscrpt_scheduled_grace_end'; | |
| 177 | + if ( function_exists( 'as_unschedule_action' ) ) { | |
| 178 | + as_unschedule_action( $hook, [ 'subscription_id' => $subscription_id ] ); | |
| 179 | + } | |
| 180 | + wp_clear_scheduled_hook( $hook, [ $subscription_id ] ); | |
| 100 | 181 | } |
| 101 | 182 | } |