[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7