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