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-import.php

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

601 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit();
6 }
7
8 if ( defined( 'WP_CLI' ) && WP_CLI ) {
9
10 /**
11 * Manage WP-Members via WP-CLI
12 */
13 class WP_Members_Import_CLI {
14
15 public function __construct() {
16 // WP-Members admin needs to be loaded manually.
17 global $wpmem;
18 if ( ! isset( $wpmem->admin ) ) {
19 $wpmem->load_admin();
20 }
21 }
22
23 /**
24 * Displays default path.
25 *
26 * ## OPTIONS
27 *
28 * [--dir=<dir_path>]
29 * : Additional path value to add to ABSPATH.
30 *
31 * @subcommand check-path
32 * @alias get-abspath
33 */
34 public function check_path( $args, $assoc_args ) {
35 $file_path = ABSPATH . 'wp-content/uploads/';
36
37 if ( isset( $assoc_args['dir'] ) ) {
38 $file_path = trailingslashit( trailingslashit( $file_path ) . $assoc_args['dir'] ); //2025/05/
39 }
40
41 WP_CLI::line( 'File path:' . ' ' . $file_path );
42 return;
43 }
44
45 /**
46 * Import memberships from a CSV file.
47 *
48 * ## OPTIONS
49 *
50 * --file=<file_name>
51 * : The filename of the import csv.
52 *
53 * [--dir=<dir_path>]
54 * : Additional path value to add to ABSPATH.
55 *
56 * [--verbose]
57 * : Displays verbose results.
58 *
59 * [--dry-run]
60 * : Preview what memberships will be set.
61 *
62 * [--cleanup]
63 * : Deletes the import file when import is completed.
64 *
65 * [--cols=<columns>]
66 * : A comma separated string of column names.
67 *
68 * @since Unknown
69 */
70 public function memberships( $args, $assoc_args ) {
71
72 // Get memberships the site has (before we do anything).
73 $site_memberships = wpmem_get_memberships();
74
75 // Set specific criteria.
76 $membership_key = ( isset( $assoc_args['key'] ) ) ? $assoc_args['key'] : "import_membership";
77 $expiration_key = ( isset( $assoc_args['exp'] ) ) ? $assoc_args['exp'] : "import_expires";
78
79 // Get the file contents.
80 $csv = $this->get_csv_from_file( $assoc_args );
81
82 $x = 0;
83 $e = 0;
84 foreach ( $csv as $row ) {
85
86 $row = wpmem_sanitize_array( $row );
87
88 // Do we have a field we can get the user by?
89 $user_id = $this->get_user_id_from_row( $row );
90
91 // Set specific criteria.
92 $membership = ( isset( $row[ $membership_key ] ) ) ? $row[ $membership_key ] : false;
93 $expiration = ( isset( $row[ $expiration_key ] ) ) ? $row[ $expiration_key ] : false;
94
95 // Set expiration date - either "false" or MySQL timestamp.
96 $date = ( $expiration ) ? date( "Y-m-d H:i:s", strtotime( $expiration ) ) : false;
97
98 if ( ! $user_id ) {
99 if ( ! $membership ) {
100 $membership = 'unknown';
101 }
102 $e++; // Add to error count.
103 } else {
104
105 // Does the membership exist?
106 if ( ! array_key_exists( $membership, $site_memberships ) ) {
107 $membership = 'unknown' . ': ' . $membership;
108 $e++; // Add to error count.
109 } else {
110
111 // Set user product access.
112 if ( $membership && ! isset( $assoc_args['dry-run'] ) ) {
113 wpmem_set_user_membership( $membership, $user_id, $date );
114 }
115 $x++; // Add to success count.
116 }
117 }
118
119 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
120 // Set columns for output.
121 $columns = array();
122 if ( isset( $row['ID'] ) ) {
123 $list_items['user ID'] = $user_id;
124 if ( ! in_array( 'user ID', $columns ) ) {
125 $columns[] = 'user ID';
126 }
127 }
128 if ( isset( $row['user_login'] ) ) {
129 $list_items['user_login'] = $row['user_login'];
130 if ( ! in_array( 'user_login', $columns ) ) {
131 $columns[] = 'user_login';
132 }
133 }
134 if ( isset( $row['user_email'] ) ) {
135 $list_items['user_email'] = $row['user_email'];
136 if ( ! in_array( 'user_email', $columns ) ) {
137 $columns[] = 'user_email';
138 }
139 }
140 $list_items['membership'] = $membership;
141 if ( ! in_array( 'membership', $columns ) ) {
142 $columns[] = 'membership';
143 }
144 if ( isset( $row[ $expiration_key ] ) ) {
145 $list_items[ $expiration_key ] = $date;
146 if ( ! in_array( $expiration_key, $columns ) ) {
147 $columns[] = $expiration_key;
148 }
149 }
150
151 $list[] = $list_items;
152 }
153 }
154
155 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
156 $formatter = new \WP_CLI\Formatter( $assoc_args, $columns );
157 $formatter->display_items( $list );
158 }
159
160 if ( isset( $assoc_args['cleanup'] ) ) {
161 WP_CLI::line( WP_CLI::colorize( "%GSuccess:%n " ) . sprintf( 'Imported memberships for %s users with %s errors.', $x, $e ) );
162 $this->cleanup( $assoc_args );
163 } else {
164 if ( isset( $assoc_args['dry-run'] ) ) {
165 WP_CLI::line( sprintf( 'Import memberships for %s users with %s errors.', $x, $e ) );
166 } else {
167 WP_CLI::success( sprintf( 'Imported memberships for %s users with %s errors.', $x, $e ) );
168 }
169 }
170 }
171
172 /**
173 * Activates all users in an imported CSV file.
174 *
175 * ## OPTIONS
176 *
177 * --file=<file_name>
178 * : The filename of the import csv.
179 *
180 * [--dir=<dir_path>]
181 * : Additional path value to add to ABSPATH.
182 *
183 * [--notify]
184 * : Send email notification to user. Defaults to false.
185 *
186 * [--pwd]
187 * : Sets a password for the user (used with notify). Defaults to false.
188 *
189 * [--verbose]
190 * : Displays verbose results.
191 *
192 * [--dry-run]
193 * : Preview what users will be activated.
194 *
195 * [--cleanup]
196 * : Deletes the import file when import is completed.
197 *
198 * [--cols=<columns>]
199 * : A comma separated string of column names.
200 */
201 public function activate( $args, $assoc_args ) {
202 $csv = $this->get_csv_from_file( $assoc_args );
203
204 $notify = ( isset( $assoc_args['notify'] ) ) ? true : false;
205 $setpwd = ( isset( $assoc_args['pwd'] ) ) ? true : false;
206
207 if ( $notify ) {
208 WP_CLI::confirm( 'You have selected to send email notification to activated users. Is this correct?' );
209 }
210
211 if ( $setpwd ) {
212 if ( $setpwd && ! $notify ) {
213 WP_CLI::confirm( 'You have selected to set user passwords without sending notification to the user. This is not recommended. Do you wish to continue?' );
214 } else {
215 WP_CLI::confirm( 'You have selected to set passwords when activating the user. Is this correct?' );
216 }
217 }
218
219 $x = 0;
220 $e = 0;
221 foreach ( $csv as $row ) {
222 $row = wpmem_sanitize_array( $row );
223 $user_id = $this->get_user_id_from_row( $row );
224 if ( $user_id ) {
225 if ( ! isset( $assoc_args['dry-run'] ) ) {
226 wpmem_activate_user( $user_id, $notify, $setpwd );
227 }
228 $x++;
229 } else {
230 $e++;
231 }
232
233 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
234 // Set columns for output.
235 $columns = array();
236 if ( isset( $row['ID'] ) ) {
237 $list_items['user ID'] = $user_id;
238 if ( ! in_array( 'user ID', $columns ) ) {
239 $columns[] = 'user ID';
240 }
241 }
242 if ( isset( $row['user_login'] ) ) {
243 $list_items['user_login'] = $row['user_login'];
244 if ( ! in_array( 'user_login', $columns ) ) {
245 $columns[] = 'user_login';
246 }
247 }
248 if ( isset( $row['user_email'] ) ) {
249 $list_items['user_email'] = $row['user_email'];
250 if ( ! in_array( 'user_email', $columns ) ) {
251 $columns[] = 'user_email';
252 }
253 }
254 $list[] = $list_items;
255 }
256 }
257
258 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
259 $formatter = new \WP_CLI\Formatter( $assoc_args, $columns );
260 $formatter->display_items( $list );
261 }
262
263 if ( isset( $assoc_args['cleanup'] ) ) {
264 WP_CLI::line( WP_CLI::colorize( "%GSuccess:%n " ) . sprintf( 'Set %s users as activated with %s errors.', $x, $e ) );
265 $this->cleanup( $assoc_args );
266 } else {
267 if ( isset( $assoc_args['dry-run'] ) ) {
268 WP_CLI::line( sprintf( 'Set %s users as activated with %s errors.', $x, $e ) );
269 } else {
270 WP_CLI::success( sprintf( 'Set %s users as activated with %s errors.', $x, $e ) );
271 }
272 }
273 }
274
275 /**
276 * Deactivates all users in an imported CSV file.
277 *
278 * ## OPTIONS
279 *
280 * --file=<file_name>
281 * : The filename of the import csv.
282 *
283 * [--dir=<dir_path>]
284 * : Additional path value to add to ABSPATH.
285 *
286 * [--verbose]
287 * : Displays verbose results.
288 *
289 * [--dry-run]
290 * : Preview what users will be deactivated.
291 *
292 * [--cleanup]
293 * : Deletes the import file when import is completed.
294 *
295 * [--cols=<columns>]
296 * : A comma separated string of column names.
297 *
298 * @since 3.5.4
299 */
300 public function deactivate( $args, $assoc_args ) {
301 $csv = $this->get_csv_from_file( $assoc_args );
302
303 $x = 0;
304 $e = 0;
305 foreach ( $csv as $row ) {
306 $row = wpmem_sanitize_array( $row );
307 $user_id = $this->get_user_id_from_row( $row );
308 if ( $user_id ) {
309 if ( ! isset( $assoc_args['dry-run'] ) ) {
310 wpmem_deactivate_user( $user_id );
311 }
312 $x++;
313 } else {
314 $e++;
315 }
316
317 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
318 // Set columns for output.
319 $columns = array();
320 if ( isset( $row['ID'] ) ) {
321 $list_items['user ID'] = $user_id;
322 if ( ! in_array( 'user ID', $columns ) ) {
323 $columns[] = 'user ID';
324 }
325 }
326 if ( isset( $row['user_login'] ) ) {
327 $list_items['user_login'] = $row['user_login'];
328 if ( ! in_array( 'user_login', $columns ) ) {
329 $columns[] = 'user_login';
330 }
331 }
332 if ( isset( $row['user_email'] ) ) {
333 $list_items['user_email'] = $row['user_email'];
334 if ( ! in_array( 'user_email', $columns ) ) {
335 $columns[] = 'user_email';
336 }
337 }
338 $list[] = $list_items;
339 }
340 }
341
342 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
343 $formatter = new \WP_CLI\Formatter( $assoc_args, $columns );
344 $formatter->display_items( $list );
345 }
346
347 if ( isset( $assoc_args['cleanup'] ) ) {
348 WP_CLI::line( WP_CLI::colorize( "%GSuccess:%n " ) . sprintf( 'Set %s users as deactivated with %s errors.', $x, $e ) );
349 $this->cleanup( $assoc_args );
350 } else {
351 if ( isset( $assoc_args['dry-run'] ) ) {
352 WP_CLI::line( sprintf( 'Set %s users as deactivated with %s errors.', $x, $e ) );
353 } else {
354 WP_CLI::success( sprintf( 'Set %s users as deactivated with %s errors.', $x, $e ) );
355 }
356 }
357 }
358
359 /**
360 * Confirms all users in an imported CSV file.
361 *
362 * ## OPTIONS
363 *
364 * --file=<file_name>
365 * : The filename of the import csv.
366 *
367 * [--dir=<dir_path>]
368 * : Additional path value to add to ABSPATH.
369 *
370 * [--verbose]
371 * : Displays verbose results.
372 *
373 * [--dry-run]
374 * : Preview what users will be confirmed.
375 *
376 * [--cleanup]
377 * : Deletes the import file when import is completed.
378 *
379 * [--cols=<columns>]
380 * : A comma separated string of column names.
381 *
382 * @since 3.5.4
383 */
384 public function confirm( $args, $assoc_args ) {
385 $csv = $this->get_csv_from_file( $assoc_args );
386
387 $x = 0;
388 $e = 0;
389 foreach ( $csv as $row ) {
390 $row = wpmem_sanitize_array( $row );
391 $user_id = $this->get_user_id_from_row( $row );
392 if ( $user_id ) {
393 if ( ! isset( $assoc_args['dry-run'] ) ) {
394 wpmem_set_user_as_confirmed( $user_id );
395 }
396 $x++;
397 } else {
398 $e++;
399 }
400
401 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
402 // Set columns for output.
403 $columns = array();
404 if ( isset( $row['ID'] ) ) {
405 $list_items['user ID'] = $user_id;
406 if ( ! in_array( 'user ID', $columns ) ) {
407 $columns[] = 'user ID';
408 }
409 }
410 if ( isset( $row['user_login'] ) ) {
411 $list_items['user_login'] = $row['user_login'];
412 if ( ! in_array( 'user_login', $columns ) ) {
413 $columns[] = 'user_login';
414 }
415 }
416 if ( isset( $row['user_email'] ) ) {
417 $list_items['user_email'] = $row['user_email'];
418 if ( ! in_array( 'user_email', $columns ) ) {
419 $columns[] = 'user_email';
420 }
421 }
422 $list[] = $list_items;
423 }
424 }
425
426 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
427 $formatter = new \WP_CLI\Formatter( $assoc_args, $columns );
428 $formatter->display_items( $list );
429 }
430
431 if ( isset( $assoc_args['cleanup'] ) ) {
432 WP_CLI::line( WP_CLI::colorize( "%GSuccess:%n " ) . sprintf( 'Set %s users as confirmed with %s errors.', $x, $e ) );
433 $this->cleanup( $assoc_args );
434 } else {
435 if ( isset( $assoc_args['dry-run'] ) ) {
436 WP_CLI::line( sprintf( 'Set %s users as confirmed with %s errors.', $x, $e ) );
437 } else {
438 WP_CLI::success( sprintf( 'Set %s users as confirmed with %s errors.', $x, $e ) );
439 }
440 }
441 }
442
443 /**
444 * Unonfirms all users in an imported CSV file.
445 *
446 * ## OPTIONS
447 *
448 * --file=<file_name>
449 * : The filename of the import csv.
450 *
451 * [--dir=<dir_path>]
452 * : Additional path value to add to ABSPATH.
453 *
454 * [--verbose]
455 * : Displays verbose results.
456 *
457 * [--dry-run]
458 * : Preview what users will be unconfirmed.
459 *
460 * [--cleanup]
461 * : Deletes the import file when import is completed.
462 *
463 * [--cols=<columns>]
464 * : A comma separated string of column names.
465 *
466 * @since 3.5.4
467 */
468 public function unconfirm( $args, $assoc_args ) {
469 $csv = $this->get_csv_from_file( $assoc_args );
470
471 $x = 0;
472 $e = 0;
473 foreach ( $csv as $row ) {
474 $row = wpmem_sanitize_array( $row );
475 $user_id = $this->get_user_id_from_row( $row );
476 if ( $user_id ) {
477 if ( ! isset( $assoc_args['dry-run'] ) ) {
478 wpmem_set_user_as_unconfirmed( $user_id );
479 }
480 $x++;
481 } else {
482 $e++;
483 }
484
485 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
486 // Set columns for output.
487 $columns = array();
488 if ( isset( $row['ID'] ) ) {
489 $list_items['user ID'] = $user_id;
490 if ( ! in_array( 'user ID', $columns ) ) {
491 $columns[] = 'user ID';
492 }
493 }
494 if ( isset( $row['user_login'] ) ) {
495 $list_items['user_login'] = $row['user_login'];
496 if ( ! in_array( 'user_login', $columns ) ) {
497 $columns[] = 'user_login';
498 }
499 }
500 if ( isset( $row['user_email'] ) ) {
501 $list_items['user_email'] = $row['user_email'];
502 if ( ! in_array( 'user_email', $columns ) ) {
503 $columns[] = 'user_email';
504 }
505 }
506 $list[] = $list_items;
507 }
508 }
509
510 if ( isset( $assoc_args['verbose'] ) || isset( $assoc_args['dry-run'] ) ) {
511 $formatter = new \WP_CLI\Formatter( $assoc_args, $columns );
512 $formatter->display_items( $list );
513 }
514
515 if ( isset( $assoc_args['cleanup'] ) ) {
516 WP_CLI::line( WP_CLI::colorize( "%GSuccess:%n " ) . sprintf( 'Set %s users as unconfirmed with %s errors.', $x, $e ) );
517 $this->cleanup( $assoc_args );
518 } else {
519 if ( isset( $assoc_args['dry-run'] ) ) {
520 WP_CLI::line( sprintf( 'Set %s users as unconfirmed with %s errors.', $x, $e ) );
521 } else {
522 WP_CLI::success( sprintf( 'Set %s users as unconfirmed with %s errors.', $x, $e ) );
523 }
524 }
525 }
526
527 /**
528 * Gets the user ID, checks for columns in order of
529 * ID, user_login, and user_email.
530 * @since 3.5.4
531 */
532 private function get_user_id_from_row( $row ) {
533 $user_id = false;
534 // Do we have a field we can get the user by?
535 if ( isset( $row['ID'] ) ) {
536 $user_id = $row['ID'];
537 } elseif ( isset( $row['user_login'] ) ) {
538 $user = get_user_by( 'login', $row['user_login'] );
539 $user_id = $user->ID;
540 $user_id = ( $user ) ? $user->ID : false;
541 } elseif ( isset( $row['user_email'] ) ) {
542 $user = get_user_by( 'email', sanitize_email( $row['user_email'] ) );
543 $user_id = ( $user ) ? $user->ID : false;
544 }
545 return $user_id;
546 }
547
548 /**
549 * Gets the file path info.
550 * @since 3.5.4
551 */
552 private function get_file_info( $assoc_args ) {
553 $file_info['name'] = $assoc_args['file'];
554 $file_path = ( isset( $assoc_args['path'] ) ) ? ABSPATH . $assoc_args['path'] : ABSPATH . 'wp-content/uploads/';
555
556 $file_info['path'] = ( isset( $assoc_args['dir'] ) ) ? trailingslashit( trailingslashit( $file_path ) . $assoc_args['dir'] ) : $file_path;
557
558 $file_info['file'] = trailingslashit( $file_info['path'] ) . $file_info['name'];
559 return $file_info;
560 }
561
562 /**
563 * Converts the CSV to a keyed array.
564 * @since 3.5.4
565 */
566 private function get_csv_from_file( $assoc_args ) {
567
568 $file_info = $this->get_file_info( $assoc_args );
569
570 // Check if file exists.
571 if ( ! file_exists( $file_info['file'] ) ) {
572 WP_CLI::line( 'File name:' . ' ' . $file_info['name'] );
573 WP_CLI::line( 'File path:' . ' ' . $file_info['path'] );
574 WP_CLI::error( 'Parameters given did not return a valid file. Check your filename and path values.' );
575 }
576
577 $cols = ( isset( $assoc_args['cols'] ) ) ? explode( ",", $assoc_args['cols'] ) : false;
578 return wpmem_csv_to_array( $file_info['file'], $cols );
579 }
580
581 /**
582 * Deletes an import file when complete.
583 * @since 3.5.4
584 */
585 private function cleanup( $assoc_args ) {
586 // Don't delete if it's a dry run.
587 if ( ! isset( $assoc_args['dry-run'] ) ) {
588 $file_info = $this->get_file_info( $assoc_args );
589 WP_CLI::confirm( sprintf( 'You have selected to delete %s on completion. This cannot be undone. Do you with to continue?', $file_info['name'] ) );
590 $result = unlink( $file_info['file'] );
591 if ( $result ) {
592 WP_CLI::success( sprintf( '%s was deleted.', $file_info['name'] ) );
593 } else {
594 WP_CLI::success( sprintf( 'Unable to delete %s.', $file_info['name'] ) );
595 }
596 }
597 }
598 }
599
600 WP_CLI::add_command( 'mem import', 'WP_Members_Import_CLI' );
601 }