PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.2.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.2.0
2.4.0 2.3.0 2.2.0 2.1.0 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 All 65 releases
← All changes | includes/Illuminate/AutoRenewal.php +146 -52 1.3.1 → 2.2.0 View file →
@@ -13,66 +13,86 @@
13 13 * Initialize the class
14 14 */
15 15 public function __construct() {
16 16 add_action( 'subscrpt_subscription_expired', array( $this, 'after_subscription_expired' ) );
17 - add_filter(
18 - 'subscrpt_renewal_item_meta',
19 - function ( $item_meta, $product ) {
20 - if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) || ! $product ) {
21 - return $item_meta;
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' );
22 54 }
23 - if ( $product->is_type( 'variable' ) ) {
24 - $product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) );
25 - if ( ! $product ) {
26 - return $item_meta;
27 - }
28 - }
55 + return false;
56 + }
57 + }
29 58
30 - $timing_per = $product->get_meta( '_subscrpt_timing_per' );
31 - $timing_option = $product->get_meta( '_subscrpt_timing_option' );
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 + );
32 64
33 - return array(
34 - 'time' => empty( $timing_per ) ? 1 : $timing_per,
35 - 'type' => $timing_option,
36 - 'trial' => null,
37 - );
38 - },
39 - 10,
40 - 2
41 - );
42 - add_filter(
43 - 'subscrpt_renewal_product_args',
44 - function ( $product_args, $product, $order_item ) {
45 - if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) ) {
46 - return $product_args;
47 - }
65 + return $product_args;
66 + }
48 67
49 - if ( ! $product ) {
50 - if ( ! is_admin() ) {
51 - wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' );
52 - }
53 - return false;
54 - }
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 + }
55 80
56 - if ( $product->is_type( 'variable' ) ) {
57 - $product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) );
58 - if ( ! $product ) {
59 - if ( ! is_admin() ) {
60 - wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' );
61 - }
62 - return false;
63 - }
64 - }
65 - $product_args = array(
66 - 'name' => $product->get_name(),
67 - 'subtotal' => $product->get_price() * $order_item->get_quantity(),
68 - 'total' => $product->get_price() * $order_item->get_quantity(),
69 - );
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 + }
70 87
71 - return $product_args;
72 - },
73 - 10,
74 - 3
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,
75 95 );
76 96 }
77 97
78 98 /**
@@ -80,9 +100,83 @@
80 100 *
81 101 * @param int $subscription_id Subscription ID.
82 102 */
83 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 +
84 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.
85 118 Helper::create_renewal_order( $subscription_id );
86 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 ] );
87 181 }
88 182 }