wp/wp-admin/includes/class-wp-ms-sites-list-table.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 <?php
       
     2 /**
       
     3  * Sites List Table class.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage List_Table
       
     7  * @since 3.1.0
       
     8  * @access private
       
     9  */
       
    10 class WP_MS_Sites_List_Table extends WP_List_Table {
       
    11 
       
    12 	function __construct( $args = array() ) {
       
    13 		parent::__construct( array(
       
    14 			'plural' => 'sites',
       
    15 			'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
       
    16 		) );
       
    17 	}
       
    18 
       
    19 	function ajax_user_can() {
       
    20 		return current_user_can( 'manage_sites' );
       
    21 	}
       
    22 
       
    23 	function prepare_items() {
       
    24 		global $s, $mode, $wpdb, $current_site;
       
    25 
       
    26 		$mode = ( empty( $_REQUEST['mode'] ) ) ? 'list' : $_REQUEST['mode'];
       
    27 
       
    28 		$per_page = $this->get_items_per_page( 'sites_network_per_page' );
       
    29 
       
    30 		$pagenum = $this->get_pagenum();
       
    31 
       
    32 		$s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
       
    33 		$wild = '';
       
    34 		if ( false !== strpos($s, '*') ) {
       
    35 			$wild = '%';
       
    36 			$s = trim($s, '*');
       
    37 		}
       
    38 
       
    39 		$like_s = esc_sql( like_escape( $s ) );
       
    40 
       
    41 		// If the network is large and a search is not being performed, show only the latest blogs with no paging in order
       
    42 		// to avoid expensive count queries.
       
    43 		if ( !$s && wp_is_large_network() ) {
       
    44 			if ( !isset($_REQUEST['orderby']) )
       
    45 				$_GET['orderby'] = $_REQUEST['orderby'] = '';
       
    46 			if ( !isset($_REQUEST['order']) )
       
    47 				$_GET['order'] = $_REQUEST['order'] = 'DESC';
       
    48 		}
       
    49 
       
    50 		$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
       
    51 
       
    52 		if ( empty($s) ) {
       
    53 			// Nothing to do.
       
    54 		} elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
       
    55 					preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
       
    56 					preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
       
    57 					preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
       
    58 			// IPv4 address
       
    59 			$reg_blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE ( '{$like_s}$wild' )" );
       
    60 
       
    61 			if ( !$reg_blog_ids )
       
    62 				$reg_blog_ids = array( 0 );
       
    63 
       
    64 			$query = "SELECT *
       
    65 				FROM {$wpdb->blogs}
       
    66 				WHERE site_id = '{$wpdb->siteid}'
       
    67 				AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
       
    68 		} else {
       
    69 			if ( is_numeric($s) && empty( $wild ) ) {
       
    70 				$query .= " AND ( {$wpdb->blogs}.blog_id = '{$like_s}' )";
       
    71 			} elseif ( is_subdomain_install() ) {
       
    72 				$blog_s = str_replace( '.' . $current_site->domain, '', $like_s );
       
    73 				$blog_s .= $wild . '.' . $current_site->domain;
       
    74 				$query .= " AND ( {$wpdb->blogs}.domain LIKE '$blog_s' ) ";
       
    75 			} else {
       
    76 				if ( $like_s != trim('/', $current_site->path) )
       
    77 					$blog_s = $current_site->path . $like_s . $wild . '/';
       
    78 				else
       
    79 					$blog_s = $like_s;
       
    80 				$query .= " AND  ( {$wpdb->blogs}.path LIKE '$blog_s' )";
       
    81 			}
       
    82 		}
       
    83 
       
    84 		$order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
       
    85 		if ( $order_by == 'registered' ) {
       
    86 			$query .= ' ORDER BY registered ';
       
    87 		} elseif ( $order_by == 'lastupdated' ) {
       
    88 			$query .= ' ORDER BY last_updated ';
       
    89 		} elseif ( $order_by == 'blogname' ) {
       
    90 			if ( is_subdomain_install() )
       
    91 				$query .= ' ORDER BY domain ';
       
    92 			else
       
    93 				$query .= ' ORDER BY path ';
       
    94 		} elseif ( $order_by == 'blog_id' ) {
       
    95 			$query .= ' ORDER BY blog_id ';
       
    96 		} else {
       
    97 			$order_by = null;
       
    98 		}
       
    99 
       
   100 		if ( isset( $order_by ) ) {
       
   101 			$order = ( isset( $_REQUEST['order'] ) && 'DESC' == strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
       
   102 			$query .= $order;
       
   103 		}
       
   104 
       
   105 		// Don't do an unbounded count on large networks
       
   106 		if ( ! wp_is_large_network() )
       
   107 			$total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) );
       
   108 
       
   109 		$query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
       
   110 		$this->items = $wpdb->get_results( $query, ARRAY_A );
       
   111 
       
   112 		if ( wp_is_large_network() )
       
   113 			$total = count($this->items);
       
   114 
       
   115 		$this->set_pagination_args( array(
       
   116 			'total_items' => $total,
       
   117 			'per_page' => $per_page,
       
   118 		) );
       
   119 	}
       
   120 
       
   121 	function no_items() {
       
   122 		_e( 'No sites found.' );
       
   123 	}
       
   124 
       
   125 	function get_bulk_actions() {
       
   126 		$actions = array();
       
   127 		if ( current_user_can( 'delete_sites' ) )
       
   128 			$actions['delete'] = __( 'Delete' );
       
   129 		$actions['spam'] = _x( 'Mark as Spam', 'site' );
       
   130 		$actions['notspam'] = _x( 'Not Spam', 'site' );
       
   131 
       
   132 		return $actions;
       
   133 	}
       
   134 
       
   135 	function pagination( $which ) {
       
   136 		global $mode;
       
   137 
       
   138 		parent::pagination( $which );
       
   139 
       
   140 		if ( 'top' == $which )
       
   141 			$this->view_switcher( $mode );
       
   142 	}
       
   143 
       
   144 	function get_columns() {
       
   145 		$blogname_columns = ( is_subdomain_install() ) ? __( 'Domain' ) : __( 'Path' );
       
   146 		$sites_columns = array(
       
   147 			'cb'          => '<input type="checkbox" />',
       
   148 			'blogname'    => $blogname_columns,
       
   149 			'lastupdated' => __( 'Last Updated' ),
       
   150 			'registered'  => _x( 'Registered', 'site' ),
       
   151 			'users'       => __( 'Users' )
       
   152 		);
       
   153 
       
   154 		if ( has_filter( 'wpmublogsaction' ) )
       
   155 			$sites_columns['plugins'] = __( 'Actions' );
       
   156 
       
   157 		$sites_columns = apply_filters( 'wpmu_blogs_columns', $sites_columns );
       
   158 
       
   159 		return $sites_columns;
       
   160 	}
       
   161 
       
   162 	function get_sortable_columns() {
       
   163 		return array(
       
   164 			'blogname'    => 'blogname',
       
   165 			'lastupdated' => 'lastupdated',
       
   166 			'registered'  => 'blog_id',
       
   167 		);
       
   168 	}
       
   169 
       
   170 	function display_rows() {
       
   171 		global $current_site, $mode;
       
   172 
       
   173 		$status_list = array(
       
   174 			'archived' => array( 'site-archived', __( 'Archived' ) ),
       
   175 			'spam'     => array( 'site-spammed', _x( 'Spam', 'site' ) ),
       
   176 			'deleted'  => array( 'site-deleted', __( 'Deleted' ) ),
       
   177 			'mature'   => array( 'site-mature', __( 'Mature' ) )
       
   178 		);
       
   179 
       
   180 		$class = '';
       
   181 		foreach ( $this->items as $blog ) {
       
   182 			$class = ( 'alternate' == $class ) ? '' : 'alternate';
       
   183 			reset( $status_list );
       
   184 
       
   185 			$blog_states = array();
       
   186 			foreach ( $status_list as $status => $col ) {
       
   187 				if ( get_blog_status( $blog['blog_id'], $status ) == 1 ) {
       
   188 					$class = $col[0];
       
   189 					$blog_states[] = $col[1];
       
   190 				}
       
   191 			}
       
   192 			$blog_state = '';
       
   193 			if ( ! empty( $blog_states ) ) {
       
   194 				$state_count = count( $blog_states );
       
   195 				$i = 0;
       
   196 				$blog_state .= ' - ';
       
   197 				foreach ( $blog_states as $state ) {
       
   198 					++$i;
       
   199 					( $i == $state_count ) ? $sep = '' : $sep = ', ';
       
   200 					$blog_state .= "<span class='post-state'>$state$sep</span>";
       
   201 				}
       
   202 			}
       
   203 			echo "<tr class='$class'>";
       
   204 
       
   205 			$blogname = ( is_subdomain_install() ) ? str_replace( '.'.$current_site->domain, '', $blog['domain'] ) : $blog['path'];
       
   206 
       
   207 			list( $columns, $hidden ) = $this->get_column_info();
       
   208 
       
   209 			foreach ( $columns as $column_name => $column_display_name ) {
       
   210 				$style = '';
       
   211 				if ( in_array( $column_name, $hidden ) )
       
   212 					$style = ' style="display:none;"';
       
   213 
       
   214 				switch ( $column_name ) {
       
   215 					case 'cb': ?>
       
   216 						<th scope="row" class="check-column">
       
   217 							<?php if ( ! is_main_site( $blog['blog_id'] ) ) : ?>
       
   218 							<label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php printf( __( 'Select %s' ), $blogname ); ?></label>
       
   219 							<input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
       
   220 							<?php endif; ?>
       
   221 						</th>
       
   222 					<?php
       
   223 					break;
       
   224 
       
   225 					case 'id':?>
       
   226 						<th valign="top" scope="row">
       
   227 							<?php echo $blog['blog_id'] ?>
       
   228 						</th>
       
   229 					<?php
       
   230 					break;
       
   231 
       
   232 					case 'blogname':
       
   233 						echo "<td class='column-$column_name $column_name'$style>"; ?>
       
   234 							<a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
       
   235 							<?php
       
   236 							if ( 'list' != $mode ) {
       
   237 								switch_to_blog( $blog['blog_id'] );
       
   238 								echo '<p>' . sprintf( _x( '%1$s &#8211; <em>%2$s</em>', '%1$s: site name. %2$s: site tagline.' ), get_option( 'blogname' ), get_option( 'blogdescription ' ) ) . '</p>';
       
   239 								restore_current_blog();
       
   240 							}
       
   241 
       
   242 							// Preordered.
       
   243 							$actions = array(
       
   244 								'edit' => '', 'backend' => '',
       
   245 								'activate' => '', 'deactivate' => '',
       
   246 								'archive' => '', 'unarchive' => '',
       
   247 								'spam' => '', 'unspam' => '',
       
   248 								'delete' => '',
       
   249 								'visit' => '',
       
   250 							);
       
   251 
       
   252 							$actions['edit']	= '<span class="edit"><a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a></span>';
       
   253 							$actions['backend']	= "<span class='backend'><a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a></span>';
       
   254 							if ( $current_site->blog_id != $blog['blog_id'] ) {
       
   255 								if ( get_blog_status( $blog['blog_id'], 'deleted' ) == '1' )
       
   256 									$actions['activate']	= '<span class="activate"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=activateblog&amp;id=' . $blog['blog_id'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to activate the site %s' ), $blogname ) ) ), 'confirm' ) ) . '">' . __( 'Activate' ) . '</a></span>';
       
   257 								else
       
   258 									$actions['deactivate']	= '<span class="activate"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=deactivateblog&amp;id=' . $blog['blog_id'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to deactivate the site %s' ), $blogname ) ) ), 'confirm') ) . '">' . __( 'Deactivate' ) . '</a></span>';
       
   259 
       
   260 								if ( get_blog_status( $blog['blog_id'], 'archived' ) == '1' )
       
   261 									$actions['unarchive']	= '<span class="archive"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=unarchiveblog&amp;id=' . $blog['blog_id'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to unarchive the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . __( 'Unarchive' ) . '</a></span>';
       
   262 								else
       
   263 									$actions['archive']	= '<span class="archive"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=archiveblog&amp;id=' . $blog['blog_id'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to archive the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . _x( 'Archive', 'verb; site' ) . '</a></span>';
       
   264 
       
   265 								if ( get_blog_status( $blog['blog_id'], 'spam' ) == '1' )
       
   266 									$actions['unspam']	= '<span class="spam"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=unspamblog&amp;id=' . $blog['blog_id'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to unspam the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . _x( 'Not Spam', 'site' ) . '</a></span>';
       
   267 								else
       
   268 									$actions['spam']	= '<span class="spam"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=spamblog&amp;id=' . $blog['blog_id'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to mark the site %s as spam.' ), $blogname ) ) ), 'confirm') ) . '">' . _x( 'Spam', 'site' ) . '</a></span>';
       
   269 
       
   270 								if ( current_user_can( 'delete_site', $blog['blog_id'] ) )
       
   271 									$actions['delete']	= '<span class="delete"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=deleteblog&amp;id=' . $blog['blog_id'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to delete the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . __( 'Delete' ) . '</a></span>';
       
   272 							}
       
   273 
       
   274 							$actions['visit']	= "<span class='view'><a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a></span>';
       
   275 
       
   276 							$actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
       
   277 							echo $this->row_actions( $actions );
       
   278 					?>
       
   279 						</td>
       
   280 					<?php
       
   281 					break;
       
   282 
       
   283 					case 'lastupdated':
       
   284 						echo "<td valign='top' class='$column_name column-$column_name'$style>";
       
   285 							if ( 'list' == $mode )
       
   286 								$date = 'Y/m/d';
       
   287 							else
       
   288 								$date = 'Y/m/d \<\b\r \/\> g:i:s a';
       
   289 							echo ( $blog['last_updated'] == '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] ); ?>
       
   290 						</td>
       
   291 					<?php
       
   292 					break;
       
   293 				case 'registered':
       
   294 						echo "<td valign='top' class='$column_name column-$column_name'$style>";
       
   295 						if ( $blog['registered'] == '0000-00-00 00:00:00' )
       
   296 							echo '&#x2014;';
       
   297 						else
       
   298 							echo mysql2date( $date, $blog['registered'] );
       
   299 						?>
       
   300 						</td>
       
   301 					<?php
       
   302 					break;
       
   303 				case 'users':
       
   304 						echo "<td valign='top' class='$column_name column-$column_name'$style>";
       
   305 							$blogusers = get_users( array( 'blog_id' => $blog['blog_id'], 'number' => 6) );
       
   306 							if ( is_array( $blogusers ) ) {
       
   307 								$blogusers_warning = '';
       
   308 								if ( count( $blogusers ) > 5 ) {
       
   309 									$blogusers = array_slice( $blogusers, 0, 5 );
       
   310 									$blogusers_warning = __( 'Only showing first 5 users.' ) . ' <a href="' . esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'More' ) . '</a>';
       
   311 								}
       
   312 								foreach ( $blogusers as $user_object ) {
       
   313 									echo '<a href="' . esc_url( network_admin_url( 'user-edit.php?user_id=' . $user_object->ID ) ) . '">' . esc_html( $user_object->user_login ) . '</a> ';
       
   314 									if ( 'list' != $mode )
       
   315 										echo '( ' . $user_object->user_email . ' )';
       
   316 									echo '<br />';
       
   317 								}
       
   318 								if ( $blogusers_warning != '' )
       
   319 									echo '<strong>' . $blogusers_warning . '</strong><br />';
       
   320 							}
       
   321 							?>
       
   322 						</td>
       
   323 					<?php
       
   324 					break;
       
   325 
       
   326 				case 'plugins': ?>
       
   327 					<?php if ( has_filter( 'wpmublogsaction' ) ) {
       
   328 					echo "<td valign='top' class='$column_name column-$column_name'$style>";
       
   329 						do_action( 'wpmublogsaction', $blog['blog_id'] ); ?>
       
   330 					</td>
       
   331 					<?php }
       
   332 					break;
       
   333 
       
   334 				default:
       
   335 					echo "<td class='$column_name column-$column_name'$style>";
       
   336 					do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
       
   337 					echo "</td>";
       
   338 					break;
       
   339 				}
       
   340 			}
       
   341 			?>
       
   342 			</tr>
       
   343 			<?php
       
   344 		}
       
   345 	}
       
   346 }