-
Notifications
You must be signed in to change notification settings - Fork 5
/
class.jetpack-network-sites-list-table.php
211 lines (184 loc) · 5.98 KB
/
class.jetpack-network-sites-list-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Jetpack network sites list table.
*
* @package automattic/jetpack
*/
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* Jetpack network sites list table.
*/
class Jetpack_Network_Sites_List_Table extends WP_List_Table {
/**
* Get columns.
*
* @return array name => header HTML.
*/
public function get_columns() {
// site name, status, username connected under.
$columns = array(
'cb' => '<input type="checkbox" />',
'blogname' => __( 'Site Name', 'jetpack' ),
'blog_path' => __( 'Path', 'jetpack' ),
'connected' => __( 'Connected', 'jetpack' ),
);
return $columns;
}
/**
* Prepare items.
*/
public function prepare_items() {
// Make sure Jetpack_Network is initialized.
Jetpack_Network::init();
// Deal with bulk actions if any were requested by the user.
$this->process_bulk_action();
$sites = get_sites(
array(
'site__not_in' => array( get_current_blog_id() ),
'archived' => false,
'number' => 0,
'network_id' => get_current_network_id(),
)
);
// Setup pagination.
$per_page = 25;
$current_page = $this->get_pagenum();
$total_items = is_countable( $sites ) ? count( $sites ) : 0;
$sites = array_slice( $sites, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
)
);
$columns = $this->get_columns();
$hidden = array();
$sortable = array();
$this->_column_headers = array( $columns, $hidden, $sortable );
$this->items = $sites;
}
/**
* Column blogname.
*
* @param object|array $item Item.
* @return string HTML.
*/
public function column_blogname( $item ) {
// <http://jpms/wp-admin/network/site-info.php?id=1>.
switch_to_blog( $item->blog_id );
$jp_url = admin_url( 'admin.php?page=jetpack' );
restore_current_blog();
$actions = array(
'edit' => '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $item->blog_id ) ) . '">' . esc_html__( 'Edit', 'jetpack' ) . '</a>',
'dashboard' => '<a href="' . esc_url( get_admin_url( $item->blog_id, '', 'admin' ) ) . '">' . esc_html__( 'Dashboard', 'jetpack' ) . '</a>',
'view' => '<a href="' . esc_url( get_site_url( $item->blog_id, '', 'admin' ) ) . '">' . esc_html__( 'View', 'jetpack' ) . '</a>',
'jetpack-' . $item->blog_id => '<a href="' . esc_url( $jp_url ) . '">Jetpack</a>',
);
return sprintf( '%1$s %2$s', '<strong>' . get_blog_option( $item->blog_id, 'blogname' ) . '</strong>', $this->row_actions( $actions ) );
}
/**
* Column blog path.
*
* @param object|array $item Item.
* @return string HTML.
*/
public function column_blog_path( $item ) {
return '<a href="' .
get_site_url( $item->blog_id, '', 'admin' ) .
'">' .
str_replace( array( 'http://', 'https://' ), '', get_site_url( $item->blog_id, '', 'admin' ) ) .
'</a>';
}
/**
* Column connected.
*
* @param object|array $item Item.
* @return string HTML.
*/
public function column_connected( $item ) {
$jpms = Jetpack_Network::init();
$jp = Jetpack::init();
switch_to_blog( $item->blog_id );
// Checks for both the stock version of Jetpack and the one managed by the Jetpack Beta Plugin.
if ( ! is_plugin_active( 'jetpack/jetpack.php' ) && ! is_plugin_active( 'jetpack-dev/jetpack.php' ) && ! array_key_exists( 'jetpack.php', get_mu_plugins() ) ) {
$title = __( 'Jetpack is not active on this site.', 'jetpack' );
$action = array(
'manage-plugins' => '<a href="' . get_admin_url( $item->blog_id, 'plugins.php', 'admin' ) . '">' . __( 'Manage Plugins', 'jetpack' ) . '</a>',
);
restore_current_blog();
return sprintf( '%1$s %2$s', $title, $this->row_actions( $action ) );
}
if ( $jp->is_connection_ready() ) {
// Build url for disconnecting.
$url = $jpms->get_url(
array(
'name' => 'subsitedisconnect',
'site_id' => $item->blog_id,
)
);
restore_current_blog();
return '<a href="' . wp_nonce_url( $url, 'jetpack-subsite-disconnect' ) . '">' . esc_html__( 'Disconnect', 'jetpack' ) . '</a>';
}
restore_current_blog();
// Build URL for connecting.
$url = $jpms->get_url(
array(
'name' => 'subsiteregister',
'site_id' => $item->blog_id,
)
);
return '<a href="' . wp_nonce_url( $url, 'jetpack-subsite-register' ) . '">' . esc_html__( 'Connect', 'jetpack' ) . '</a>';
}
/**
* Get bulk actions.
*
* @return array Code => HTML.
*/
public function get_bulk_actions() {
$actions = array(
'connect' => esc_html__( 'Connect', 'jetpack' ),
'disconnect' => esc_html__( 'Disconnect', 'jetpack' ),
);
return $actions;
}
/**
* Column checkbox.
*
* @param object|array $item Item.
* @return string HTML.
*/
public function column_cb( $item ) {
return sprintf(
'<input type="checkbox" name="bulk[]" value="%s" />',
$item->blog_id
);
}
/**
* Process bulk actions.
*/
public function process_bulk_action() {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Check if we have anything to do before checking the nonce.
if ( empty( $_POST['bulk'] ) ) {
return; // Thou shall not pass! There is nothing to do.
}
check_admin_referer( 'bulk-toplevel_page_jetpack-network' );
$jpms = Jetpack_Network::init();
$action = $this->current_action();
switch ( $action ) {
case 'connect':
$bulk = wp_unslash( $_POST['bulk'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( $bulk as $site ) {
$jpms->do_subsiteregister( $site );
}
break;
case 'disconnect':
$bulk = wp_unslash( $_POST['bulk'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( $bulk as $site ) {
$jpms->do_subsitedisconnect( $site );
}
break;
}
}
} // end h