[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/network/ -> site-users.php (source)

   1  <?php
   2  /**
   3   * Edit Site Users Administration Screen
   4   *
   5   * @package WordPress
   6   * @subpackage Multisite
   7   * @since 3.1.0
   8   */
   9  
  10  /** Load WordPress Administration Bootstrap */
  11  require_once  __DIR__ . '/admin.php';
  12  
  13  if ( ! current_user_can( 'manage_sites' ) ) {
  14      wp_die( __( 'Sorry, you are not allowed to edit this site.' ), 403 );
  15  }
  16  
  17  $wp_list_table = _get_list_table( 'WP_Users_List_Table' );
  18  $wp_list_table->prepare_items();
  19  
  20  get_current_screen()->add_help_tab( get_site_screen_help_tab_args() );
  21  get_current_screen()->set_help_sidebar( get_site_screen_help_sidebar_content() );
  22  
  23  get_current_screen()->set_screen_reader_content(
  24      array(
  25          'heading_views'      => __( 'Filter site users list' ),
  26          'heading_pagination' => __( 'Site users list navigation' ),
  27          'heading_list'       => __( 'Site users list' ),
  28      )
  29  );
  30  
  31  $_SERVER['REQUEST_URI'] = remove_query_arg( 'update', $_SERVER['REQUEST_URI'] );
  32  $referer                = remove_query_arg( 'update', wp_get_referer() );
  33  
  34  if ( ! empty( $_REQUEST['paged'] ) ) {
  35      $referer = add_query_arg( 'paged', (int) $_REQUEST['paged'], $referer );
  36  }
  37  
  38  $id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0;
  39  
  40  if ( ! $id ) {
  41      wp_die( __( 'Invalid site ID.' ) );
  42  }
  43  
  44  $details = get_site( $id );
  45  if ( ! $details ) {
  46      wp_die( __( 'The requested site does not exist.' ) );
  47  }
  48  
  49  if ( ! can_edit_network( $details->site_id ) ) {
  50      wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 );
  51  }
  52  
  53  $is_main_site = is_main_site( $id );
  54  
  55  switch_to_blog( $id );
  56  
  57  $action = $wp_list_table->current_action();
  58  
  59  if ( $action ) {
  60  
  61      switch ( $action ) {
  62          case 'newuser':
  63              check_admin_referer( 'add-user', '_wpnonce_add-new-user' );
  64              $user = $_POST['user'];
  65              if ( ! is_array( $_POST['user'] ) || empty( $user['username'] ) || empty( $user['email'] ) ) {
  66                  $update = 'err_new';
  67              } else {
  68                  $password = wp_generate_password( 12, false );
  69                  $user_id  = wpmu_create_user( esc_html( strtolower( $user['username'] ) ), $password, esc_html( $user['email'] ) );
  70  
  71                  if ( false === $user_id ) {
  72                      $update = 'err_new_dup';
  73                  } else {
  74                      $result = add_user_to_blog( $id, $user_id, $_POST['new_role'] );
  75  
  76                      if ( is_wp_error( $result ) ) {
  77                          $update = 'err_add_fail';
  78                      } else {
  79                          $update = 'newuser';
  80  
  81                          /**
  82                           * Fires after a user has been created via the network site-users.php page.
  83                           *
  84                           * @since 4.4.0
  85                           *
  86                           * @param int $user_id ID of the newly created user.
  87                           */
  88                          do_action( 'network_site_users_created_user', $user_id );
  89                      }
  90                  }
  91              }
  92              break;
  93  
  94          case 'adduser':
  95              check_admin_referer( 'add-user', '_wpnonce_add-user' );
  96              if ( ! empty( $_POST['newuser'] ) ) {
  97                  $update  = 'adduser';
  98                  $newuser = $_POST['newuser'];
  99                  $user    = get_user_by( 'login', $newuser );
 100                  if ( $user && $user->exists() ) {
 101                      if ( ! is_user_member_of_blog( $user->ID, $id ) ) {
 102                          $result = add_user_to_blog( $id, $user->ID, $_POST['new_role'] );
 103  
 104                          if ( is_wp_error( $result ) ) {
 105                              $update = 'err_add_fail';
 106                          }
 107                      } else {
 108                          $update = 'err_add_member';
 109                      }
 110                  } else {
 111                      $update = 'err_add_notfound';
 112                  }
 113              } else {
 114                  $update = 'err_add_notfound';
 115              }
 116              break;
 117  
 118          case 'remove':
 119              if ( ! current_user_can( 'remove_users' ) ) {
 120                  wp_die( __( 'Sorry, you are not allowed to remove users.' ), 403 );
 121              }
 122  
 123              check_admin_referer( 'bulk-users' );
 124  
 125              $update = 'remove';
 126              if ( isset( $_REQUEST['users'] ) ) {
 127                  $userids = $_REQUEST['users'];
 128  
 129                  foreach ( $userids as $user_id ) {
 130                      $user_id = (int) $user_id;
 131                      remove_user_from_blog( $user_id, $id );
 132                  }
 133              } elseif ( isset( $_GET['user'] ) ) {
 134                  remove_user_from_blog( $_GET['user'] );
 135              } else {
 136                  $update = 'err_remove';
 137              }
 138              break;
 139  
 140          case 'promote':
 141              check_admin_referer( 'bulk-users' );
 142              $editable_roles = get_editable_roles();
 143              $role           = $_REQUEST['new_role'];
 144  
 145              if ( empty( $editable_roles[ $role ] ) ) {
 146                  wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
 147              }
 148  
 149              if ( isset( $_REQUEST['users'] ) ) {
 150                  $userids = $_REQUEST['users'];
 151                  $update  = 'promote';
 152                  foreach ( $userids as $user_id ) {
 153                      $user_id = (int) $user_id;
 154  
 155                      // If the user doesn't already belong to the blog, bail.
 156                      if ( ! is_user_member_of_blog( $user_id ) ) {
 157                          wp_die(
 158                              '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
 159                              '<p>' . __( 'One of the selected users is not a member of this site.' ) . '</p>',
 160                              403
 161                          );
 162                      }
 163  
 164                      $user = get_userdata( $user_id );
 165                      $user->set_role( $role );
 166                  }
 167              } else {
 168                  $update = 'err_promote';
 169              }
 170              break;
 171          default:
 172              if ( ! isset( $_REQUEST['users'] ) ) {
 173                  break;
 174              }
 175              check_admin_referer( 'bulk-users' );
 176              $userids = $_REQUEST['users'];
 177  
 178              /** This action is documented in wp-admin/network/site-themes.php */
 179              $referer = apply_filters( 'handle_network_bulk_actions-' . get_current_screen()->id, $referer, $action, $userids, $id ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 180  
 181              $update = $action;
 182              break;
 183      }
 184  
 185      wp_safe_redirect( add_query_arg( 'update', $update, $referer ) );
 186      exit;
 187  }
 188  
 189  restore_current_blog();
 190  
 191  if ( isset( $_GET['action'] ) && 'update-site' === $_GET['action'] ) {
 192      wp_safe_redirect( $referer );
 193      exit;
 194  }
 195  
 196  add_screen_option( 'per_page' );
 197  
 198  // Used in the HTML title tag.
 199  /* translators: %s: Site title. */
 200  $title = sprintf( __( 'Edit Site: %s' ), esc_html( $details->blogname ) );
 201  
 202  $parent_file  = 'sites.php';
 203  $submenu_file = 'sites.php';
 204  
 205  /**
 206   * Filters whether to show the Add Existing User form on the Multisite Users screen.
 207   *
 208   * @since 3.1.0
 209   *
 210   * @param bool $bool Whether to show the Add Existing User form. Default true.
 211   */
 212  if ( ! wp_is_large_network( 'users' ) && apply_filters( 'show_network_site_users_add_existing_form', true ) ) {
 213      wp_enqueue_script( 'user-suggest' );
 214  }
 215  
 216  require_once ABSPATH . 'wp-admin/admin-header.php'; ?>
 217  
 218  <script type="text/javascript">
 219  var current_site_id = <?php echo absint( $id ); ?>;
 220  </script>
 221  
 222  
 223  <div class="wrap">
 224  <h1 id="edit-site"><?php echo $title; ?></h1>
 225  <p class="edit-site-actions"><a href="<?php echo esc_url( get_home_url( $id, '/' ) ); ?>"><?php _e( 'Visit' ); ?></a> | <a href="<?php echo esc_url( get_admin_url( $id ) ); ?>"><?php _e( 'Dashboard' ); ?></a></p>
 226  <?php
 227  
 228  network_edit_site_nav(
 229      array(
 230          'blog_id'  => $id,
 231          'selected' => 'site-users',
 232      )
 233  );
 234  
 235  if ( isset( $_GET['update'] ) ) :
 236      switch ( $_GET['update'] ) {
 237          case 'adduser':
 238              echo '<div id="message" class="updated notice is-dismissible"><p>' . __( 'User added.' ) . '</p></div>';
 239              break;
 240          case 'err_add_member':
 241              echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'User is already a member of this site.' ) . '</p></div>';
 242              break;
 243          case 'err_add_fail':
 244              echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'User could not be added to this site.' ) . '</p></div>';
 245              break;
 246          case 'err_add_notfound':
 247              echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Enter the username of an existing user.' ) . '</p></div>';
 248              break;
 249          case 'promote':
 250              echo '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Changed roles.' ) . '</p></div>';
 251              break;
 252          case 'err_promote':
 253              echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Select a user to change role.' ) . '</p></div>';
 254              break;
 255          case 'remove':
 256              echo '<div id="message" class="updated notice is-dismissible"><p>' . __( 'User removed from this site.' ) . '</p></div>';
 257              break;
 258          case 'err_remove':
 259              echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Select a user to remove.' ) . '</p></div>';
 260              break;
 261          case 'newuser':
 262              echo '<div id="message" class="updated notice is-dismissible"><p>' . __( 'User created.' ) . '</p></div>';
 263              break;
 264          case 'err_new':
 265              echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Enter the username and email.' ) . '</p></div>';
 266              break;
 267          case 'err_new_dup':
 268              echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Duplicated username or email address.' ) . '</p></div>';
 269              break;
 270      }
 271  endif;
 272  ?>
 273  
 274  <form class="search-form" method="get">
 275  <?php $wp_list_table->search_box( __( 'Search Users' ), 'user' ); ?>
 276  <input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
 277  </form>
 278  
 279  <?php $wp_list_table->views(); ?>
 280  
 281  <form method="post" action="site-users.php?action=update-site">
 282      <input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
 283  
 284  <?php $wp_list_table->display(); ?>
 285  
 286  </form>
 287  
 288  <?php
 289  /**
 290   * Fires after the list table on the Users screen in the Multisite Network Admin.
 291   *
 292   * @since 3.1.0
 293   */
 294  do_action( 'network_site_users_after_list_table' );
 295  
 296  /** This filter is documented in wp-admin/network/site-users.php */
 297  if ( current_user_can( 'promote_users' ) && apply_filters( 'show_network_site_users_add_existing_form', true ) ) :
 298      ?>
 299  <h2 id="add-existing-user"><?php _e( 'Add Existing User' ); ?></h2>
 300  <form action="site-users.php?action=adduser" id="adduser" method="post">
 301      <input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
 302      <table class="form-table" role="presentation">
 303          <tr>
 304              <th scope="row"><label for="newuser"><?php _e( 'Username' ); ?></label></th>
 305              <td><input type="text" class="regular-text wp-suggest-user" name="newuser" id="newuser" /></td>
 306          </tr>
 307          <tr>
 308              <th scope="row"><label for="new_role_adduser"><?php _e( 'Role' ); ?></label></th>
 309              <td><select name="new_role" id="new_role_adduser">
 310              <?php
 311              switch_to_blog( $id );
 312              wp_dropdown_roles( get_option( 'default_role' ) );
 313              restore_current_blog();
 314              ?>
 315              </select></td>
 316          </tr>
 317      </table>
 318      <?php wp_nonce_field( 'add-user', '_wpnonce_add-user' ); ?>
 319      <?php submit_button( __( 'Add User' ), 'primary', 'add-user', true, array( 'id' => 'submit-add-existing-user' ) ); ?>
 320  </form>
 321  <?php endif; ?>
 322  
 323  <?php
 324  /**
 325   * Filters whether to show the Add New User form on the Multisite Users screen.
 326   *
 327   * @since 3.1.0
 328   *
 329   * @param bool $bool Whether to show the Add New User form. Default true.
 330   */
 331  if ( current_user_can( 'create_users' ) && apply_filters( 'show_network_site_users_add_new_form', true ) ) :
 332      ?>
 333  <h2 id="add-new-user"><?php _e( 'Add New User' ); ?></h2>
 334  <form action="<?php echo esc_url( network_admin_url( 'site-users.php?action=newuser' ) ); ?>" id="newuser" method="post">
 335      <input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
 336      <table class="form-table" role="presentation">
 337          <tr>
 338              <th scope="row"><label for="user_username"><?php _e( 'Username' ); ?></label></th>
 339              <td><input type="text" class="regular-text" name="user[username]" id="user_username" /></td>
 340          </tr>
 341          <tr>
 342              <th scope="row"><label for="user_email"><?php _e( 'Email' ); ?></label></th>
 343              <td><input type="text" class="regular-text" name="user[email]" id="user_email" /></td>
 344          </tr>
 345          <tr>
 346              <th scope="row"><label for="new_role_newuser"><?php _e( 'Role' ); ?></label></th>
 347              <td><select name="new_role" id="new_role_newuser">
 348              <?php
 349              switch_to_blog( $id );
 350              wp_dropdown_roles( get_option( 'default_role' ) );
 351              restore_current_blog();
 352              ?>
 353              </select></td>
 354          </tr>
 355          <tr class="form-field">
 356              <td colspan="2" class="td-full"><?php _e( 'A password reset link will be sent to the user via email.' ); ?></td>
 357          </tr>
 358      </table>
 359      <?php wp_nonce_field( 'add-user', '_wpnonce_add-new-user' ); ?>
 360      <?php submit_button( __( 'Add New User' ), 'primary', 'add-user', true, array( 'id' => 'submit-add-user' ) ); ?>
 361  </form>
 362  <?php endif; ?>
 363  </div>
 364  <?php
 365  require_once ABSPATH . 'wp-admin/admin-footer.php';


Generated: Fri Jul 30 01:00:09 2021 Cross-referenced by PHPXref 0.7.1