PluginProbe
WP-Members Membership Plugin / trunk
WP-Members Membership Plugin vtrunk
trunk 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.4.2 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.4.9.1 3.4.9.2 3.4.9.3 3.4.9.4 3.4.9.5 3.4.9.6 3.4.9.7 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 All 34 releases
wp-members / includes / cli / class-wp-members-cli-user.php

class-wp-members-cli-user.php in WP-Members Membership Plugin trunk, at includes/cli/class-wp-members-cli-user.php

530 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( defined( 'WP_CLI' ) && WP_CLI ) {
3
4 class WP_Members_CLI_User {
5 function __construct() {
6 // Need the admin api for some CLI commands.
7 global $wpmem;
8 require_once $wpmem->path . 'includes/admin/api.php';
9 }
10
11 /**
12 * CLI command to activate users.
13 *
14 * ## OPTIONS
15 *
16 * [--id=<user_id>]
17 * : Activate user by user ID.
18 *
19 * [--login=<user_login>]
20 * : Activate user by login (username).
21 *
22 * [--email=<user_email>]
23 * : Activate user by email.
24 *
25 * [--notify=<boolean>]
26 * : Whether to send notifcation to user (true if omitted).
27 *
28 * [--all]
29 * : Activates all pending users.
30 *
31 * [--deactivated]
32 * : Activates all deactivated users (instead of pending) when using --all.
33 *
34 * @since 3.3.5
35 */
36 public function activate( $args, $assoc_args ) {
37
38 if ( ! wpmem_is_enabled( 'mod_reg' ) ) {
39 WP_CLI::error( 'Moderated registration is not enabled in WP-Members options.' );
40 }
41
42 if ( ! isset( $assoc_args['all'] ) ) {
43 if ( ! isset( $assoc_args['id'] ) && ! isset( $assoc_args['login'] ) && ! isset( $assoc_args['email'] ) ) {
44 WP_CLI::error( 'Missing required parameter. Specify user ID with --id=<user_id> or --all to activate all users.' );
45 }
46 }
47
48 // Sending notification? (sends notification if omitted).
49 $notify = ( isset( $assoc_args['notify'] ) && 'false' == $assoc_args['notify'] ) ? false : true;
50
51 // Are we doing all or individual?
52 if ( isset( $assoc_args['all'] ) ) {
53 if ( isset( $assoc_args['deactivated'] ) ) {
54 WP_CLI::confirm( 'This will set all deactivated users as activated. Are you sure?' );
55 } else {
56 WP_CLI::confirm( 'This will set all pending users as activated. Are you sure?' );
57 }
58 $users = ( isset( $assoc_args['deactivated'] ) ) ? wpmem_get_deactivated_users() : wpmem_get_pending_users();
59 foreach ( $users as $user ) {
60 wpmem_activate_user( $user, $notify );
61 }
62 WP_CLI::success( 'Activated ' . count( $users ) . ' users.' );
63 return;
64 }
65
66 // If doing an individual user (wpmem_cli_get_user() throws its own error if invalid).
67 $user = wpmem_cli_get_user( $assoc_args );
68
69 // Is the user already activated?
70 if ( false == wpmem_is_user_activated($user->ID ) ) {
71 // If valid user and not activated, activate.
72 wpmem_activate_user( $user->ID, $notify );
73 WP_CLI::success( 'User activated.' );
74 if ( $notify ) {
75 WP_CLI::success( 'Email notification sent to user.' );
76 }
77 } else {
78 WP_CLI::error( 'User is already activated.' );
79 }
80 }
81
82 /**
83 * CLI command to deactivate users.
84 *
85 * ## OPTIONS
86 *
87 * [--id=<user_id>]
88 * : Deactivate user by user ID.
89 *
90 * [--login=<user_login>]
91 * : Dectivate user by login (username).
92 *
93 * [--email=<user_email>]
94 * : Dectivate user by email.
95 *
96 * [--all]
97 * : Deactivates all activated users (automatically excludes admin role).
98 *
99 * [--admin]
100 * : Include admin role accounts when deactivating all users.
101 *
102 * @since 3.3.5
103 */
104 public function deactivate( $args, $assoc_args ) {
105
106 if ( ! wpmem_is_enabled( 'mod_reg' ) ) {
107 WP_CLI::error( 'Moderated registration is not enabled in WP-Members options.' );
108 }
109
110 if ( ! isset( $assoc_args['all'] ) ) {
111 if ( ! isset( $assoc_args['id'] ) && ! isset( $assoc_args['login'] ) && ! isset( $assoc_args['email'] ) ) {
112 WP_CLI::error( 'Missing required parameter. Specify user ID with --id=<user_id> or --all to deactivate all users.' );
113 }
114 }
115
116 // Are we doing all or individual?
117 if ( isset( $assoc_args['all'] ) ) {
118 if ( isset( $assoc_args['admin'] ) ) {
119 WP_CLI::confirm( 'This will set all activated users (including administrators) as deactivated. Are you sure?' );
120 } else {
121 WP_CLI::confirm( 'This will set all activated users as deactivated. Are you sure?' );
122 }
123
124 $users = wpmem_get_activated_users();
125 $x = 0;
126 foreach ( $users as $user ) {
127 $user_roles = wpmem_get_user_role( $user, true ); // To make sure we don't deactivate admins unintentionally.
128 if ( isset( $assoc_args['admin'] ) ) {
129 wpmem_deactivate_user( $user );
130 $x++;
131 } else {
132 if ( ! in_array( 'administrator', $user_roles ) ) {
133 wpmem_deactivate_user( $user );
134 $x++;
135 }
136 }
137 }
138 WP_CLI::success( 'Dectivated ' . $x . ' users.' );
139 return;
140 }
141
142 // If doing an individual user (wpmem_cli_get_user() throws its own error if invalid).
143 $user = wpmem_cli_get_user( $assoc_args );
144 wpmem_deactivate_user( $user->ID );
145 WP_CLI::success( 'User deactivated.' );
146 }
147
148 /**
149 * Lists users by activation state.
150 *
151 * ## OPTIONS
152 *
153 * <pending|activated|deactivated|confirmed|unconfirmed|memberships>
154 * : status of the user
155 *
156 * [--id=<user_id>]
157 * : User ID if listing memberships.
158 *
159 * @subcommand list
160 *
161 * @since 3.3.5
162 */
163 public function list_users( $args, $assoc_args ) {
164
165 // Accepted list args.
166 $accepted = array( 'pending', 'activated', 'deactivated', 'confirmed', 'unconfirmed', 'memberships', 'membership' );
167
168 $status = $args[0];
169
170 if ( ( 'memberships' == $status || 'membership' == $status ) && ! isset( $assoc_args['id'] ) ) {
171 WP_CLI::error( 'Listing user memberships requires a user ID as [--id]' );
172 }
173
174 switch ( $status ) {
175 case 'pending':
176 $users = wpmem_get_pending_users();
177 break;
178 case 'activated':
179 $users = wpmem_get_activated_users();
180 break;
181 case 'deactivated':
182 $users = wpmem_get_deactivated_users();
183 break;
184 case 'confirmed':
185 $users = wpmem_get_confirmed_users();
186 break;
187 case 'unconfirmed':
188 $users = wpmem_get_users_by_meta( '_wpmem_user_confirmed', false );
189 break;
190 case 'memberships':
191 case 'membership':
192 $user_id = $assoc_args['id'];
193 $memberships = wpmem_get_user_memberships( $user_id );
194 break;
195 }
196
197 if ( 'memberships' == $status || 'membership' == $status ) {
198
199 foreach ( $memberships as $key => $time ) {
200
201 $expires = rktgk_format_date( $time );
202 $days_to_go = wpmem_get_user_time_remaining( $key, $user_id );
203
204 $list[] = array(
205 'membership' => wpmem_get_membership_name( $key ),
206 'meta' => $key,
207 'expires' => $expires,
208 'remaining' => $days_to_go . ' days',
209 );
210 }
211
212 $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'membership', 'meta', 'expires', 'remaining' ) );
213 $formatter->display_items( $list );
214
215 } else {
216
217 if ( ! empty( $users ) ) {
218 foreach ( $users as $user_id ) {
219 $user = get_userdata( $user_id );
220 $list[] = array(
221 'ID' => $user->ID,
222 'username' => $user->user_login,
223 'email' => $user->user_email,
224 'status' => $status,
225 );
226 }
227
228 $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'ID', 'username', 'email', 'status' ) );
229 $formatter->display_items( $list );
230 } else {
231 WP_CLI::line( sprintf( 'Currently there are no %s users.', $status ) );
232 }
233 }
234 }
235
236 /**
237 * Gets detail of requested user.
238 *
239 * ## OPTIONS
240 *
241 * [--id]
242 * : Get user by ID.
243 *
244 * [--login]
245 * : Get user by user login.
246 *
247 * [--email]
248 * : Get user by user email.
249 *
250 * [--all]
251 * : Gets all user meta.
252 *
253 * @subcommand get-user-by
254 * @since 3.3.5
255 *
256 * @todo Needs debugging.
257 */
258 public function detail( $args, $assoc_args ) {
259 // is user by id, email, or login
260 $user = wpmem_cli_get_user( $assoc_args );
261 if ( ! $user ) {
262 WP_CLI::error( 'User does not exist. Try wp user list' );
263 }
264 $all = ( isset( $assoc_args['all'] ) ) ? true : false;
265 $this->display_user_detail( $user, $all );
266 }
267
268 /**
269 * Manually set a user as confirmed.
270 *
271 * ## OPTIONS
272 *
273 * [--id=<user_id>]
274 * : Confirm user by user ID.
275 *
276 * [--login=<user_login>]
277 * : Confirm user by login (username).
278 *
279 * [--email=<user_email>]
280 * : Confirm user by email.
281 *
282 * [--all]
283 * : Marks all users as confirmed.
284 *
285 * @since 3.3.5
286 */
287 public function confirm( $args, $assoc_args ) {
288 if ( ! wpmem_is_enabled( 'act_link' ) ) {
289 WP_CLI::error( 'User confirmation is not enabled in WP-Members options.' );
290 }
291
292 if ( ! isset( $assoc_args['all'] ) ) {
293 if ( ! isset( $assoc_args['id'] ) && ! isset( $assoc_args['login'] ) && ! isset( $assoc_args['email'] ) ) {
294 WP_CLI::error( 'Missing required parameter. Specify user ID with --id=<user_id> or --all to confirm all users.' );
295 }
296 }
297
298 // Are we doing all or individual?
299 if ( isset( $assoc_args['all'] ) ) {
300 WP_CLI::confirm( 'This will set all unconfirmed users as confirmed. Are you sure?' );
301 // Get all unconfirmed users.
302 $users = wpmem_get_users_by_meta( '_wpmem_user_confirmed', false );
303 if ( $users ) {
304 foreach ( $users as $user ) {
305 wpmem_set_user_as_confirmed( $user );
306 }
307 WP_CLI::success( 'Marked ' . count( $users ) . ' users as confirmed.' );
308 return;
309 } else {
310 WP_CLI::error( 'There are no unconfirmed users' );
311 }
312 } else {
313 // If doing an individual user (wpmem_cli_get_user() throws its own error if invalid).
314 $user = wpmem_cli_get_user( $assoc_args );
315 if ( $user ) {
316 wpmem_set_user_as_confirmed( $user->ID );
317 WP_CLI::success( 'User confirmed' );
318 }
319 }
320 }
321
322 /**
323 * Manually set a user as unconfirmed.
324 *
325 * ## OPTIONS
326 *
327 * [--id=<user_id>]
328 * : Unonfirm user by user ID.
329 *
330 * [--login=<user_login>]
331 * : Unonfirm user by login (username).
332 *
333 * [--email=<user_email>]
334 * : Unonfirm user by email.
335 *
336 * [--all]
337 * : Marks all users as unconfirmed.
338 *
339 * @since 3.3.5
340 */
341 public function unconfirm( $args, $assoc_args ) {
342 if ( ! wpmem_is_enabled( 'act_link' ) ) {
343 WP_CLI::error( 'User confirmation is not enabled in WP-Members options.' );
344 }
345
346 if ( ! isset( $assoc_args['all'] ) ) {
347 if ( ! isset( $assoc_args['id'] ) && ! isset( $assoc_args['login'] ) && ! isset( $assoc_args['email'] ) ) {
348 WP_CLI::error( 'Missing required parameter. Specify user ID with --id=<user_id> or --all to confirm all users.' );
349 }
350 }
351
352 // Are we doing all or individual?
353 if ( isset( $assoc_args['all'] ) ) {
354 WP_CLI::confirm( 'This will set all confirmed users as unconfirmed. Are you sure?' );
355 $users = wpmem_get_confirmed_users();
356 if ( $users ) {
357 $x=0;
358 foreach ( $users as $user ) {
359 $user_roles = wpmem_get_user_role( $user, true ); // To make sure we don't deactivate admins unintentionally.
360 if ( ! in_array( 'administrator', $user_roles ) ) {
361 wpmem_set_user_as_unconfirmed( $user );
362 $x++;
363 }
364 }
365 WP_CLI::success( 'Marked ' . $x . ' users as unconfirmed.' );
366 return;
367 } else {
368 WP_CLI::error( 'There are no confirmed users' );
369 }
370 } else {
371 // If doing an individual user (wpmem_cli_get_user() throws its own error if invalid).
372 $user = wpmem_cli_get_user( $assoc_args );
373 if ( $user ) {
374 wpmem_set_user_as_unconfirmed( $user->ID );
375 WP_CLI::success( 'User unconfirmed' );
376 }
377 }
378 }
379
380 /**
381 * Gets the role of a user.
382 *
383 * [--id=<user_id>]
384 * : The WP ID of the user.
385 *
386 * [--login=<user_login>]
387 * : The WP username of the user.
388 *
389 * [--email=<user_email>]
390 * : The user email.
391 *
392 * [--all]
393 * :
394 *
395 * @subcommand get-role
396 */
397 public function get_role( $args, $assoc_args ) {
398
399 $user = wpmem_cli_get_user( $assoc_args );
400
401 $all = ( isset( $assoc_args['all'] ) ) ? true : false;
402 $role = wpmem_get_user_role( $user->ID, $all );
403 if ( is_array( $role ) ) {
404 foreach ( $role as $r ) {
405 $list[] = array(
406 'roles' => $r,
407 );
408 }
409 WP_CLI::line( 'Displaying all user roles for the following user:' );
410 WP_CLI::line( 'Username' . ': ' . $user->user_login );
411 WP_CLI::line( 'Email' . ': ' . $user->user_email );
412 $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'roles' ) );
413 $formatter->display_items( $list );
414 } else {
415 WP_CLI::line( sprintf( 'User role: %s', $role ) );
416 }
417 }
418
419 /**
420 * @subcommand set-membership
421 */
422 public function set_membership( $args, $assoc_args ) {
423
424 // is user by id, email, or login
425 $user_by = ( isset( $assoc_args['user_by'] ) ) ? $assoc_args['user_by'] : 'login';
426 $user = get_user_by( $user_by, $args[0] );
427 if ( empty( $user ) || ! $user ) {
428 WP_CLI::error( 'User does not exist. Try wp user list' );
429 }
430
431 $membership = $assoc_args['key'];
432
433 $date = ( isset( $assoc_args['date'] ) ) ? $assoc_args['date'] : false;
434
435 wpmem_set_user_membership( $membership, $user->ID, $date );
436
437 WP_CLI::line( sprintf( 'Set %s membership for user %s', $membership, $user->user_login ) );
438 }
439
440 /**
441 * Displays a given users expiration date for a specific membership.
442 *
443 * --membership=<meta_key>
444 * : The membership to check.
445 *
446 * --id=<user_id>
447 * : The user ID to check.
448 *
449 * [--format=<date_format>]
450 * : Date format to display (defaults to WP date format setting).
451 */
452 public function expires( $args, $assoc_args ) {
453 $product_key = $assoc_args['membership'];
454 $user_id = $assoc_args['id'];
455 $format = ( isset( $assoc_args['format'] ) ) ? $assoc_args['format'] : get_option( 'date_format' );
456 $expires = wpmem_get_user_expiration( $product_key, $user_id, $format );
457 WP_CLI::line( sprintf( 'User membership for %s expires %s.', wpmem_get_membership_name( $product_key ), $expires ) );
458 }
459
460 /**
461 * Displays the user's remaining time for a specific membership.
462 *
463 * --membership=<meta_key>
464 * : The membership to check.
465 *
466 * --id=<user_id>
467 * : The user ID to check.
468 *
469 * [--interval=<days|months>]
470 * : Date interval displayed as result (defaults to "days").
471 */
472 public function remaining( $args, $assoc_args ) {
473 $product_key = $assoc_args['membership'];
474 $user_id = $assoc_args['id'];
475 $interval = ( isset( $assoc_args['interval'] ) ) ? $interval : 'days';
476 $remaining = wpmem_get_user_time_remaining( $product_key, $user_id, $interval );
477 WP_CLI::line( sprintf( 'User %s has %s %s remaining for %s.', $user_id, $remaining, $interval, wpmem_get_membership_name( $product_key ) ) );
478 }
479
480 /**
481 * Gets prorated value of user's remaining time for a specific membership.
482 *
483 * --membership=<meta_key>
484 * : The membership to check.
485 *
486 * --id=<user_id>
487 * : The user ID to check.
488 *
489 * --value=<price>
490 * : The full value of the membership to prorate.
491 *
492 * [--interval=<days|months>]
493 * : Date interval displayed as result (defaults to "days").
494 */
495 public function prorate( $args, $assoc_args ) {
496 $product_key = $assoc_args['membership'];
497 $user_id = $assoc_args['id'];
498 $value = $assoc_args['value'];
499 $interval = ( isset( $assoc_args['interval'] ) ) ? $assoc_args['interval'] : 'days';
500 $val_remaining = wpmem_prorate_membership( $product_key, $user_id, $value, $interval );
501 WP_CLI::line( sprintf( 'Remaining value of %s for user ID %s is %s', wpmem_get_membership_name( $product_key ), $user_id, round( $val_remaining, 2 ) ) );
502 }
503
504 /**
505 * Handles user detail display.
506 *
507 * @since 3.3.5
508 *
509 * @param object $user
510 * @param $all
511 */
512 private function display_user_detail( $user, $all ) {
513
514 WP_CLI::line( sprintf( 'User: %s', $user->user_email) );
515
516 $values = wpmem_user_data( $user->ID, $all );
517 foreach ( $values as $key => $meta ) {
518 $list[] = array(
519 'meta' => $key,
520 'value' => $meta,
521 );
522 }
523
524 $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'meta', 'value' ) );
525
526 $formatter->display_items( $list );
527 }
528 }
529 }
530 WP_CLI::add_command( 'mem user', 'WP_Members_CLI_User' );