PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/network/site-new.php

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