Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

566 строки
15 KiB

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