WordPress 4.7
[autoinstalls/wordpress.git] / wp-admin / network / site-new.php
1 <?php
2 /**
3  * Add Site Administration Screen
4  *
5  * @package WordPress
6  * @subpackage Multisite
7  * @since 3.1.0
8  */
9
10 /** Load WordPress Administration Bootstrap */
11 require_once( dirname( __FILE__ ) . '/admin.php' );
12
13 /** WordPress Translation Install API */
14 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
15
16 if ( ! current_user_can( 'manage_sites' ) )
17         wp_die( __( 'Sorry, you are not allowed to add sites to this network.' ) );
18
19 get_current_screen()->add_help_tab( array(
20         'id'      => 'overview',
21         'title'   => __('Overview'),
22         'content' =>
23                 '<p>' . __('This screen is for Super Admins to add new sites to the network. This is not affected by the registration settings.') . '</p>' .
24                 '<p>' . __('If the admin email for the new site does not exist in the database, a new user will also be created.') . '</p>'
25 ) );
26
27 get_current_screen()->set_help_sidebar(
28         '<p><strong>' . __('For more information:') . '</strong></p>' .
29         '<p>' . __('<a href="https://codex.wordpress.org/Network_Admin_Sites_Screen">Documentation on Site Management</a>') . '</p>' .
30         '<p>' . __('<a href="https://wordpress.org/support/forum/multisite/">Support Forums</a>') . '</p>'
31 );
32
33 if ( isset($_REQUEST['action']) && 'add-site' == $_REQUEST['action'] ) {
34         check_admin_referer( 'add-blog', '_wpnonce_add-blog' );
35
36         if ( ! is_array( $_POST['blog'] ) )
37                 wp_die( __( 'Can&#8217;t create an empty site.' ) );
38
39         $blog = $_POST['blog'];
40         $domain = '';
41         if ( preg_match( '|^([a-zA-Z0-9-])+$|', $blog['domain'] ) )
42                 $domain = strtolower( $blog['domain'] );
43
44         // If not a subdomain install, make sure the domain isn't a reserved word
45         if ( ! is_subdomain_install() ) {
46                 $subdirectory_reserved_names = get_subdirectory_reserved_names();
47
48                 if ( in_array( $domain, $subdirectory_reserved_names ) ) {
49                         wp_die(
50                                 /* translators: %s: reserved names list */
51                                 sprintf( __( 'The following words are reserved for use by WordPress functions and cannot be used as blog names: %s' ),
52                                         '<code>' . implode( '</code>, <code>', $subdirectory_reserved_names ) . '</code>'
53                                 )
54                         );
55                 }
56         }
57
58         $title = $blog['title'];
59
60         $meta = array(
61                 'public' => 1
62         );
63
64         // Handle translation install for the new site.
65         if ( isset( $_POST['WPLANG'] ) ) {
66                 if ( '' === $_POST['WPLANG'] ) {
67                         $meta['WPLANG'] = ''; // en_US
68                 } elseif ( wp_can_install_language_pack() ) {
69                         $language = wp_download_language_pack( wp_unslash( $_POST['WPLANG'] ) );
70                         if ( $language ) {
71                                 $meta['WPLANG'] = $language;
72                         }
73                 }
74         }
75
76         if ( empty( $domain ) )
77                 wp_die( __( 'Missing or invalid site address.' ) );
78
79         if ( isset( $blog['email'] ) && '' === trim( $blog['email'] ) ) {
80                 wp_die( __( 'Missing email address.' ) );
81         }
82
83         $email = sanitize_email( $blog['email'] );
84         if ( ! is_email( $email ) ) {
85                 wp_die( __( 'Invalid email address.' ) );
86         }
87
88         if ( is_subdomain_install() ) {
89                 $newdomain = $domain . '.' . preg_replace( '|^www\.|', '', get_network()->domain );
90                 $path      = get_network()->path;
91         } else {
92                 $newdomain = get_network()->domain;
93                 $path      = get_network()->path . $domain . '/';
94         }
95
96         $password = 'N/A';
97         $user_id = email_exists($email);
98         if ( !$user_id ) { // Create a new user with a random password
99                 /**
100                  * Fires immediately before a new user is created via the network site-new.php page.
101                  *
102                  * @since 4.5.0
103                  *
104                  * @param string $email Email of the non-existent user.
105                  */
106                 do_action( 'pre_network_site_new_created_user', $email );
107
108                 $user_id = username_exists( $domain );
109                 if ( $user_id ) {
110                         wp_die( __( 'The domain or path entered conflicts with an existing username.' ) );
111                 }
112                 $password = wp_generate_password( 12, false );
113                 $user_id = wpmu_create_user( $domain, $password, $email );
114                 if ( false === $user_id ) {
115                         wp_die( __( 'There was an error creating the user.' ) );
116                 }
117
118                 /**
119                   * Fires after a new user has been created via the network site-new.php page.
120                   *
121                   * @since 4.4.0
122                   *
123                   * @param int $user_id ID of the newly created user.
124                   */
125                 do_action( 'network_site_new_created_user', $user_id );
126         }
127
128         $wpdb->hide_errors();
129         $id = wpmu_create_blog( $newdomain, $path, $title, $user_id, $meta, get_current_network_id() );
130         $wpdb->show_errors();
131         if ( ! is_wp_error( $id ) ) {
132                 if ( ! is_super_admin( $user_id ) && !get_user_option( 'primary_blog', $user_id ) ) {
133                         update_user_option( $user_id, 'primary_blog', $id, true );
134                 }
135
136                 wp_mail(
137                         get_site_option( 'admin_email' ),
138                         sprintf(
139                                 /* translators: %s: network name */
140                                 __( '[%s] New Site Created' ),
141                                 get_network()->site_name
142                         ),
143                         sprintf(
144                                 /* translators: 1: user login, 2: site url, 3: site name/title */
145                                 __( 'New site created by %1$s
146
147 Address: %2$s
148 Name: %3$s' ),
149                                 $current_user->user_login,
150                                 get_site_url( $id ),
151                                 wp_unslash( $title )
152                         ),
153                         sprintf(
154                                 'From: "%1$s" <%2$s>',
155                                 _x( 'Site Admin', 'email "From" field' ),
156                                 get_site_option( 'admin_email' )
157                         )
158                 );
159                 wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) );
160                 wp_redirect( add_query_arg( array( 'update' => 'added', 'id' => $id ), 'site-new.php' ) );
161                 exit;
162         } else {
163                 wp_die( $id->get_error_message() );
164         }
165 }
166
167 if ( isset($_GET['update']) ) {
168         $messages = array();
169         if ( 'added' == $_GET['update'] )
170                 $messages[] = sprintf(
171                         /* translators: 1: dashboard url, 2: network admin edit url */
172                         __( 'Site added. <a href="%1$s">Visit Dashboard</a> or <a href="%2$s">Edit Site</a>' ),
173                         esc_url( get_admin_url( absint( $_GET['id'] ) ) ),
174                         network_admin_url( 'site-info.php?id=' . absint( $_GET['id'] ) )
175                 );
176 }
177
178 $title = __('Add New Site');
179 $parent_file = 'sites.php';
180
181 wp_enqueue_script( 'user-suggest' );
182
183 require( ABSPATH . 'wp-admin/admin-header.php' );
184
185 ?>
186
187 <div class="wrap">
188 <h1 id="add-new-site"><?php _e( 'Add New Site' ); ?></h1>
189 <?php
190 if ( ! empty( $messages ) ) {
191         foreach ( $messages as $msg )
192                 echo '<div id="message" class="updated notice is-dismissible"><p>' . $msg . '</p></div>';
193 } ?>
194 <form method="post" action="<?php echo network_admin_url( 'site-new.php?action=add-site' ); ?>" novalidate="novalidate">
195 <?php wp_nonce_field( 'add-blog', '_wpnonce_add-blog' ) ?>
196         <table class="form-table">
197                 <tr class="form-field form-required">
198                         <th scope="row"><label for="site-address"><?php _e( 'Site Address (URL)' ) ?></label></th>
199                         <td>
200                         <?php if ( is_subdomain_install() ) { ?>
201                                 <input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc" autocapitalize="none" autocorrect="off"/><span class="no-break">.<?php echo preg_replace( '|^www\.|', '', get_network()->domain ); ?></span>
202                         <?php } else {
203                                 echo get_network()->domain . get_network()->path ?><input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc"  autocapitalize="none" autocorrect="off" />
204                         <?php }
205                         echo '<p class="description" id="site-address-desc">' . __( 'Only lowercase letters (a-z), numbers, and hyphens are allowed.' ) . '</p>';
206                         ?>
207                         </td>
208                 </tr>
209                 <tr class="form-field form-required">
210                         <th scope="row"><label for="site-title"><?php _e( 'Site Title' ) ?></label></th>
211                         <td><input name="blog[title]" type="text" class="regular-text" id="site-title" /></td>
212                 </tr>
213                 <?php
214                 $languages    = get_available_languages();
215                 $translations = wp_get_available_translations();
216                 if ( ! empty( $languages ) || ! empty( $translations ) ) :
217                         ?>
218                         <tr class="form-field form-required">
219                                 <th scope="row"><label for="site-language"><?php _e( 'Site Language' ); ?></label></th>
220                                 <td>
221                                         <?php
222                                         // Network default.
223                                         $lang = get_site_option( 'WPLANG' );
224
225                                         // Use English if the default isn't available.
226                                         if ( ! in_array( $lang, $languages ) ) {
227                                                 $lang = '';
228                                         }
229
230                                         wp_dropdown_languages( array(
231                                                 'name'                        => 'WPLANG',
232                                                 'id'                          => 'site-language',
233                                                 'selected'                    => $lang,
234                                                 'languages'                   => $languages,
235                                                 'translations'                => $translations,
236                                                 'show_available_translations' => wp_can_install_language_pack(),
237                                         ) );
238                                         ?>
239                                 </td>
240                         </tr>
241                 <?php endif; // Languages. ?>
242                 <tr class="form-field form-required">
243                         <th scope="row"><label for="admin-email"><?php _e( 'Admin Email' ) ?></label></th>
244                         <td><input name="blog[email]" type="email" class="regular-text wp-suggest-user" id="admin-email" data-autocomplete-type="search" data-autocomplete-field="user_email" /></td>
245                 </tr>
246                 <tr class="form-field">
247                         <td colspan="2"><?php _e( 'A new user will be created if the above email address is not in the database.' ) ?><br /><?php _e( 'The username and password will be mailed to this email address.' ) ?></td>
248                 </tr>
249         </table>
250
251         <?php
252         /**
253          * Fires at the end of the new site form in network admin.
254          *
255          * @since 4.5.0
256          */
257         do_action( 'network_site_new_form' );
258
259         submit_button( __( 'Add Site' ), 'primary', 'add-site' );
260         ?>
261         </form>
262 </div>
263 <?php
264 require( ABSPATH . 'wp-admin/admin-footer.php' );