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