PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.9.5
Subscriptions for WooCommerce with Stripe Recurring Payments v1.9.5
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Illuminate / AutoRenewal.php

AutoRenewal.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.9.5, at includes/Illuminate/AutoRenewal.php

183 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Illuminate;
4
5 /**
6 * Class AutoRenewal
7 *
8 * @package SpringDevs\Subscription\Illuminate
9 */
10 class AutoRenewal {
11
12 /**
13 * Initialize the class
14 */
15 public function __construct() {
16 add_action( 'subscrpt_subscription_expired', array( $this, 'after_subscription_expired' ) );
17 add_filter( 'subscrpt_renewal_item_meta', array( $this, 'filter_renewal_item_meta' ), 10, 2 );
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' ] );
26 }
27
28 /**
29 * Filter renewal product args.
30 *
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.
34 *
35 * @return array
36 */
37 public function filter_renewal_product_args( $product_args, $product, $order_item ) {
38 if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) ) {
39 return $product_args;
40 }
41
42 if ( ! $product ) {
43 if ( ! is_admin() ) {
44 wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'subscription' ), 'error' );
45 }
46 return false;
47 }
48
49 if ( $product->is_type( 'variable' ) ) {
50 $product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) );
51 if ( ! $product ) {
52 if ( ! is_admin() ) {
53 wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'subscription' ), 'error' );
54 }
55 return false;
56 }
57 }
58
59 $product_args = array(
60 'name' => $product->get_name(),
61 'subtotal' => $product->get_price() * $order_item->get_quantity(),
62 'total' => $product->get_price() * $order_item->get_quantity(),
63 );
64
65 return $product_args;
66 }
67
68 /**
69 * Filter renewal item's meta.
70 *
71 * @param array $item_meta Item meta.
72 * @param \WC_Product $product Product Object.
73 *
74 * @return array
75 */
76 public function filter_renewal_item_meta( $item_meta, $product ) {
77 if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) || ! $product ) {
78 return $item_meta;
79 }
80
81 if ( $product->is_type( 'variable' ) ) {
82 $product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) );
83 if ( ! $product ) {
84 return $item_meta;
85 }
86 }
87
88 $timing_per = $product->get_meta( '_subscrpt_timing_per' );
89 $timing_option = $product->get_meta( '_subscrpt_timing_option' );
90
91 return array(
92 'time' => empty( $timing_per ) ? 1 : $timing_per,
93 'type' => $timing_option,
94 'trial' => null,
95 );
96 }
97
98 /**
99 * After Expired Subscription.
100 *
101 * @param int $subscription_id Subscription ID.
102 */
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
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.
118 Helper::create_renewal_order( $subscription_id );
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 ] );
181 }
182 }
183