site-new.php 9.2 KB

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