diff --git a/projects/packages/connection/src/class-manager.php b/projects/packages/connection/src/class-manager.php index c2f8c751c11f0..b6676644b6a62 100644 --- a/projects/packages/connection/src/class-manager.php +++ b/projects/packages/connection/src/class-manager.php @@ -147,8 +147,17 @@ public static function configure() { add_action( 'deleted_user', array( $manager, 'disconnect_user_force' ), 9, 1 ); add_action( 'remove_user_from_blog', array( $manager, 'disconnect_user_force' ), 9, 1 ); + // Force is_connected() to recompute after important actions. add_action( 'jetpack_site_registered', array( $manager, 'reset_connection_status' ) ); add_action( 'jetpack_site_disconnected', array( $manager, 'reset_connection_status' ) ); + add_action( 'jetpack_sync_register_user', array( $manager, 'reset_connection_status' ) ); + add_action( 'pre_update_jetpack_option_id', array( $manager, 'reset_connection_status' ) ); + add_action( 'pre_update_jetpack_blog_token', array( $manager, 'reset_connection_status' ) ); + add_action( 'pre_update_jetpack_user_token', array( $manager, 'reset_connection_status' ) ); + add_action( 'pre_update_jetpack_user_tokens', array( $manager, 'reset_connection_status' ) ); + add_action( 'update_option', array( $manager, 'maybe_reset_connection_status' ), 10, 1 ); + add_action( 'add_option', array( $manager, 'maybe_reset_connection_status' ), 10, 1 ); + add_action( 'delete_option', array( $manager, 'maybe_reset_connection_status' ), 10, 1 ); // Set up package version hook. add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' ); @@ -629,6 +638,22 @@ public function reset_connection_status() { self::$is_connected = null; } + /** + * Resets the memoized connection status if jetpack_private_options is updated. + * + * Ideally, we would only do this on certain "sub" options stored inside + * jetpack_private_options, but delete_option() -> delete_grouped_option() + * provides us no hook to detect which options are being deleted. + * + * @param string $option The option name. + */ + public function maybe_reset_connection_status( $option ) { + $reset_options = array( 'jetpack_private_options' ); + if ( in_array( $option, $reset_options, true ) ) { + self::$is_connected = null; + } + } + /** * Returns true if the site has at least one connected administrator. *