/
required-hooks.php
143 lines (112 loc) · 3.78 KB
/
required-hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* File Description
*
* @author timothybjacobs
* @since 7/28/14
*/
/**
* Assign the selected role to the transaction customer
*
* @param $transaction_id int
* @param $store_old boolean
*/
function it_exchange_wpr_assign_role( $transaction_id, $store_old = true ) {
if ( ! it_exchange_transaction_is_cleared_for_delivery( $transaction_id ) ) {
return;
}
$user_id = it_exchange_get_transaction_customer_id( $transaction_id );
$user = get_user_by( 'id', $user_id );
foreach ( it_exchange_get_transaction_products( $transaction_id ) as $product ) {
$product_id = $product['product_id'];
if ( it_exchange_product_has_feature( $product_id, 'wp-roles' ) ) {
if ( $store_old ) {
update_user_meta( $user_id, '_it_exchange_wpr_prev_role', $user->roles[0] );
}
$role_slug = it_exchange_get_product_feature( $product_id, 'wp-roles' );
$user->set_role( $role_slug );
return;
}
}
}
/**
* Revert user's role to previous role
* before it was set
*
* @param $transaction_id int
*/
function it_exchange_wpr_revert_role( $transaction_id ) {
$user_id = it_exchange_get_transaction_customer_id( $transaction_id );
$user = get_user_by( 'id', $user_id );
$prev_role = get_user_meta( $user_id, '_it_exchange_wpr_prev_role', true );
if ( empty( $prev_role ) ) {
$prev_role = get_option( 'default_role', 'subscriber' );
}
$user->set_role( $prev_role );
}
/**
* When a transaction is made that is cleared for delivery
* assign requested roles.
*
* @param $transaction_id int
*/
function it_exchange_wpr_assign_role_on_transaction_success( $transaction_id ) {
if ( ! it_exchange_transaction_is_cleared_for_delivery( $transaction_id ) ) {
return;
}
it_exchange_wpr_assign_role( $transaction_id );
}
add_action( 'it_exchange_add_transaction_success', 'it_exchange_wpr_assign_role_on_transaction_success' );
/**
* When a transaction is transitioned from cleared to not cleared,
* assign requested roles.
*
* @param $transaction IT_Exchange_Transaction
* @param $old_status string
* @param $old_status_cleared bool
*/
function it_exchange_wpr_assign_role_on_cleared_for_delivery( $transaction, $old_status, $old_status_cleared ) {
$old_status_cleared = (bool) $old_status_cleared;
/*
* We have to re-pull the transaction from the database,
* so that the transaction status is properly determined
*/
$transaction = it_exchange_get_transaction( $transaction->ID );
/*
* If the transaction is cleared for delivery now,
* and the transaction wasn't cleared before this transition,
* assign the requested role.
*/
if ( it_exchange_transaction_is_cleared_for_delivery( $transaction ) && $old_status_cleared == false ) {
it_exchange_wpr_assign_role( $transaction->ID );
return;
}
/*
* If the transaction is no longer cleared for delivery,
* and the transaction was previously cleared,
* revert to previous role.
*/
if ( it_exchange_transaction_is_cleared_for_delivery( $transaction ) == false && $old_status_cleared == true ) {
it_exchange_wpr_revert_role( $transaction->ID );
return;
}
}
add_action( 'it_exchange_update_transaction_status', 'it_exchange_wpr_assign_role_on_cleared_for_delivery', 10, 3 );
/**
* When a subscriber status is changed,
* update the roles.
*
* @param $transaction IT_Exchange_Transaction
* @param $status string
*/
function it_exchange_wpr_assign_role_on_subscriber_status_change( $transaction, $status ) {
if ( $status == 'deactivated' || $status == 'cancelled' ) {
it_exchange_wpr_revert_role( $transaction->ID );
return;
}
if ( $status == 'active' ) {
it_exchange_wpr_assign_role( $transaction->ID, false );
return;
}
}
add_action( 'it_exchange_recurring_payments_addon_update_transaction_subscriber_status', 'it_exchange_wpr_assign_role_on_subscriber_status_change', 10, 2 );