@@ -487,6 +487,148 @@ public function create( $args, $assoc_args ) {
487
487
}
488
488
}
489
489
490
+ /**
491
+ * Generate some sites.
492
+ *
493
+ * Creates a specified number of new sites.
494
+ *
495
+ * ## OPTIONS
496
+ *
497
+ * [--count=<number>]
498
+ * : How many sites to generates?
499
+ * ---
500
+ * default: 100
501
+ * ---
502
+ *
503
+ * [--email=<email>]
504
+ * : Email for admin user. User will be created if none exists. Assignment to super admin if not included.
505
+ *
506
+ * [--network_id=<network-id>]
507
+ * : Network to associate new site with. Defaults to current network (typically 1).
508
+ *
509
+ * [--private]
510
+ * : If set, the new site will be non-public (not indexed)
511
+ *
512
+ * [--format=<format>]
513
+ * : Render output in a particular format.
514
+ * ---
515
+ * default: progress
516
+ * options:
517
+ * - progress
518
+ * - ids
519
+ * ---
520
+ *
521
+ * ## EXAMPLES
522
+ *
523
+ * # Generate 10 sites.
524
+ * $ wp site generate --count=10
525
+ * Generating sites 100% [================================================] 0:01 / 0:04
526
+ */
527
+ public function generate ( $ args , $ assoc_args ) {
528
+ if ( ! is_multisite () ) {
529
+ WP_CLI ::error ( 'This is not a multisite installation. ' );
530
+ }
531
+
532
+ global $ wpdb , $ current_site ;
533
+
534
+ $ defaults = [
535
+ 'count ' => 100 ,
536
+ 'email ' => '' ,
537
+ 'network_id ' => 1 ,
538
+ ];
539
+
540
+ $ assoc_args = array_merge ( $ defaults , $ assoc_args );
541
+
542
+ // Network.
543
+ if ( ! empty ( $ assoc_args ['network_id ' ] ) ) {
544
+ $ network = $ this ->get_network ( $ assoc_args ['network_id ' ] );
545
+ if ( false === $ network ) {
546
+ WP_CLI ::error ( "Network with id {$ assoc_args ['network_id ' ]} does not exist. " );
547
+ }
548
+ } else {
549
+ $ network = $ current_site ;
550
+ }
551
+
552
+ // Public.
553
+ $ public = ! Utils \get_flag_value ( $ assoc_args , 'private ' );
554
+
555
+ // Limit.
556
+ $ limit = $ assoc_args ['count ' ];
557
+
558
+ // Email.
559
+ $ email = sanitize_email ( $ assoc_args ['email ' ] );
560
+ if ( empty ( $ email ) || ! is_email ( $ email ) ) {
561
+ $ super_admins = get_super_admins ();
562
+ $ email = '' ;
563
+ if ( ! empty ( $ super_admins ) && is_array ( $ super_admins ) ) {
564
+ $ super_login = reset ( $ super_admins );
565
+ $ super_user = get_user_by ( 'login ' , $ super_login );
566
+ if ( $ super_user ) {
567
+ $ email = $ super_user ->user_email ;
568
+ }
569
+ }
570
+ }
571
+
572
+ $ user_id = email_exists ( $ email );
573
+ if ( ! $ user_id ) {
574
+ $ password = wp_generate_password ( 24 , false );
575
+ $ user_id = wpmu_create_user ( 'site-admin ' , $ password , $ email );
576
+
577
+ if ( false === $ user_id ) {
578
+ WP_CLI ::error ( "Can't create user. " );
579
+ } else {
580
+ User_Command::wp_new_user_notification ( $ user_id , $ password );
581
+ }
582
+ }
583
+
584
+ $ is_subdomain_install = is_subdomain_install ();
585
+
586
+ $ format = Utils \get_flag_value ( $ assoc_args , 'format ' , 'progress ' );
587
+
588
+ $ notify = false ;
589
+ if ( 'progress ' === $ format ) {
590
+ $ notify = Utils \make_progress_bar ( 'Generating sites ' , $ limit );
591
+ }
592
+
593
+ for ( $ index = 1 ; $ index <= $ limit ; $ index ++ ) {
594
+ $ base = 'site ' . $ index ;
595
+ $ title = 'Site ' . $ index ;
596
+
597
+ $ new_domain = '' ;
598
+ $ path = '' ;
599
+
600
+ if ( $ is_subdomain_install ) {
601
+ $ new_domain = $ base . '. ' . preg_replace ( '|^www\.| ' , '' , $ network ->domain );
602
+ $ path = $ network ->path ;
603
+ } else {
604
+ $ new_domain = $ network ->domain ;
605
+ $ path = $ network ->path . $ base . '/ ' ;
606
+ }
607
+
608
+ $ wpdb ->hide_errors ();
609
+ $ title = wp_slash ( $ title );
610
+ $ id = wpmu_create_blog ( $ new_domain , $ path , $ title , $ user_id , [ 'public ' => $ public ], $ network ->id );
611
+ $ wpdb ->show_errors ();
612
+ if ( ! is_wp_error ( $ id ) ) {
613
+ if ( ! is_super_admin ( $ user_id ) && ! get_user_option ( 'primary_blog ' , $ user_id ) ) {
614
+ update_user_option ( $ user_id , 'primary_blog ' , $ id , true );
615
+ }
616
+ } else {
617
+ WP_CLI ::error ( $ id ->get_error_message () );
618
+ }
619
+
620
+ if ( 'progress ' === $ format ) {
621
+ $ notify ->tick ();
622
+ } else {
623
+ WP_CLI ::line ( $ id );
624
+ }
625
+ }
626
+
627
+ if ( 'progress ' === $ format ) {
628
+ $ notify ->finish ();
629
+ }
630
+ }
631
+
490
632
/**
491
633
* Gets network data for a given id.
492
634
*
0 commit comments