Completed
Push — renovate/gridicons-3.x ( c004c1...f8ccd4 )
by
unknown
284:06 queued 275:32
created

Jetpack_Network::get_option()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Connection\Client;
4
5
/**
6
 * Used to manage Jetpack installation on Multisite Network installs
7
 *
8
 * SINGLETON: To use call Jetpack_Network::init()
9
 *
10
 * DO NOT USE ANY STATIC METHODS IN THIS CLASS!!!!!!
11
 *
12
 * @since 2.9
13
 */
14
class Jetpack_Network {
15
16
	/**
17
	 * Holds a static copy of Jetpack_Network for the singleton
18
	 *
19
	 * @since 2.9
20
	 * @var Jetpack_Network
21
	 */
22
	private static $instance = null;
23
24
	/**
25
	 * Name of the network wide settings
26
	 *
27
	 * @since 2.9
28
	 * @var string
29
	 */
30
	private $settings_name = 'jetpack-network-settings';
31
32
	/**
33
	 * Defaults for settings found on the Jetpack > Settings page
34
	 *
35
	 * @since 2.9
36
	 * @var array
37
	 */
38
	private $setting_defaults = array(
39
		'auto-connect'                  => 0,
40
		'sub-site-connection-override'  => 1,
41
		//'manage_auto_activated_modules' => 0,
42
	);
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @since 2.9
48
	 */
49
	private function __construct() {
50
		require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); // For the is_plugin... check
51
		require_once( JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php' ); // For managing the global whitelist
52
		/*
53
		 * Sanity check to ensure the install is Multisite and we
54
		 * are in Network Admin
55
		 */
56
		if ( is_multisite() && is_network_admin() ) {
57
			add_action( 'network_admin_menu', array( $this, 'add_network_admin_menu' ) );
58
			add_action( 'network_admin_edit_jetpack-network-settings', array( $this, 'save_network_settings_page' ), 10, 0 );
59
			add_filter( 'admin_body_class', array( $this, 'body_class' ) );
60
61
			if ( isset( $_GET['page'] ) && 'jetpack' == $_GET['page'] ) {
62
				add_action( 'admin_init', array( $this, 'jetpack_sites_list' ) );
63
			}
64
		}
65
66
		/*
67
		 * Things that should only run on multisite
68
		 */
69
		if ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
70
			add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) );
71
72
			/*
73
			 * If admin wants to automagically register new sites set the hook here
74
			 *
75
			 * This is a hacky way because xmlrpc is not available on wp_initialize_site
76
			 */
77
			if ( $this->get_option( 'auto-connect' ) == 1 ) {
78
				add_action( 'wp_initialize_site', array( $this, 'do_automatically_add_new_site' ) );
79
			}
80
		}
81
82
		// 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.
83
		// add_filter( 'jetpack_get_default_modules', array( $this, 'set_auto_activated_modules' ) );
84
	}
85
86
	/**
87
	 * Sets which modules get activated by default on subsite connection.
88
	 * Modules can be set in Network Admin > Jetpack > Settings
89
	 *
90
	 * @since 2.9
91
	 *
92
	 * @param array $modules
93
	 *
94
	 * @return array
95
	 **/
96
	public function set_auto_activated_modules( $modules ) {
97
		return $modules;
98
99
		/* 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.
100
		if( 1 == $this->get_option( 'manage_auto_activated_modules' ) ) {
101
			return (array) $this->get_option( 'modules' );
102
		} else {
103
			return $modules;
104
		}
105
		*/
106
	}
107
108
	/**
109
	 * Registers new sites upon creation
110
	 *
111
	 * @since 2.9
112
	 * @since 7.4.0 Uses a WP_Site object.
113
	 * @uses  wp_initialize_site
114
	 *
115
	 * @param WP_Site $site
116
	 **/
117
	public function do_automatically_add_new_site( $site ) {
118
		if ( is_a( $site, 'WP_Site') ) {
119
			$this->do_subsiteregister( $site->id );
120
		}
121
	}
122
123
	/**
124
	 * Adds .network-admin class to the body tag
125
	 * Helps distinguish network admin JP styles from regular site JP styles
126
	 *
127
	 * @since 2.9
128
	 */
129
	public function body_class( $classes ) {
130
		return trim( $classes ) . ' network-admin ';
131
	}
132
133
	/**
134
	 * Provides access to an instance of Jetpack_Network
135
	 *
136
	 * This is how the Jetpack_Network object should *always* be accessed
137
	 *
138
	 * @since 2.9
139
	 * @return Jetpack_Network
140
	 */
141
	public static function init() {
142
		if ( ! self::$instance || ! is_a( self::$instance, 'Jetpack_Network' ) ) {
143
			self::$instance = new Jetpack_Network;
144
		}
145
146
		return self::$instance;
147
	}
148
149
	/**
150
	 * Registers the Multisite admin bar menu item shortcut.
151
	 * This shortcut helps users quickly and easily navigate to the Jetpack Network Admin
152
	 * menu from anywhere in their network.
153
	 *
154
	 * @since 2.9
155
	 */
156
	public function register_menubar() {
157
		add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) );
158
	}
159
160
	/**
161
	 * Runs when Jetpack is deactivated from the network admin plugins menu.
162
	 * Each individual site will need to have Jetpack::disconnect called on it.
163
	 * Site that had Jetpack individually enabled will not be disconnected as
164
	 * on Multisite individually activated plugins are still activated when
165
	 * a plugin is deactivated network wide.
166
	 *
167
	 * @since 2.9
168
	 **/
169
	public function deactivate() {
170
		// Only fire if in network admin
171
		if ( ! is_network_admin() ) {
172
			return;
173
		}
174
175
		$sites = get_sites();
176
177
		foreach ( $sites as $s ) {
178
			switch_to_blog( $s->blog_id );
179
			$active_plugins = get_option( 'active_plugins' );
180
181
			/*
182
			 * If this plugin was activated in the subsite individually
183
			 * we do not want to call disconnect. Plugins activated
184
		 	 * individually (before network activation) stay activated
185
		 	 * when the network deactivation occurs
186
		 	 */
187
			if ( ! in_array( 'jetpack/jetpack.php', $active_plugins ) ) {
188
				Jetpack::disconnect();
189
			}
190
		}
191
		restore_current_blog();
192
	}
193
194
	/**
195
	 * Adds a link to the Jetpack Network Admin page in the network admin menu bar.
196
	 *
197
	 * @since 2.9
198
	 **/
199
	public function add_to_menubar() {
200
		global $wp_admin_bar;
201
		// Don't show for logged out users or single site mode.
202
		if ( ! is_user_logged_in() || ! is_multisite() ) {
203
			return;
204
		}
205
206
		$wp_admin_bar->add_node( array(
207
			'parent' => 'network-admin',
208
			'id'     => 'network-admin-jetpack',
209
			'title'  => 'Jetpack',
210
			'href'   => $this->get_url( 'network_admin_page' ),
211
		) );
212
	}
213
214
	/**
215
	 * Returns various URL strings. Factory like
216
	 *
217
	 * $args can be a string or an array.
218
	 * If $args is an array there must be an element called name for the switch statement
219
	 *
220
	 * Currently supports:
221
	 * - subsiteregister: Pass array( 'name' => 'subsiteregister', 'site_id' => SITE_ID )
222
	 * - network_admin_page: Provides link to /wp-admin/network/JETPACK
223
	 * - subsitedisconnect: Pass array( 'name' => 'subsitedisconnect', 'site_id' => SITE_ID )
224
	 *
225
	 * @since 2.9
226
	 *
227
	 * @param Mixed $args
228
	 *
229
	 * @return String
230
	 **/
231
	public function get_url( $args ) {
232
		$url = null; // Default url value
233
234
		if ( is_string( $args ) ) {
235
			$name = $args;
236
		} else {
237
			$name = $args['name'];
238
		}
239
240
		switch ( $name ) {
241
			case 'subsiteregister':
242
				if ( ! isset( $args['site_id'] ) ) {
243
					break; // If there is not a site id present we cannot go further
244
				}
245
				$url = network_admin_url(
246
					'admin.php?page=jetpack&action=subsiteregister&site_id='
247
					. $args['site_id']
248
				);
249
				break;
250
251
			case 'network_admin_page':
252
				$url = network_admin_url( 'admin.php?page=jetpack' );
253
				break;
254
255
			case 'subsitedisconnect':
256
				if ( ! isset( $args['site_id'] ) ) {
257
					break; // If there is not a site id present we cannot go further
258
				}
259
				$url = network_admin_url(
260
					'admin.php?page=jetpack&action=subsitedisconnect&site_id='
261
					. $args['site_id']
262
				);
263
				break;
264
		}
265
266
		return $url;
267
	}
268
269
	/**
270
	 * Adds the Jetpack  menu item to the Network Admin area
271
	 *
272
	 * @since 2.9
273
	 */
274
	public function add_network_admin_menu() {
275
		add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_network_admin_page', 'jetpack', array( $this, 'wrap_network_admin_page' ), 'div', 3 );
276
		$jetpack_sites_page_hook = add_submenu_page( 'jetpack', __( 'Jetpack Sites', 'jetpack' ), __( 'Sites', 'jetpack' ), 'jetpack_network_sites_page', 'jetpack', array( $this, 'wrap_network_admin_page' ) );
277
		$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' ) );
278
		add_action( "admin_print_styles-$jetpack_sites_page_hook",  array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) );
279
		add_action( "admin_print_styles-$jetpack_settings_page_hook",  array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) );
280
		/**
281
		 * As jetpack_register_genericons is by default fired off a hook,
282
		 * the hook may have already fired by this point.
283
		 * So, let's just trigger it manually.
284
		 */
285
		require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' );
286
		jetpack_register_genericons();
287
288 View Code Duplication
		if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) {
289
			wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION );
290
		}
291
292
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) );
293
	}
294
295
	/**
296
	 * Adds JP menu icon
297
	 *
298
	 * @since 2.9
299
	 **/
300
	function admin_menu_css() {
301
		wp_enqueue_style( 'jetpack-icons' );
302
	}
303
304
	/**
305
	 * Provides functionality for the Jetpack > Sites page.
306
	 * Does not do the display!
307
	 *
308
	 * @since 2.9
309
	 */
310
	public function jetpack_sites_list() {
311
		Jetpack::init();
312
313
		if ( isset( $_GET['action'] ) ) {
314
			switch ( $_GET['action'] ) {
315
				case 'subsiteregister':
316
					/*
317
					 * @todo check_admin_referer( 'jetpack-subsite-register' );
318
					 */
319
					Jetpack::log( 'subsiteregister' );
320
321
					// If !$_GET['site_id'] stop registration and error
322 View Code Duplication
					if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) {
323
						// Log error to state cookie for display later
324
						/**
325
						 * @todo Make state messages show on Jetpack NA pages
326
						 **/
327
						Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to register a sub-site.', 'jetpack' ) );
328
						break;
329
					}
330
331
					// Send data to register endpoint and retrieve shadow blog details
332
					$result = $this->do_subsiteregister();
333
					$url    = $this->get_url( 'network_admin_page' );
334
335
					if ( is_wp_error( $result ) ) {
336
						$url = add_query_arg( 'action', 'connection_failed', $url );
337
					} else {
338
						$url = add_query_arg( 'action', 'connected', $url );
339
					}
340
341
					wp_safe_redirect( $url );
342
					exit;
343
344
				case 'subsitedisconnect':
345
					Jetpack::log( 'subsitedisconnect' );
346
347 View Code Duplication
					if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) {
348
						Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to disconnect a sub-site.', 'jetpack' ) );
349
						break;
350
					}
351
352
					$this->do_subsitedisconnect();
353
					break;
354
355
				case 'connected':
356
				case 'connection_failed':
357
					add_action( 'jetpack_notices', array( $this, 'show_jetpack_notice' ) );
358
					break;
359
			}
360
		}
361
	}
362
363
	public function show_jetpack_notice() {
364
		if ( isset( $_GET['action'] ) && 'connected' == $_GET['action'] ) {
365
			$notice = __( 'Site successfully connected.', 'jetpack' );
366
			$classname = 'updated';
367
		} else if ( isset( $_GET['action'] ) && 'connection_failed' == $_GET['action'] ) {
368
			$notice = __( 'Site connection failed!', 'jetpack' );
369
			$classname = 'error';
370
		}
371
		?>
372
		<div id="message" class="<?php echo esc_attr( $classname );?> jetpack-message jp-connect" style="display:block !important;">
0 ignored issues
show
Bug introduced by
The variable $classname does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
373
			<p><?php echo esc_html( $notice ); ?></p>
0 ignored issues
show
Bug introduced by
The variable $notice does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
374
		</div>
375
		<?php
376
	}
377
378
	/**
379
	 * Disconnect functionality for an individual site
380
	 *
381
	 * @since 2.9
382
	 * @see   Jetpack_Network::jetpack_sites_list()
383
	 */
384
	public function do_subsitedisconnect( $site_id = null ) {
385
		if ( ! current_user_can( 'jetpack_disconnect' ) ) {
386
			return;
387
		}
388
		$site_id = ( is_null( $site_id ) ) ? $_GET['site_id'] : $site_id;
389
		switch_to_blog( $site_id );
390
		Jetpack::disconnect();
391
		restore_current_blog();
392
	}
393
394
	/**
395
	 * Registers a subsite with the Jetpack servers
396
	 *
397
	 * @since 2.9
398
	 * @todo  Break apart into easier to manage chunks that can be unit tested
399
	 * @see   Jetpack_Network::jetpack_sites_list();
400
	 */
401
	public function do_subsiteregister( $site_id = null ) {
402
		if ( ! current_user_can( 'jetpack_disconnect' ) ) {
403
			return;
404
		}
405
406
		if ( Jetpack::is_development_mode() ) {
407
			return;
408
		}
409
410
		$jp = Jetpack::init();
411
412
		// Figure out what site we are working on
413
		$site_id = ( is_null( $site_id ) ) ? $_GET['site_id'] : $site_id;
414
415
		// better to try (and fail) to set a higher timeout than this system
416
		// supports than to have register fail for more users than it should
417
		$timeout = Jetpack::set_min_time_limit( 60 ) / 2;
418
419
		// The blog id on WordPress.com of the primary network site
420
		$network_wpcom_blog_id = Jetpack_Options::get_option( 'id' );
421
422
		/*
423
		 * Here we need to switch to the subsite
424
		 * For the registration process we really only hijack how it
425
		 * works for an individual site and pass in some extra data here
426
		 */
427
		switch_to_blog( $site_id );
428
429
		// Save the secrets in the subsite so when the wpcom server does a pingback it
430
		// will be able to validate the connection
431
		$secrets = $jp->generate_secrets( 'register' );
432 View Code Duplication
		if (
433
			empty( $secrets['secret_1'] ) ||
434
			empty( $secrets['secret_2']  ) ||
435
			empty( $secrets['exp'] )
436
		) {
437
			return new Jetpack_Error( 'missing_secrets' );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'missing_secrets'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
438
		}
439
440
		// Gra info for gmt offset
441
		$gmt_offset = get_option( 'gmt_offset' );
442
		if ( ! $gmt_offset ) {
443
			$gmt_offset = 0;
444
		}
445
446
		/*
447
		 * Get the stats_option option from the db.
448
		 * It looks like the server strips this out so maybe it is not necessary?
449
		 * Does it match the Jetpack site with the old stats plugin id?
450
		 *
451
		 * @todo Find out if sending the stats_id is necessary
452
		 */
453
		$stats_options = get_option( 'stats_options' );
454
		$stat_id = $stat_options = isset( $stats_options['blog_id'] ) ? $stats_options['blog_id'] : null;
0 ignored issues
show
Unused Code introduced by
$stat_options is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
455
		$user_id = get_current_user_id();
456
457
		$tracks = new Automattic\Jetpack\Tracking();
458
		$tracks_identity = $tracks->tracks_get_identity( get_current_user_id() );
459
460
		/*
461
		 * Use the subsite's registration date as the site creation date.
462
		 *
463
		 * This is in contrast to regular standalone sites, where we use the helper
464
		 * `Jetpack::get_assumed_site_creation_date()` to assume the site's creation date.
465
		 */
466
		$blog_details = get_blog_details();
467
		$site_creation_date = $blog_details->registered;
468
469
		/**
470
		 * Both `state` and `user_id` need to be sent in the request, even though they are the same value.
471
		 * Connecting via the network admin combines `register()` and `authorize()` methods into one step,
472
		 * because we assume the main site is already authorized. `state` is used to verify the `register()`
473
		 * request, while `user_id()` is used to create the token in the `authorize()` request.
474
		 */
475
		$args = array(
476
			'method'  => 'POST',
477
			'body'    => array(
478
				'network_url'           => $this->get_url( 'network_admin_page' ),
479
				'network_wpcom_blog_id' => $network_wpcom_blog_id,
480
				'siteurl'               => site_url(),
481
				'home'                  => home_url(),
482
				'gmt_offset'            => $gmt_offset,
483
				'timezone_string'       => (string) get_option( 'timezone_string' ),
484
				'site_name'             => (string) get_option( 'blogname' ),
485
				'secret_1'              => $secrets['secret_1'],
486
				'secret_2'              => $secrets['secret_2'],
487
				'site_lang'             => get_locale(),
488
				'timeout'               => $timeout,
489
				'stats_id'              => $stat_id, // Is this still required?
490
				'user_id'               => $user_id,
491
				'state'                 => $user_id,
492
				'_ui'                   => $tracks_identity['_ui'],
493
				'_ut'                   => $tracks_identity['_ut'],
494
				'site_created'          => $site_creation_date,
495
				'jetpack_version'       => JETPACK__VERSION
496
			),
497
			'headers' => array(
498
				'Accept' => 'application/json',
499
			),
500
			'timeout' => $timeout,
501
		);
502
503
		Jetpack::apply_activation_source_to_args( $args['body'] );
504
505
		// Attempt to retrieve shadow blog details
506
		$response = Client::_wp_remote_request(
507
			Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'subsiteregister' ) ), $args, true
508
		);
509
510
		/*
511
		 * $response should either be invalid or contain:
512
		 * - jetpack_id	=> id
513
		 * - jetpack_secret => blog_token
514
		 * - jetpack_public
515
		 *
516
		 * Store the wpcom site details
517
		 */
518
		$valid_response = $jp->validate_remote_register_response( $response );
519
520
		if ( is_wp_error( $valid_response ) || ! $valid_response ) {
521
			restore_current_blog();
522
			return $valid_response;
523
		}
524
525
		// Grab the response values to work with
526
		$code   = wp_remote_retrieve_response_code( $response );
527
		$entity = wp_remote_retrieve_body( $response );
528
		if ( $entity ) {
529
			$json = json_decode( $entity );
530
		} else {
531
			$json = false;
532
		}
533
534 View Code Duplication
		if ( empty( $json->jetpack_secret ) || ! is_string( $json->jetpack_secret ) ) {
535
			restore_current_blog();
536
			return new Jetpack_Error( 'jetpack_secret', '', $code );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with 'jetpack_secret'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
537
		}
538
539
		if ( isset( $json->jetpack_public ) ) {
540
			$jetpack_public = (int) $json->jetpack_public;
541
		} else {
542
			$jetpack_public = false;
543
		}
544
545
		Jetpack_Options::update_options( array(
546
			'id'         => (int) $json->jetpack_id,
547
			'blog_token' => (string) $json->jetpack_secret,
548
			'public'     => $jetpack_public,
549
		) );
550
551
		/*
552
		 * Update the subsiteregister method on wpcom so that it also sends back the
553
		 * token in this same request
554
		 */
555
		$is_master_user = ! Jetpack::is_active();
556
		Jetpack::update_user_token(
557
			get_current_user_id(),
558
			sprintf( '%s.%d', $json->token->secret, get_current_user_id() ),
559
			$is_master_user
560
		);
561
562
		Jetpack::activate_default_modules();
563
564
		restore_current_blog();
565
	}
566
567
	function wrap_network_admin_page() {
568
		Jetpack_Admin_Page::wrap_ui( array( $this, 'network_admin_page' ) );
569
	}
570
571
	/**
572
	 * Handles the displaying of all sites on the network that are
573
	 * dis/connected to Jetpack
574
	 *
575
	 * @since 2.9
576
	 * @see   Jetpack_Network::jetpack_sites_list()
577
	 */
578
	function network_admin_page() {
579
		global $current_site;
580
		$this->network_admin_page_header();
581
582
		$jp = Jetpack::init();
583
584
		// We should be, but ensure we are on the main blog
585
		switch_to_blog( $current_site->blog_id );
586
		$main_active = $jp->is_active();
587
		restore_current_blog();
588
589
		// If we are in dev mode, just show the notice and bail
590
		if ( Jetpack::is_development_mode() ) {
591
			Jetpack::show_development_mode_notice();
592
			return;
593
		}
594
595
		/*
596
		 * Ensure the main blog is connected as all other subsite blog
597
		 * connections will feed off this one
598
		 */
599
		if ( ! $main_active ) {
600
			$url  = $this->get_url( array(
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
601
				'name'    => 'subsiteregister',
602
				'site_id' => 1,
603
			) );
604
			$data = array( 'url' => $jp->build_connect_url() );
605
			Jetpack::init()->load_view( 'admin/must-connect-main-blog.php', $data );
606
607
			return;
608
		}
609
610
		require_once( 'class.jetpack-network-sites-list-table.php' );
611
		$myListTable = new Jetpack_Network_Sites_List_Table();
612
		echo '<div class="wrap"><h2>' . __( 'Sites', 'jetpack' ) . '</h2>';
613
		echo '<form method="post">';
614
		$myListTable->prepare_items();
615
		$myListTable->display();
616
		echo '</form></div>';
617
618
	}
619
620
	/**
621
	 * Stylized JP header formatting
622
	 *
623
	 * @since 2.9
624
	 */
625
	function network_admin_page_header() {
626
		global $current_user;
627
628
		$is_connected = Jetpack::is_active();
629
630
		$data = array(
631
			'is_connected' => $is_connected
632
		);
633
		Jetpack::init()->load_view( 'admin/network-admin-header.php', $data );
634
	}
635
636
637
	/**
638
	 * Fires when the Jetpack > Settings page is saved.
639
	 *
640
	 * @since 2.9
641
	 */
642
	public function save_network_settings_page() {
643
644
		if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'jetpack-network-settings' ) ) {
645
			// no nonce, push back to settings page
646
			wp_safe_redirect(
647
				add_query_arg(
648
					array( 'page' => 'jetpack-settings' ),
649
					network_admin_url( 'admin.php' )
650
				)
651
			);
652
			exit();
653
		}
654
655
		// try to save the Protect whitelist before anything else, since that action can result in errors
656
		$whitelist = str_replace( ' ', '', $_POST['global-whitelist'] );
657
		$whitelist = explode( PHP_EOL, $whitelist );
658
		$result    = jetpack_protect_save_whitelist( $whitelist, $global = true );
659
		if ( is_wp_error( $result ) ) {
660
			wp_safe_redirect(
661
				add_query_arg(
662
					array( 'page' => 'jetpack-settings', 'error' => 'jetpack_protect_whitelist' ),
663
					network_admin_url( 'admin.php' )
664
				)
665
			);
666
			exit();
667
		}
668
669
		/*
670
		 * Fields
671
		 *
672
		 * auto-connect - Checkbox for global Jetpack connection
673
		 * sub-site-connection-override - Allow sub-site admins to (dis)reconnect with their own Jetpack account
674
		 */
675
		$auto_connect = 0;
676
		if ( isset( $_POST['auto-connect'] ) ) {
677
			$auto_connect = 1;
678
		}
679
680
		$sub_site_connection_override = 0;
681
		if ( isset( $_POST['sub-site-connection-override'] ) ) {
682
			$sub_site_connection_override = 1;
683
		}
684
685
		/* 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.
686
		$manage_auto_activated_modules = 0;
687
		if ( isset( $_POST['manage_auto_activated_modules'] ) ) {
688
			$manage_auto_activated_modules = 1;
689
		}
690
691
		$modules = array();
692
		if ( isset( $_POST['modules'] ) ) {
693
			$modules = $_POST['modules'];
694
		}
695
		*/
696
697
		$data = array(
698
			'auto-connect'                  => $auto_connect,
699
			'sub-site-connection-override'  => $sub_site_connection_override,
700
			//'manage_auto_activated_modules' => $manage_auto_activated_modules,
701
			//'modules'                       => $modules,
702
		);
703
704
		update_site_option( $this->settings_name, $data );
705
		wp_safe_redirect(
706
			add_query_arg(
707
				array( 'page' => 'jetpack-settings', 'updated' => 'true' ),
708
				network_admin_url( 'admin.php' )
709
			)
710
		);
711
		exit();
712
	}
713
714
	public function wrap_render_network_admin_settings_page() {
715
		Jetpack_Admin_Page::wrap_ui( array( $this, 'render_network_admin_settings_page' ) );
716
	}
717
718
	public function render_network_admin_settings_page() {
719
		$this->network_admin_page_header();
720
		$options = wp_parse_args( get_site_option( $this->settings_name ), $this->setting_defaults );
721
722
		$modules = array();
723
		$module_slugs = Jetpack::get_available_modules();
724
		foreach ( $module_slugs as $slug ) {
725
			$module           = Jetpack::get_module( $slug );
726
			$module['module'] = $slug;
727
			$modules[]        = $module;
728
		}
729
730
		usort( $modules, array( 'Jetpack', 'sort_modules' ) );
731
732
		if ( ! isset( $options['modules'] ) ) {
733
			$options['modules'] = $modules;
734
		}
735
736
		$data = array(
737
			'modules' => $modules,
738
			'options' => $options,
739
			'jetpack_protect_whitelist' => jetpack_protect_format_whitelist(),
740
		);
741
742
		Jetpack::init()->load_view( 'admin/network-settings.php', $data );
743
	}
744
745
	/**
746
	 * Updates a site wide option
747
	 *
748
	 * @since 2.9
749
	 *
750
	 * @param string $key
751
	 * @param mixed  $value
752
	 *
753
	 * @return boolean
754
	 **/
755
	public function update_option( $key, $value ) {
756
		$options  = get_site_option( $this->settings_name, $this->setting_defaults );
757
		$options[ $key ] = $value;
758
759
		return update_site_option( $this->settings_name, $options );
760
	}
761
762
	/**
763
	 * Retrieves a site wide option
764
	 *
765
	 * @since 2.9
766
	 *
767
	 * @param string $name - Name of the option in the database
768
	 **/
769
	public function get_option( $name ) {
770
		$options = get_site_option( $this->settings_name, $this->setting_defaults );
771
		$options = wp_parse_args( $options, $this->setting_defaults );
772
773
		if ( ! isset( $options[ $name ] ) ) {
774
			$options[ $name ] = null;
775
		}
776
777
		return $options[ $name ];
778
	}
779
780
}
781
782
// end class
783