| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Illuminate; |
| 4 |
|
| 5 |
/** |
| 6 |
* Action [ helper class ] |
| 7 |
* |
| 8 |
* @package SpringDevs\Subscription\Illuminate |
| 9 |
*/ |
| 10 |
class Action { |
| 11 |
|
| 12 |
/** |
| 13 |
* Did when status changes. |
| 14 |
* |
| 15 |
* @param string $status Status. |
| 16 |
* @param int $subscription_id Subscription ID. |
| 17 |
* @param bool $write_comment Write comment?. |
| 18 |
*/ |
| 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 |
|
| 33 |
wp_update_post( |
| 34 |
array( |
| 35 |
'ID' => $subscription_id, |
| 36 |
'post_status' => $status, |
| 37 |
) |
| 38 |
); |
| 39 |
|
| 40 |
// Only note a real transition — never re-log the same status. |
| 41 |
if ( $write_comment && $old_status !== $status ) { |
| 42 |
self::write_comment( $status, $subscription_id ); |
| 43 |
} |
| 44 |
|
| 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 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Write Comment based on status. |
| 56 |
* |
| 57 |
* @param string $status Status. |
| 58 |
* @param Int $subscription_id Subscription ID. |
| 59 |
*/ |
| 60 |
public static function write_comment( string $status, int $subscription_id ) { |
| 61 |
switch ( $status ) { |
| 62 |
case 'expired': |
| 63 |
self::expired( $subscription_id ); |
| 64 |
break; |
| 65 |
case 'active': |
| 66 |
self::active( $subscription_id ); |
| 67 |
break; |
| 68 |
case 'pending': |
| 69 |
self::pending( $subscription_id ); |
| 70 |
break; |
| 71 |
case 'cancelled': |
| 72 |
self::cancelled( $subscription_id ); |
| 73 |
break; |
| 74 |
case 'pe_cancelled': |
| 75 |
self::pe_cancelled( $subscription_id ); |
| 76 |
break; |
| 77 |
case 'on-hold': |
| 78 |
self::on_hold( $subscription_id ); |
| 79 |
break; |
| 80 |
case 'completed': |
| 81 |
self::completed( $subscription_id ); |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 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 |
/** |
| 107 |
* Write Comment About expired Subscription. |
| 108 |
* |
| 109 |
* @param int $subscription_id Subscription ID. |
| 110 |
*/ |
| 111 |
private static function expired( int $subscription_id ) { |
| 112 |
$comment_id = wp_insert_comment( |
| 113 |
array( |
| 114 |
'comment_author' => 'Subscription for WooCommerce', |
| 115 |
'comment_content' => 'Subscription is Expired', |
| 116 |
'comment_post_ID' => $subscription_id, |
| 117 |
'comment_type' => 'order_note', |
| 118 |
) |
| 119 |
); |
| 120 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Expired' ); |
| 121 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_expired' ); |
| 122 |
|
| 123 |
do_action( 'subscrpt_subscription_expired', $subscription_id ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Write Comment About Active Subscription. |
| 128 |
* |
| 129 |
* @param int $subscription_id Subscription ID. |
| 130 |
*/ |
| 131 |
private static function active( int $subscription_id ) { |
| 132 |
$comment_id = wp_insert_comment( |
| 133 |
array( |
| 134 |
'comment_author' => 'Subscription for WooCommerce', |
| 135 |
'comment_content' => 'Subscription activated. Next payment due date set.', |
| 136 |
'comment_post_ID' => $subscription_id, |
| 137 |
'comment_type' => 'order_note', |
| 138 |
) |
| 139 |
); |
| 140 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Activated' ); |
| 141 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_activated' ); |
| 142 |
|
| 143 |
do_action( 'subscrpt_subscription_activated', $subscription_id ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Write Comment About Subscription Pending. |
| 148 |
* |
| 149 |
* @param int $subscription_id Subscription ID. |
| 150 |
*/ |
| 151 |
private static function pending( int $subscription_id ) { |
| 152 |
$comment_id = wp_insert_comment( |
| 153 |
array( |
| 154 |
'comment_author' => 'Subscription for WooCommerce', |
| 155 |
'comment_content' => 'Subscription is pending.', |
| 156 |
'comment_post_ID' => $subscription_id, |
| 157 |
'comment_type' => 'order_note', |
| 158 |
) |
| 159 |
); |
| 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 ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Write Comment About cancelled Subscription. |
| 168 |
* |
| 169 |
* @param int $subscription_id Subscription ID. |
| 170 |
*/ |
| 171 |
private static function cancelled( int $subscription_id ) { |
| 172 |
$comment_id = wp_insert_comment( |
| 173 |
array( |
| 174 |
'comment_author' => 'Subscription for WooCommerce', |
| 175 |
'comment_content' => 'Subscription is Cancelled.', |
| 176 |
'comment_post_ID' => $subscription_id, |
| 177 |
'comment_type' => 'order_note', |
| 178 |
) |
| 179 |
); |
| 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 ); |
| 189 |
} |
| 190 |
|
| 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 |
/** |
| 212 |
* Write Comment About Pending Cancellation. |
| 213 |
* |
| 214 |
* @param int $subscription_id Subscription ID. |
| 215 |
*/ |
| 216 |
private static function pe_cancelled( int $subscription_id ) { |
| 217 |
$comment_id = wp_insert_comment( |
| 218 |
array( |
| 219 |
'comment_author' => 'Subscription for WooCommerce', |
| 220 |
'comment_content' => 'Subscription is Pending Cancellation.', |
| 221 |
'comment_post_ID' => $subscription_id, |
| 222 |
'comment_type' => 'order_note', |
| 223 |
) |
| 224 |
); |
| 225 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' ); |
| 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(); |
| 233 |
|
| 234 |
do_action( 'subscrpt_subscription_pending_cancellation', $subscription_id ); |
| 235 |
} |
| 236 |
} |
| 237 |
|