home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-admin / network / site-new.php < prev    next >
Encoding:
PHP Script  |  2018-01-24  |  9.0 KB  |  270 lines

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