ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-ms-users-list-table.php (14111B)


      1 <?php
      2 /**
      3  * List Table API: WP_MS_Users_List_Table class
      4  *
      5  * @package WordPress
      6  * @subpackage Administration
      7  * @since 3.1.0
      8  */
      9 
     10 /**
     11  * Core class used to implement displaying users in a list table for the network admin.
     12  *
     13  * @since 3.1.0
     14  * @access private
     15  *
     16  * @see WP_List_Table
     17  */
     18 class WP_MS_Users_List_Table extends WP_List_Table {
     19 	/**
     20 	 * @return bool
     21 	 */
     22 	public function ajax_user_can() {
     23 		return current_user_can( 'manage_network_users' );
     24 	}
     25 
     26 	/**
     27 	 * @global string $mode       List table view mode.
     28 	 * @global string $usersearch
     29 	 * @global string $role
     30 	 */
     31 	public function prepare_items() {
     32 		global $mode, $usersearch, $role;
     33 
     34 		if ( ! empty( $_REQUEST['mode'] ) ) {
     35 			$mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
     36 			set_user_setting( 'network_users_list_mode', $mode );
     37 		} else {
     38 			$mode = get_user_setting( 'network_users_list_mode', 'list' );
     39 		}
     40 
     41 		$usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
     42 
     43 		$users_per_page = $this->get_items_per_page( 'users_network_per_page' );
     44 
     45 		$role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
     46 
     47 		$paged = $this->get_pagenum();
     48 
     49 		$args = array(
     50 			'number'  => $users_per_page,
     51 			'offset'  => ( $paged - 1 ) * $users_per_page,
     52 			'search'  => $usersearch,
     53 			'blog_id' => 0,
     54 			'fields'  => 'all_with_meta',
     55 		);
     56 
     57 		if ( wp_is_large_network( 'users' ) ) {
     58 			$args['search'] = ltrim( $args['search'], '*' );
     59 		} elseif ( '' !== $args['search'] ) {
     60 			$args['search'] = trim( $args['search'], '*' );
     61 			$args['search'] = '*' . $args['search'] . '*';
     62 		}
     63 
     64 		if ( 'super' === $role ) {
     65 			$args['login__in'] = get_super_admins();
     66 		}
     67 
     68 		/*
     69 		 * If the network is large and a search is not being performed,
     70 		 * show only the latest users with no paging in order to avoid
     71 		 * expensive count queries.
     72 		 */
     73 		if ( ! $usersearch && wp_is_large_network( 'users' ) ) {
     74 			if ( ! isset( $_REQUEST['orderby'] ) ) {
     75 				$_GET['orderby']     = 'id';
     76 				$_REQUEST['orderby'] = 'id';
     77 			}
     78 			if ( ! isset( $_REQUEST['order'] ) ) {
     79 				$_GET['order']     = 'DESC';
     80 				$_REQUEST['order'] = 'DESC';
     81 			}
     82 			$args['count_total'] = false;
     83 		}
     84 
     85 		if ( isset( $_REQUEST['orderby'] ) ) {
     86 			$args['orderby'] = $_REQUEST['orderby'];
     87 		}
     88 
     89 		if ( isset( $_REQUEST['order'] ) ) {
     90 			$args['order'] = $_REQUEST['order'];
     91 		}
     92 
     93 		/** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
     94 		$args = apply_filters( 'users_list_table_query_args', $args );
     95 
     96 		// Query the user IDs for this page.
     97 		$wp_user_search = new WP_User_Query( $args );
     98 
     99 		$this->items = $wp_user_search->get_results();
    100 
    101 		$this->set_pagination_args(
    102 			array(
    103 				'total_items' => $wp_user_search->get_total(),
    104 				'per_page'    => $users_per_page,
    105 			)
    106 		);
    107 	}
    108 
    109 	/**
    110 	 * @return array
    111 	 */
    112 	protected function get_bulk_actions() {
    113 		$actions = array();
    114 		if ( current_user_can( 'delete_users' ) ) {
    115 			$actions['delete'] = __( 'Delete' );
    116 		}
    117 		$actions['spam']    = _x( 'Mark as spam', 'user' );
    118 		$actions['notspam'] = _x( 'Not spam', 'user' );
    119 
    120 		return $actions;
    121 	}
    122 
    123 	/**
    124 	 */
    125 	public function no_items() {
    126 		_e( 'No users found.' );
    127 	}
    128 
    129 	/**
    130 	 * @global string $role
    131 	 * @return array
    132 	 */
    133 	protected function get_views() {
    134 		global $role;
    135 
    136 		$total_users  = get_user_count();
    137 		$super_admins = get_super_admins();
    138 		$total_admins = count( $super_admins );
    139 
    140 		$current_link_attributes = 'super' !== $role ? ' class="current" aria-current="page"' : '';
    141 		$role_links              = array();
    142 		$role_links['all']       = sprintf(
    143 			'<a href="%s"%s>%s</a>',
    144 			network_admin_url( 'users.php' ),
    145 			$current_link_attributes,
    146 			sprintf(
    147 				/* translators: Number of users. */
    148 				_nx(
    149 					'All <span class="count">(%s)</span>',
    150 					'All <span class="count">(%s)</span>',
    151 					$total_users,
    152 					'users'
    153 				),
    154 				number_format_i18n( $total_users )
    155 			)
    156 		);
    157 		$current_link_attributes = 'super' === $role ? ' class="current" aria-current="page"' : '';
    158 		$role_links['super']     = sprintf(
    159 			'<a href="%s"%s>%s</a>',
    160 			network_admin_url( 'users.php?role=super' ),
    161 			$current_link_attributes,
    162 			sprintf(
    163 				/* translators: Number of users. */
    164 				_n(
    165 					'Super Admin <span class="count">(%s)</span>',
    166 					'Super Admins <span class="count">(%s)</span>',
    167 					$total_admins
    168 				),
    169 				number_format_i18n( $total_admins )
    170 			)
    171 		);
    172 
    173 		return $role_links;
    174 	}
    175 
    176 	/**
    177 	 * @global string $mode List table view mode.
    178 	 *
    179 	 * @param string $which
    180 	 */
    181 	protected function pagination( $which ) {
    182 		global $mode;
    183 
    184 		parent::pagination( $which );
    185 
    186 		if ( 'top' === $which ) {
    187 			$this->view_switcher( $mode );
    188 		}
    189 	}
    190 
    191 	/**
    192 	 * @return array
    193 	 */
    194 	public function get_columns() {
    195 		$users_columns = array(
    196 			'cb'         => '<input type="checkbox" />',
    197 			'username'   => __( 'Username' ),
    198 			'name'       => __( 'Name' ),
    199 			'email'      => __( 'Email' ),
    200 			'registered' => _x( 'Registered', 'user' ),
    201 			'blogs'      => __( 'Sites' ),
    202 		);
    203 		/**
    204 		 * Filters the columns displayed in the Network Admin Users list table.
    205 		 *
    206 		 * @since MU (3.0.0)
    207 		 *
    208 		 * @param string[] $users_columns An array of user columns. Default 'cb', 'username',
    209 		 *                                'name', 'email', 'registered', 'blogs'.
    210 		 */
    211 		return apply_filters( 'wpmu_users_columns', $users_columns );
    212 	}
    213 
    214 	/**
    215 	 * @return array
    216 	 */
    217 	protected function get_sortable_columns() {
    218 		return array(
    219 			'username'   => 'login',
    220 			'name'       => 'name',
    221 			'email'      => 'email',
    222 			'registered' => 'id',
    223 		);
    224 	}
    225 
    226 	/**
    227 	 * Handles the checkbox column output.
    228 	 *
    229 	 * @since 4.3.0
    230 	 *
    231 	 * @param WP_User $user The current WP_User object.
    232 	 */
    233 	public function column_cb( $user ) {
    234 		if ( is_super_admin( $user->ID ) ) {
    235 			return;
    236 		}
    237 		?>
    238 		<label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>">
    239 			<?php
    240 			/* translators: %s: User login. */
    241 			printf( __( 'Select %s' ), $user->user_login );
    242 			?>
    243 		</label>
    244 		<input type="checkbox" id="blog_<?php echo $user->ID; ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ); ?>" />
    245 		<?php
    246 	}
    247 
    248 	/**
    249 	 * Handles the ID column output.
    250 	 *
    251 	 * @since 4.4.0
    252 	 *
    253 	 * @param WP_User $user The current WP_User object.
    254 	 */
    255 	public function column_id( $user ) {
    256 		echo $user->ID;
    257 	}
    258 
    259 	/**
    260 	 * Handles the username column output.
    261 	 *
    262 	 * @since 4.3.0
    263 	 *
    264 	 * @param WP_User $user The current WP_User object.
    265 	 */
    266 	public function column_username( $user ) {
    267 		$super_admins = get_super_admins();
    268 		$avatar       = get_avatar( $user->user_email, 32 );
    269 
    270 		echo $avatar;
    271 
    272 		if ( current_user_can( 'edit_user', $user->ID ) ) {
    273 			$edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
    274 			$edit      = "<a href=\"{$edit_link}\">{$user->user_login}</a>";
    275 		} else {
    276 			$edit = $user->user_login;
    277 		}
    278 
    279 		?>
    280 		<strong>
    281 			<?php
    282 			echo $edit;
    283 
    284 			if ( in_array( $user->user_login, $super_admins, true ) ) {
    285 				echo ' &mdash; ' . __( 'Super Admin' );
    286 			}
    287 			?>
    288 		</strong>
    289 		<?php
    290 	}
    291 
    292 	/**
    293 	 * Handles the name column output.
    294 	 *
    295 	 * @since 4.3.0
    296 	 *
    297 	 * @param WP_User $user The current WP_User object.
    298 	 */
    299 	public function column_name( $user ) {
    300 		if ( $user->first_name && $user->last_name ) {
    301 			echo "$user->first_name $user->last_name";
    302 		} elseif ( $user->first_name ) {
    303 			echo $user->first_name;
    304 		} elseif ( $user->last_name ) {
    305 			echo $user->last_name;
    306 		} else {
    307 			echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . _x( 'Unknown', 'name' ) . '</span>';
    308 		}
    309 	}
    310 
    311 	/**
    312 	 * Handles the email column output.
    313 	 *
    314 	 * @since 4.3.0
    315 	 *
    316 	 * @param WP_User $user The current WP_User object.
    317 	 */
    318 	public function column_email( $user ) {
    319 		echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
    320 	}
    321 
    322 	/**
    323 	 * Handles the registered date column output.
    324 	 *
    325 	 * @since 4.3.0
    326 	 *
    327 	 * @global string $mode List table view mode.
    328 	 *
    329 	 * @param WP_User $user The current WP_User object.
    330 	 */
    331 	public function column_registered( $user ) {
    332 		global $mode;
    333 		if ( 'list' === $mode ) {
    334 			$date = __( 'Y/m/d' );
    335 		} else {
    336 			$date = __( 'Y/m/d g:i:s a' );
    337 		}
    338 		echo mysql2date( $date, $user->user_registered );
    339 	}
    340 
    341 	/**
    342 	 * @since 4.3.0
    343 	 *
    344 	 * @param WP_User $user
    345 	 * @param string  $classes
    346 	 * @param string  $data
    347 	 * @param string  $primary
    348 	 */
    349 	protected function _column_blogs( $user, $classes, $data, $primary ) {
    350 		echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
    351 		echo $this->column_blogs( $user );
    352 		echo $this->handle_row_actions( $user, 'blogs', $primary );
    353 		echo '</td>';
    354 	}
    355 
    356 	/**
    357 	 * Handles the sites column output.
    358 	 *
    359 	 * @since 4.3.0
    360 	 *
    361 	 * @param WP_User $user The current WP_User object.
    362 	 */
    363 	public function column_blogs( $user ) {
    364 		$blogs = get_blogs_of_user( $user->ID, true );
    365 		if ( ! is_array( $blogs ) ) {
    366 			return;
    367 		}
    368 
    369 		foreach ( $blogs as $val ) {
    370 			if ( ! can_edit_network( $val->site_id ) ) {
    371 				continue;
    372 			}
    373 
    374 			$path         = ( '/' === $val->path ) ? '' : $val->path;
    375 			$site_classes = array( 'site-' . $val->site_id );
    376 			/**
    377 			 * Filters the span class for a site listing on the mulisite user list table.
    378 			 *
    379 			 * @since 5.2.0
    380 			 *
    381 			 * @param string[] $site_classes Array of class names used within the span tag. Default "site-#" with the site's network ID.
    382 			 * @param int      $site_id      Site ID.
    383 			 * @param int      $network_id   Network ID.
    384 			 * @param WP_User  $user         WP_User object.
    385 			 */
    386 			$site_classes = apply_filters( 'ms_user_list_site_class', $site_classes, $val->userblog_id, $val->site_id, $user );
    387 			if ( is_array( $site_classes ) && ! empty( $site_classes ) ) {
    388 				$site_classes = array_map( 'sanitize_html_class', array_unique( $site_classes ) );
    389 				echo '<span class="' . esc_attr( implode( ' ', $site_classes ) ) . '">';
    390 			} else {
    391 				echo '<span>';
    392 			}
    393 			echo '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) . '">' . str_replace( '.' . get_network()->domain, '', $val->domain . $path ) . '</a>';
    394 			echo ' <small class="row-actions">';
    395 			$actions         = array();
    396 			$actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) . '">' . __( 'Edit' ) . '</a>';
    397 
    398 			$class = '';
    399 			if ( 1 == $val->spam ) {
    400 				$class .= 'site-spammed ';
    401 			}
    402 			if ( 1 == $val->mature ) {
    403 				$class .= 'site-mature ';
    404 			}
    405 			if ( 1 == $val->deleted ) {
    406 				$class .= 'site-deleted ';
    407 			}
    408 			if ( 1 == $val->archived ) {
    409 				$class .= 'site-archived ';
    410 			}
    411 
    412 			$actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
    413 
    414 			/**
    415 			 * Filters the action links displayed next the sites a user belongs to
    416 			 * in the Network Admin Users list table.
    417 			 *
    418 			 * @since 3.1.0
    419 			 *
    420 			 * @param string[] $actions     An array of action links to be displayed. Default 'Edit', 'View'.
    421 			 * @param int      $userblog_id The site ID.
    422 			 */
    423 			$actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
    424 
    425 			$action_count = count( $actions );
    426 
    427 			$i = 0;
    428 
    429 			foreach ( $actions as $action => $link ) {
    430 				++$i;
    431 
    432 				$sep = ( $i < $action_count ) ? ' | ' : '';
    433 
    434 				echo "<span class='$action'>$link$sep</span>";
    435 			}
    436 
    437 			echo '</small></span><br/>';
    438 		}
    439 	}
    440 
    441 	/**
    442 	 * Handles the default column output.
    443 	 *
    444 	 * @since 4.3.0
    445 	 *
    446 	 * @param WP_User $user        The current WP_User object.
    447 	 * @param string  $column_name The current column name.
    448 	 */
    449 	public function column_default( $user, $column_name ) {
    450 		/** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
    451 		echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
    452 	}
    453 
    454 	public function display_rows() {
    455 		foreach ( $this->items as $user ) {
    456 			$class = '';
    457 
    458 			$status_list = array(
    459 				'spam'    => 'site-spammed',
    460 				'deleted' => 'site-deleted',
    461 			);
    462 
    463 			foreach ( $status_list as $status => $col ) {
    464 				if ( $user->$status ) {
    465 					$class .= " $col";
    466 				}
    467 			}
    468 
    469 			?>
    470 			<tr class="<?php echo trim( $class ); ?>">
    471 				<?php $this->single_row_columns( $user ); ?>
    472 			</tr>
    473 			<?php
    474 		}
    475 	}
    476 
    477 	/**
    478 	 * Gets the name of the default primary column.
    479 	 *
    480 	 * @since 4.3.0
    481 	 *
    482 	 * @return string Name of the default primary column, in this case, 'username'.
    483 	 */
    484 	protected function get_default_primary_column_name() {
    485 		return 'username';
    486 	}
    487 
    488 	/**
    489 	 * Generates and displays row action links.
    490 	 *
    491 	 * @since 4.3.0
    492 	 *
    493 	 * @param WP_User $user        User being acted upon.
    494 	 * @param string  $column_name Current column name.
    495 	 * @param string  $primary     Primary column name.
    496 	 * @return string Row actions output for users in Multisite, or an empty string
    497 	 *                if the current column is not the primary column.
    498 	 */
    499 	protected function handle_row_actions( $user, $column_name, $primary ) {
    500 		if ( $primary !== $column_name ) {
    501 			return '';
    502 		}
    503 
    504 		$super_admins = get_super_admins();
    505 
    506 		$actions = array();
    507 
    508 		if ( current_user_can( 'edit_user', $user->ID ) ) {
    509 			$edit_link       = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
    510 			$actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
    511 		}
    512 
    513 		if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins, true ) ) {
    514 			$actions['delete'] = '<a href="' . esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&amp;action=deleteuser&amp;id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
    515 		}
    516 
    517 		/**
    518 		 * Filters the action links displayed under each user in the Network Admin Users list table.
    519 		 *
    520 		 * @since 3.2.0
    521 		 *
    522 		 * @param string[] $actions An array of action links to be displayed. Default 'Edit', 'Delete'.
    523 		 * @param WP_User  $user    WP_User object.
    524 		 */
    525 		$actions = apply_filters( 'ms_user_row_actions', $actions, $user );
    526 
    527 		return $this->row_actions( $actions );
    528 	}
    529 }