[ 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  if ( ! is_multisite() )
  14      wp_die( __( 'Multisite support is not enabled.' ) );
  15  
  16  if ( ! current_user_can( 'manage_sites' ) )
  17      wp_die( __( 'You do not have sufficient permissions to add sites to this network.' ) );
  18  
  19      get_current_screen()->add_help_tab( array(
  20          'id'      => 'overview',
  21          'title'   => __('Overview'),
  22          'content' =>
  23              '<p>' . __('This screen is for Super Admins to add new sites to the network. This is not affected by the registration settings.') . '</p>' .
  24              '<p>' . __('If the admin email for the new site does not exist in the database, a new user will also be created.') . '</p>'
  25  ) );
  26  
  27  get_current_screen()->set_help_sidebar(
  28      '<p><strong>' . __('For more information:') . '</strong></p>' .
  29      '<p>' . __('<a href="https://codex.wordpress.org/Network_Admin_Sites_Screen" target="_blank">Documentation on Site Management</a>') . '</p>' .
  30      '<p>' . __('<a href="https://wordpress.org/support/forum/multisite/" target="_blank">Support Forums</a>') . '</p>'
  31  );
  32  
  33  if ( isset($_REQUEST['action']) && 'add-site' == $_REQUEST['action'] ) {
  34      check_admin_referer( 'add-blog', '_wpnonce_add-blog' );
  35  
  36      if ( ! is_array( $_POST['blog'] ) )
  37          wp_die( __( 'Can&#8217;t create an empty site.' ) );
  38  
  39      $blog = $_POST['blog'];
  40      $domain = '';
  41      if ( preg_match( '|^([a-zA-Z0-9-])+$|', $blog['domain'] ) )
  42          $domain = strtolower( $blog['domain'] );
  43  
  44      // If not a subdomain install, make sure the domain isn't a reserved word
  45      if ( ! is_subdomain_install() ) {
  46          /** This filter is documented in wp-includes/ms-functions.php */
  47          $subdirectory_reserved_names = apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) );
  48          if ( in_array( $domain, $subdirectory_reserved_names ) )
  49              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 ) ) );
  50      }
  51  
  52      $title = $blog['title'];
  53  
  54      if ( empty( $domain ) )
  55          wp_die( __( 'Missing or invalid site address.' ) );
  56  
  57      if ( isset( $blog['email'] ) && '' === trim( $blog['email'] ) ) {
  58          wp_die( __( 'Missing email address.' ) );
  59      }
  60  
  61      $email = sanitize_email( $blog['email'] );
  62      if ( ! is_email( $email ) ) {
  63          wp_die( __( 'Invalid email address.' ) );
  64      }
  65  
  66      if ( is_subdomain_install() ) {
  67          $newdomain = $domain . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
  68          $path      = $current_site->path;
  69      } else {
  70          $newdomain = $current_site->domain;
  71          $path      = $current_site->path . $domain . '/';
  72      }
  73  
  74      $password = 'N/A';
  75      $user_id = email_exists($email);
  76      if ( !$user_id ) { // Create a new user with a random password
  77          $password = wp_generate_password( 12, false );
  78          $user_id = wpmu_create_user( $domain, $password, $email );
  79          if ( false == $user_id )
  80              wp_die( __( 'There was an error creating the user.' ) );
  81          else
  82              wp_new_user_notification( $user_id, $password );
  83      }
  84  
  85      $wpdb->hide_errors();
  86      $id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ), $current_site->id );
  87      $wpdb->show_errors();
  88      if ( ! is_wp_error( $id ) ) {
  89          if ( ! is_super_admin( $user_id ) && !get_user_option( 'primary_blog', $user_id ) ) {
  90              update_user_option( $user_id, 'primary_blog', $id, true );
  91          }
  92  
  93          $content_mail = sprintf(
  94              /* translators: 1: user login, 2: site url, 3: site name/title */
  95              __( 'New site created by %1$s
  96  
  97  Address: %2$s
  98  Name: %3$s' ),
  99              $current_user->user_login,
 100              get_site_url( $id ),
 101              wp_unslash( $title )
 102          );
 103          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' ) . '>' );
 104          wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) );
 105          wp_redirect( add_query_arg( array( 'update' => 'added', 'id' => $id ), 'site-new.php' ) );
 106          exit;
 107      } else {
 108          wp_die( $id->get_error_message() );
 109      }
 110  }
 111  
 112  if ( isset($_GET['update']) ) {
 113      $messages = array();
 114      if ( 'added' == $_GET['update'] )
 115          $messages[] = sprintf(
 116              /* translators: 1: dashboard url, 2: network admin edit url */
 117              __( 'Site added. <a href="%1$s">Visit Dashboard</a> or <a href="%2$s">Edit Site</a>' ),
 118              esc_url( get_admin_url( absint( $_GET['id'] ) ) ),
 119              network_admin_url( 'site-info.php?id=' . absint( $_GET['id'] ) )
 120          );
 121  }
 122  
 123  $title = __('Add New Site');
 124  $parent_file = 'sites.php';
 125  
 126  wp_enqueue_script( 'user-suggest' );
 127  
 128  require ( ABSPATH . 'wp-admin/admin-header.php' );
 129  
 130  ?>
 131  
 132  <div class="wrap">
 133  <h2 id="add-new-site"><?php _e('Add New Site') ?></h2>
 134  <?php
 135  if ( ! empty( $messages ) ) {
 136      foreach ( $messages as $msg )
 137          echo '<div id="message" class="updated notice is-dismissible"><p>' . $msg . '</p></div>';
 138  } ?>
 139  <form method="post" action="<?php echo network_admin_url( 'site-new.php?action=add-site' ); ?>" novalidate="novalidate">
 140  <?php wp_nonce_field( 'add-blog', '_wpnonce_add-blog' ) ?>
 141      <table class="form-table">
 142          <tr class="form-field form-required">
 143              <th scope="row"><label for="site-address"><?php _e( 'Site Address' ) ?></label></th>
 144              <td>
 145              <?php if ( is_subdomain_install() ) { ?>
 146                  <input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc" /><span class="no-break">.<?php echo preg_replace( '|^www\.|', '', $current_site->domain ); ?></span>
 147              <?php } else {
 148                  echo $current_site->domain . $current_site->path ?><input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc" />
 149              <?php }
 150              echo '<p id="site-address-desc">' . __( 'Only lowercase letters (a-z) and numbers are allowed.' ) . '</p>';
 151              ?>
 152              </td>
 153          </tr>
 154          <tr class="form-field form-required">
 155              <th scope="row"><label for="site-title"><?php _e( 'Site Title' ) ?></label></th>
 156              <td><input name="blog[title]" type="text" class="regular-text" id="site-title" /></td>
 157          </tr>
 158          <tr class="form-field form-required">
 159              <th scope="row"><label for="admin-email"><?php _e( 'Admin Email' ) ?></label></th>
 160              <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>
 161          </tr>
 162          <tr class="form-field">
 163              <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>
 164          </tr>
 165      </table>
 166      <?php submit_button( __('Add Site'), 'primary', 'add-site' ); ?>
 167      </form>
 168  </div>
 169  <?php
 170  require ( ABSPATH . 'wp-admin/admin-footer.php' );


Generated: Tue May 26 03:55:52 2015 Hosted by follow the white rabbit.