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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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( array(
  21. 'site__not_in' => array( get_current_blog_id() ),
  22. 'archived' => false,
  23. 'number' => 0,
  24. ) );
  25. // Setup pagination
  26. $per_page = 25;
  27. $current_page = $this->get_pagenum();
  28. $total_items = count( $sites );
  29. $sites = array_slice( $sites, ( ( $current_page-1 ) * $per_page ), $per_page );
  30. $this->set_pagination_args( array(
  31. 'total_items' => $total_items,
  32. 'per_page' => $per_page
  33. ) );
  34. $columns = $this->get_columns();
  35. $hidden = array();
  36. $sortable = array();
  37. $this->_column_headers = array( $columns, $hidden, $sortable );
  38. $this->items = $sites;
  39. }
  40. public function column_blogname( $item ) {
  41. // http://jpms/wp-admin/network/site-info.php?id=1
  42. switch_to_blog( $item->blog_id );
  43. $jp_url = admin_url( 'admin.php?page=jetpack' );
  44. restore_current_blog();
  45. $actions = array(
  46. 'edit' => '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $item->blog_id ) ) . '">' . esc_html__( 'Edit', 'jetpack' ) . '</a>',
  47. 'dashboard' => '<a href="' . esc_url( get_admin_url( $item->blog_id, '', 'admin' ) ) . '">' . esc_html__( 'Dashboard', 'jetpack' ) . '</a>',
  48. 'view' => '<a href="' . esc_url( get_site_url( $item->blog_id, '', 'admin' ) ) . '">' . esc_html__( 'View', 'jetpack' ) . '</a>',
  49. 'jetpack-' . $item->blog_id => '<a href="' . esc_url( $jp_url ) . '">Jetpack</a>',
  50. );
  51. return sprintf('%1$s %2$s', '<strong>' . get_blog_option( $item->blog_id, 'blogname' ) . '</strong>', $this->row_actions($actions) );
  52. }
  53. public function column_blog_path( $item ) {
  54. return
  55. '<a href="' .
  56. get_site_url( $item->blog_id, '', 'admin' ) .
  57. '">' .
  58. str_replace( array( 'http://', 'https://' ), '', get_site_url( $item->blog_id, '', 'admin' ) ) .
  59. '</a>';
  60. }
  61. public function column_connected( $item ) {
  62. $jpms = Jetpack_Network::init();
  63. $jp = Jetpack::init();
  64. switch_to_blog( $item->blog_id );
  65. if ( ! is_plugin_active( 'jetpack/jetpack.php' ) ) {
  66. $title = __( 'Jetpack is not active on this site.', 'jetpack' );
  67. $action = array(
  68. 'manage-plugins' => '<a href="' . get_admin_url( $item->blog_id, 'plugins.php', 'admin' ) . '">' . __( 'Manage Plugins', 'jetpack' ) . '</a>',
  69. );
  70. restore_current_blog();
  71. return sprintf( '%1$s %2$s', $title, $this->row_actions( $action ) );
  72. }
  73. if( $jp->is_active() ) {
  74. // Build url for disconnecting
  75. $url = $jpms->get_url( array(
  76. 'name' => 'subsitedisconnect',
  77. 'site_id' => $item->blog_id,
  78. ) );
  79. restore_current_blog();
  80. return '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Disconnect', 'jetpack' ) . '</a>';
  81. }
  82. restore_current_blog();
  83. // Build URL for connecting
  84. $url = $jpms->get_url( array(
  85. 'name' => 'subsiteregister',
  86. 'site_id' => $item->blog_id,
  87. ) );
  88. return '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Connect', 'jetpack' ) . '</a>';
  89. }
  90. public function get_bulk_actions() {
  91. $actions = array(
  92. 'connect' => esc_html__( 'Connect', 'jetpack' ),
  93. 'disconnect' => esc_html__( 'Disconnect', 'jetpack' )
  94. );
  95. return $actions;
  96. }
  97. function column_cb($item) {
  98. return sprintf(
  99. '<input type="checkbox" name="bulk[]" value="%s" />', $item->blog_id
  100. );
  101. }
  102. public function process_bulk_action() {
  103. if( !isset( $_POST['bulk'] ) || empty ( $_POST['bulk'] ) )
  104. return; // Thou shall not pass! There is nothing to do
  105. $jpms = Jetpack_Network::init();
  106. $action = $this->current_action();
  107. switch ( $action ) {
  108. case 'connect':
  109. foreach( $_POST['bulk'] as $k => $site ) {
  110. $jpms->do_subsiteregister( $site );
  111. }
  112. break;
  113. case 'disconnect':
  114. foreach( $_POST['bulk'] as $k => $site ) {
  115. $jpms->do_subsitedisconnect( $site );
  116. }
  117. break;
  118. }
  119. }
  120. } // end h