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 ccb964d commit cfc8f78
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 122 deletions.
65 changes: 62 additions & 3 deletions jetpack_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
4 changes: 2 additions & 2 deletions jetpack_vendor/i18n-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
),
'jetpack-connection' => array(
'path' => 'jetpack_vendor/automattic/jetpack-connection',
'ver' => '5.0.0-alpha1727098196',
'ver' => '5.0.0-alpha1727102804',
),
'jetpack-explat' => array(
'path' => 'jetpack_vendor/automattic/jetpack-explat',
Expand Down Expand Up @@ -66,7 +66,7 @@
),
'jetpack-protect-status' => array(
'path' => 'jetpack_vendor/automattic/jetpack-protect-status',
'ver' => '0.2.0-alpha1726505426',
'ver' => '0.2.0-alpha1727102804',
),
'jetpack-sync' => array(
'path' => 'jetpack_vendor/automattic/jetpack-sync',
Expand Down
64 changes: 32 additions & 32 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-a8c-mc-stats",
"reference": "078055cf0a2898bc315c0a409413a88be86d6f8e"
"reference": "3f2133d0bc2cbc3d56fc344e143a82a7d4ba4373"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -60,7 +60,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-admin-ui",
"reference": "e25d06f6f1cf7715ab9a0a2c8bca9161facd59d7"
"reference": "0d674afdcda4eebc60ecd07b8a3b9ccdf6b5fd9d"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -125,7 +125,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-assets",
"reference": "f054db8c737142226e4653c9cac29588d2e3b042"
"reference": "bfea91b6f1a285a3b2401e5fcbb4d568cc9bcc1d"
},
"require": {
"automattic/jetpack-constants": "^2.0.4",
Expand Down Expand Up @@ -194,7 +194,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-autoloader",
"reference": "e14edd450ef2e30d11f2ef08760c03e139694a3c"
"reference": "1ebc6c2386deaa901e77875f3ffdd207510fd673"
},
"require": {
"composer-plugin-api": "^1.1 || ^2.0",
Expand Down Expand Up @@ -261,7 +261,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-backup",
"reference": "aed97f37cef425bd72cf696bdad9aca164a7bab7"
"reference": "59f8c8cf4ca5b6e4e1b93e80e81cb92b8c34e4c5"
},
"require": {
"automattic/jetpack-admin-ui": "^0.4.5",
Expand Down Expand Up @@ -351,7 +351,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-backup-helper-script-manager",
"reference": "2ed9e1817bafb4e49b915407a9e595b53c3d2994"
"reference": "c05157eede6ba184b28b9b30c87ee9d61112adef"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -411,7 +411,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-boost-core",
"reference": "dee8f0af379864120083cdc041ca9eee7a970a5e"
"reference": "bda27e6cae34ea24fa922ec2ab9f51d45ae6c151"
},
"require": {
"automattic/jetpack-connection": "^5.0.0-alpha",
Expand Down Expand Up @@ -479,7 +479,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-boost-speed-score",
"reference": "16fb6b1df4204ce5dcfd4de59a324d18dc76b4de"
"reference": "844e4a10f66722b6d07d7147209c664cbde28b35"
},
"require": {
"automattic/jetpack-boost-core": "^0.2.11",
Expand Down Expand Up @@ -555,7 +555,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-composer-plugin",
"reference": "886a786ccff6e941bb8779ef36a97dcbb9b7c421"
"reference": "f68d3450b2498d3abf7b159e5e6d897bba81cf46"
},
"require": {
"composer-plugin-api": "^2.1.0",
Expand Down Expand Up @@ -615,7 +615,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-config",
"reference": "8f11fc2d495d9e62cdfe584b5dd66f76ec4207ad"
"reference": "71c78bd0ac1cc37d057b0a9fe250aa05d5c7ae87"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -685,12 +685,12 @@
},
{
"name": "automattic/jetpack-connection",
"version": "5.0.0-alpha.1727098196",
"version_normalized": "5.0.0.0-alpha1727098196",
"version": "5.0.0-alpha.1727102804",
"version_normalized": "5.0.0.0-alpha1727102804",
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-connection",
"reference": "291ccef67c8393772d0ee7a3c9b609a71c32e65b"
"reference": "1b323b3b025e1fd1b2cc0e3814b3409ddbbb93be"
},
"require": {
"automattic/jetpack-a8c-mc-stats": "^2.0.2",
Expand Down Expand Up @@ -780,7 +780,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-constants",
"reference": "adf357a1cefb098272fee1103d1e250ee940b49c"
"reference": "3b466602cba4ffac6044654b627033e36ef2c45d"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -834,7 +834,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-device-detection",
"reference": "fc7ce0aa0ba9bc98945c080fbb157a470a504b68"
"reference": "1d12998922d95f363ee09b9794b0937911016336"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -887,7 +887,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-explat",
"reference": "b6bc2a0b7f51e5bff15624c74241e31e15232d04"
"reference": "1f39297d94e5fae546c2a6c721a2fa3268285563"
},
"require": {
"automattic/jetpack-connection": "^5.0.0-alpha",
Expand Down Expand Up @@ -962,7 +962,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-ip",
"reference": "ab75af1e76f7e596958379fedfa277628d9ac7f8"
"reference": "f04e2d711f5514f3943c9aac604ea72b672fe299"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -1020,7 +1020,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-jitm",
"reference": "7001f447e12069dee475d1f2a664244f73e16606"
"reference": "2987ab4f6cd8c7fad524d47f22d88fbe0f8090cc"
},
"require": {
"automattic/jetpack-a8c-mc-stats": "^2.0.2",
Expand Down Expand Up @@ -1095,7 +1095,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-licensing",
"reference": "74d38fea18af495682633bc79ca7af84c3b39145"
"reference": "fc50ecd6a90c141f95f7e972b035a1480bcbef52"
},
"require": {
"automattic/jetpack-connection": "^5.0.0-alpha",
Expand Down Expand Up @@ -1157,7 +1157,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-logo",
"reference": "b5897256a3729a82b58ac00766f2660bff19aca0"
"reference": "f3b6f2e58c55d3242ac6e2fbd92a11a2944ffbb3"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -1210,7 +1210,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-my-jetpack",
"reference": "b8c7cf2a75ace2e74e08c04bbf49e363606b3131"
"reference": "a31db4ba3118c009bba83a673c252e3277027cab"
},
"require": {
"automattic/jetpack-admin-ui": "^0.4.5",
Expand Down Expand Up @@ -1312,7 +1312,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-password-checker",
"reference": "a999a823d74eccf1632359727688ac2015e2abb8"
"reference": "26e9697df161f7e704dbc18d2f11c20f3afea226"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -1373,7 +1373,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-plans",
"reference": "c3b8ac7199f2bce90cad86cfc799189195115fcd"
"reference": "f68c578c35d0ba9a8634c05124c833c1c0e311bd"
},
"require": {
"automattic/jetpack-connection": "^5.0.0-alpha",
Expand Down Expand Up @@ -1441,7 +1441,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-plugins-installer",
"reference": "925aef5ee99cd66a2f75c47aaca0e83c19308d27"
"reference": "bdcb8f0981f8886075caa72216692e58df0b3e70"
},
"require": {
"automattic/jetpack-a8c-mc-stats": "^2.0.2",
Expand Down Expand Up @@ -1497,7 +1497,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-protect-models",
"reference": "947395e1746dd7b89207dac3e834922e071f5120"
"reference": "ddfbb6e742b6076fd73c1e0f348188fc99fcedc0"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -1562,12 +1562,12 @@
},
{
"name": "automattic/jetpack-protect-status",
"version": "0.2.0-alpha.1726505426",
"version_normalized": "0.2.0.0-alpha1726505426",
"version": "0.2.0-alpha.1727102804",
"version_normalized": "0.2.0.0-alpha1727102804",
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-protect-status",
"reference": "fe5e97c58731b8f661548b8b083c95f353ae204a"
"reference": "62d692f893da17a444af4ffc9abfebcdd8a68e0f"
},
"require": {
"automattic/jetpack-connection": "^5.0.0-alpha",
Expand Down Expand Up @@ -1642,7 +1642,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-redirect",
"reference": "c0f7756641f2fe2c208de31832ef33999d2c46cb"
"reference": "80eec6d5e422958271ec6d6e3382d818dce34824"
},
"require": {
"automattic/jetpack-status": "^4.0.1",
Expand Down Expand Up @@ -1697,7 +1697,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-roles",
"reference": "8c7502585380680e2910d51f5ef0dba76152d9f4"
"reference": "928b2b83764cd06e56ac6a01d64e935c030c2c81"
},
"require": {
"php": ">=7.0"
Expand Down Expand Up @@ -1751,7 +1751,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-status",
"reference": "901fbea67f6652b9ea4b8a8e2dda297e08bee361"
"reference": "bdc42421392a324dedfc1c64dc8a46a68d771422"
},
"require": {
"automattic/jetpack-constants": "^2.0.4",
Expand Down Expand Up @@ -1815,7 +1815,7 @@
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-sync",
"reference": "74d0bade4f5d59de6bbd3517738ca26e72a4eaf2"
"reference": "53ca3d71110ac7b551fadbb2da259297d7d2ea86"
},
"require": {
"automattic/jetpack-connection": "^5.0.0-alpha",
Expand Down
Loading

0 comments on commit cfc8f78

Please sign in to comment.