| 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 |
self::user( $subscription_id ); |
| 34 |
|
| 35 |
// Trigger status change action |
| 36 |
do_action('subscrpt_subscription_status_changed', $subscription_id, $old_status, $status); |
| 37 |
|
| 38 |
// Trigger resumption event if subscription is being activated from cancelled or pending cancellation |
| 39 |
if ($status === 'active' && in_array($old_status, array('cancelled', 'pe_cancelled'))) { |
| 40 |
do_action('subscrpt_subscription_resumed', $subscription_id, $old_status); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Write Comment based on status. |
| 46 |
* |
| 47 |
* @param string $status Status. |
| 48 |
* @param Int $subscription_id Subscription ID. |
| 49 |
*/ |
| 50 |
public static function write_comment( string $status, int $subscription_id ) { |
| 51 |
switch ( $status ) { |
| 52 |
case 'expired': |
| 53 |
self::expired( $subscription_id ); |
| 54 |
break; |
| 55 |
case 'active': |
| 56 |
self::active( $subscription_id ); |
| 57 |
break; |
| 58 |
case 'pending': |
| 59 |
self::pending( $subscription_id ); |
| 60 |
break; |
| 61 |
case 'cancelled': |
| 62 |
self::cancelled( $subscription_id ); |
| 63 |
break; |
| 64 |
case 'pe_cancelled': |
| 65 |
self::pe_cancelled( $subscription_id ); |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Write Comment About expired Subscription. |
| 72 |
* |
| 73 |
* @param int $subscription_id Subscription ID. |
| 74 |
*/ |
| 75 |
private static function expired( int $subscription_id ) { |
| 76 |
$comment_id = wp_insert_comment( |
| 77 |
array( |
| 78 |
'comment_author' => 'Subscription for WooCommerce', |
| 79 |
'comment_content' => 'Subscription is Expired', |
| 80 |
'comment_post_ID' => $subscription_id, |
| 81 |
'comment_type' => 'order_note', |
| 82 |
) |
| 83 |
); |
| 84 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Expired' ); |
| 85 |
|
| 86 |
do_action( 'subscrpt_subscription_expired', $subscription_id ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Write Comment About Active Subscription. |
| 91 |
* |
| 92 |
* @param int $subscription_id Subscription ID. |
| 93 |
*/ |
| 94 |
private static function active( int $subscription_id ) { |
| 95 |
$comment_id = wp_insert_comment( |
| 96 |
array( |
| 97 |
'comment_author' => 'Subscription for WooCommerce', |
| 98 |
'comment_content' => 'Subscription activated.Next payment due date set.', |
| 99 |
'comment_post_ID' => $subscription_id, |
| 100 |
'comment_type' => 'order_note', |
| 101 |
) |
| 102 |
); |
| 103 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Activated' ); |
| 104 |
do_action( 'subscrpt_subscription_activated', $subscription_id ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Write Comment About Subscription Pending. |
| 109 |
* |
| 110 |
* @param int $subscription_id Subscription ID. |
| 111 |
*/ |
| 112 |
private static function pending( int $subscription_id ) { |
| 113 |
$comment_id = wp_insert_comment( |
| 114 |
array( |
| 115 |
'comment_author' => 'Subscription for WooCommerce', |
| 116 |
'comment_content' => 'Subscription is pending.', |
| 117 |
'comment_post_ID' => $subscription_id, |
| 118 |
'comment_type' => 'order_note', |
| 119 |
) |
| 120 |
); |
| 121 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending' ); |
| 122 |
do_action( 'subscrpt_subscription_pending', $subscription_id ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Write Comment About Subscription Cancelled. |
| 127 |
* |
| 128 |
* @param int $subscription_id Subscription ID. |
| 129 |
*/ |
| 130 |
private static function cancelled( int $subscription_id ) { |
| 131 |
$comment_id = wp_insert_comment( |
| 132 |
array( |
| 133 |
'comment_author' => 'Subscription for WooCommerce', |
| 134 |
'comment_content' => 'Subscription is Cancelled.', |
| 135 |
'comment_post_ID' => $subscription_id, |
| 136 |
'comment_type' => 'order_note', |
| 137 |
) |
| 138 |
); |
| 139 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Cancelled' ); |
| 140 |
|
| 141 |
WC()->mailer(); |
| 142 |
do_action( 'subscrpt_subscription_cancelled_email_notification', $subscription_id ); |
| 143 |
do_action( 'subscrpt_subscription_cancelled', $subscription_id ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Write Comment About Pending Cancellation. |
| 148 |
* |
| 149 |
* @param int $subscription_id Subscription ID. |
| 150 |
*/ |
| 151 |
private static function pe_cancelled( int $subscription_id ) { |
| 152 |
$comment_id = wp_insert_comment( |
| 153 |
array( |
| 154 |
'comment_author' => 'Subscription for WooCommerce', |
| 155 |
'comment_content' => 'Subscription is Pending Cancellation.', |
| 156 |
'comment_post_ID' => $subscription_id, |
| 157 |
'comment_type' => 'order_note', |
| 158 |
) |
| 159 |
); |
| 160 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' ); |
| 161 |
do_action( 'subscrpt_subscription_pending_cancellation', $subscription_id ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Update user role. |
| 166 |
* |
| 167 |
* @param Int $subscription_id Subscription ID. |
| 168 |
*/ |
| 169 |
private static function user( $subscription_id ) { |
| 170 |
$user = new \WP_User( get_current_user_id() ); |
| 171 |
if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'administrator', $user->roles, true ) ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
if ( Helper::subscription_exists( $subscription_id, 'active' ) ) { |
| 176 |
$user->set_role( get_option( 'subscrpt_active_role', 'subscriber' ) ); |
| 177 |
} elseif ( Helper::subscription_exists( $subscription_id, array( 'cancelled', 'expired' ) ) ) { |
| 178 |
$user->set_role( get_option( 'subscrpt_unactive_role', 'customer' ) ); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|