This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-vip-support-cli.php
123 lines (100 loc) · 2.87 KB
/
class-vip-support-cli.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
<?php
/**
* CLI commands
*/
namespace Automattic\VIP\Support_User;
use WP_CLI_Command;
/**
* Implements a WP CLI command that converts guid users to meta users
* Class command
*
* @package a8c\vip_support
*/
class Command extends WP_CLI_Command {
/**
* Creates a user in the VIP Support role, already verified,
* and suppresses all emails.
*
* @subcommand create-user
*
* @synopsis <user-login> <user-email> <user-pass> [--display-name=<display-name>]
*
* ## EXAMPLES
*
* wp vipsupport create-user username user@domain.tld user_password --display-name="display name"
*
*/
public function add_support_user( $args, $assoc_args ) {
$user_login = $args[0];
$user_email = $args[1];
$user_pass = $args[2];
$display_name = $assoc_args['display-name'];
// @TODO Check the email address is an A8c domain, will need to convert the method to static
if ( ! is_email( $user_email ) ) {
\WP_CLI::error( "User cannot be added as '{$user_email}' is not a valid email address" );
}
$user_data = array();
$user_data['user_pass'] = $user_pass;
$user_data['user_login'] = $user_login;
$user_data['user_email'] = $user_email;
$user_data['display_name'] = $display_name;
$user_id = User::add( $user_data );
if ( is_wp_error( $user_id ) ) {
\WP_CLI::error( $user_id );
}
$msg = "Added user $user_id with login {$user_login}, they are verified as a VIP Support user and ready to go";
\WP_CLI::success( $msg );
}
/**
* Removes a user in the VIP Support role.
*
* @subcommand remove-user
*
* @synopsis <user-email>
*
* ## EXAMPLES
*
* wp vipsupport remove-user user@domain.tld
*
*/
public function remove_support_user( $args, $assoc_args ) {
$user_email = $args[0];
$success = User::remove( $user_email );
if ( is_wp_error( $success ) ) {
\WP_CLI::error( $success->get_error_message() );
}
\WP_CLI::success( "Deleted user with email {$user_email}" );
}
/**
* Marks the user with the provided ID as having a verified email.
*
*
* <user-id>
* : The WP User ID to mark as having a verified email address
*
* @subcommand verify
*
* ## EXAMPLES
*
* wp vipsupport verify 99
*
*/
public function verify( $args ) {
$user_id = absint( $args[0] );
if ( ! $user_id ) {
\WP_CLI::error( "Please provide the ID of the user to verify" );
}
$user = get_user_by( 'id', $user_id );
if ( ! $user ) {
\WP_CLI::error( "Could not find a user with ID $user_id" );
}
// If this is a multisite, commence super powers!
if ( is_multisite() ) {
grant_super_admin( $user->ID );
}
User::init()->mark_user_email_verified( $user->ID, $user->user_email );
// Print a success message
\WP_CLI::success( "Verified user $user_id with email {$user->user_email}, you can now change their role to VIP Support" );
}
}
\WP_CLI::add_command( 'vipsupport', __NAMESPACE__ . '\Command' );