forked from wp-e-commerce/wpec-members
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpec_members_access_admin.php
365 lines (285 loc) · 14.4 KB
/
wpec_members_access_admin.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/*
* This file contains all the admin functions required to add edit and update users / members and subscriptions.
* This file is divided into thre parts - Members / user functions, subscription functions and then general functions.
*/
/* User functions */
/*
This function will remove the capability from
users if the subscription is deleted
*/
function wpec_members_remove_capability_from_users( $capability ) {
$users = new WP_User_Search( '', '', $capability );
$user_ids = $users->results;
foreach ($user_ids as $user_id) {
$user = new WP_User( $user_id );
$user->remove_cap( $capability );
$members_lengths = get_user_meta( $user_id, '_subscription_ends', true );
$subscription_lengths = get_user_meta( $user_id, '_subscription_length', true );
$subscription_starts = get_user_meta( $user_id, '_subscription_starts', true );
unset( $subscription_lengths[ $capability ] );
unset( $members_lengths[ $capability ] );
unset( $subscription_starts[ $capability ] );
//if the array is now empty then we know they only had one sub
if ( empty( $members_lengths ) ){
delete_user_meta( $user_id, '_subscription_starts' );
delete_user_meta( $user_id, '_subscription_ends' );
delete_user_meta( $user_id, '_subscription_length' );
delete_user_meta( $user_id, '_has_current_subscription' );
} else {
update_user_meta( $user_id, '_subscription_starts', $subscription_starts );
update_user_meta( $user_id, '_subscription_ends', $members_lengths );
update_user_meta( $user_id, '_subscription_length', $subscription_lengths );
}
}
}
/* This will remove all subscriptions from a user */
function wpec_members_remove_all_capabilities( $user_id ) {
global $wp_roles;
$remove_user = new WP_User( $user_id );
$remove_user = sanitize_user_object( $remove_user, 'display' );
$user = get_userdata( $user_id );
$to = $user->user_email;
wpec_members_mail_subscription_end_notification( $to );
foreach ( $remove_user->caps as $cap => $value ) {
if ( ! $wp_roles->is_role( $cap ) )
$remove_user->remove_cap( $cap );
};
if ( count( $remove_user->caps ) > 1 ) {
$remove_user->remove_role( 'subscriber' );
} elseif ( count( $remove_user->caps ) < 1 ) {
$remove_user->add_role( 'subscriber' );
};
delete_user_meta( $user_id, '_subscription_ends' );
delete_user_meta( $user_id, '_subscription_length' );
delete_user_meta( $user_id, '_subscription_starts' );
delete_user_meta( $user_id, '_has_current_subscription' );
wpec_members_display_update( __('Removed ok.', 'wpsc_members') );
}
// Function Saves the capabilitites to the user (Both bulk add and single add)
function wpec_members_save_user( $user_id, $length, $role ) {
$add_user = new WP_User( $user_id );
error_log( "wpec_members_save_user" );
//$members_lengths = array();
$members_lengths = get_user_meta( $user_id, '_subscription_ends', true );
$members_starts = get_user_meta( $user_id, '_subscription_starts', true );
$future_time = mktime( date('h'), date('m'), date('s') + $length, date('m'), ( date('d') ), date('Y') );
$current_time = time();
$members_lengths[ $role ]= $future_time;
$members_starts[ $role ]= $current_time;
$add_user->add_cap( $role, true );
//$subscription_lengths = array();
$subscription_lengths = get_user_meta( $user_id, '_subscription_length', true );
$subscription_lengths[ $role ]= $length;
// dont think we need this line...
$add_user->add_role( 'subscriber' );
update_user_meta( $user_id, '_subscription_ends', $members_lengths );
update_user_meta( $user_id,'_subscription_length', $subscription_lengths );
update_user_meta( $user_id,'_subscription_starts', $members_starts );
update_user_meta( $user_id,'_has_current_subscription', 'true' );
}
function wpec_members_mail_subscription_end_notification( $to ) {
$subscription_end_email = get_site_option( 'subscription_end_email' );
$subscription_end_email_subject = get_site_option( 'subscription_end_email_subject' );
$subject = $subscription_end_email_subject;
$message = $subscription_end_email;
wp_mail( $to, $subject, $message );
}
// as we now allow a bulk update and can update all the caps at once we are
// best to run the remove all function then add the users cap back
//possible bug: subscription starts was not updated in the previous version nor this one??
function wpec_members_edit_user() {
if ( ! isset( $_REQUEST['page'] ) || $_REQUEST['page'] != 'wpec_members' )
return;
$length = $_POST['length'];
$user_id = $_POST['user_id'];
$roles = $_POST['roles'];
if ( $length == 0 ) { // Subscription Deactivated
// cancel the subscription locally
wpec_members_remove_all_capabilities( $user_id );
update_user_meta( $user_id, '_subscription_canceled', true );
} else {
//exit('<pre>'.print_r($_POST,1)."\n".print_r(get_user_meta($user_id, '_subscription_starts'), true).'</pre>');
$current_subscription_starts = get_user_meta( $user_id, '_subscription_starts' );
if ( empty($current_subscription_starts ) ) {
wp_safe_redirect( '?page=wpec_members' );
return;
}
foreach ( $current_subscription_starts[0] as $key => $value ) {
$current_role = $key;
$current_start = $value;
}
foreach ( $roles as $key => $value ) {
$current_role = $key;
$new_role = $value;
}
$new_subscription_ends = array();
$new_subscription_ends[ $new_role ] = (int) $current_start + (int) $length;
$new_subscription_starts = array();
$new_subscription_starts[ $new_role ] = $current_start;
$new_subscription_length = array();
$new_subscription_length[ $new_role ] = $length;
update_user_meta( $user_id, '_subscription_length', $new_subscription_length );
update_user_meta( $user_id, '_subscription_starts', $new_subscription_starts );
update_user_meta( $user_id, '_subscription_ends', $new_subscription_ends );
update_user_meta( $user_id, '_subscription_canceled', false );
}
wp_safe_redirect( '?page=wpec_members' );
//wpec_members_display_update('Subscriber Edited.');
}
function wpec_members_cancel_user_subscription() {
$user_id = $_GET['id'];
//delete all the old stuff first
wpec_members_remove_all_capabilities( $user_id );
delete_user_meta( $user_id, '_subscription_ends' );
delete_user_meta( $user_id, '_subscription_length' );
$user = get_userdata( $user_id );
$to = $user->user_email;
wpec_members_mail_subscription_end_notification( $to );
// cancel the subscription locally
update_user_meta( $user_id, '_subscription_canceled', true );
update_user_meta( $user_id, '_subscription_length', 0 );
update_user_meta( $user_id, '_subscription_ends', $subscription_ends );
wp_safe_redirect( '?page=wpec_members' );
}
/*
currently not implimented corretly into the importer table need to do this
but for now we will use the old bulk action importer save function refactored a
bit.
*/
function wpec_members_import_wp_users() {
///bulk option value 1 = add sub 2 = remove sub 3=remove allcapabilities else = 'no bulk option chosen'
$bulk_option = $_POST['bulkchange'];
$user_ids = $_POST['import_user'];
$subscription = $_POST['roles'];
$length = $_POST['length'];
switch ( $bulk_option ) {
case 1:
if ( empty( $user_ids ) || empty( $length ) || empty( $subscription ) ) {
wpec_members_display_error( __('You Must select Users, Subscription and a subscription duration to use this bulk option!', 'wpsc_members') );
return;
}
foreach ( $user_ids as $user_id )
wpec_members_save_user( $user_id, $length, $subscription );
//wpec_members_display_update('User\'s Subscriptions Added');
wp_safe_redirect( '?page=wpec_members' );
break;
case 2:
if ( empty($user_ids) ) {
wpec_members_display_error( __('You Must select some users in order to remove their subscriptions', 'wpsc_members') );
return;
}
foreach ( $user_ids as $user_id )
wpec_members_remove_all_capabilities( $user_id );
wp_safe_redirect( '?page=wpec_members' );
break;
default:
wpec_members_display_error( __('You must select a bulk option!', 'wpsc_members') );
}
}
/* Single add user function - should merge in with the import
function above which are both more validation functions*/
function wpec_members_single_add_wp_users() {
$user_id = $_POST['add_user_subscription'];
$subscription = $_POST['roles'];
$length = $_POST['length'];
if ( empty( $user_id ) || empty( $length ) || empty( $subscription ) ) {
wpec_members_display_error( __('You must select a subscription and duration to add this user.', 'wpsc_members') );
return;
}
wpec_members_save_user( $user_id, $length, $subscription );
//user added so redirrect back to the table
wp_safe_redirect( '?page=wpec_members' );
}
/* Subscription functions */
/* remove the subscription - need to put a warning message in here */
function wpec_members_delete_subscription( $capability ) {
global $wpsc_product_capability_list;
if ( isset( $wpsc_product_capability_list[ $capability ] ) ) {
unset( $wpsc_product_capability_list[ $capability ] );
update_option( 'wpsc_product_capability_list', $wpsc_product_capability_list );
wpec_members_remove_capability_from_users( $capability );
}
wpec_members_display_update( __('Subscription Deleted', 'wpsc_members') );
}
/* edit/add subscriptions functions */
function wpec_members_edit_subscriptions() {
global $wpdb, $wpsc_product_capability_list;
check_admin_referer( 'edit-capability', 'wpsc-edit-capability' );
//exit('<pre>'.print_r($_POST,1).'</pre>');
$submitted_capability_data = $_POST['capability_list'];
//check user has filled out required info before saving
if ( $_POST['page_action'] == 'add' && ! empty( $_POST['capability_list']['new']['capability'] ) && ! empty( $_POST['capability_list']['new']['name'] ) ) {
$new_capability = $submitted_capability_data['new']['capability'];
$new_capability_item = array();
$new_capability_item[ $new_capability ]['name'] = $submitted_capability_data['new']['name'];
$new_capability_item[ $new_capability ]['message-details'] = $submitted_capability_data['new']['message-details'];
if ( is_array( $wpsc_product_capability_list ) )
$wpsc_product_capability_list = array_merge( $wpsc_product_capability_list, $new_capability_item );
else
if ( empty( $wpsc_product_capability_list ) )
$wpsc_product_capability_list = $new_capability_item;
else {
error_log( "There has been an error" );
$wpsc_product_capability_list = array_merge( $wpsc_product_capability_list, $new_capability_item );
}
} else {
foreach ( $submitted_capability_data as $submitted_capability_key => $submitted_capability_item ) {
if ( count( $wpsc_product_capability_list[ $submitted_capability_key ] ) < 2 ) {
$wpsc_product_capability_list[ $submitted_capability_key ] = array();
}
$wpsc_product_capability_list[ $submitted_capability_key ]['name'] = $submitted_capability_data[ $submitted_capability_key ]['name'];
$wpsc_product_capability_list[ $submitted_capability_key ]['message-details'] = $submitted_capability_data[ $submitted_capability_key ]['message-details'];
}
}
update_option( 'wpsc_product_capability_list', $wpsc_product_capability_list );
wp_safe_redirect( '?page=wpec_members&tab=wpec_manage_subscriptions' );
}
if ( isset ( $_REQUEST['wpsc_admin_action'] ) && $_REQUEST['wpsc_admin_action'] == 'capability_action' ) {
add_action( 'admin_init', 'wpec_members_edit_subscriptions' );
}
/* General Functions */
//creates the update css for all updates messages
function wpec_members_display_update( $message ) {
echo '<div class="updated"><p>' . $message . '</p></div>';
}
///used to display the css for all error messages
function wpec_members_display_error( $message ) {
echo '<div class="error"><p>' . $message . '</p></div>';
}
// save settings
function wpsc_members_save_settings() {
$_POST = array_map( 'stripslashes_deep', $_POST );
$subscription_end_email = $_POST['subscription_end_email'];
$subscription_recurring_email = $_POST['subscription_recurring_email'];
$subscription_end_email_subject = $_POST['subscription_end_email_subject'];
$subscription_recurring_email_subject = $_POST['subscription_recurring_email_subject'];
$ipn_error_notification_email = $_POST['ipn_error_notification_email'];
update_site_option( 'subscription_end_email', $subscription_end_email );
update_site_option( 'subscription_recurring_email', $subscription_recurring_email );
update_site_option( 'subscription_end_email_subject', $subscription_end_email_subject );
update_site_option( 'subscription_recurring_email_subject', $subscription_recurring_email_subject );
if ( is_email( $ipn_error_notification_email ) ) {
update_site_option( 'ipn_error_notification_email', $ipn_error_notification_email );
}
}
/*
this function will check for forms been filled out and will call the function for processing,
want to get rid of this and use all the bulk actions etc with the table. Or at least impliment it in a non ugly way
*/
function wpsc_members_check_for_form_updates() {
if ( isset( $_POST['action'] ) && $_POST['action'] == 'create_new' )
wpec_members_single_add_wp_users();
if ( isset( $_POST['action'] ) && $_POST['action'] == 'bulksave' )
wpec_members_import_wp_users();
if ( isset( $_POST['action'] ) && $_POST['action'] == 'update' )
wpec_members_edit_user();
if ( isset( $_POST['action'] ) && $_POST['action'] == 'update-settings' )
wpsc_members_save_settings();
}
add_action( 'admin_init', 'wpsc_members_check_for_form_updates' );
function wpsc_members_admin_css() {
wp_enqueue_style( 'wpsc_capabilities', WPSC_MEMBERS_URL . '/css-and-js/style.css');
}
add_action( 'admin_init', 'wpsc_members_admin_css');
?>