| 1 |
<?php |
| 2 |
/** |
| 3 |
* Role Management File |
| 4 |
* |
| 5 |
* @package SpringDevs\Subscription\Illuminate |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SpringDevs\Subscription\Illuminate; |
| 9 |
|
| 10 |
/** |
| 11 |
* RoleManagement [ helper class ] |
| 12 |
* |
| 13 |
* @package SpringDevs\Subscription\Illuminate |
| 14 |
*/ |
| 15 |
class RoleManagement { |
| 16 |
/** |
| 17 |
* Initialize the class |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'subscrpt_subscription_activated', [ $this, 'maybe_change_user_role_on_subscription_activation' ] ); |
| 21 |
|
| 22 |
add_action( 'subscrpt_subscription_expired', [ $this, 'maybe_change_user_role_on_subscription_deactivation' ] ); |
| 23 |
add_action( 'subscrpt_subscription_cancelled', [ $this, 'maybe_change_user_role_on_subscription_deactivation' ] ); |
| 24 |
|
| 25 |
// A failed renewal suspends the subscription rather than ending it, so the |
| 26 |
// role has to come down here too. Nothing listened to this before, which |
| 27 |
// left a customer who stopped paying holding their subscriber role for the |
| 28 |
// whole retry ladder. Reversible: a recovered payment fires |
| 29 |
// `subscrpt_subscription_activated`, which puts the role back. |
| 30 |
add_action( 'subscrpt_subscription_on_hold', [ $this, 'maybe_change_user_role_on_subscription_deactivation' ] ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get default active role |
| 35 |
*/ |
| 36 |
public static function get_default_active_role(): string { |
| 37 |
$default_active_role = get_option( 'wp_subscription_active_role' ); |
| 38 |
|
| 39 |
// Migrate legacy option if needed. |
| 40 |
if ( empty( $default_active_role ) ) { |
| 41 |
$default_active_role = get_option( 'subscrpt_active_role', 'subscriber' ); |
| 42 |
update_option( 'wp_subscription_active_role', $default_active_role ); |
| 43 |
} |
| 44 |
|
| 45 |
return $default_active_role; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get default inactive role |
| 50 |
*/ |
| 51 |
public static function get_default_inactive_role(): string { |
| 52 |
$default_inactive_role = get_option( 'wp_subscription_unactive_role' ); // ? Yeap! it is a typo from the ancient times. Too lazy to change! |
| 53 |
|
| 54 |
// Migrate legacy option if needed. |
| 55 |
if ( empty( $default_inactive_role ) ) { |
| 56 |
$default_inactive_role = get_option( 'subscrpt_unactive_role', 'customer' ); |
| 57 |
update_option( 'wp_subscription_unactive_role', $default_inactive_role ); |
| 58 |
} |
| 59 |
|
| 60 |
return $default_inactive_role; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Maybe change user role on subscription activation |
| 65 |
* |
| 66 |
* @param int $subscription_id Subscription ID. |
| 67 |
*/ |
| 68 |
public function maybe_change_user_role_on_subscription_activation( $subscription_id ) { |
| 69 |
// Get the subscription owner's user ID |
| 70 |
$user_id = get_post_field( 'post_author', (int) $subscription_id ); |
| 71 |
|
| 72 |
if ( ! $user_id ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$user = new \WP_User( $user_id ); |
| 77 |
|
| 78 |
// Don't change roles for administrators |
| 79 |
if ( in_array( 'administrator', $user->roles ?? [], true ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$default_active_role = self::get_default_active_role(); |
| 84 |
|
| 85 |
$user->set_role( $default_active_role ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Maybe change user role on subscription deactivation |
| 90 |
* |
| 91 |
* @param int $subscription_id Subscription ID. |
| 92 |
*/ |
| 93 |
public function maybe_change_user_role_on_subscription_deactivation( $subscription_id ) { |
| 94 |
// Get the subscription owner's user ID |
| 95 |
$user_id = get_post_field( 'post_author', (int) $subscription_id ); |
| 96 |
|
| 97 |
if ( ! $user_id ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$user = new \WP_User( $user_id ); |
| 102 |
|
| 103 |
// Don't change roles for administrators |
| 104 |
if ( in_array( 'administrator', $user->roles ?? [], true ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$args = [ |
| 109 |
'author' => $user_id, |
| 110 |
'status' => 'active', |
| 111 |
]; |
| 112 |
$all_subscriptions = Helper::get_subscriptions( $args ); |
| 113 |
|
| 114 |
// Don't change role if user has other active subscriptions |
| 115 |
if ( count( $all_subscriptions ) > 0 ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
$default_inactive_role = self::get_default_inactive_role(); |
| 120 |
|
| 121 |
$user->set_role( $default_inactive_role ); |
| 122 |
} |
| 123 |
} |
| 124 |
|