wcs-user-functions.php
11.2 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* WooCommerce Subscriptions User Functions
*
* @author Prospress
* @category Core
* @package WooCommerce Subscriptions/Functions
* @version 2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Give a user the Subscription's default subscriber role
*
* @since 2.0
*/
function wcs_make_user_active( $user_id ) {
wcs_update_users_role( $user_id, 'default_subscriber_role' );
}
/**
* Give a user the Subscription's default subscriber's inactive role
*
* @since 2.0
*/
function wcs_make_user_inactive( $user_id ) {
wcs_update_users_role( $user_id, 'default_inactive_role' );
}
/**
* Give a user the Subscription's default subscriber's inactive role if they do not have an active subscription
*
* @since 2.0
*/
function wcs_maybe_make_user_inactive( $user_id ) {
if ( ! wcs_user_has_subscription( $user_id, '', 'active' ) ) {
wcs_update_users_role( $user_id, 'default_inactive_role' );
}
}
/**
* Update a user's role to a special subscription's role
*
* @param int $user_id The ID of a user
* @param string $role_new The special name assigned to the role by Subscriptions, one of 'default_subscriber_role', 'default_inactive_role' or 'default_cancelled_role'
* @return WP_User The user with the new role.
* @since 2.0
*/
function wcs_update_users_role( $user_id, $role_new ) {
$user = new WP_User( $user_id );
// Never change an admin's role to avoid locking out admins testing the plugin
if ( ! empty( $user->roles ) && in_array( 'administrator', $user->roles ) ) {
return;
}
// Allow plugins to prevent Subscriptions from handling roles
if ( ! apply_filters( 'woocommerce_subscriptions_update_users_role', true, $user, $role_new ) ) {
return;
}
$roles = wcs_get_new_user_role_names( $role_new );
$role_new = $roles['new'];
$role_old = $roles['old'];
if ( ! empty( $role_old ) ) {
$user->remove_role( $role_old );
}
$user->add_role( $role_new );
do_action( 'woocommerce_subscriptions_updated_users_role', $role_new, $user, $role_old );
return $user;
}
/**
* Gets default new and old role names if the new role is 'default_subscriber_role'. Otherwise returns role_new and an
* empty string.
*
* @param $role_new string the new role of the user
* @return array with keys 'old' and 'new'.
*/
function wcs_get_new_user_role_names( $role_new ) {
$default_subscriber_role = get_option( WC_Subscriptions_Admin::$option_prefix . '_subscriber_role' );
$default_cancelled_role = get_option( WC_Subscriptions_Admin::$option_prefix . '_cancelled_role' );
$role_old = '';
if ( 'default_subscriber_role' == $role_new ) {
$role_old = $default_cancelled_role;
$role_new = $default_subscriber_role;
} elseif ( in_array( $role_new, array( 'default_inactive_role', 'default_cancelled_role' ) ) ) {
$role_old = $default_subscriber_role;
$role_new = $default_cancelled_role;
}
return array(
'new' => $role_new,
'old' => $role_old,
);
}
/**
* Check if a user has a subscription, optionally to a specific product and/or with a certain status.
*
* @param int (optional) The ID of a user in the store. If left empty, the current user's ID will be used.
* @param int (optional) The ID of a product in the store. If left empty, the function will see if the user has any subscription.
* @param string (optional) A valid subscription status. If left empty, the function will see if the user has a subscription of any status.
* @since 2.0
*/
function wcs_user_has_subscription( $user_id = 0, $product_id = '', $status = 'any' ) {
$subscriptions = wcs_get_users_subscriptions( $user_id );
$has_subscription = false;
if ( empty( $product_id ) ) { // Any subscription
if ( ! empty( $status ) && 'any' != $status ) { // We need to check for a specific status
foreach ( $subscriptions as $subscription ) {
if ( $subscription->get_status() == $status ) {
$has_subscription = true;
break;
}
}
} elseif ( ! empty( $subscriptions ) ) {
$has_subscription = true;
}
} else {
foreach ( $subscriptions as $subscription ) {
if ( $subscription->has_product( $product_id ) && ( empty( $status ) || 'any' == $status || $subscription->get_status() == $status ) ) {
$has_subscription = true;
break;
}
}
}
return apply_filters( 'wcs_user_has_subscription', $has_subscription, $user_id, $product_id, $status );
}
/**
* Gets all the active and inactive subscriptions for a user, as specified by $user_id
*
* @param int $user_id (optional) The id of the user whose subscriptions you want. Defaults to the currently logged in user.
* @since 2.0
*/
function wcs_get_users_subscriptions( $user_id = 0 ) {
if ( 0 === $user_id || empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$subscriptions = apply_filters( 'wcs_pre_get_users_subscriptions', array(), $user_id );
if ( empty( $subscriptions ) ) {
$post_ids = get_posts( array(
'posts_per_page' => -1,
'post_status' => 'any',
'post_type' => 'shop_subscription',
'orderby' => 'date',
'order' => 'desc',
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'meta_compare' => '=',
'fields' => 'ids',
) );
foreach ( $post_ids as $post_id ) {
$subscriptions[ $post_id ] = wcs_get_subscription( $post_id );
}
}
return apply_filters( 'wcs_get_users_subscriptions', $subscriptions, $user_id );
}
/**
* Return a link for subscribers to change the status of their subscription, as specified with $status parameter
*
* @param int $subscription_id A subscription's post ID
* @param string $status A subscription's post ID
* @param string $current_status A subscription's current status
* @since 1.0
*/
function wcs_get_users_change_status_link( $subscription_id, $status, $current_status = '' ) {
if ( '' === $current_status ) {
$subscription = wcs_get_subscription( $subscription_id );
if ( $subscription instanceof WC_Subscription ) {
$current_status = $subscription->get_status();
}
}
$action_link = add_query_arg( array( 'subscription_id' => $subscription_id, 'change_subscription_to' => $status ) );
$action_link = wp_nonce_url( $action_link, $subscription_id . $current_status );
return apply_filters( 'wcs_users_change_status_link', $action_link, $subscription_id, $status );
}
/**
* Check if a given user (or the currently logged in user) has permission to put a subscription on hold.
*
* By default, a store manager can put all subscriptions on hold, while other users can only suspend their own subscriptions.
*
* @param int|WC_Subscription $subscription An instance of a WC_Snbscription object or ID representing a 'shop_subscription' post
* @since 2.0
*/
function wcs_can_user_put_subscription_on_hold( $subscription, $user = '' ) {
$user_can_suspend = false;
if ( empty( $user ) ) {
$user = wp_get_current_user();
} elseif ( is_int( $user ) ) {
$user = get_user_by( 'id', $user );
}
if ( user_can( $user, 'manage_woocommerce' ) ) { // Admin, so can always suspend a subscription
$user_can_suspend = true;
} else { // Need to make sure user owns subscription & the suspension limit hasn't been reached
if ( ! is_object( $subscription ) ) {
$subscription = wcs_get_subscription( $subscription );
}
// Make sure current user owns subscription
if ( $user->ID == $subscription->get_user_id() ) {
// Make sure subscription suspension count hasn't been reached
$suspension_count = $subscription->suspension_count;
$allowed_suspensions = get_option( WC_Subscriptions_Admin::$option_prefix . '_max_customer_suspensions', 0 );
if ( 'unlimited' === $allowed_suspensions || $allowed_suspensions > $suspension_count ) { // 0 not > anything so prevents a customer ever being able to suspend
$user_can_suspend = true;
}
}
}
return apply_filters( 'wcs_can_user_put_subscription_on_hold', $user_can_suspend, $subscription );
}
/**
* Retrieve available actions that a user can perform on the subscription
*
* @since 2.0
*/
function wcs_get_all_user_actions_for_subscription( $subscription, $user_id ) {
$actions = array();
if ( user_can( $user_id, 'edit_shop_subscription_status', $subscription->id ) ) {
$admin_with_suspension_disallowed = ( current_user_can( 'manage_woocommerce' ) && '0' === get_option( WC_Subscriptions_Admin::$option_prefix . '_max_customer_suspensions', '0' ) ) ? true : false;
$current_status = $subscription->get_status();
if ( $subscription->can_be_updated_to( 'on-hold' ) && wcs_can_user_put_subscription_on_hold( $subscription, $user_id ) && ! $admin_with_suspension_disallowed ) {
$actions['suspend'] = array(
'url' => wcs_get_users_change_status_link( $subscription->id, 'on-hold', $current_status ),
'name' => __( 'Suspend', 'woocommerce-subscriptions' ),
);
} elseif ( $subscription->can_be_updated_to( 'active' ) && ! $subscription->needs_payment() ) {
$actions['reactivate'] = array(
'url' => wcs_get_users_change_status_link( $subscription->id, 'active', $current_status ),
'name' => __( 'Reactivate', 'woocommerce-subscriptions' ),
);
}
if ( wcs_can_user_resubscribe_to( $subscription, $user_id ) ) {
$actions['resubscribe'] = array(
'url' => wcs_get_users_resubscribe_link( $subscription ),
'name' => __( 'Resubscribe', 'woocommerce-subscriptions' ),
);
}
// Show button for subscriptions which can be cancelled and which may actually require cancellation (i.e. has a future payment)
if ( $subscription->can_be_updated_to( 'cancelled' ) && $subscription->get_time( 'next_payment' ) > 0 ) {
$actions['cancel'] = array(
'url' => wcs_get_users_change_status_link( $subscription->id, 'cancelled', $current_status ),
'name' => _x( 'Cancel', 'an action on a subscription', 'woocommerce-subscriptions' ),
);
}
}
return apply_filters( 'wcs_view_subscription_actions', $actions, $subscription );
}
/**
* Checks if a user has a certain capability
*
* @access public
* @param array $allcaps
* @param array $caps
* @param array $args
* @return bool
*/
function wcs_user_has_capability( $allcaps, $caps, $args ) {
if ( isset( $caps[0] ) ) {
switch ( $caps[0] ) {
case 'edit_shop_subscription_payment_method' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_payment_method'] = true;
}
break;
case 'edit_shop_subscription_status' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_status'] = true;
}
break;
case 'edit_shop_subscription_line_items' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_line_items'] = true;
}
break;
case 'switch_shop_subscription' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['switch_shop_subscription'] = true;
}
break;
case 'subscribe_again' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['subscribe_again'] = true;
}
break;
}
}
return $allcaps;
}
add_filter( 'user_has_cap', 'wcs_user_has_capability', 10, 3 );