site-new.php 8.8 KB

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