| @@ -16,8 +16,19 @@ | ||
| 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 | + | |
| 20 | 31 | $old_status = get_post_status( $subscription_id ); |
| 21 | 32 | |
| 22 | 33 | wp_update_post( |
| 23 | 34 | array( |
| @@ -25,9 +36,10 @@ | ||
| 25 | 36 | 'post_status' => $status, |
| 26 | 37 | ) |
| 27 | 38 | ); |
| 28 | 39 | |
| 29 | - if ( $write_comment ) { | |
| 40 | + // Only note a real transition — never re-log the same status. | |
| 41 | + if ( $write_comment && $old_status !== $status ) { | |
| 30 | 42 | self::write_comment( $status, $subscription_id ); |
| 31 | 43 | } |
| 32 | 44 | |
| 33 | 45 | // Trigger status change action |
| @@ -64,12 +76,35 @@ | ||
| 64 | 76 | break; |
| 65 | 77 | case 'on-hold': |
| 66 | 78 | self::on_hold( $subscription_id ); |
| 67 | 79 | break; |
| 80 | + case 'completed': | |
| 81 | + self::completed( $subscription_id ); | |
| 82 | + break; | |
| 68 | 83 | } |
| 69 | 84 | } |
| 70 | 85 | |
| 71 | 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.', 'subscription' ), | |
| 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 | + /** | |
| 72 | 107 | * Write Comment About expired Subscription. |
| 73 | 108 | * |
| 74 | 109 | * @param int $subscription_id Subscription ID. |
| 75 | 110 | */ |
| @@ -76,9 +111,9 @@ | ||
| 76 | 111 | private static function expired( int $subscription_id ) { |
| 77 | 112 | $comment_id = wp_insert_comment( |
| 78 | 113 | array( |
| 79 | 114 | 'comment_author' => 'Subscription for WooCommerce', |
| 80 | - 'comment_content' => 'Subscription is Expired', | |
| 115 | + 'comment_content' => __( 'Subscription is Expired', 'subscription' ), | |
| 81 | 116 | 'comment_post_ID' => $subscription_id, |
| 82 | 117 | 'comment_type' => 'order_note', |
| 83 | 118 | ) |
| 84 | 119 | ); |
| @@ -96,9 +131,9 @@ | ||
| 96 | 131 | private static function active( int $subscription_id ) { |
| 97 | 132 | $comment_id = wp_insert_comment( |
| 98 | 133 | array( |
| 99 | 134 | 'comment_author' => 'Subscription for WooCommerce', |
| 100 | - 'comment_content' => 'Subscription activated.Next payment due date set.', | |
| 135 | + 'comment_content' => __( 'Subscription activated. Next payment due date set.', 'subscription' ), | |
| 101 | 136 | 'comment_post_ID' => $subscription_id, |
| 102 | 137 | 'comment_type' => 'order_note', |
| 103 | 138 | ) |
| 104 | 139 | ); |
| @@ -116,9 +151,9 @@ | ||
| 116 | 151 | private static function pending( int $subscription_id ) { |
| 117 | 152 | $comment_id = wp_insert_comment( |
| 118 | 153 | array( |
| 119 | 154 | 'comment_author' => 'Subscription for WooCommerce', |
| 120 | - 'comment_content' => 'Subscription is pending.', | |
| 155 | + 'comment_content' => __( 'Subscription is pending.', 'subscription' ), | |
| 121 | 156 | 'comment_post_ID' => $subscription_id, |
| 122 | 157 | 'comment_type' => 'order_note', |
| 123 | 158 | ) |
| 124 | 159 | ); |
| @@ -136,9 +171,9 @@ | ||
| 136 | 171 | private static function cancelled( int $subscription_id ) { |
| 137 | 172 | $comment_id = wp_insert_comment( |
| 138 | 173 | array( |
| 139 | 174 | 'comment_author' => 'Subscription for WooCommerce', |
| 140 | - 'comment_content' => 'Subscription is Cancelled.', | |
| 175 | + 'comment_content' => __( 'Subscription is Cancelled.', 'subscription' ), | |
| 141 | 176 | 'comment_post_ID' => $subscription_id, |
| 142 | 177 | 'comment_type' => 'order_note', |
| 143 | 178 | ) |
| 144 | 179 | ); |
| @@ -161,9 +196,9 @@ | ||
| 161 | 196 | private static function on_hold( int $subscription_id ) { |
| 162 | 197 | $comment_id = wp_insert_comment( |
| 163 | 198 | array( |
| 164 | 199 | 'comment_author' => 'Subscription for WooCommerce', |
| 165 | - 'comment_content' => 'Subscription is On Hold. Access suspended after payment failure.', | |
| 200 | + 'comment_content' => __( 'Subscription is On Hold. Access suspended after payment failure.', 'subscription' ), | |
| 166 | 201 | 'comment_post_ID' => $subscription_id, |
| 167 | 202 | 'comment_type' => 'order_note', |
| 168 | 203 | ) |
| 169 | 204 | ); |
| @@ -181,9 +216,9 @@ | ||
| 181 | 216 | private static function pe_cancelled( int $subscription_id ) { |
| 182 | 217 | $comment_id = wp_insert_comment( |
| 183 | 218 | array( |
| 184 | 219 | 'comment_author' => 'Subscription for WooCommerce', |
| 185 | - 'comment_content' => 'Subscription is Pending Cancellation.', | |
| 220 | + 'comment_content' => __( 'Subscription is Pending Cancellation.', 'subscription' ), | |
| 186 | 221 | 'comment_post_ID' => $subscription_id, |
| 187 | 222 | 'comment_type' => 'order_note', |
| 188 | 223 | ) |
| 189 | 224 | ); |
| @@ -188,8 +223,14 @@ | ||
| 188 | 223 | ) |
| 189 | 224 | ); |
| 190 | 225 | update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' ); |
| 191 | 226 | update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_pe_cancel' ); |
| 227 | + | |
| 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(); | |
| 192 | 233 | |
| 193 | 234 | do_action( 'subscrpt_subscription_pending_cancellation', $subscription_id ); |
| 194 | 235 | } |
| 195 | 236 | } |