| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | Plugin Name: Generate Fake Sites |
|---|
| 5 | Description: WP-CLI command to create fake sites |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | if ( ! defined( 'ABSPATH' ) ) die( "Cannot access directly." ); |
|---|
| 9 | |
|---|
| 10 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|---|
| 11 | |
|---|
| 12 | class GenerateFakeSites extends WP_CLI_Command { |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * @synopsis <number_of_sites> |
|---|
| 16 | */ |
|---|
| 17 | public function generate( $args, $assoc_args ) { |
|---|
| 18 | |
|---|
| 19 | if ( !isset( $_SERVER['REMOTE_ADDR'] ) ) { |
|---|
| 20 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | if ( !isset( $args[0] ) || !is_numeric( $args[0] ) || intval( $args[0] ) != $args[0] ) { |
|---|
| 24 | WP_CLI::error( "Invalid number of sites" ); |
|---|
| 25 | die(); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | $num_sites = $args[0]; |
|---|
| 29 | $num_sites_int = intval( $num_sites ); |
|---|
| 30 | |
|---|
| 31 | for ( $site_index = 0; $site_index < $num_sites_int; $site_index++ ) { |
|---|
| 32 | |
|---|
| 33 | // $blog_name = Generate_Fake_Sites_UUID::v4(); |
|---|
| 34 | $blog_name = NewGUID(); |
|---|
| 35 | $user_name = $blog_name; |
|---|
| 36 | $password = wp_generate_password( 40 ); |
|---|
| 37 | $email = $blog_name . '@localhost'; |
|---|
| 38 | |
|---|
| 39 | $user_id = wpmu_create_user( $user_name, $password, $email ); |
|---|
| 40 | |
|---|
| 41 | $domain = parse_url( network_site_url(), PHP_URL_HOST ); |
|---|
| 42 | $slug = strtolower( $blog_name ); |
|---|
| 43 | |
|---|
| 44 | $slug . '.' . parse_url( network_site_url(), PHP_URL_HOST ); |
|---|
| 45 | $meta = array(); |
|---|
| 46 | |
|---|
| 47 | $blog_id = wpmu_create_blog( $domain, $slug, $blog_name, $user_id, array( 'public' => 1 ) ); |
|---|
| 48 | do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $blog_name, $meta ); |
|---|
| 49 | |
|---|
| 50 | WP_CLI::success( sprintf( 'Created site %s', $domain . '/' . $slug ) ); |
|---|
| 51 | |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | WP_CLI::add_command( 'generate_fake_sites', 'GenerateFakeSites' ); |
|---|
| 57 | |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Generate a UUID without needing PHP's uuid extension |
|---|
| 62 | * |
|---|
| 63 | * @see http://www.largeresource.com/21/call-to-undefined-function-uuid_create-in-php |
|---|
| 64 | * @return string UUID |
|---|
| 65 | */ |
|---|
| 66 | function NewGUID() { |
|---|
| 67 | $guidstr = ""; |
|---|
| 68 | for ( $i=1;$i <= 16;$i++ ) { |
|---|
| 69 | $b = (int)rand( 0, 0xff ); |
|---|
| 70 | if ( $i == 7 ) { $b &= 0x0f; $b |= 0x40; } // version 4 (random) |
|---|
| 71 | if ( $i == 9 ) { $b &= 0x3f; $b |= 0x80; } // variant |
|---|
| 72 | $guidstr .= sprintf( "%02s", base_convert( $b, 10, 16 ) ); |
|---|
| 73 | if ( $i == 4 || $i == 6 || $i == 8 || $i == 10 ) { $guidstr .= '-'; } |
|---|
| 74 | } |
|---|
| 75 | return $guidstr; |
|---|
| 76 | } |
|---|