| @@ -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,14 +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' ); | |
| 129 | 182 | |
| 130 | 183 | WC()->mailer(); |
| 131 | 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 ); | |
| 132 | 189 | } |
| 133 | 190 | |
| 134 | 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 | + /** | |
| 135 | 212 | * Write Comment About Pending Cancellation. |
| 136 | 213 | * |
| 137 | 214 | * @param int $subscription_id Subscription ID. |
| 138 | 215 | */ |
| @@ -145,24 +222,15 @@ | ||
| 145 | 222 | 'comment_type' => 'order_note', |
| 146 | 223 | ) |
| 147 | 224 | ); |
| 148 | 225 | update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' ); |
| 149 | - } | |
| 226 | + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_pe_cancel' ); | |
| 150 | 227 | |
| 151 | - /** | |
| 152 | - * Update user role. | |
| 153 | - * | |
| 154 | - * @param Int $subscription_id Subscription ID. | |
| 155 | - */ | |
| 156 | - private static function user( $subscription_id ) { | |
| 157 | - $user = new \WP_User( get_current_user_id() ); | |
| 158 | - if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'administrator', $user->roles, true ) ) { | |
| 159 | - return; | |
| 160 | - } | |
| 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(); | |
| 161 | 233 | |
| 162 | - if ( Helper::subscription_exists( $subscription_id, 'active' ) ) { | |
| 163 | - $user->set_role( get_option( 'subscrpt_active_role', 'subscriber' ) ); | |
| 164 | - } elseif ( Helper::subscription_exists( $subscription_id, array( 'cancelled', 'expired' ) ) ) { | |
| 165 | - $user->set_role( get_option( 'subscrpt_unactive_role', 'customer' ) ); | |
| 166 | - } | |
| 234 | + do_action( 'subscrpt_subscription_pending_cancellation', $subscription_id ); | |
| 167 | 235 | } |
| 168 | 236 | } |