PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.3.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.3.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/Action.php +91 -20 1.3.0 → 2.3.0 View file →
@@ -16,8 +16,21 @@
16 16 * @param int $subscription_id Subscription ID.
17 17 * @param bool $write_comment Write comment?.
18 18 */
19 19 public static function status( string $status, int $subscription_id, bool $write_comment = true ) {
20 + // A split-payment subscription whose final installment is paid is terminal:
21 + // never (re)activate it. Any renewal-payment path that tries to set it active
22 + // after completion is coerced to `completed` so the status can't flip back.
23 + if (
24 + 'active' === $status
25 + && function_exists( 'subscrpt_is_max_payments_reached' )
26 + && subscrpt_is_max_payments_reached( $subscription_id )
27 + ) {
28 + $status = 'completed';
29 + }
30 +
31 + $old_status = get_post_status( $subscription_id );
32 +
20 33 wp_update_post(
21 34 array(
22 35 'ID' => $subscription_id,
23 36 'post_status' => $status,
@@ -23,13 +36,20 @@
23 36 'post_status' => $status,
24 37 )
25 38 );
26 39
27 - if ( $write_comment ) {
40 + // Only note a real transition — never re-log the same status.
41 + if ( $write_comment && $old_status !== $status ) {
28 42 self::write_comment( $status, $subscription_id );
29 43 }
30 44
31 - self::user( $subscription_id );
45 + // Trigger status change action
46 + do_action( 'subscrpt_subscription_status_changed', $subscription_id, $old_status, $status );
47 +
48 + // Trigger resumption event if subscription is being activated from cancelled or pending cancellation
49 + if ( $status === 'active' && in_array( $old_status, array( 'cancelled', 'pe_cancelled' ) ) ) {
50 + do_action( 'subscrpt_subscription_resumed', $subscription_id, $old_status );
51 + }
32 52 }
33 53
34 54 /**
35 55 * Write Comment based on status.
@@ -53,12 +73,38 @@
53 73 break;
54 74 case 'pe_cancelled':
55 75 self::pe_cancelled( $subscription_id );
56 76 break;
77 + case 'on-hold':
78 + self::on_hold( $subscription_id );
79 + break;
80 + case 'completed':
81 + self::completed( $subscription_id );
82 + break;
57 83 }
58 84 }
59 85
60 86 /**
87 + * Write Comment About Completed Subscription.
88 + *
89 + * @param int $subscription_id Subscription ID.
90 + */
91 + private static function completed( int $subscription_id ) {
92 + $comment_id = wp_insert_comment(
93 + array(
94 + 'comment_author' => 'Subscription for WooCommerce',
95 + 'comment_content' => 'Subscription completed. All payments made.',
96 + 'comment_post_ID' => $subscription_id,
97 + 'comment_type' => 'order_note',
98 + )
99 + );
100 + update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Completed' );
101 + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_completed' );
102 +
103 + do_action( 'subscrpt_subscription_completed', $subscription_id );
104 + }
105 +
106 + /**
61 107 * Write Comment About expired Subscription.
62 108 *
63 109 * @param int $subscription_id Subscription ID.
64 110 */
@@ -71,8 +117,9 @@
71 117 'comment_type' => 'order_note',
72 118 )
73 119 );
74 120 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Expired' );
121 + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_expired' );
75 122
76 123 do_action( 'subscrpt_subscription_expired', $subscription_id );
77 124 }
78 125
@@ -84,14 +131,16 @@
84 131 private static function active( int $subscription_id ) {
85 132 $comment_id = wp_insert_comment(
86 133 array(
87 134 'comment_author' => 'Subscription for WooCommerce',
88 - 'comment_content' => 'Subscription activated.Next payment due date set.',
135 + 'comment_content' => 'Subscription activated. Next payment due date set.',
89 136 'comment_post_ID' => $subscription_id,
90 137 'comment_type' => 'order_note',
91 138 )
92 139 );
93 140 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Activated' );
141 + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_activated' );
142 +
94 143 do_action( 'subscrpt_subscription_activated', $subscription_id );
95 144 }
96 145
97 146 /**
@@ -108,12 +157,15 @@
108 157 'comment_type' => 'order_note',
109 158 )
110 159 );
111 160 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending' );
161 + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_pending' );
162 +
163 + do_action( 'subscrpt_subscription_pending', $subscription_id );
112 164 }
113 165
114 166 /**
115 - * Write Comment About Subscription Cancelled.
167 + * Write Comment About cancelled Subscription.
116 168 *
117 169 * @param int $subscription_id Subscription ID.
118 170 */
119 171 private static function cancelled( int $subscription_id ) {
@@ -125,11 +177,39 @@
125 177 'comment_type' => 'order_note',
126 178 )
127 179 );
128 180 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Cancelled' );
181 + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_cancelled' );
182 +
183 + WC()->mailer();
184 + do_action( 'subscrpt_subscription_cancelled_email_notification', $subscription_id );
185 + do_action( 'subscrpt_subscription_cancelled', $subscription_id );
186 +
187 + // Fire split payment cancelled action
188 + do_action( 'subscrpt_split_payment_cancelled', $subscription_id );
129 189 }
130 190
131 191 /**
192 + * Write Comment About On-Hold Subscription.
193 + *
194 + * @param int $subscription_id Subscription ID.
195 + */
196 + private static function on_hold( int $subscription_id ) {
197 + $comment_id = wp_insert_comment(
198 + array(
199 + 'comment_author' => 'Subscription for WooCommerce',
200 + 'comment_content' => 'Subscription is On Hold. Access suspended after payment failure.',
201 + 'comment_post_ID' => $subscription_id,
202 + 'comment_type' => 'order_note',
203 + )
204 + );
205 + update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription On Hold' );
206 + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_on_hold' );
207 +
208 + do_action( 'subscrpt_subscription_on_hold', $subscription_id );
209 + }
210 +
211 + /**
132 212 * Write Comment About Pending Cancellation.
133 213 *
134 214 * @param int $subscription_id Subscription ID.
135 215 */
@@ -142,24 +222,15 @@
142 222 'comment_type' => 'order_note',
143 223 )
144 224 );
145 225 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' );
146 - }
226 + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_pe_cancel' );
147 227
148 - /**
149 - * Update user role.
150 - *
151 - * @param Int $subscription_id Subscription ID.
152 - */
153 - private static function user( $subscription_id ) {
154 - $user = new \WP_User( get_current_user_id() );
155 - if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'administrator', $user->roles, true ) ) {
156 - return;
157 - }
228 + // WC_Email classes only exist once the mailer has been built, and they
229 + // attach their own listeners from their constructors. Without this, an
230 + // email listening for a pending cancellation is simply not registered yet
231 + // when the action fires - the same reason cancelled() calls it.
232 + WC()->mailer();
158 233
159 - if ( Helper::subscription_exists( $subscription_id, 'active' ) ) {
160 - $user->set_role( get_option( 'subscrpt_active_role', 'subscriber' ) );
161 - } elseif ( Helper::subscription_exists( $subscription_id, array( 'cancelled', 'expired' ) ) ) {
162 - $user->set_role( get_option( 'subscrpt_unactive_role', 'customer' ) );
163 - }
234 + do_action( 'subscrpt_subscription_pending_cancellation', $subscription_id );
164 235 }
165 236 }