-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathSignup_Command.php
More file actions
336 lines (292 loc) · 8.26 KB
/
Signup_Command.php
File metadata and controls
336 lines (292 loc) · 8.26 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
<?php
use WP_CLI\CommandWithDBObject;
use WP_CLI\Utils;
use WP_CLI\Fetchers\Signup as SignupFetcher;
/**
* Manages signups on a multisite installation.
*
* ## EXAMPLES
*
* # List signups.
* $ wp user signup list
* +-----------+------------+---------------------+---------------------+--------+------------------+
* | signup_id | user_login | user_email | registered | active | activation_key |
* +-----------+------------+---------------------+---------------------+--------+------------------+
* | 1 | bobuser | bobuser@example.com | 2024-03-13 05:46:53 | 1 | 7320b2f009266618 |
* | 2 | johndoe | johndoe@example.com | 2024-03-13 06:24:44 | 0 | 9068d859186cd0b5 |
* +-----------+------------+---------------------+---------------------+--------+------------------+
*
* # Activate signup.
* $ wp user signup activate 2
* Signup 2 activated. Password: bZFSGsfzb9xs
* Success: Activated 1 of 1 signups.
*
* # Delete signup.
* $ wp user signup delete 3
* Signup 3 deleted.
* Success: Deleted 1 of 1 signups.
*
* @package wp-cli
*/
class Signup_Command extends CommandWithDBObject {
protected $obj_type = 'signup';
protected $obj_id_key = 'signup_id';
protected $obj_fields = [
'signup_id',
'user_login',
'user_email',
'registered',
'active',
'activation_key',
];
private $fetcher;
public function __construct() {
$this->fetcher = new SignupFetcher();
}
/**
* Lists signups.
*
* ## OPTIONS
*
* [--<field>=<value>]
* : Filter the list by a specific field.
*
* [--field=<field>]
* : Prints the value of a single field for each signup.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - ids
* - json
* - count
* - yaml
* ---
*
* [--per_page=<per_page>]
* : Limits the signups to the given number. Defaults to none.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each signup:
*
* * signup_id
* * user_login
* * user_email
* * registered
* * active
* * activation_key
*
* These fields are optionally available:
*
* * domain
* * path
* * title
* * activated
* * meta
*
* ## EXAMPLES
*
* # List signup IDs.
* $ wp user signup list --field=signup_id
* 1
*
* # List all signups.
* $ wp user signup list
* +-----------+------------+---------------------+---------------------+--------+------------------+
* | signup_id | user_login | user_email | registered | active | activation_key |
* +-----------+------------+---------------------+---------------------+--------+------------------+
* | 1 | bobuser | bobuser@example.com | 2024-03-13 05:46:53 | 1 | 7320b2f009266618 |
* | 2 | johndoe | johndoe@example.com | 2024-03-13 06:24:44 | 0 | 9068d859186cd0b5 |
* +-----------+------------+---------------------+---------------------+--------+------------------+
*
* @subcommand list
*
* @package wp-cli
*/
public function list_( $args, $assoc_args ) {
global $wpdb;
if ( isset( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = explode( ',', $assoc_args['fields'] );
} else {
$assoc_args['fields'] = $this->obj_fields;
}
$signups = array();
$per_page = (int) Utils\get_flag_value( $assoc_args, 'per_page' );
$limit = $per_page ? $wpdb->prepare( 'LIMIT %d', $per_page ) : '';
$query = "SELECT * FROM $wpdb->signups {$limit}";
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared properly above.
$results = $wpdb->get_results( $query, ARRAY_A );
if ( $results ) {
foreach ( $results as $item ) {
// Support features like --active=0.
foreach ( array_keys( $item ) as $field ) {
if ( isset( $assoc_args[ $field ] ) && $assoc_args[ $field ] !== $item[ $field ] ) {
continue 2;
}
}
$signups[] = $item;
}
}
$format = Utils\get_flag_value( $assoc_args, 'format', 'table' );
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $format ) {
WP_CLI::line( implode( ' ', wp_list_pluck( $signups, 'signup_id' ) ) );
} else {
$formatter->display_items( $signups );
}
}
/**
* Gets details about a signup.
*
* ## OPTIONS
*
* <signup>
* : The signup ID, user login, user email, or activation key.
*
* [--field=<field>]
* : Instead of returning the whole signup, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get signup.
* $ wp user signup get 1 --field=user_login
* bobuser
*
* # Get signup and export to JSON file.
* $ wp user signup get bobuser --format=json > bobuser.json
*
* @package wp-cli
*/
public function get( $args, $assoc_args ) {
$signup = $this->fetcher->get_check( $args[0] );
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = array_keys( (array) $signup );
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( array( $signup ) );
}
/**
* Activates one or more signups.
*
* ## OPTIONS
*
* <signup>...
* : The signup ID, user login, user email, or activation key of the signup(s) to activate.
*
* ## EXAMPLES
*
* # Activate signup.
* $ wp user signup activate 2
* Signup 2 activated. Password: bZFSGsfzb9xs
* Success: Activated 1 of 1 signups.
*
* @package wp-cli
*/
public function activate( $args, $assoc_args ) {
$signups = $this->fetcher->get_many( $args );
$successes = 0;
$errors = 0;
foreach ( $signups as $signup ) {
$result = wpmu_activate_signup( $signup->activation_key );
if ( is_wp_error( $result ) ) {
WP_CLI::warning( "Failed activating signup {$signup->signup_id}." );
++$errors;
} else {
WP_CLI::log( "Signup {$signup->signup_id} activated. Password: {$result['password']}" );
++$successes;
}
}
Utils\report_batch_operation_results( 'signup', 'activate', count( $args ), $successes, $errors );
}
/**
* Deletes one or more signups.
*
* ## OPTIONS
*
* [<signup>...]
* : The signup ID, user login, user email, or activation key of the signup(s) to delete.
*
* [--all]
* : If set, all signups will be deleted.
*
* ## EXAMPLES
*
* # Delete signup.
* $ wp user signup delete 3
* Signup 3 deleted.
* Success: Deleted 1 of 1 signups.
*
* @package wp-cli
*/
public function delete( $args, $assoc_args ) {
$count = count( $args );
$all = Utils\get_flag_value( $assoc_args, 'all', false );
if ( ( 0 < $count && true === $all ) || ( 0 === $count && true !== $all ) ) {
WP_CLI::error( 'You need to specify either one or more signups or provide the --all flag.' );
}
if ( true === $all ) {
if ( ! $this->delete_all_signups() ) {
WP_CLI::error( 'Error deleting signups.' );
}
WP_CLI::success( 'Deleted all signups.' );
WP_CLI::halt( 0 );
}
$signups = $this->fetcher->get_many( $args );
$successes = 0;
$errors = 0;
foreach ( $signups as $signup ) {
if ( $this->delete_signup( $signup ) ) {
WP_CLI::log( "Signup {$signup->signup_id} deleted." );
++$successes;
} else {
WP_CLI::warning( "Failed deleting signup {$signup->signup_id}." );
++$errors;
}
}
Utils\report_batch_operation_results( 'signup', 'delete', $count, $successes, $errors );
}
/**
* Deletes signup.
*
* @param stdClasss $signup
* @return bool True if success; otherwise false.
*/
private function delete_signup( $signup ) {
global $wpdb;
$signup_id = $signup->signup_id;
$result = $wpdb->delete( $wpdb->signups, array( 'signup_id' => $signup_id ), array( '%d' ) );
return $result ? true : false;
}
/**
* Deletes all signup.
*
* @return bool True if success; otherwise false.
*/
private function delete_all_signups() {
global $wpdb;
$results = $wpdb->query( 'DELETE FROM ' . $wpdb->signups );
return $results ? true : false;
}
}