disconnect_site( true ); } else { $connection->disconnect_site( false ); } // Clear IDC options. self::clear_all_idc_options(); } /** * Filter to prevent site from disconnecting from WPCOM if it's in an IDC. * * @see jetpack_connection_disconnect_site_wpcom filter. * * @return bool False if the site is in IDC, true otherwise. */ public static function jetpack_connection_disconnect_site_wpcom_filter() { return ! self::validate_sync_error_idc_option(); } /** * Gets the link to the support document used to explain Safe Mode to users. * * @return string */ public static function get_safe_mod_doc_url() { return Redirect::get_url( 'jetpack-support-safe-mode' ); } /** * This method loops through the array of processed items from sync and checks if one of the items was the * home_url or site_url callable. If so, then we delete the jetpack_migrate_for_idc option. * * @param array $processed_items Array of processed items that were synced to WordPress.com. */ public function maybe_clear_migrate_option( $processed_items ) { foreach ( (array) $processed_items as $item ) { // First, is this item a jetpack_sync_callable action? If so, then proceed. $callable_args = ( is_array( $item ) && isset( $item[0], $item[1] ) && 'jetpack_sync_callable' === $item[0] ) ? $item[1] : null; // Second, if $callable_args is set, check if the callable was home_url or site_url. If so, // clear the migrate option. if ( isset( $callable_args, $callable_args[0] ) && ( 'home_url' === $callable_args[0] || 'site_url' === $callable_args[1] ) ) { Jetpack_Options::delete_option( 'migrate_for_idc' ); break; } } } /** * WordPress init. * * @return void */ public function wordpress_init() { if ( current_user_can( 'jetpack_disconnect' ) ) { if ( isset( $_GET['jetpack_idc_clear_confirmation'], $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'jetpack_idc_clear_confirmation' ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- WordPress core doesn't unslash or verify nonces either. ) { Jetpack_Options::delete_option( 'safe_mode_confirmed' ); self::$is_safe_mode_confirmed = false; } else { self::$is_safe_mode_confirmed = (bool) Jetpack_Options::get_option( 'safe_mode_confirmed' ); } } // 121 Priority so that it's the most inner Jetpack item in the admin bar. add_action( 'admin_bar_menu', array( $this, 'display_admin_bar_button' ), 121 ); UI::init(); } /** * Add the idc query arguments to the url. * * @param string $url The remote request url. */ public function add_idc_query_args_to_url( $url ) { $status = new Status(); if ( ! is_string( $url ) || $status->is_offline_mode() || self::validate_sync_error_idc_option() ) { return $url; } $query_args = array( 'home' => Urls::home_url(), 'siteurl' => Urls::site_url(), ); if ( self::should_handle_idc() ) { $query_args['idc'] = true; } if ( \Jetpack_Options::get_option( 'migrate_for_idc', false ) ) { $query_args['migrate_for_idc'] = true; } return add_query_arg( $query_args, $url ); } /** * Non-admins current screen check. * * @param object $current_screen Current screen. * * @return null * @deprecated 0.5.0 Use `@automattic/jetpack-idc` instead. */ public function non_admins_current_screen_check( $current_screen ) { _deprecated_function( __METHOD__, '0.5.0' ); self::$current_screen = $current_screen; if ( isset( $current_screen->id ) && 'toplevel_page_jetpack' === $current_screen->id ) { return null; } return null; } /** * Renders the admin bar button. * * @return void */ public function display_admin_bar_button() { global $wp_admin_bar; $href = is_admin() ? add_query_arg( 'jetpack_idc_clear_confirmation', '1' ) : add_query_arg( 'jetpack_idc_clear_confirmation', '1', admin_url() ); $href = wp_nonce_url( $href, 'jetpack_idc_clear_confirmation' ); $consumer_data = UI::get_consumer_data(); $label = isset( $consumer_data['customContent']['adminBarSafeModeLabel'] ) ? esc_html( $consumer_data['customContent']['adminBarSafeModeLabel'] ) : esc_html__( 'Jetpack Safe Mode', 'jetpack-idc' ); $title = sprintf( '
', '', $label ); $menu = array( 'id' => 'jetpack-idc', 'title' => $title, 'href' => esc_url( $href ), 'parent' => 'top-secondary', ); if ( ! self::$is_safe_mode_confirmed ) { $menu['meta'] = array( 'class' => 'hide', ); } $wp_admin_bar->add_node( $menu ); } /** * Checks if the site is currently in an identity crisis. * * @return array|bool Array of options that are in a crisis, or false if everything is OK. */ public static function check_identity_crisis() { $connection = new Connection_Manager( 'jetpack' ); if ( ! $connection->is_connected() || ( new Status() )->is_offline_mode() || ! self::validate_sync_error_idc_option() ) { return false; } return Jetpack_Options::get_option( 'sync_error_idc' ); } /** * Checks the HTTP response body for the 'idc_detected' key. If the key exists, * checks the idc_detected value for a valid idc error. * * @param array|WP_Error $http_response The HTTP response. * * @return bool Whether the site is in an identity crisis. */ public function check_http_response_for_idc_detected( $http_response ) { if ( ! is_array( $http_response ) ) { return false; } $response_body = json_decode( wp_remote_retrieve_body( $http_response ), true ); if ( isset( $response_body['idc_detected'] ) ) { return $this->check_response_for_idc( $response_body['idc_detected'] ); } if ( isset( $response_body['migrated_for_idc'] ) ) { Jetpack_Options::delete_option( 'migrate_for_idc' ); } return false; } /** * Checks the WPCOM response to determine if the site is in an identity crisis. Updates the * sync_error_idc option if it is. * * @param array $response The response data. * * @return bool Whether the site is in an identity crisis. */ public function check_response_for_idc( $response ) { if ( ! is_array( $response ) ) { return false; } if ( is_array( $response ) && isset( $response['error_code'] ) ) { $error_code = $response['error_code']; $allowed_idc_error_codes = array( 'jetpack_url_mismatch', 'jetpack_home_url_mismatch', 'jetpack_site_url_mismatch', ); if ( in_array( $error_code, $allowed_idc_error_codes, true ) ) { \Jetpack_Options::update_option( 'sync_error_idc', self::get_sync_error_idc_option( $response ) ); } return true; } return false; } /** * Prepare URL for display. * * @param string $url URL to display. * * @return string */ public static function prepare_url_for_display( $url ) { return untrailingslashit( self::normalize_url_protocol_agnostic( $url ) ); } /** * Clears all IDC specific options. This method is used on disconnect and reconnect. * * @return void */ public static function clear_all_idc_options() { // If the site is currently in IDC, let's also clear the VaultPress connection options. // We have to check if the site is in IDC, otherwise we'd be clearing the VaultPress // connection any time the Jetpack connection is cycled. if ( self::validate_sync_error_idc_option() ) { delete_option( 'vaultpress' ); delete_option( 'vaultpress_auto_register' ); } Jetpack_Options::delete_option( array( 'sync_error_idc', 'safe_mode_confirmed', 'migrate_for_idc', ) ); delete_transient( 'jetpack_idc_possible_dynamic_site_url_detected' ); } /** * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. * * @return bool * @since-jetpack 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC * * @since 0.2.0 * @since-jetpack 4.4.0 */ public static function validate_sync_error_idc_option() { $is_valid = false; // Is the site opted in and does the stored sync_error_idc option match what we now generate? $sync_error = Jetpack_Options::get_option( 'sync_error_idc' ); if ( $sync_error && self::should_handle_idc() ) { $local_options = self::get_sync_error_idc_option(); // Ensure all values are set. if ( isset( $sync_error['home'] ) && isset( $local_options['home'] ) && isset( $sync_error['siteurl'] ) && isset( $local_options['siteurl'] ) ) { // If the WP.com expected home and siteurl match local home and siteurl it is not valid IDC. if ( isset( $sync_error['wpcom_home'] ) && isset( $sync_error['wpcom_siteurl'] ) && $sync_error['wpcom_home'] === $local_options['home'] && $sync_error['wpcom_siteurl'] === $local_options['siteurl'] ) { $is_valid = false; // Enable migrate_for_idc so that sync actions are accepted. Jetpack_Options::update_option( 'migrate_for_idc', true ); } elseif ( $sync_error['home'] === $local_options['home'] && $sync_error['siteurl'] === $local_options['siteurl'] ) { $is_valid = true; } } } /** * Filters whether the sync_error_idc option is valid. * * @param bool $is_valid If the sync_error_idc is valid or not. * * @since 0.2.0 * @since-jetpack 4.4.0 */ $is_valid = (bool) apply_filters( 'jetpack_sync_error_idc_validation', $is_valid ); if ( ! $is_valid && $sync_error ) { // Since the option exists, and did not validate, delete it. Jetpack_Options::delete_option( 'sync_error_idc' ); } return $is_valid; } /** * Normalizes a url by doing three things: * - Strips protocol * - Strips www * - Adds a trailing slash * * @param string $url URL to parse. * * @return WP_Error|string * @since 0.2.0 * @since-jetpack 4.4.0 */ public static function normalize_url_protocol_agnostic( $url ) { $parsed_url = wp_parse_url( trailingslashit( esc_url_raw( $url ) ) ); if ( ! $parsed_url || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) { return new WP_Error( 'cannot_parse_url', sprintf( /* translators: %s: URL to parse. */ esc_html__( 'Cannot parse URL %s', 'jetpack-idc' ), $url ) ); } // Strip www and protocols. $url = preg_replace( '/^www\./i', '', $parsed_url['host'] . $parsed_url['path'] ); return $url; } /** * Gets the value that is to be saved in the jetpack_sync_error_idc option. * * @param array $response HTTP response. * * @return array Array of the local urls, wpcom urls, and error code. * @since 0.2.0 * @since-jetpack 4.4.0 * @since-jetpack 5.4.0 Add transient since home/siteurl retrieved directly from DB. */ public static function get_sync_error_idc_option( $response = array() ) { // Since the local options will hit the database directly, store the values // in a transient to allow for autoloading and caching on subsequent views. $local_options = get_transient( 'jetpack_idc_local' ); if ( false === $local_options ) { $local_options = array( 'home' => Urls::home_url(), 'siteurl' => Urls::site_url(), ); set_transient( 'jetpack_idc_local', $local_options, MINUTE_IN_SECONDS ); } $options = array_merge( $local_options, $response ); $returned_values = array(); foreach ( $options as $key => $option ) { if ( 'error_code' === $key ) { $returned_values[ $key ] = $option; continue; } $normalized_url = self::normalize_url_protocol_agnostic( $option ); if ( is_wp_error( $normalized_url ) ) { continue; } $returned_values[ $key ] = $normalized_url; } return $returned_values; } /** * Returns the value of the jetpack_sync_idc_optin filter, or constant. * If set to true, the site will be put into staging mode. * * @return bool * @since 0.2.0 * @since-jetpack 4.3.2 * @deprecated 0.2.6 Use should_handle_idc() * @see Automattic\Jetpack\Identity_Crisis::should_handle_idc */ public static function sync_idc_optin() { _deprecated_function( __METHOD__, '0.2.6', 'Automattic\\Jetpack\\Identity_Crisis::should_handle_idc' ); return self::should_handle_idc(); } /** * Returns the value of the jetpack_should_handle_idc filter or constant. * If set to true, the site will be put into staging mode. * * This method uses both the current jetpack_should_handle_idc filter and constant and the * legacy jetpack_sync_idc_optin filter and constant to determine whether an IDC should be * handled. * * @return bool * @since 0.2.6 */ public static function should_handle_idc() { if ( Constants::is_defined( 'JETPACK_SHOULD_HANDLE_IDC' ) ) { $default = Constants::get_constant( 'JETPACK_SHOULD_HANDLE_IDC' ); } elseif ( Constants::is_defined( 'JETPACK_SYNC_IDC_OPTIN' ) ) { // Check the legacy constant. This constant should be considered deprecated as of version 0.2.6. $default = Constants::get_constant( 'JETPACK_SYNC_IDC_OPTIN' ); } else { $default = ! Constants::is_defined( 'SUNRISE' ) && ! is_multisite(); } // Add a callback which uses the legacy filter 'jetpack_sync_idc_optin'. add_filter( 'jetpack_should_handle_idc', array( __CLASS__, 'legacy_jetpack_sync_idc_optin_filter' ) ); /** * Allows sites to opt in for IDC mitigation which blocks the site from syncing to WordPress.com when the home * URL or site URL do not match what WordPress.com expects. The default value is either true, or the value of * JETPACK_SHOULD_HANDLE_IDC constant if set. * * @param bool $default Whether the site is opted in to IDC mitigation. * * @since 0.2.6 */ return (bool) apply_filters( 'jetpack_should_handle_idc', $default ); } /** * Returns the value for the deprecated filter, 'jetpack_sync_idc_optin'. That filter has been replaced with the * 'jetpack_should_handle_idc' filter. * * @since 0.2.6 * * @param bool $default Whether the site is opted in to IDC mitigation. */ public static function legacy_jetpack_sync_idc_optin_filter( $default ) { /** * Allows sites to opt in for IDC mitigation which blocks the site from syncing to WordPress.com when the home * URL or site URL do not match what WordPress.com expects. The default value is either true, or the value of * JETPACK_SYNC_IDC_OPTIN constant if set. * * @param bool $default Whether the site is opted in to IDC mitigation. * * @since 0.2.0 * @since-jetpack 4.3.2 * @deprecated 0.2.6 Use jetpack_should_handle_idc */ return (bool) apply_filters_deprecated( 'jetpack_sync_idc_optin', array( $default ), '0.2.6', 'jetpack_should_handle_idc' ); } /** * Does the current admin page have help tabs? * * @return bool * @deprecated 0.5.0 Use `@automattic/jetpack-idc` instead. */ public function admin_page_has_help_tabs() { _deprecated_function( __METHOD__, '0.5.0' ); if ( ! function_exists( 'get_current_screen' ) ) { return false; } $current_screen = get_current_screen(); $tabs = $current_screen->get_help_tabs(); return ! empty( $tabs ); } /** * Renders the non-admin IDC notice. * * @return void * @deprecated 0.5.0 Use `@automattic/jetpack-idc` instead. */ public function display_non_admin_idc_notice() { _deprecated_function( __METHOD__, '0.5.0' ); $classes = 'jp-idc-notice inline is-non-admin notice notice-warning'; if ( isset( self::$current_screen ) && 'toplevel_page_jetpack' !== self::$current_screen->id ) { $classes .= ' is-dismissible'; } if ( $this->admin_page_has_help_tabs() ) { $classes .= ' has-help-tabs'; } ?>get_non_admin_contact_admin_text(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
get_first_step_header_explanation(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
get_confirm_safe_mode_action_explanation(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
get_first_step_fix_connection_action_explanation(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
get_migrate_site_action_explanation(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
get_start_fresh_action_explanation(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
get_unsure_prompt(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>