[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/network/ -> site-new.php (source)

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


Generated: Mon Feb 8 03:55:53 2016 Hosted by follow the white rabbit.