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


Generated: Wed Dec 11 01:00:03 2019 Cross-referenced by PHPXref 0.7.1