class-wp-ms-sites-list-table.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /**
  3. * List Table API: WP_MS_Sites_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Core class used to implement displaying sites in a list table for the network admin.
  11. *
  12. * @since 3.1.0
  13. * @access private
  14. *
  15. * @see WP_List_Table
  16. */
  17. class WP_MS_Sites_List_Table extends WP_List_Table {
  18. /**
  19. * Site status list.
  20. *
  21. * @since 4.3.0
  22. * @var array
  23. */
  24. public $status_list;
  25. /**
  26. * Constructor.
  27. *
  28. * @since 3.1.0
  29. *
  30. * @see WP_List_Table::__construct() for more information on default arguments.
  31. *
  32. * @param array $args An associative array of arguments.
  33. */
  34. public function __construct( $args = array() ) {
  35. $this->status_list = array(
  36. 'archived' => array( 'site-archived', __( 'Archived' ) ),
  37. 'spam' => array( 'site-spammed', _x( 'Spam', 'site' ) ),
  38. 'deleted' => array( 'site-deleted', __( 'Deleted' ) ),
  39. 'mature' => array( 'site-mature', __( 'Mature' ) )
  40. );
  41. parent::__construct( array(
  42. 'plural' => 'sites',
  43. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  44. ) );
  45. }
  46. /**
  47. *
  48. * @return bool
  49. */
  50. public function ajax_user_can() {
  51. return current_user_can( 'manage_sites' );
  52. }
  53. /**
  54. * Prepares the list of sites for display.
  55. *
  56. * @since 3.1.0
  57. *
  58. * @global string $s
  59. * @global string $mode
  60. * @global wpdb $wpdb
  61. */
  62. public function prepare_items() {
  63. global $s, $mode, $wpdb;
  64. if ( ! empty( $_REQUEST['mode'] ) ) {
  65. $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
  66. set_user_setting( 'sites_list_mode', $mode );
  67. } else {
  68. $mode = get_user_setting( 'sites_list_mode', 'list' );
  69. }
  70. $per_page = $this->get_items_per_page( 'sites_network_per_page' );
  71. $pagenum = $this->get_pagenum();
  72. $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
  73. $wild = '';
  74. if ( false !== strpos($s, '*') ) {
  75. $wild = '*';
  76. $s = trim($s, '*');
  77. }
  78. /*
  79. * If the network is large and a search is not being performed, show only
  80. * the latest sites with no paging in order to avoid expensive count queries.
  81. */
  82. if ( !$s && wp_is_large_network() ) {
  83. if ( !isset($_REQUEST['orderby']) )
  84. $_GET['orderby'] = $_REQUEST['orderby'] = '';
  85. if ( !isset($_REQUEST['order']) )
  86. $_GET['order'] = $_REQUEST['order'] = 'DESC';
  87. }
  88. $args = array(
  89. 'number' => intval( $per_page ),
  90. 'offset' => intval( ( $pagenum - 1 ) * $per_page ),
  91. 'network_id' => get_current_network_id(),
  92. );
  93. if ( empty($s) ) {
  94. // Nothing to do.
  95. } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
  96. preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
  97. preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
  98. preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
  99. // IPv4 address
  100. $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
  101. $reg_blog_ids = $wpdb->get_col( $sql );
  102. if ( $reg_blog_ids ) {
  103. $args['site__in'] = $reg_blog_ids;
  104. }
  105. } elseif ( is_numeric( $s ) && empty( $wild ) ) {
  106. $args['ID'] = $s;
  107. } else {
  108. $args['search'] = $s;
  109. if ( ! is_subdomain_install() ) {
  110. $args['search_columns'] = array( 'path' );
  111. }
  112. }
  113. $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
  114. if ( 'registered' === $order_by ) {
  115. // registered is a valid field name.
  116. } elseif ( 'lastupdated' === $order_by ) {
  117. $order_by = 'last_updated';
  118. } elseif ( 'blogname' === $order_by ) {
  119. if ( is_subdomain_install() ) {
  120. $order_by = 'domain';
  121. } else {
  122. $order_by = 'path';
  123. }
  124. } elseif ( 'blog_id' === $order_by ) {
  125. $order_by = 'id';
  126. } elseif ( ! $order_by ) {
  127. $order_by = false;
  128. }
  129. $args['orderby'] = $order_by;
  130. if ( $order_by ) {
  131. $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
  132. }
  133. if ( wp_is_large_network() ) {
  134. $args['no_found_rows'] = true;
  135. } else {
  136. $args['no_found_rows'] = false;
  137. }
  138. /**
  139. * Filters the arguments for the site query in the sites list table.
  140. *
  141. * @since 4.6.0
  142. *
  143. * @param array $args An array of get_sites() arguments.
  144. */
  145. $args = apply_filters( 'ms_sites_list_table_query_args', $args );
  146. $_sites = get_sites( $args );
  147. if ( is_array( $_sites ) ) {
  148. update_site_cache( $_sites );
  149. $this->items = array_slice( $_sites, 0, $per_page );
  150. }
  151. $total_sites = get_sites( array_merge( $args, array(
  152. 'count' => true,
  153. 'offset' => 0,
  154. 'number' => 0,
  155. ) ) );
  156. $this->set_pagination_args( array(
  157. 'total_items' => $total_sites,
  158. 'per_page' => $per_page,
  159. ) );
  160. }
  161. /**
  162. */
  163. public function no_items() {
  164. _e( 'No sites found.' );
  165. }
  166. /**
  167. *
  168. * @return array
  169. */
  170. protected function get_bulk_actions() {
  171. $actions = array();
  172. if ( current_user_can( 'delete_sites' ) )
  173. $actions['delete'] = __( 'Delete' );
  174. $actions['spam'] = _x( 'Mark as Spam', 'site' );
  175. $actions['notspam'] = _x( 'Not Spam', 'site' );
  176. return $actions;
  177. }
  178. /**
  179. * @global string $mode List table view mode.
  180. *
  181. * @param string $which
  182. */
  183. protected function pagination( $which ) {
  184. global $mode;
  185. parent::pagination( $which );
  186. if ( 'top' === $which )
  187. $this->view_switcher( $mode );
  188. }
  189. /**
  190. * @return array
  191. */
  192. public function get_columns() {
  193. $sites_columns = array(
  194. 'cb' => '<input type="checkbox" />',
  195. 'blogname' => __( 'URL' ),
  196. 'lastupdated' => __( 'Last Updated' ),
  197. 'registered' => _x( 'Registered', 'site' ),
  198. 'users' => __( 'Users' ),
  199. );
  200. if ( has_filter( 'wpmublogsaction' ) ) {
  201. $sites_columns['plugins'] = __( 'Actions' );
  202. }
  203. /**
  204. * Filters the displayed site columns in Sites list table.
  205. *
  206. * @since MU (3.0.0)
  207. *
  208. * @param array $sites_columns An array of displayed site columns. Default 'cb',
  209. * 'blogname', 'lastupdated', 'registered', 'users'.
  210. */
  211. return apply_filters( 'wpmu_blogs_columns', $sites_columns );
  212. }
  213. /**
  214. * @return array
  215. */
  216. protected function get_sortable_columns() {
  217. return array(
  218. 'blogname' => 'blogname',
  219. 'lastupdated' => 'lastupdated',
  220. 'registered' => 'blog_id',
  221. );
  222. }
  223. /**
  224. * Handles the checkbox column output.
  225. *
  226. * @since 4.3.0
  227. *
  228. * @param array $blog Current site.
  229. */
  230. public function column_cb( $blog ) {
  231. if ( ! is_main_site( $blog['blog_id'] ) ) :
  232. $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
  233. ?>
  234. <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php
  235. printf( __( 'Select %s' ), $blogname );
  236. ?></label>
  237. <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
  238. <?php endif;
  239. }
  240. /**
  241. * Handles the ID column output.
  242. *
  243. * @since 4.4.0
  244. *
  245. * @param array $blog Current site.
  246. */
  247. public function column_id( $blog ) {
  248. echo $blog['blog_id'];
  249. }
  250. /**
  251. * Handles the site name column output.
  252. *
  253. * @since 4.3.0
  254. *
  255. * @global string $mode List table view mode.
  256. *
  257. * @param array $blog Current site.
  258. */
  259. public function column_blogname( $blog ) {
  260. global $mode;
  261. $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
  262. $blog_states = array();
  263. reset( $this->status_list );
  264. foreach ( $this->status_list as $status => $col ) {
  265. if ( $blog[ $status ] == 1 ) {
  266. $blog_states[] = $col[1];
  267. }
  268. }
  269. $blog_state = '';
  270. if ( ! empty( $blog_states ) ) {
  271. $state_count = count( $blog_states );
  272. $i = 0;
  273. $blog_state .= ' &mdash; ';
  274. foreach ( $blog_states as $state ) {
  275. ++$i;
  276. $sep = ( $i == $state_count ) ? '' : ', ';
  277. $blog_state .= "<span class='post-state'>$state$sep</span>";
  278. }
  279. }
  280. ?>
  281. <strong>
  282. <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname; ?></a>
  283. <?php echo $blog_state; ?>
  284. </strong>
  285. <?php
  286. if ( 'list' !== $mode ) {
  287. switch_to_blog( $blog['blog_id'] );
  288. echo '<p>';
  289. printf(
  290. /* translators: 1: site name, 2: site tagline. */
  291. __( '%1$s &#8211; %2$s' ),
  292. get_option( 'blogname' ),
  293. '<em>' . get_option( 'blogdescription ' ) . '</em>'
  294. );
  295. echo '</p>';
  296. restore_current_blog();
  297. }
  298. }
  299. /**
  300. * Handles the lastupdated column output.
  301. *
  302. * @since 4.3.0
  303. *
  304. * @global string $mode List table view mode.
  305. *
  306. * @param array $blog Current site.
  307. */
  308. public function column_lastupdated( $blog ) {
  309. global $mode;
  310. if ( 'list' === $mode ) {
  311. $date = __( 'Y/m/d' );
  312. } else {
  313. $date = __( 'Y/m/d g:i:s a' );
  314. }
  315. echo ( $blog['last_updated'] === '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] );
  316. }
  317. /**
  318. * Handles the registered column output.
  319. *
  320. * @since 4.3.0
  321. *
  322. * @global string $mode List table view mode.
  323. *
  324. * @param array $blog Current site.
  325. */
  326. public function column_registered( $blog ) {
  327. global $mode;
  328. if ( 'list' === $mode ) {
  329. $date = __( 'Y/m/d' );
  330. } else {
  331. $date = __( 'Y/m/d g:i:s a' );
  332. }
  333. if ( $blog['registered'] === '0000-00-00 00:00:00' ) {
  334. echo '&#x2014;';
  335. } else {
  336. echo mysql2date( $date, $blog['registered'] );
  337. }
  338. }
  339. /**
  340. * Handles the users column output.
  341. *
  342. * @since 4.3.0
  343. *
  344. * @param array $blog Current site.
  345. */
  346. public function column_users( $blog ) {
  347. $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
  348. if ( ! $user_count ) {
  349. $blog_users = get_users( array( 'blog_id' => $blog['blog_id'], 'fields' => 'ID' ) );
  350. $user_count = count( $blog_users );
  351. unset( $blog_users );
  352. wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
  353. }
  354. printf(
  355. '<a href="%s">%s</a>',
  356. esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
  357. number_format_i18n( $user_count )
  358. );
  359. }
  360. /**
  361. * Handles the plugins column output.
  362. *
  363. * @since 4.3.0
  364. *
  365. * @param array $blog Current site.
  366. */
  367. public function column_plugins( $blog ) {
  368. if ( has_filter( 'wpmublogsaction' ) ) {
  369. /**
  370. * Fires inside the auxiliary 'Actions' column of the Sites list table.
  371. *
  372. * By default this column is hidden unless something is hooked to the action.
  373. *
  374. * @since MU (3.0.0)
  375. *
  376. * @param int $blog_id The site ID.
  377. */
  378. do_action( 'wpmublogsaction', $blog['blog_id'] );
  379. }
  380. }
  381. /**
  382. * Handles output for the default column.
  383. *
  384. * @since 4.3.0
  385. *
  386. * @param array $blog Current site.
  387. * @param string $column_name Current column name.
  388. */
  389. public function column_default( $blog, $column_name ) {
  390. /**
  391. * Fires for each registered custom column in the Sites list table.
  392. *
  393. * @since 3.1.0
  394. *
  395. * @param string $column_name The name of the column to display.
  396. * @param int $blog_id The site ID.
  397. */
  398. do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
  399. }
  400. /**
  401. *
  402. * @global string $mode
  403. */
  404. public function display_rows() {
  405. foreach ( $this->items as $blog ) {
  406. $blog = $blog->to_array();
  407. $class = '';
  408. reset( $this->status_list );
  409. foreach ( $this->status_list as $status => $col ) {
  410. if ( $blog[ $status ] == 1 ) {
  411. $class = " class='{$col[0]}'";
  412. }
  413. }
  414. echo "<tr{$class}>";
  415. $this->single_row_columns( $blog );
  416. echo '</tr>';
  417. }
  418. }
  419. /**
  420. * Gets the name of the default primary column.
  421. *
  422. * @since 4.3.0
  423. *
  424. * @return string Name of the default primary column, in this case, 'blogname'.
  425. */
  426. protected function get_default_primary_column_name() {
  427. return 'blogname';
  428. }
  429. /**
  430. * Generates and displays row action links.
  431. *
  432. * @since 4.3.0
  433. *
  434. * @param object $blog Site being acted upon.
  435. * @param string $column_name Current column name.
  436. * @param string $primary Primary column name.
  437. * @return string Row actions output.
  438. */
  439. protected function handle_row_actions( $blog, $column_name, $primary ) {
  440. if ( $primary !== $column_name ) {
  441. return;
  442. }
  443. $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
  444. // Preordered.
  445. $actions = array(
  446. 'edit' => '', 'backend' => '',
  447. 'activate' => '', 'deactivate' => '',
  448. 'archive' => '', 'unarchive' => '',
  449. 'spam' => '', 'unspam' => '',
  450. 'delete' => '',
  451. 'visit' => '',
  452. );
  453. $actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a>';
  454. $actions['backend'] = "<a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a>';
  455. if ( get_network()->site_id != $blog['blog_id'] ) {
  456. if ( $blog['deleted'] == '1' ) {
  457. $actions['activate'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=activateblog&amp;id=' . $blog['blog_id'] ), 'activateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Activate' ) . '</a>';
  458. } else {
  459. $actions['deactivate'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=deactivateblog&amp;id=' . $blog['blog_id'] ), 'deactivateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Deactivate' ) . '</a>';
  460. }
  461. if ( $blog['archived'] == '1' ) {
  462. $actions['unarchive'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=unarchiveblog&amp;id=' . $blog['blog_id'] ), 'unarchiveblog_' . $blog['blog_id'] ) ) . '">' . __( 'Unarchive' ) . '</a>';
  463. } else {
  464. $actions['archive'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=archiveblog&amp;id=' . $blog['blog_id'] ), 'archiveblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Archive', 'verb; site' ) . '</a>';
  465. }
  466. if ( $blog['spam'] == '1' ) {
  467. $actions['unspam'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=unspamblog&amp;id=' . $blog['blog_id'] ), 'unspamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Not Spam', 'site' ) . '</a>';
  468. } else {
  469. $actions['spam'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=spamblog&amp;id=' . $blog['blog_id'] ), 'spamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Spam', 'site' ) . '</a>';
  470. }
  471. if ( current_user_can( 'delete_site', $blog['blog_id'] ) ) {
  472. $actions['delete'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=deleteblog&amp;id=' . $blog['blog_id'] ), 'deleteblog_' . $blog['blog_id'] ) ) . '">' . __( 'Delete' ) . '</a>';
  473. }
  474. }
  475. $actions['visit'] = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='bookmark'>" . __( 'Visit' ) . '</a>';
  476. /**
  477. * Filters the action links displayed for each site in the Sites list table.
  478. *
  479. * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
  480. * default for each site. The site's status determines whether to show the
  481. * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
  482. * 'Not Spam' or 'Spam' link for each site.
  483. *
  484. * @since 3.1.0
  485. *
  486. * @param array $actions An array of action links to be displayed.
  487. * @param int $blog_id The site ID.
  488. * @param string $blogname Site path, formatted depending on whether it is a sub-domain
  489. * or subdirectory multisite installation.
  490. */
  491. $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
  492. return $this->row_actions( $actions );
  493. }
  494. }