<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFilename
* Jetpack Network Manager class file.
* @package automattic/jetpack
use Automattic\Jetpack\Assets\Logo;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Connection\Tokens;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions;
* Used to manage Jetpack installation on Multisite Network installs
* SINGLETON: To use call Jetpack_Network::init()
* DO NOT USE ANY STATIC METHODS IN THIS CLASS!!!!!!
* Holds a static copy of Jetpack_Network for the singleton
private static $instance = null;
* An instance of the connection manager object.
* @var Automattic\Jetpack\Connection\Manager
* Name of the network wide settings
private $settings_name = 'jetpack-network-settings';
* Defaults for settings found on the Jetpack > Settings page
private $setting_defaults = array(
'sub-site-connection-override' => 1,
private function __construct() {
require_once ABSPATH . '/wp-admin/includes/plugin.php'; // For the is_plugin... check.
* Sanity check to ensure the install is Multisite and we
if ( is_multisite() && is_network_admin() ) {
add_action( 'network_admin_menu', array( $this, 'add_network_admin_menu' ) );
add_action( 'network_admin_edit_jetpack-network-settings', array( $this, 'save_network_settings_page' ), 10, 0 );
add_filter( 'admin_body_class', array( $this, 'body_class' ) );
if ( isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is view logic.
add_action( 'admin_init', array( $this, 'jetpack_sites_list' ) );
* Things that should only run on multisite
if ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) );
add_filter( 'jetpack_disconnect_cap', array( $this, 'set_multisite_disconnect_cap' ) );
* If admin wants to automagically register new sites set the hook here
* This is a hacky way because xmlrpc is not available on wp_initialize_site
if ( 1 === $this->get_option( 'auto-connect' ) ) {
add_action( 'wp_initialize_site', array( $this, 'do_automatically_add_new_site' ) );
* Sets a connection object.
* @param Automattic\Jetpack\Connection\Manager $connection the connection manager object.
public function set_connection( Manager $connection ) {
$this->connection = $connection;
* Registers new sites upon creation
* @since 7.4.0 Uses a WP_Site object.
* @uses wp_initialize_site
* @param WP_Site $site the WordPress site object.
public function do_automatically_add_new_site( $site ) {
if ( is_a( $site, 'WP_Site' ) ) {
$this->do_subsiteregister( $site->id );
* Adds .network-admin class to the body tag
* Helps distinguish network admin JP styles from regular site JP styles
* @param String $classes current assigned body classes.
* @return String amended class string.
public function body_class( $classes ) {
return trim( $classes ) . ' network-admin ';
* Provides access to an instance of Jetpack_Network
* This is how the Jetpack_Network object should *always* be accessed
* @return Jetpack_Network
public static function init() {
if ( ! self::$instance || ! is_a( self::$instance, 'Jetpack_Network' ) ) {
self::$instance = new Jetpack_Network();
* Registers the Multisite admin bar menu item shortcut.
* This shortcut helps users quickly and easily navigate to the Jetpack Network Admin
* menu from anywhere in their network.
public function register_menubar() {
add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) );
* Runs when Jetpack is deactivated from the network admin plugins menu.
* Each individual site will need to have Jetpack::disconnect called on it.
* Site that had Jetpack individually enabled will not be disconnected as
* on Multisite individually activated plugins are still activated when
* a plugin is deactivated network wide.
public function deactivate() {
// Only fire if in network admin.
if ( ! is_network_admin() ) {
foreach ( $sites as $s ) {
switch_to_blog( (int) $s->blog_id );
$active_plugins = get_option( 'active_plugins' );
* If this plugin was activated in the subsite individually
* we do not want to call disconnect. Plugins activated
* individually (before network activation) stay activated
* when the network deactivation occurs
if ( ! in_array( 'jetpack/jetpack.php', $active_plugins, true ) ) {
Jetpack_Options::delete_option( 'version' );
* Adds a link to the Jetpack Network Admin page in the network admin menu bar.
public function add_to_menubar() {
// Don't show for logged out users or single site mode.
if ( ! is_user_logged_in() || ! is_multisite() ) {
'parent' => 'network-admin',
'id' => 'network-admin-jetpack',
'href' => $this->get_url( 'network_admin_page' ),
* Returns various URL strings. Factory like
* $args can be a string or an array.
* If $args is an array there must be an element called name for the switch statement
* - subsiteregister: Pass array( 'name' => 'subsiteregister', 'site_id' => SITE_ID )
* - network_admin_page: Provides link to /wp-admin/network/JETPACK
* - subsitedisconnect: Pass array( 'name' => 'subsitedisconnect', 'site_id' => SITE_ID )
* @param Mixed $args URL parameters.
public function get_url( $args ) {
$url = null; // Default url value.
if ( is_string( $args ) ) {
} elseif ( is_array( $args ) ) {
if ( ! isset( $args['site_id'] ) ) {
break; // If there is not a site id present we cannot go further.
$url = network_admin_url(
'admin.php?page=jetpack&action=subsiteregister&site_id='
case 'network_admin_page':
$url = network_admin_url( 'admin.php?page=jetpack' );
case 'subsitedisconnect':
if ( ! isset( $args['site_id'] ) ) {
break; // If there is not a site id present we cannot go further.
$url = network_admin_url(
'admin.php?page=jetpack&action=subsitedisconnect&site_id='
* Adds the Jetpack menu item to the Network Admin area
public function add_network_admin_menu() {
$icon = ( new Logo() )->get_base64_logo();
add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_network_admin_page', 'jetpack', array( $this, 'wrap_network_admin_page' ), $icon, 3 );
$jetpack_sites_page_hook = add_submenu_page( 'jetpack', __( 'Jetpack Sites', 'jetpack' ), __( 'Sites', 'jetpack' ), 'jetpack_network_sites_page', 'jetpack', array( $this, 'wrap_network_admin_page' ) );
$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' ) );
add_action( "admin_print_styles-$jetpack_sites_page_hook", array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) );
add_action( "admin_print_styles-$jetpack_settings_page_hook", array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) );
* As jetpack_register_genericons is by default fired off a hook,
* the hook may have already fired by this point.
* So, let's just trigger it manually.
require_once JETPACK__PLUGIN_DIR . '_inc/genericons.php';
jetpack_register_genericons();
* Provides functionality for the Jetpack > Sites page.
* Does not do the display!
public function jetpack_sites_list() {
if ( isset( $_GET['action'] ) ) {
switch ( $_GET['action'] ) {
check_admin_referer( 'jetpack-subsite-register' );
Jetpack::log( 'subsiteregister' );
// If no site_id, stop registration and error.
if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) {
* Log error to state cookie for display later.
* @todo Make state messages show on Jetpack NA pages
Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to register a sub-site.', 'jetpack' ) );
// Send data to register endpoint and retrieve shadow blog details.
$result = $this->do_subsiteregister();
$url = $this->get_url( 'network_admin_page' );
if ( is_wp_error( $result ) ) {
$url = add_query_arg( 'action', 'connection_failed', $url );
$url = add_query_arg( 'action', 'connected', $url );
wp_safe_redirect( $url );
case 'subsitedisconnect':
check_admin_referer( 'jetpack-subsite-disconnect' );
Jetpack::log( 'subsitedisconnect' );
if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) {
Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to disconnect a sub-site.', 'jetpack' ) );
$this->do_subsitedisconnect();
case 'connection_failed':
add_action( 'jetpack_notices', array( $this, 'show_jetpack_notice' ) );
* Set the disconnect capability for multisite.
* @param array $caps The capabilities array.
public function set_multisite_disconnect_cap( $caps ) {
// Can individual site admins manage their own connection?
if ( ! is_super_admin() && ! $this->get_option( 'sub-site-connection-override' ) ) {
* We need to update the option name -- it's terribly unclear which
* direction the override goes.
* @todo: Update the option name to `sub-sites-can-manage-own-connections`
return array( 'do_not_allow' );
* Shows the Jetpack plugin notices.
public function show_jetpack_notice() {
if ( isset( $_GET['action'] ) && 'connected' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is view logic.
$notice = __( 'Site successfully connected.', 'jetpack' );
} elseif ( isset( $_GET['action'] ) && 'connection_failed' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is view logic.
$notice = __( 'Site connection failed!', 'jetpack' );
<div id="message" class="<?php echo esc_attr( $classname ); ?> jetpack-message jp-connect" style="display:block !important;">
<p><?php echo esc_html( $notice ); ?></p>
* Disconnect functionality for an individual site
* @see Jetpack_Network::jetpack_sites_list()
* @param int $site_id the site identifier.
public function do_subsitedisconnect( $site_id = null ) {
if ( ! current_user_can( 'jetpack_disconnect' ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Caller (i.e. `$this->jetpack_sites_list()`) should check.
$site_id = ( $site_id === null ) ? ( isset( $_GET['site_id'] ) ? (int) $_GET['site_id'] : null ) : $site_id;
switch_to_blog( $site_id );
* Registers a subsite with the Jetpack servers
* @todo Break apart into easier to manage chunks that can be unit tested
* @see Jetpack_Network::jetpack_sites_list();
* @param int $site_id the site identifier.
public function do_subsiteregister( $site_id = null ) {
if ( ! current_user_can( 'jetpack_disconnect' ) ) {
if ( ( new Status() )->is_offline_mode() ) {
// Figure out what site we are working on.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Caller (i.e. `$this->jetpack_sites_list()`) should check.
$site_id = ( $site_id === null ) ? ( isset( $_GET['site_id'] ) ? (int) $_GET['site_id'] : null ) : $site_id;
* Here we need to switch to the subsite
* For the registration process we really only hijack how it
* works for an individual site and pass in some extra data here
switch_to_blog( $site_id );
add_filter( 'jetpack_register_request_body', array( $this, 'filter_register_request_body' ) );
add_action( 'jetpack_site_registered_user_token', array( $this, 'filter_register_user_token' ) );
// Save the secrets in the subsite so when the wpcom server does a pingback it
// will be able to validate the connection.
$result = $this->connection->register( 'subsiteregister' );
if ( is_wp_error( $result ) || ! $result ) {
Jetpack::activate_default_modules( false, false, array(), false );
* Receives the registration response token.
* @param Object $token the received token.
public function filter_register_user_token( $token ) {
$is_connection_owner = ! $this->connection->has_connected_owner();
( new Tokens() )->update_user_token(
sprintf( '%s.%d', $token->secret, get_current_user_id() ),
* Filters the registration request body to include additional properties.
* @param array $properties standard register request body properties.
* @return array amended properties.
public function filter_register_request_body( $properties ) {
$blog_details = get_blog_details();
$network = get_network();
switch_to_blog( $network->blog_id );
// The blog id on WordPress.com of the primary network site.
$network_wpcom_blog_id = Jetpack_Options::get_option( 'id' );
* Both `state` and `user_id` need to be sent in the request, even though they are the same value.
* Connecting via the network admin combines `register()` and `authorize()` methods into one step,
* because we assume the main site is already authorized. `state` is used to verify the `register()`
* request, while `user_id()` is used to create the token in the `authorize()` request.
'network_url' => $this->get_url( 'network_admin_page' ),