| 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 |
$old_status = get_post_status( $subscription_id ); |
| 21 |
|
| 22 |
wp_update_post( |
| 23 |
array( |
| 24 |
'ID' => $subscription_id, |
| 25 |
'post_status' => $status, |
| 26 |
) |
| 27 |
); |
| 28 |
|
| 29 |
if ( $write_comment ) { |
| 30 |
self::write_comment( $status, $subscription_id ); |
| 31 |
} |
| 32 |
|
| 33 |
// Trigger status change action |
| 34 |
do_action( 'subscrpt_subscription_status_changed', $subscription_id, $old_status, $status ); |
| 35 |
|
| 36 |
// Trigger resumption event if subscription is being activated from cancelled or pending cancellation |
| 37 |
if ( $status === 'active' && in_array( $old_status, array( 'cancelled', 'pe_cancelled' ) ) ) { |
| 38 |
do_action( 'subscrpt_subscription_resumed', $subscription_id, $old_status ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Write Comment based on status. |
| 44 |
* |
| 45 |
* @param string $status Status. |
| 46 |
* @param Int $subscription_id Subscription ID. |
| 47 |
*/ |
| 48 |
public static function write_comment( string $status, int $subscription_id ) { |
| 49 |
switch ( $status ) { |
| 50 |
case 'expired': |
| 51 |
self::expired( $subscription_id ); |
| 52 |
break; |
| 53 |
case 'active': |
| 54 |
self::active( $subscription_id ); |
| 55 |
break; |
| 56 |
case 'pending': |
| 57 |
self::pending( $subscription_id ); |
| 58 |
break; |
| 59 |
case 'cancelled': |
| 60 |
self::cancelled( $subscription_id ); |
| 61 |
break; |
| 62 |
case 'pe_cancelled': |
| 63 |
self::pe_cancelled( $subscription_id ); |
| 64 |
break; |
| 65 |
case 'on-hold': |
| 66 |
self::on_hold( $subscription_id ); |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Write Comment About expired Subscription. |
| 73 |
* |
| 74 |
* @param int $subscription_id Subscription ID. |
| 75 |
*/ |
| 76 |
private static function expired( int $subscription_id ) { |
| 77 |
$comment_id = wp_insert_comment( |
| 78 |
array( |
| 79 |
'comment_author' => 'Subscription for WooCommerce', |
| 80 |
'comment_content' => 'Subscription is Expired', |
| 81 |
'comment_post_ID' => $subscription_id, |
| 82 |
'comment_type' => 'order_note', |
| 83 |
) |
| 84 |
); |
| 85 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Expired' ); |
| 86 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_expired' ); |
| 87 |
|
| 88 |
do_action( 'subscrpt_subscription_expired', $subscription_id ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Write Comment About Active Subscription. |
| 93 |
* |
| 94 |
* @param int $subscription_id Subscription ID. |
| 95 |
*/ |
| 96 |
private static function active( int $subscription_id ) { |
| 97 |
$comment_id = wp_insert_comment( |
| 98 |
array( |
| 99 |
'comment_author' => 'Subscription for WooCommerce', |
| 100 |
'comment_content' => 'Subscription activated.Next payment due date set.', |
| 101 |
'comment_post_ID' => $subscription_id, |
| 102 |
'comment_type' => 'order_note', |
| 103 |
) |
| 104 |
); |
| 105 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Activated' ); |
| 106 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_activated' ); |
| 107 |
|
| 108 |
do_action( 'subscrpt_subscription_activated', $subscription_id ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Write Comment About Subscription Pending. |
| 113 |
* |
| 114 |
* @param int $subscription_id Subscription ID. |
| 115 |
*/ |
| 116 |
private static function pending( int $subscription_id ) { |
| 117 |
$comment_id = wp_insert_comment( |
| 118 |
array( |
| 119 |
'comment_author' => 'Subscription for WooCommerce', |
| 120 |
'comment_content' => 'Subscription is pending.', |
| 121 |
'comment_post_ID' => $subscription_id, |
| 122 |
'comment_type' => 'order_note', |
| 123 |
) |
| 124 |
); |
| 125 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending' ); |
| 126 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_pending' ); |
| 127 |
|
| 128 |
do_action( 'subscrpt_subscription_pending', $subscription_id ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Write Comment About cancelled Subscription. |
| 133 |
* |
| 134 |
* @param int $subscription_id Subscription ID. |
| 135 |
*/ |
| 136 |
private static function cancelled( int $subscription_id ) { |
| 137 |
$comment_id = wp_insert_comment( |
| 138 |
array( |
| 139 |
'comment_author' => 'Subscription for WooCommerce', |
| 140 |
'comment_content' => 'Subscription is Cancelled.', |
| 141 |
'comment_post_ID' => $subscription_id, |
| 142 |
'comment_type' => 'order_note', |
| 143 |
) |
| 144 |
); |
| 145 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Cancelled' ); |
| 146 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_cancelled' ); |
| 147 |
|
| 148 |
WC()->mailer(); |
| 149 |
do_action( 'subscrpt_subscription_cancelled_email_notification', $subscription_id ); |
| 150 |
do_action( 'subscrpt_subscription_cancelled', $subscription_id ); |
| 151 |
|
| 152 |
// Fire split payment cancelled action |
| 153 |
do_action( 'subscrpt_split_payment_cancelled', $subscription_id ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Write Comment About On-Hold Subscription. |
| 158 |
* |
| 159 |
* @param int $subscription_id Subscription ID. |
| 160 |
*/ |
| 161 |
private static function on_hold( int $subscription_id ) { |
| 162 |
$comment_id = wp_insert_comment( |
| 163 |
array( |
| 164 |
'comment_author' => 'Subscription for WooCommerce', |
| 165 |
'comment_content' => 'Subscription is On Hold. Access suspended after payment failure.', |
| 166 |
'comment_post_ID' => $subscription_id, |
| 167 |
'comment_type' => 'order_note', |
| 168 |
) |
| 169 |
); |
| 170 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription On Hold' ); |
| 171 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_on_hold' ); |
| 172 |
|
| 173 |
do_action( 'subscrpt_subscription_on_hold', $subscription_id ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Write Comment About Pending Cancellation. |
| 178 |
* |
| 179 |
* @param int $subscription_id Subscription ID. |
| 180 |
*/ |
| 181 |
private static function pe_cancelled( int $subscription_id ) { |
| 182 |
$comment_id = wp_insert_comment( |
| 183 |
array( |
| 184 |
'comment_author' => 'Subscription for WooCommerce', |
| 185 |
'comment_content' => 'Subscription is Pending Cancellation.', |
| 186 |
'comment_post_ID' => $subscription_id, |
| 187 |
'comment_type' => 'order_note', |
| 188 |
) |
| 189 |
); |
| 190 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' ); |
| 191 |
update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_pe_cancel' ); |
| 192 |
|
| 193 |
do_action( 'subscrpt_subscription_pending_cancellation', $subscription_id ); |
| 194 |
} |
| 195 |
} |
| 196 |
|