No Description

class.jetpack-network-sites-list-table.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. if ( ! class_exists( 'WP_List_Table' ) ) {
  3. require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
  4. }
  5. class Jetpack_Network_Sites_List_Table extends WP_List_Table {
  6. public function get_columns() {
  7. // site name, status, username connected under
  8. $columns = array(
  9. 'cb' => '<input type="checkbox" />',
  10. 'blogname' => __( 'Site Name', 'jetpack' ),
  11. 'blog_path' => __( 'Path', 'jetpack' ),
  12. 'connected' => __( 'Connected', 'jetpack' ),
  13. );
  14. return $columns;
  15. }
  16. public function prepare_items() {
  17. $jpms = Jetpack_Network::init();
  18. // Deal with bulk actions if any were requested by the user
  19. $this->process_bulk_action();
  20. $sites = get_sites(
  21. array(
  22. 'site__not_in' => array( get_current_blog_id() ),
  23. 'archived' => false,
  24. 'number' => 0,
  25. 'network_id' => get_current_network_id(),
  26. )
  27. );
  28. // Setup pagination
  29. $per_page = 25;
  30. $current_page = $this->get_pagenum();
  31. $total_items = count( $sites );
  32. $sites = array_slice( $sites, ( ( $current_page - 1 ) * $per_page ), $per_page );
  33. $this->set_pagination_args(
  34. array(
  35. 'total_items' => $total_items,
  36. 'per_page' => $per_page,
  37. )
  38. );
  39. $columns = $this->get_columns();
  40. $hidden = array();
  41. $sortable = array();
  42. $this->_column_headers = array( $columns, $hidden, $sortable );
  43. $this->items = $sites;
  44. }
  45. public function column_blogname( $item ) {
  46. // http://jpms/wp-admin/network/site-info.php?id=1
  47. switch_to_blog( $item->blog_id );
  48. $jp_url = admin_url( 'admin.php?page=jetpack' );
  49. restore_current_blog();
  50. $actions = array(
  51. 'edit' => '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $item->blog_id ) ) . '">' . esc_html__( 'Edit', 'jetpack' ) . '</a>',
  52. 'dashboard' => '<a href="' . esc_url( get_admin_url( $item->blog_id, '', 'admin' ) ) . '">' . esc_html__( 'Dashboard', 'jetpack' ) . '</a>',
  53. 'view' => '<a href="' . esc_url( get_site_url( $item->blog_id, '', 'admin' ) ) . '">' . esc_html__( 'View', 'jetpack' ) . '</a>',
  54. 'jetpack-' . $item->blog_id => '<a href="' . esc_url( $jp_url ) . '">Jetpack</a>',
  55. );
  56. return sprintf( '%1$s %2$s', '<strong>' . get_blog_option( $item->blog_id, 'blogname' ) . '</strong>', $this->row_actions( $actions ) );
  57. }
  58. public function column_blog_path( $item ) {
  59. return '<a href="' .
  60. get_site_url( $item->blog_id, '', 'admin' ) .
  61. '">' .
  62. str_replace( array( 'http://', 'https://' ), '', get_site_url( $item->blog_id, '', 'admin' ) ) .
  63. '</a>';
  64. }
  65. public function column_connected( $item ) {
  66. $jpms = Jetpack_Network::init();
  67. $jp = Jetpack::init();
  68. switch_to_blog( $item->blog_id );
  69. // Checks for both the stock version of Jetpack and the one managed by the Jetpack Beta Plugin.
  70. if ( ! is_plugin_active( 'jetpack/jetpack.php' ) && ! is_plugin_active( 'jetpack-dev/jetpack.php' ) ) {
  71. $title = __( 'Jetpack is not active on this site.', 'jetpack' );
  72. $action = array(
  73. 'manage-plugins' => '<a href="' . get_admin_url( $item->blog_id, 'plugins.php', 'admin' ) . '">' . __( 'Manage Plugins', 'jetpack' ) . '</a>',
  74. );
  75. restore_current_blog();
  76. return sprintf( '%1$s %2$s', $title, $this->row_actions( $action ) );
  77. }
  78. if ( $jp->is_connection_ready() ) {
  79. // Build url for disconnecting
  80. $url = $jpms->get_url(
  81. array(
  82. 'name' => 'subsitedisconnect',
  83. 'site_id' => $item->blog_id,
  84. )
  85. );
  86. restore_current_blog();
  87. return '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Disconnect', 'jetpack' ) . '</a>';
  88. }
  89. restore_current_blog();
  90. // Build URL for connecting
  91. $url = $jpms->get_url(
  92. array(
  93. 'name' => 'subsiteregister',
  94. 'site_id' => $item->blog_id,
  95. )
  96. );
  97. return '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Connect', 'jetpack' ) . '</a>';
  98. }
  99. public function get_bulk_actions() {
  100. $actions = array(
  101. 'connect' => esc_html__( 'Connect', 'jetpack' ),
  102. 'disconnect' => esc_html__( 'Disconnect', 'jetpack' ),
  103. );
  104. return $actions;
  105. }
  106. function column_cb( $item ) {
  107. return sprintf(
  108. '<input type="checkbox" name="bulk[]" value="%s" />',
  109. $item->blog_id
  110. );
  111. }
  112. public function process_bulk_action() {
  113. if ( ! isset( $_POST['bulk'] ) || empty( $_POST['bulk'] ) ) {
  114. return; // Thou shall not pass! There is nothing to do
  115. }
  116. $jpms = Jetpack_Network::init();
  117. $action = $this->current_action();
  118. switch ( $action ) {
  119. case 'connect':
  120. foreach ( $_POST['bulk'] as $k => $site ) {
  121. $jpms->do_subsiteregister( $site );
  122. }
  123. break;
  124. case 'disconnect':
  125. foreach ( $_POST['bulk'] as $k => $site ) {
  126. $jpms->do_subsitedisconnect( $site );
  127. }
  128. break;
  129. }
  130. }
  131. } // end h