| 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 |
wp_update_post( |
| 21 |
array( |
| 22 |
'ID' => $subscription_id, |
| 23 |
'post_status' => $status, |
| 24 |
) |
| 25 |
); |
| 26 |
|
| 27 |
if ( $write_comment ) { |
| 28 |
self::write_comment( $status, $subscription_id ); |
| 29 |
} |
| 30 |
|
| 31 |
self::user( $subscription_id ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Write Comment based on status. |
| 36 |
* |
| 37 |
* @param string $status Status. |
| 38 |
* @param Int $subscription_id Subscription ID. |
| 39 |
*/ |
| 40 |
public static function write_comment( string $status, int $subscription_id ) { |
| 41 |
switch ( $status ) { |
| 42 |
case 'expired': |
| 43 |
self::expired( $subscription_id ); |
| 44 |
break; |
| 45 |
case 'active': |
| 46 |
self::active( $subscription_id ); |
| 47 |
break; |
| 48 |
case 'pending': |
| 49 |
self::pending( $subscription_id ); |
| 50 |
break; |
| 51 |
case 'cancelled': |
| 52 |
self::cancelled( $subscription_id ); |
| 53 |
break; |
| 54 |
case 'pe_cancelled': |
| 55 |
self::pe_cancelled( $subscription_id ); |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Write Comment About expired Subscription. |
| 62 |
* |
| 63 |
* @param int $subscription_id Subscription ID. |
| 64 |
*/ |
| 65 |
private static function expired( int $subscription_id ) { |
| 66 |
$comment_id = wp_insert_comment( |
| 67 |
array( |
| 68 |
'comment_author' => 'Subscription for WooCommerce', |
| 69 |
'comment_content' => 'Subscription is Expired', |
| 70 |
'comment_post_ID' => $subscription_id, |
| 71 |
'comment_type' => 'order_note', |
| 72 |
) |
| 73 |
); |
| 74 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Expired' ); |
| 75 |
|
| 76 |
do_action( 'subscrpt_subscription_expired', $subscription_id ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Write Comment About Active Subscription. |
| 81 |
* |
| 82 |
* @param int $subscription_id Subscription ID. |
| 83 |
*/ |
| 84 |
private static function active( int $subscription_id ) { |
| 85 |
$comment_id = wp_insert_comment( |
| 86 |
array( |
| 87 |
'comment_author' => 'Subscription for WooCommerce', |
| 88 |
'comment_content' => 'Subscription activated.Next payment due date set.', |
| 89 |
'comment_post_ID' => $subscription_id, |
| 90 |
'comment_type' => 'order_note', |
| 91 |
) |
| 92 |
); |
| 93 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Activated' ); |
| 94 |
do_action( 'subscrpt_subscription_activated', $subscription_id ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Write Comment About Subscription Pending. |
| 99 |
* |
| 100 |
* @param int $subscription_id Subscription ID. |
| 101 |
*/ |
| 102 |
private static function pending( int $subscription_id ) { |
| 103 |
$comment_id = wp_insert_comment( |
| 104 |
array( |
| 105 |
'comment_author' => 'Subscription for WooCommerce', |
| 106 |
'comment_content' => 'Subscription is pending.', |
| 107 |
'comment_post_ID' => $subscription_id, |
| 108 |
'comment_type' => 'order_note', |
| 109 |
) |
| 110 |
); |
| 111 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending' ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Write Comment About Subscription Cancelled. |
| 116 |
* |
| 117 |
* @param int $subscription_id Subscription ID. |
| 118 |
*/ |
| 119 |
private static function cancelled( int $subscription_id ) { |
| 120 |
$comment_id = wp_insert_comment( |
| 121 |
array( |
| 122 |
'comment_author' => 'Subscription for WooCommerce', |
| 123 |
'comment_content' => 'Subscription is Cancelled.', |
| 124 |
'comment_post_ID' => $subscription_id, |
| 125 |
'comment_type' => 'order_note', |
| 126 |
) |
| 127 |
); |
| 128 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Cancelled' ); |
| 129 |
|
| 130 |
WC()->mailer(); |
| 131 |
do_action( 'subscrpt_subscription_cancelled_email_notification', $subscription_id ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Write Comment About Pending Cancellation. |
| 136 |
* |
| 137 |
* @param int $subscription_id Subscription ID. |
| 138 |
*/ |
| 139 |
private static function pe_cancelled( int $subscription_id ) { |
| 140 |
$comment_id = wp_insert_comment( |
| 141 |
array( |
| 142 |
'comment_author' => 'Subscription for WooCommerce', |
| 143 |
'comment_content' => 'Subscription is Pending Cancellation.', |
| 144 |
'comment_post_ID' => $subscription_id, |
| 145 |
'comment_type' => 'order_note', |
| 146 |
) |
| 147 |
); |
| 148 |
update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' ); |
| 149 |
} |
| 150 |
|
| 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 |
} |
| 161 |
|
| 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 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|