site-new.php 8.8 KB

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