wp/wp-admin/network/site-new.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    15 
    15 
    16 if ( ! current_user_can( 'create_sites' ) ) {
    16 if ( ! current_user_can( 'create_sites' ) ) {
    17 	wp_die( __( 'Sorry, you are not allowed to add sites to this network.' ) );
    17 	wp_die( __( 'Sorry, you are not allowed to add sites to this network.' ) );
    18 }
    18 }
    19 
    19 
    20 get_current_screen()->add_help_tab( array(
    20 get_current_screen()->add_help_tab(
    21 	'id'      => 'overview',
    21 	array(
    22 	'title'   => __('Overview'),
    22 		'id'      => 'overview',
    23 	'content' =>
    23 		'title'   => __( 'Overview' ),
    24 		'<p>' . __('This screen is for Super Admins to add new sites to the network. This is not affected by the registration settings.') . '</p>' .
    24 		'content' =>
    25 		'<p>' . __('If the admin email for the new site does not exist in the database, a new user will also be created.') . '</p>'
    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 ) );
    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 );
    27 
    29 
    28 get_current_screen()->set_help_sidebar(
    30 get_current_screen()->set_help_sidebar(
    29 	'<p><strong>' . __('For more information:') . '</strong></p>' .
    31 	'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
    30 	'<p>' . __('<a href="https://codex.wordpress.org/Network_Admin_Sites_Screen">Documentation on Site Management</a>') . '</p>' .
    32 	'<p>' . __( '<a href="https://codex.wordpress.org/Network_Admin_Sites_Screen">Documentation on Site Management</a>' ) . '</p>' .
    31 	'<p>' . __('<a href="https://wordpress.org/support/forum/multisite/">Support Forums</a>') . '</p>'
    33 	'<p>' . __( '<a href="https://wordpress.org/support/forum/multisite/">Support Forums</a>' ) . '</p>'
    32 );
    34 );
    33 
    35 
    34 if ( isset($_REQUEST['action']) && 'add-site' == $_REQUEST['action'] ) {
    36 if ( isset( $_REQUEST['action'] ) && 'add-site' == $_REQUEST['action'] ) {
    35 	check_admin_referer( 'add-blog', '_wpnonce_add-blog' );
    37 	check_admin_referer( 'add-blog', '_wpnonce_add-blog' );
    36 
    38 
    37 	if ( ! is_array( $_POST['blog'] ) )
    39 	if ( ! is_array( $_POST['blog'] ) ) {
    38 		wp_die( __( 'Can&#8217;t create an empty site.' ) );
    40 		wp_die( __( 'Can&#8217;t create an empty site.' ) );
    39 
    41 	}
    40 	$blog = $_POST['blog'];
    42 
       
    43 	$blog   = $_POST['blog'];
    41 	$domain = '';
    44 	$domain = '';
    42 	if ( preg_match( '|^([a-zA-Z0-9-])+$|', $blog['domain'] ) )
    45 	if ( preg_match( '|^([a-zA-Z0-9-])+$|', $blog['domain'] ) ) {
    43 		$domain = strtolower( $blog['domain'] );
    46 		$domain = strtolower( $blog['domain'] );
       
    47 	}
    44 
    48 
    45 	// If not a subdomain installation, make sure the domain isn't a reserved word
    49 	// If not a subdomain installation, make sure the domain isn't a reserved word
    46 	if ( ! is_subdomain_install() ) {
    50 	if ( ! is_subdomain_install() ) {
    47 		$subdirectory_reserved_names = get_subdirectory_reserved_names();
    51 		$subdirectory_reserved_names = get_subdirectory_reserved_names();
    48 
    52 
    49 		if ( in_array( $domain, $subdirectory_reserved_names ) ) {
    53 		if ( in_array( $domain, $subdirectory_reserved_names ) ) {
    50 			wp_die(
    54 			wp_die(
    51 				/* translators: %s: reserved names list */
    55 				/* translators: %s: reserved names list */
    52 				sprintf( __( 'The following words are reserved for use by WordPress functions and cannot be used as blog names: %s' ),
    56 				sprintf(
       
    57 					__( 'The following words are reserved for use by WordPress functions and cannot be used as blog names: %s' ),
    53 					'<code>' . implode( '</code>, <code>', $subdirectory_reserved_names ) . '</code>'
    58 					'<code>' . implode( '</code>, <code>', $subdirectory_reserved_names ) . '</code>'
    54 				)
    59 				)
    55 			);
    60 			);
    56 		}
    61 		}
    57 	}
    62 	}
    58 
    63 
    59 	$title = $blog['title'];
    64 	$title = $blog['title'];
    60 
    65 
    61 	$meta = array(
    66 	$meta = array(
    62 		'public' => 1
    67 		'public' => 1,
    63 	);
    68 	);
    64 
    69 
    65 	// Handle translation installation for the new site.
    70 	// Handle translation installation for the new site.
    66 	if ( isset( $_POST['WPLANG'] ) ) {
    71 	if ( isset( $_POST['WPLANG'] ) ) {
    67 		if ( '' === $_POST['WPLANG'] ) {
    72 		if ( '' === $_POST['WPLANG'] ) {
    74 				$meta['WPLANG'] = $language;
    79 				$meta['WPLANG'] = $language;
    75 			}
    80 			}
    76 		}
    81 		}
    77 	}
    82 	}
    78 
    83 
    79 	if ( empty( $domain ) )
    84 	if ( empty( $domain ) ) {
    80 		wp_die( __( 'Missing or invalid site address.' ) );
    85 		wp_die( __( 'Missing or invalid site address.' ) );
       
    86 	}
    81 
    87 
    82 	if ( isset( $blog['email'] ) && '' === trim( $blog['email'] ) ) {
    88 	if ( isset( $blog['email'] ) && '' === trim( $blog['email'] ) ) {
    83 		wp_die( __( 'Missing email address.' ) );
    89 		wp_die( __( 'Missing email address.' ) );
    84 	}
    90 	}
    85 
    91 
    95 		$newdomain = get_network()->domain;
   101 		$newdomain = get_network()->domain;
    96 		$path      = get_network()->path . $domain . '/';
   102 		$path      = get_network()->path . $domain . '/';
    97 	}
   103 	}
    98 
   104 
    99 	$password = 'N/A';
   105 	$password = 'N/A';
   100 	$user_id = email_exists($email);
   106 	$user_id  = email_exists( $email );
   101 	if ( !$user_id ) { // Create a new user with a random password
   107 	if ( ! $user_id ) { // Create a new user with a random password
   102 		/**
   108 		/**
   103 		 * Fires immediately before a new user is created via the network site-new.php page.
   109 		 * Fires immediately before a new user is created via the network site-new.php page.
   104 		 *
   110 		 *
   105 		 * @since 4.5.0
   111 		 * @since 4.5.0
   106 		 *
   112 		 *
   111 		$user_id = username_exists( $domain );
   117 		$user_id = username_exists( $domain );
   112 		if ( $user_id ) {
   118 		if ( $user_id ) {
   113 			wp_die( __( 'The domain or path entered conflicts with an existing username.' ) );
   119 			wp_die( __( 'The domain or path entered conflicts with an existing username.' ) );
   114 		}
   120 		}
   115 		$password = wp_generate_password( 12, false );
   121 		$password = wp_generate_password( 12, false );
   116 		$user_id = wpmu_create_user( $domain, $password, $email );
   122 		$user_id  = wpmu_create_user( $domain, $password, $email );
   117 		if ( false === $user_id ) {
   123 		if ( false === $user_id ) {
   118 			wp_die( __( 'There was an error creating the user.' ) );
   124 			wp_die( __( 'There was an error creating the user.' ) );
   119 		}
   125 		}
   120 
   126 
   121 		/**
   127 		/**
   122 		  * Fires after a new user has been created via the network site-new.php page.
   128 		 * Fires after a new user has been created via the network site-new.php page.
   123 		  *
   129 		 *
   124 		  * @since 4.4.0
   130 		 * @since 4.4.0
   125 		  *
   131 		 *
   126 		  * @param int $user_id ID of the newly created user.
   132 		 * @param int $user_id ID of the newly created user.
   127 		  */
   133 		 */
   128 		do_action( 'network_site_new_created_user', $user_id );
   134 		do_action( 'network_site_new_created_user', $user_id );
   129 	}
   135 	}
   130 
   136 
   131 	$wpdb->hide_errors();
   137 	$wpdb->hide_errors();
   132 	$id = wpmu_create_blog( $newdomain, $path, $title, $user_id, $meta, get_current_network_id() );
   138 	$id = wpmu_create_blog( $newdomain, $path, $title, $user_id, $meta, get_current_network_id() );
   133 	$wpdb->show_errors();
   139 	$wpdb->show_errors();
   134 	if ( ! is_wp_error( $id ) ) {
   140 	if ( ! is_wp_error( $id ) ) {
   135 		if ( ! is_super_admin( $user_id ) && !get_user_option( 'primary_blog', $user_id ) ) {
   141 		if ( ! is_super_admin( $user_id ) && ! get_user_option( 'primary_blog', $user_id ) ) {
   136 			update_user_option( $user_id, 'primary_blog', $id, true );
   142 			update_user_option( $user_id, 'primary_blog', $id, true );
   137 		}
   143 		}
   138 
   144 
   139 		wp_mail(
   145 		wp_mail(
   140 			get_site_option( 'admin_email' ),
   146 			get_site_option( 'admin_email' ),
   143 				__( '[%s] New Site Created' ),
   149 				__( '[%s] New Site Created' ),
   144 				get_network()->site_name
   150 				get_network()->site_name
   145 			),
   151 			),
   146 			sprintf(
   152 			sprintf(
   147 				/* translators: 1: user login, 2: site url, 3: site name/title */
   153 				/* translators: 1: user login, 2: site url, 3: site name/title */
   148 				__( 'New site created by %1$s
   154 				__(
       
   155 					'New site created by %1$s
   149 
   156 
   150 Address: %2$s
   157 Address: %2$s
   151 Name: %3$s' ),
   158 Name: %3$s'
       
   159 				),
   152 				$current_user->user_login,
   160 				$current_user->user_login,
   153 				get_site_url( $id ),
   161 				get_site_url( $id ),
   154 				wp_unslash( $title )
   162 				wp_unslash( $title )
   155 			),
   163 			),
   156 			sprintf(
   164 			sprintf(
   158 				_x( 'Site Admin', 'email "From" field' ),
   166 				_x( 'Site Admin', 'email "From" field' ),
   159 				get_site_option( 'admin_email' )
   167 				get_site_option( 'admin_email' )
   160 			)
   168 			)
   161 		);
   169 		);
   162 		wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) );
   170 		wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) );
   163 		wp_redirect( add_query_arg( array( 'update' => 'added', 'id' => $id ), 'site-new.php' ) );
   171 		wp_redirect(
       
   172 			add_query_arg(
       
   173 				array(
       
   174 					'update' => 'added',
       
   175 					'id'     => $id,
       
   176 				),
       
   177 				'site-new.php'
       
   178 			)
       
   179 		);
   164 		exit;
   180 		exit;
   165 	} else {
   181 	} else {
   166 		wp_die( $id->get_error_message() );
   182 		wp_die( $id->get_error_message() );
   167 	}
   183 	}
   168 }
   184 }
   169 
   185 
   170 if ( isset($_GET['update']) ) {
   186 if ( isset( $_GET['update'] ) ) {
   171 	$messages = array();
   187 	$messages = array();
   172 	if ( 'added' == $_GET['update'] )
   188 	if ( 'added' == $_GET['update'] ) {
   173 		$messages[] = sprintf(
   189 		$messages[] = sprintf(
   174 			/* translators: 1: dashboard url, 2: network admin edit url */
   190 			/* translators: 1: dashboard url, 2: network admin edit url */
   175 			__( 'Site added. <a href="%1$s">Visit Dashboard</a> or <a href="%2$s">Edit Site</a>' ),
   191 			__( 'Site added. <a href="%1$s">Visit Dashboard</a> or <a href="%2$s">Edit Site</a>' ),
   176 			esc_url( get_admin_url( absint( $_GET['id'] ) ) ),
   192 			esc_url( get_admin_url( absint( $_GET['id'] ) ) ),
   177 			network_admin_url( 'site-info.php?id=' . absint( $_GET['id'] ) )
   193 			network_admin_url( 'site-info.php?id=' . absint( $_GET['id'] ) )
   178 		);
   194 		);
       
   195 	}
   179 }
   196 }
   180 
   197 
   181 $title = __('Add New Site');
   198 $title       = __( 'Add New Site' );
   182 $parent_file = 'sites.php';
   199 $parent_file = 'sites.php';
   183 
   200 
   184 wp_enqueue_script( 'user-suggest' );
   201 wp_enqueue_script( 'user-suggest' );
   185 
   202 
   186 require( ABSPATH . 'wp-admin/admin-header.php' );
   203 require( ABSPATH . 'wp-admin/admin-header.php' );
   189 
   206 
   190 <div class="wrap">
   207 <div class="wrap">
   191 <h1 id="add-new-site"><?php _e( 'Add New Site' ); ?></h1>
   208 <h1 id="add-new-site"><?php _e( 'Add New Site' ); ?></h1>
   192 <?php
   209 <?php
   193 if ( ! empty( $messages ) ) {
   210 if ( ! empty( $messages ) ) {
   194 	foreach ( $messages as $msg )
   211 	foreach ( $messages as $msg ) {
   195 		echo '<div id="message" class="updated notice is-dismissible"><p>' . $msg . '</p></div>';
   212 		echo '<div id="message" class="updated notice is-dismissible"><p>' . $msg . '</p></div>';
   196 } ?>
   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>
   197 <form method="post" action="<?php echo network_admin_url( 'site-new.php?action=add-site' ); ?>" novalidate="novalidate">
   225 <form method="post" action="<?php echo network_admin_url( 'site-new.php?action=add-site' ); ?>" novalidate="novalidate">
   198 <?php wp_nonce_field( 'add-blog', '_wpnonce_add-blog' ) ?>
   226 <?php wp_nonce_field( 'add-blog', '_wpnonce_add-blog' ); ?>
   199 	<table class="form-table">
   227 	<table class="form-table" role="presentation">
   200 		<tr class="form-field form-required">
   228 		<tr class="form-field form-required">
   201 			<th scope="row"><label for="site-address"><?php _e( 'Site Address (URL)' ) ?></label></th>
   229 			<th scope="row"><label for="site-address"><?php _e( 'Site Address (URL)' ); ?> <span class="required">*</span></label></th>
   202 			<td>
   230 			<td>
   203 			<?php if ( is_subdomain_install() ) { ?>
   231 			<?php if ( is_subdomain_install() ) { ?>
   204 				<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\.|', '', get_network()->domain ); ?></span>
   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>
   205 			<?php } else {
   233 				<?php
   206 				echo get_network()->domain . get_network()->path ?><input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc"  autocapitalize="none" autocorrect="off" />
   234 			} else {
   207 			<?php }
   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 			}
   208 			echo '<p class="description" id="site-address-desc">' . __( 'Only lowercase letters (a-z), numbers, and hyphens are allowed.' ) . '</p>';
   240 			echo '<p class="description" id="site-address-desc">' . __( 'Only lowercase letters (a-z), numbers, and hyphens are allowed.' ) . '</p>';
   209 			?>
   241 			?>
   210 			</td>
   242 			</td>
   211 		</tr>
   243 		</tr>
   212 		<tr class="form-field form-required">
   244 		<tr class="form-field form-required">
   213 			<th scope="row"><label for="site-title"><?php _e( 'Site Title' ) ?></label></th>
   245 			<th scope="row"><label for="site-title"><?php _e( 'Site Title' ); ?> <span class="required">*</span></label></th>
   214 			<td><input name="blog[title]" type="text" class="regular-text" id="site-title" /></td>
   246 			<td><input name="blog[title]" type="text" class="regular-text" id="site-title" required /></td>
   215 		</tr>
   247 		</tr>
   216 		<?php
   248 		<?php
   217 		$languages    = get_available_languages();
   249 		$languages    = get_available_languages();
   218 		$translations = wp_get_available_translations();
   250 		$translations = wp_get_available_translations();
   219 		if ( ! empty( $languages ) || ! empty( $translations ) ) :
   251 		if ( ! empty( $languages ) || ! empty( $translations ) ) :
   243 					?>
   275 					?>
   244 				</td>
   276 				</td>
   245 			</tr>
   277 			</tr>
   246 		<?php endif; // Languages. ?>
   278 		<?php endif; // Languages. ?>
   247 		<tr class="form-field form-required">
   279 		<tr class="form-field form-required">
   248 			<th scope="row"><label for="admin-email"><?php _e( 'Admin Email' ) ?></label></th>
   280 			<th scope="row"><label for="admin-email"><?php _e( 'Admin Email' ); ?> <span class="required">*</span></label></th>
   249 			<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>
   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>
   250 		</tr>
   282 		</tr>
   251 		<tr class="form-field">
   283 		<tr class="form-field">
   252 			<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 a link to set the password will be mailed to this email address.' ) ?></td>
   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>
   253 		</tr>
   285 		</tr>
   254 	</table>
   286 	</table>
   255 
   287 
   256 	<?php
   288 	<?php
   257 	/**
   289 	/**