Skip to content

Commit

Permalink
Connection\Manager: is_connected: Memoize (#39361)
Browse files Browse the repository at this point in the history
  • Loading branch information
mreishus authored and matticbot committed Sep 23, 2024
1 parent a3a2274 commit 423d477
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 97 deletions.
62 changes: 31 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 62 additions & 3 deletions vendor/automattic/jetpack-connection/src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ class Manager {
*/
private static $disconnected_users = array();

/**
* Cached connection status.
*
* @var bool|null True if the site is connected, false if not, null if not determined yet.
*/
private static $is_connected = null;

/**
* Tracks whether connection status invalidation hooks have been added.
*
* @var bool
*/
private static $connection_invalidators_added = false;

/**
* Initialize the object.
* Make sure to call the "Configure" first.
Expand Down Expand Up @@ -140,6 +154,8 @@ 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 );

$manager->add_connection_status_invalidation_hooks();

// Set up package version hook.
add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );

Expand All @@ -157,6 +173,27 @@ public static function configure() {
Partner::init();
}

/**
* Adds hooks to invalidate the memoized connection status.
*/
private function add_connection_status_invalidation_hooks() {
if ( self::$connection_invalidators_added ) {
return;
}

// Force is_connected() to recompute after important actions.
add_action( 'jetpack_site_registered', array( $this, 'reset_connection_status' ) );
add_action( 'jetpack_site_disconnected', array( $this, 'reset_connection_status' ) );
add_action( 'jetpack_sync_register_user', array( $this, 'reset_connection_status' ) );
add_action( 'pre_update_jetpack_option_id', array( $this, 'reset_connection_status' ) );
add_action( 'pre_update_jetpack_option_blog_token', array( $this, 'reset_connection_status' ) );
add_action( 'pre_update_jetpack_option_user_token', array( $this, 'reset_connection_status' ) );
add_action( 'pre_update_jetpack_option_user_tokens', array( $this, 'reset_connection_status' ) );
add_action( 'switch_blog', array( $this, 'reset_connection_status' ) );

self::$connection_invalidators_added = true;
}

/**
* Sets up the XMLRPC request handlers.
*
Expand Down Expand Up @@ -596,9 +633,31 @@ public function is_registered() {
* @return bool
*/
public function is_connected() {
$has_blog_id = (bool) \Jetpack_Options::get_option( 'id' );
$has_blog_token = (bool) $this->get_tokens()->get_access_token();
return $has_blog_id && $has_blog_token;
if ( self::$is_connected === null ) {
if ( ! self::$connection_invalidators_added ) {
$this->add_connection_status_invalidation_hooks();
}

$has_blog_id = (bool) \Jetpack_Options::get_option( 'id' );
if ( $has_blog_id ) {
$has_blog_token = (bool) $this->get_tokens()->get_access_token();
self::$is_connected = ( $has_blog_id && $has_blog_token );
} else {
// Short-circuit, no need to check for tokens if there's no blog ID.
self::$is_connected = false;
}
}
return self::$is_connected;
}

/**
* Resets the memoized connection status.
* This will force the connection status to be recomputed on the next check.
*
* @since 5.0.0-alpha
*/
public function reset_connection_status() {
self::$is_connected = null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion vendor/automattic/jetpack-stats-admin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"automattic/jetpack-connection": "^5.0.0-alpha",
"automattic/jetpack-constants": "^2.0.4",
"automattic/jetpack-plans": "^0.4.10",
"automattic/jetpack-stats": "^0.13.2",
"automattic/jetpack-stats": "^0.13.3-alpha",
"automattic/jetpack-status": "^4.0.1",
"automattic/jetpack-jitm": "^3.1.22"
},
Expand Down
5 changes: 5 additions & 0 deletions vendor/automattic/jetpack-stats/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.13.3-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

## [0.13.2] - 2024-09-05
### Changed
- Update dependencies. [#39253]
Expand Down Expand Up @@ -182,6 +186,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixing static method which was called without self reference. [#26640]

[0.13.3-alpha]: https://github.com/Automattic/jetpack-stats/compare/v0.13.2...v0.13.3-alpha
[0.13.2]: https://github.com/Automattic/jetpack-stats/compare/v0.13.1...v0.13.2
[0.13.1]: https://github.com/Automattic/jetpack-stats/compare/v0.13.0...v0.13.1
[0.13.0]: https://github.com/Automattic/jetpack-stats/compare/v0.12.5...v0.13.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '0.13.2';
const PACKAGE_VERSION = '0.13.3-alpha';

const PACKAGE_SLUG = 'stats';

Expand Down
Loading

0 comments on commit 423d477

Please sign in to comment.