1
|
|
|
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFilename |
2
|
|
|
/** |
3
|
|
|
* Jetpack Network Manager class file. |
4
|
|
|
* |
5
|
|
|
* @package jetpack |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Automattic\Jetpack\Connection\Client; |
9
|
|
|
use Automattic\Jetpack\Connection\Manager; |
10
|
|
|
use Automattic\Jetpack\Constants; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Used to manage Jetpack installation on Multisite Network installs |
14
|
|
|
* |
15
|
|
|
* SINGLETON: To use call Jetpack_Network::init() |
16
|
|
|
* |
17
|
|
|
* DO NOT USE ANY STATIC METHODS IN THIS CLASS!!!!!! |
18
|
|
|
* |
19
|
|
|
* @since 2.9 |
20
|
|
|
*/ |
21
|
|
|
class Jetpack_Network { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Holds a static copy of Jetpack_Network for the singleton |
25
|
|
|
* |
26
|
|
|
* @since 2.9 |
27
|
|
|
* @var Jetpack_Network |
28
|
|
|
*/ |
29
|
|
|
private static $instance = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* An instance of the connection manager object. |
33
|
|
|
* |
34
|
|
|
* @since 7.7 |
35
|
|
|
* @var Automattic\Jetpack\Connection\Manager |
36
|
|
|
*/ |
37
|
|
|
private $connection; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Name of the network wide settings |
41
|
|
|
* |
42
|
|
|
* @since 2.9 |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $settings_name = 'jetpack-network-settings'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Defaults for settings found on the Jetpack > Settings page |
49
|
|
|
* |
50
|
|
|
* @since 2.9 |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $setting_defaults = array( |
54
|
|
|
'auto-connect' => 0, |
55
|
|
|
'sub-site-connection-override' => 1, |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor |
60
|
|
|
* |
61
|
|
|
* @since 2.9 |
62
|
|
|
*/ |
63
|
|
|
private function __construct() { |
64
|
|
|
require_once ABSPATH . '/wp-admin/includes/plugin.php'; // For the is_plugin... check. |
65
|
|
|
require_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php'; // For managing the global whitelist. |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sanity check to ensure the install is Multisite and we |
69
|
|
|
* are in Network Admin |
70
|
|
|
*/ |
71
|
|
|
if ( is_multisite() && is_network_admin() ) { |
72
|
|
|
add_action( 'network_admin_menu', array( $this, 'add_network_admin_menu' ) ); |
73
|
|
|
add_action( 'network_admin_edit_jetpack-network-settings', array( $this, 'save_network_settings_page' ), 10, 0 ); |
74
|
|
|
add_filter( 'admin_body_class', array( $this, 'body_class' ) ); |
75
|
|
|
|
76
|
|
|
if ( isset( $_GET['page'] ) && 'jetpack' == $_GET['page'] ) { |
77
|
|
|
add_action( 'admin_init', array( $this, 'jetpack_sites_list' ) ); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* Things that should only run on multisite |
83
|
|
|
*/ |
84
|
|
|
if ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
85
|
|
|
add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) ); |
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* If admin wants to automagically register new sites set the hook here |
89
|
|
|
* |
90
|
|
|
* This is a hacky way because xmlrpc is not available on wp_initialize_site |
91
|
|
|
*/ |
92
|
|
|
if ( 1 === $this->get_option( 'auto-connect' ) ) { |
93
|
|
|
add_action( 'wp_initialize_site', array( $this, 'do_automatically_add_new_site' ) ); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets a connection object. |
100
|
|
|
* |
101
|
|
|
* @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
102
|
|
|
*/ |
103
|
|
|
public function set_connection( Manager $connection ) { |
104
|
|
|
$this->connection = $connection; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets which modules get activated by default on subsite connection. |
109
|
|
|
* Modules can be set in Network Admin > Jetpack > Settings |
110
|
|
|
* |
111
|
|
|
* @since 2.9 |
112
|
|
|
* @deprecated since 7.7.0 |
113
|
|
|
* |
114
|
|
|
* @param array $modules List of modules. |
115
|
|
|
*/ |
116
|
|
|
public function set_auto_activated_modules( $modules ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
117
|
|
|
_deprecated_function( __METHOD__, 'jetpack-7.7' ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Registers new sites upon creation |
122
|
|
|
* |
123
|
|
|
* @since 2.9 |
124
|
|
|
* @since 7.4.0 Uses a WP_Site object. |
125
|
|
|
* @uses wp_initialize_site |
126
|
|
|
* |
127
|
|
|
* @param WP_Site $site the WordPress site object. |
128
|
|
|
**/ |
129
|
|
|
public function do_automatically_add_new_site( $site ) { |
130
|
|
|
if ( is_a( $site, 'WP_Site' ) ) { |
131
|
|
|
$this->do_subsiteregister( $site->id ); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Adds .network-admin class to the body tag |
137
|
|
|
* Helps distinguish network admin JP styles from regular site JP styles |
138
|
|
|
* |
139
|
|
|
* @since 2.9 |
140
|
|
|
* |
141
|
|
|
* @param String $classes current assigned body classes. |
142
|
|
|
* @return String amended class string. |
143
|
|
|
*/ |
144
|
|
|
public function body_class( $classes ) { |
145
|
|
|
return trim( $classes ) . ' network-admin '; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Provides access to an instance of Jetpack_Network |
150
|
|
|
* |
151
|
|
|
* This is how the Jetpack_Network object should *always* be accessed |
152
|
|
|
* |
153
|
|
|
* @since 2.9 |
154
|
|
|
* @return Jetpack_Network |
155
|
|
|
*/ |
156
|
|
|
public static function init() { |
157
|
|
|
if ( ! self::$instance || ! is_a( self::$instance, 'Jetpack_Network' ) ) { |
158
|
|
|
self::$instance = new Jetpack_Network(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return self::$instance; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Registers the Multisite admin bar menu item shortcut. |
166
|
|
|
* This shortcut helps users quickly and easily navigate to the Jetpack Network Admin |
167
|
|
|
* menu from anywhere in their network. |
168
|
|
|
* |
169
|
|
|
* @since 2.9 |
170
|
|
|
*/ |
171
|
|
|
public function register_menubar() { |
172
|
|
|
add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Runs when Jetpack is deactivated from the network admin plugins menu. |
177
|
|
|
* Each individual site will need to have Jetpack::disconnect called on it. |
178
|
|
|
* Site that had Jetpack individually enabled will not be disconnected as |
179
|
|
|
* on Multisite individually activated plugins are still activated when |
180
|
|
|
* a plugin is deactivated network wide. |
181
|
|
|
* |
182
|
|
|
* @since 2.9 |
183
|
|
|
**/ |
184
|
|
|
public function deactivate() { |
185
|
|
|
// Only fire if in network admin. |
186
|
|
|
if ( ! is_network_admin() ) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$sites = get_sites(); |
191
|
|
|
|
192
|
|
|
foreach ( $sites as $s ) { |
193
|
|
|
switch_to_blog( $s->blog_id ); |
194
|
|
|
$active_plugins = get_option( 'active_plugins' ); |
195
|
|
|
|
196
|
|
|
/* |
197
|
|
|
* If this plugin was activated in the subsite individually |
198
|
|
|
* we do not want to call disconnect. Plugins activated |
199
|
|
|
* individually (before network activation) stay activated |
200
|
|
|
* when the network deactivation occurs |
201
|
|
|
*/ |
202
|
|
|
if ( ! in_array( 'jetpack/jetpack.php', $active_plugins, true ) ) { |
203
|
|
|
Jetpack::disconnect(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
restore_current_blog(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Adds a link to the Jetpack Network Admin page in the network admin menu bar. |
211
|
|
|
* |
212
|
|
|
* @since 2.9 |
213
|
|
|
**/ |
214
|
|
|
public function add_to_menubar() { |
215
|
|
|
global $wp_admin_bar; |
216
|
|
|
// Don't show for logged out users or single site mode. |
217
|
|
|
if ( ! is_user_logged_in() || ! is_multisite() ) { |
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$wp_admin_bar->add_node( |
222
|
|
|
array( |
223
|
|
|
'parent' => 'network-admin', |
224
|
|
|
'id' => 'network-admin-jetpack', |
225
|
|
|
'title' => 'Jetpack', |
226
|
|
|
'href' => $this->get_url( 'network_admin_page' ), |
227
|
|
|
) |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns various URL strings. Factory like |
233
|
|
|
* |
234
|
|
|
* $args can be a string or an array. |
235
|
|
|
* If $args is an array there must be an element called name for the switch statement |
236
|
|
|
* |
237
|
|
|
* Currently supports: |
238
|
|
|
* - subsiteregister: Pass array( 'name' => 'subsiteregister', 'site_id' => SITE_ID ) |
239
|
|
|
* - network_admin_page: Provides link to /wp-admin/network/JETPACK |
240
|
|
|
* - subsitedisconnect: Pass array( 'name' => 'subsitedisconnect', 'site_id' => SITE_ID ) |
241
|
|
|
* |
242
|
|
|
* @since 2.9 |
243
|
|
|
* |
244
|
|
|
* @param Mixed $args URL parameters. |
245
|
|
|
* |
246
|
|
|
* @return String |
247
|
|
|
**/ |
248
|
|
|
public function get_url( $args ) { |
249
|
|
|
$url = null; // Default url value. |
250
|
|
|
|
251
|
|
|
if ( is_string( $args ) ) { |
252
|
|
|
$name = $args; |
253
|
|
|
} else { |
254
|
|
|
$name = $args['name']; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
switch ( $name ) { |
258
|
|
|
case 'subsiteregister': |
259
|
|
|
if ( ! isset( $args['site_id'] ) ) { |
260
|
|
|
break; // If there is not a site id present we cannot go further. |
261
|
|
|
} |
262
|
|
|
$url = network_admin_url( |
263
|
|
|
'admin.php?page=jetpack&action=subsiteregister&site_id=' |
264
|
|
|
. $args['site_id'] |
265
|
|
|
); |
266
|
|
|
break; |
267
|
|
|
|
268
|
|
|
case 'network_admin_page': |
269
|
|
|
$url = network_admin_url( 'admin.php?page=jetpack' ); |
270
|
|
|
break; |
271
|
|
|
|
272
|
|
|
case 'subsitedisconnect': |
273
|
|
|
if ( ! isset( $args['site_id'] ) ) { |
274
|
|
|
break; // If there is not a site id present we cannot go further. |
275
|
|
|
} |
276
|
|
|
$url = network_admin_url( |
277
|
|
|
'admin.php?page=jetpack&action=subsitedisconnect&site_id=' |
278
|
|
|
. $args['site_id'] |
279
|
|
|
); |
280
|
|
|
break; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $url; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Adds the Jetpack menu item to the Network Admin area |
288
|
|
|
* |
289
|
|
|
* @since 2.9 |
290
|
|
|
*/ |
291
|
|
|
public function add_network_admin_menu() { |
292
|
|
|
add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_network_admin_page', 'jetpack', array( $this, 'wrap_network_admin_page' ), 'div', 3 ); |
293
|
|
|
$jetpack_sites_page_hook = add_submenu_page( 'jetpack', __( 'Jetpack Sites', 'jetpack' ), __( 'Sites', 'jetpack' ), 'jetpack_network_sites_page', 'jetpack', array( $this, 'wrap_network_admin_page' ) ); |
294
|
|
|
$jetpack_settings_page_hook = add_submenu_page( 'jetpack', __( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'jetpack_network_settings_page', 'jetpack-settings', array( $this, 'wrap_render_network_admin_settings_page' ) ); |
295
|
|
|
add_action( "admin_print_styles-$jetpack_sites_page_hook", array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) ); |
296
|
|
|
add_action( "admin_print_styles-$jetpack_settings_page_hook", array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) ); |
297
|
|
|
/** |
298
|
|
|
* As jetpack_register_genericons is by default fired off a hook, |
299
|
|
|
* the hook may have already fired by this point. |
300
|
|
|
* So, let's just trigger it manually. |
301
|
|
|
*/ |
302
|
|
|
require_once JETPACK__PLUGIN_DIR . '_inc/genericons.php'; |
303
|
|
|
jetpack_register_genericons(); |
304
|
|
|
|
305
|
|
View Code Duplication |
if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) { |
306
|
|
|
wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Adds JP menu icon |
314
|
|
|
* |
315
|
|
|
* @since 2.9 |
316
|
|
|
**/ |
317
|
|
|
public function admin_menu_css() { |
318
|
|
|
wp_enqueue_style( 'jetpack-icons' ); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Provides functionality for the Jetpack > Sites page. |
323
|
|
|
* Does not do the display! |
324
|
|
|
* |
325
|
|
|
* @since 2.9 |
326
|
|
|
*/ |
327
|
|
|
public function jetpack_sites_list() { |
328
|
|
|
Jetpack::init(); |
329
|
|
|
|
330
|
|
|
if ( isset( $_GET['action'] ) ) { |
331
|
|
|
switch ( $_GET['action'] ) { |
332
|
|
|
case 'subsiteregister': |
333
|
|
|
/** |
334
|
|
|
* Add actual referrer checking. |
335
|
|
|
* |
336
|
|
|
* @todo check_admin_referer( 'jetpack-subsite-register' ); |
337
|
|
|
*/ |
338
|
|
|
Jetpack::log( 'subsiteregister' ); |
339
|
|
|
|
340
|
|
|
// If !$_GET['site_id'] stop registration and error. |
341
|
|
View Code Duplication |
if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) { |
342
|
|
|
/** |
343
|
|
|
* Log error to state cookie for display later. |
344
|
|
|
* |
345
|
|
|
* @todo Make state messages show on Jetpack NA pages |
346
|
|
|
*/ |
347
|
|
|
Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to register a sub-site.', 'jetpack' ) ); |
348
|
|
|
break; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
// Send data to register endpoint and retrieve shadow blog details. |
352
|
|
|
$result = $this->do_subsiteregister(); |
353
|
|
|
$url = $this->get_url( 'network_admin_page' ); |
354
|
|
|
|
355
|
|
|
if ( is_wp_error( $result ) ) { |
356
|
|
|
$url = add_query_arg( 'action', 'connection_failed', $url ); |
357
|
|
|
} else { |
358
|
|
|
$url = add_query_arg( 'action', 'connected', $url ); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
wp_safe_redirect( $url ); |
362
|
|
|
exit; |
363
|
|
|
|
364
|
|
|
case 'subsitedisconnect': |
365
|
|
|
Jetpack::log( 'subsitedisconnect' ); |
366
|
|
|
|
367
|
|
View Code Duplication |
if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) { |
368
|
|
|
Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to disconnect a sub-site.', 'jetpack' ) ); |
369
|
|
|
break; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$this->do_subsitedisconnect(); |
373
|
|
|
break; |
374
|
|
|
|
375
|
|
|
case 'connected': |
376
|
|
|
case 'connection_failed': |
377
|
|
|
add_action( 'jetpack_notices', array( $this, 'show_jetpack_notice' ) ); |
378
|
|
|
break; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Shows the Jetpack plugin notices. |
385
|
|
|
*/ |
386
|
|
|
public function show_jetpack_notice() { |
387
|
|
|
if ( isset( $_GET['action'] ) && 'connected' == $_GET['action'] ) { |
388
|
|
|
$notice = __( 'Site successfully connected.', 'jetpack' ); |
389
|
|
|
$classname = 'updated'; |
390
|
|
|
} else if ( isset( $_GET['action'] ) && 'connection_failed' == $_GET['action'] ) { |
391
|
|
|
$notice = __( 'Site connection failed!', 'jetpack' ); |
392
|
|
|
$classname = 'error'; |
393
|
|
|
} |
394
|
|
|
?> |
395
|
|
|
<div id="message" class="<?php echo esc_attr( $classname ); ?> jetpack-message jp-connect" style="display:block !important;"> |
|
|
|
|
396
|
|
|
<p><?php echo esc_html( $notice ); ?></p> |
|
|
|
|
397
|
|
|
</div> |
398
|
|
|
<?php |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Disconnect functionality for an individual site |
403
|
|
|
* |
404
|
|
|
* @since 2.9 |
405
|
|
|
* @see Jetpack_Network::jetpack_sites_list() |
406
|
|
|
* |
407
|
|
|
* @param int $site_id the site identifier. |
|
|
|
|
408
|
|
|
*/ |
409
|
|
|
public function do_subsitedisconnect( $site_id = null ) { |
410
|
|
|
if ( ! current_user_can( 'jetpack_disconnect' ) ) { |
411
|
|
|
return; |
412
|
|
|
} |
413
|
|
|
$site_id = ( is_null( $site_id ) ) ? $_GET['site_id'] : $site_id; |
414
|
|
|
switch_to_blog( $site_id ); |
415
|
|
|
Jetpack::disconnect(); |
416
|
|
|
restore_current_blog(); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Registers a subsite with the Jetpack servers |
421
|
|
|
* |
422
|
|
|
* @since 2.9 |
423
|
|
|
* @todo Break apart into easier to manage chunks that can be unit tested |
424
|
|
|
* @see Jetpack_Network::jetpack_sites_list(); |
425
|
|
|
* |
426
|
|
|
* @param int $site_id the site identifier. |
|
|
|
|
427
|
|
|
*/ |
428
|
|
|
public function do_subsiteregister( $site_id = null ) { |
429
|
|
|
if ( ! current_user_can( 'jetpack_disconnect' ) ) { |
430
|
|
|
return; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
if ( Jetpack::is_development_mode() ) { |
434
|
|
|
return; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
// Figure out what site we are working on. |
438
|
|
|
$site_id = ( is_null( $site_id ) ) ? $_GET['site_id'] : $site_id; |
439
|
|
|
|
440
|
|
|
/* |
441
|
|
|
* Here we need to switch to the subsite |
442
|
|
|
* For the registration process we really only hijack how it |
443
|
|
|
* works for an individual site and pass in some extra data here |
444
|
|
|
*/ |
445
|
|
|
switch_to_blog( $site_id ); |
446
|
|
|
|
447
|
|
|
add_filter( 'jetpack_register_request_body', array( $this, 'filter_register_request_body' ) ); |
448
|
|
|
add_action( 'jetpack_site_registered_user_token', array( $this, 'filter_register_user_token' ) ); |
449
|
|
|
|
450
|
|
|
// Save the secrets in the subsite so when the wpcom server does a pingback it |
451
|
|
|
// will be able to validate the connection. |
452
|
|
|
$result = $this->connection->register( 'subsiteregister' ); |
453
|
|
|
|
454
|
|
|
if ( is_wp_error( $result ) || ! $result ) { |
455
|
|
|
restore_current_blog(); |
456
|
|
|
return $result; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
Jetpack::activate_default_modules(); |
460
|
|
|
|
461
|
|
|
restore_current_blog(); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Receives the registration response token. |
466
|
|
|
* |
467
|
|
|
* @param Object $token the received token. |
468
|
|
|
*/ |
469
|
|
|
public function filter_register_user_token( $token ) { |
470
|
|
|
$is_master_user = ! Jetpack::is_active(); |
471
|
|
|
Jetpack::update_user_token( |
472
|
|
|
get_current_user_id(), |
473
|
|
|
sprintf( '%s.%d', $token->secret, get_current_user_id() ), |
474
|
|
|
$is_master_user |
475
|
|
|
); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Filters the registration request body to include additional properties. |
480
|
|
|
* |
481
|
|
|
* @param Array $properties standard register request body properties. |
482
|
|
|
* @return Array amended properties. |
483
|
|
|
*/ |
484
|
|
|
public function filter_register_request_body( $properties ) { |
485
|
|
|
$blog_details = get_blog_details(); |
486
|
|
|
|
487
|
|
|
$network = get_network(); |
488
|
|
|
|
489
|
|
|
switch_to_blog( $network->blog_id ); |
490
|
|
|
// The blog id on WordPress.com of the primary network site. |
491
|
|
|
$network_wpcom_blog_id = Jetpack_Options::get_option( 'id' ); |
492
|
|
|
restore_current_blog(); |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Both `state` and `user_id` need to be sent in the request, even though they are the same value. |
496
|
|
|
* Connecting via the network admin combines `register()` and `authorize()` methods into one step, |
497
|
|
|
* because we assume the main site is already authorized. `state` is used to verify the `register()` |
498
|
|
|
* request, while `user_id()` is used to create the token in the `authorize()` request. |
499
|
|
|
*/ |
500
|
|
|
return array_merge( |
501
|
|
|
$properties, |
502
|
|
|
array( |
503
|
|
|
'network_url' => $this->get_url( 'network_admin_page' ), |
504
|
|
|
'network_wpcom_blog_id' => $network_wpcom_blog_id, |
505
|
|
|
'user_id' => get_current_user_id(), |
506
|
|
|
|
507
|
|
|
/* |
508
|
|
|
* Use the subsite's registration date as the site creation date. |
509
|
|
|
* |
510
|
|
|
* This is in contrast to regular standalone sites, where we use the helper |
511
|
|
|
* `Jetpack::get_assumed_site_creation_date()` to assume the site's creation date. |
512
|
|
|
*/ |
513
|
|
|
'site_created' => $blog_details->registered, |
514
|
|
|
) |
515
|
|
|
); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* A hook handler for adding admin pages and subpages. |
520
|
|
|
*/ |
521
|
|
|
public function wrap_network_admin_page() { |
522
|
|
|
Jetpack_Admin_Page::wrap_ui( array( $this, 'network_admin_page' ) ); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Handles the displaying of all sites on the network that are |
527
|
|
|
* dis/connected to Jetpack |
528
|
|
|
* |
529
|
|
|
* @since 2.9 |
530
|
|
|
* @see Jetpack_Network::jetpack_sites_list() |
531
|
|
|
*/ |
532
|
|
|
public function network_admin_page() { |
533
|
|
|
global $current_site; |
534
|
|
|
$this->network_admin_page_header(); |
535
|
|
|
|
536
|
|
|
$jp = Jetpack::init(); |
537
|
|
|
|
538
|
|
|
// We should be, but ensure we are on the main blog. |
539
|
|
|
switch_to_blog( $current_site->blog_id ); |
540
|
|
|
$main_active = $jp->is_active(); |
541
|
|
|
restore_current_blog(); |
542
|
|
|
|
543
|
|
|
// If we are in dev mode, just show the notice and bail. |
544
|
|
|
if ( Jetpack::is_development_mode() ) { |
545
|
|
|
Jetpack::show_development_mode_notice(); |
546
|
|
|
return; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/* |
550
|
|
|
* Ensure the main blog is connected as all other subsite blog |
551
|
|
|
* connections will feed off this one |
552
|
|
|
*/ |
553
|
|
|
if ( ! $main_active ) { |
554
|
|
|
$url = $this->get_url( |
|
|
|
|
555
|
|
|
array( |
556
|
|
|
'name' => 'subsiteregister', |
557
|
|
|
'site_id' => 1, |
558
|
|
|
) |
559
|
|
|
); |
560
|
|
|
$data = array( 'url' => $jp->build_connect_url() ); |
561
|
|
|
Jetpack::init()->load_view( 'admin/must-connect-main-blog.php', $data ); |
562
|
|
|
|
563
|
|
|
return; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
require_once 'class.jetpack-network-sites-list-table.php'; |
567
|
|
|
|
568
|
|
|
$network_sites_table = new Jetpack_Network_Sites_List_Table(); |
569
|
|
|
echo '<div class="wrap"><h2>' . esc_html__( 'Sites', 'jetpack' ) . '</h2>'; |
570
|
|
|
echo '<form method="post">'; |
571
|
|
|
$network_sites_table->prepare_items(); |
572
|
|
|
$network_sites_table->display(); |
573
|
|
|
echo '</form></div>'; |
574
|
|
|
|
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Stylized JP header formatting |
579
|
|
|
* |
580
|
|
|
* @since 2.9 |
581
|
|
|
*/ |
582
|
|
|
public function network_admin_page_header() { |
583
|
|
|
$is_connected = Jetpack::is_active(); |
584
|
|
|
|
585
|
|
|
$data = array( |
586
|
|
|
'is_connected' => $is_connected, |
587
|
|
|
); |
588
|
|
|
Jetpack::init()->load_view( 'admin/network-admin-header.php', $data ); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Fires when the Jetpack > Settings page is saved. |
593
|
|
|
* |
594
|
|
|
* @since 2.9 |
595
|
|
|
*/ |
596
|
|
|
public function save_network_settings_page() { |
597
|
|
|
|
598
|
|
|
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'jetpack-network-settings' ) ) { |
599
|
|
|
// No nonce, push back to settings page. |
600
|
|
|
wp_safe_redirect( |
601
|
|
|
add_query_arg( |
602
|
|
|
array( 'page' => 'jetpack-settings' ), |
603
|
|
|
network_admin_url( 'admin.php' ) |
604
|
|
|
) |
605
|
|
|
); |
606
|
|
|
exit(); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
// Try to save the Protect whitelist before anything else, since that action can result in errors. |
610
|
|
|
$whitelist = str_replace( ' ', '', $_POST['global-whitelist'] ); |
611
|
|
|
$whitelist = explode( PHP_EOL, $whitelist ); |
612
|
|
|
$result = jetpack_protect_save_whitelist( $whitelist, true ); |
613
|
|
|
if ( is_wp_error( $result ) ) { |
614
|
|
|
wp_safe_redirect( |
615
|
|
|
add_query_arg( |
616
|
|
|
array( |
617
|
|
|
'page' => 'jetpack-settings', |
618
|
|
|
'error' => 'jetpack_protect_whitelist', |
619
|
|
|
), |
620
|
|
|
network_admin_url( 'admin.php' ) |
621
|
|
|
) |
622
|
|
|
); |
623
|
|
|
exit(); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/* |
627
|
|
|
* Fields |
628
|
|
|
* |
629
|
|
|
* auto-connect - Checkbox for global Jetpack connection |
630
|
|
|
* sub-site-connection-override - Allow sub-site admins to (dis)reconnect with their own Jetpack account |
631
|
|
|
*/ |
632
|
|
|
$auto_connect = 0; |
633
|
|
|
if ( isset( $_POST['auto-connect'] ) ) { |
634
|
|
|
$auto_connect = 1; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
$sub_site_connection_override = 0; |
638
|
|
|
if ( isset( $_POST['sub-site-connection-override'] ) ) { |
639
|
|
|
$sub_site_connection_override = 1; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Remove the toggles for 2.9, re-evaluate how they're done and added for a 3.0 release. They don't feel quite right yet. |
644
|
|
|
* |
645
|
|
|
* $manage_auto_activated_modules = 0; |
646
|
|
|
* if ( isset( $_POST['manage_auto_activated_modules'] ) ) { |
647
|
|
|
* $manage_auto_activated_modules = 1; |
648
|
|
|
* } |
649
|
|
|
* |
650
|
|
|
* $modules = array(); |
651
|
|
|
* if ( isset( $_POST['modules'] ) ) { |
652
|
|
|
* $modules = $_POST['modules']; |
653
|
|
|
* } |
654
|
|
|
* |
655
|
|
|
* Removed from the data array below: |
656
|
|
|
* // 'manage_auto_activated_modules' => $manage_auto_activated_modules, |
657
|
|
|
* // 'modules' => $modules, |
658
|
|
|
*/ |
659
|
|
|
|
660
|
|
|
$data = array( |
661
|
|
|
'auto-connect' => $auto_connect, |
662
|
|
|
'sub-site-connection-override' => $sub_site_connection_override, |
663
|
|
|
); |
664
|
|
|
|
665
|
|
|
update_site_option( $this->settings_name, $data ); |
666
|
|
|
wp_safe_redirect( |
667
|
|
|
add_query_arg( |
668
|
|
|
array( |
669
|
|
|
'page' => 'jetpack-settings', |
670
|
|
|
'updated' => 'true', |
671
|
|
|
), |
672
|
|
|
network_admin_url( 'admin.php' ) |
673
|
|
|
) |
674
|
|
|
); |
675
|
|
|
exit(); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* A hook handler for adding admin pages and subpages. |
680
|
|
|
*/ |
681
|
|
|
public function wrap_render_network_admin_settings_page() { |
682
|
|
|
Jetpack_Admin_Page::wrap_ui( array( $this, 'render_network_admin_settings_page' ) ); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* A hook rendering the admin settings page. |
687
|
|
|
*/ |
688
|
|
|
public function render_network_admin_settings_page() { |
689
|
|
|
$this->network_admin_page_header(); |
690
|
|
|
$options = wp_parse_args( get_site_option( $this->settings_name ), $this->setting_defaults ); |
691
|
|
|
|
692
|
|
|
$modules = array(); |
693
|
|
|
$module_slugs = Jetpack::get_available_modules(); |
694
|
|
|
foreach ( $module_slugs as $slug ) { |
695
|
|
|
$module = Jetpack::get_module( $slug ); |
696
|
|
|
$module['module'] = $slug; |
697
|
|
|
$modules[] = $module; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
usort( $modules, array( 'Jetpack', 'sort_modules' ) ); |
701
|
|
|
|
702
|
|
|
if ( ! isset( $options['modules'] ) ) { |
703
|
|
|
$options['modules'] = $modules; |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
$data = array( |
707
|
|
|
'modules' => $modules, |
708
|
|
|
'options' => $options, |
709
|
|
|
'jetpack_protect_whitelist' => jetpack_protect_format_whitelist(), |
710
|
|
|
); |
711
|
|
|
|
712
|
|
|
Jetpack::init()->load_view( 'admin/network-settings.php', $data ); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* Updates a site wide option |
717
|
|
|
* |
718
|
|
|
* @since 2.9 |
719
|
|
|
* |
720
|
|
|
* @param string $key option name. |
721
|
|
|
* @param mixed $value option value. |
722
|
|
|
* |
723
|
|
|
* @return boolean |
724
|
|
|
**/ |
725
|
|
|
public function update_option( $key, $value ) { |
726
|
|
|
$options = get_site_option( $this->settings_name, $this->setting_defaults ); |
727
|
|
|
$options[ $key ] = $value; |
728
|
|
|
|
729
|
|
|
return update_site_option( $this->settings_name, $options ); |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
/** |
733
|
|
|
* Retrieves a site wide option |
734
|
|
|
* |
735
|
|
|
* @since 2.9 |
736
|
|
|
* |
737
|
|
|
* @param string $name - Name of the option in the database. |
738
|
|
|
**/ |
739
|
|
|
public function get_option( $name ) { |
740
|
|
|
$options = get_site_option( $this->settings_name, $this->setting_defaults ); |
741
|
|
|
$options = wp_parse_args( $options, $this->setting_defaults ); |
742
|
|
|
|
743
|
|
|
if ( ! isset( $options[ $name ] ) ) { |
744
|
|
|
$options[ $name ] = null; |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
return $options[ $name ]; |
748
|
|
|
} |
749
|
|
|
} |
750
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: