Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/my jetpack red bubble cache #41131

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

add caching for the red bubble alerts for My Jetpack
17 changes: 15 additions & 2 deletions projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Initializer {
const MISSING_CONNECTION_NOTIFICATION_KEY = 'missing-connection';
const VIDEOPRESS_STATS_KEY = 'my-jetpack-videopress-stats';
const VIDEOPRESS_PERIOD_KEY = 'my-jetpack-videopress-period';
const MY_JETPACK_RED_BUBBLE_TRANSIENT_KEY = 'my-jetpack-red-bubble-transient';

/**
* Holds info/data about the site (from the /sites/%d endpoint)
Expand Down Expand Up @@ -281,7 +282,8 @@ public static function enqueue_scripts() {
'purchases' => self::get_purchases(),
'modules' => self::get_active_modules(),
),
'redBubbleAlerts' => self::get_red_bubble_alerts(),
// Only in the My Jetpack context, we get the alerts without the cache to make sure we have the most up-to-date info
'redBubbleAlerts' => self::get_red_bubble_alerts( true ),
'recommendedModules' => array(
'modules' => self::get_recommended_modules(),
'isFirstRun' => \Jetpack_Options::get_option( 'recommendations_first_run', true ),
Expand Down Expand Up @@ -843,17 +845,28 @@ function ( $alert ) {
/**
* Collect all possible alerts that we might use a red bubble notification for
*
* @param bool $bypass_cache - whether to bypass the red bubble cache.
* @return array
*/
public static function get_red_bubble_alerts() {
public static function get_red_bubble_alerts( bool $bypass_cache = false ) {
static $red_bubble_alerts = array();

// using a static cache since we call this function more than once in the class
if ( ! empty( $red_bubble_alerts ) ) {
return $red_bubble_alerts;
}

// check for stored alerts
$stored_alerts = get_transient( self::MY_JETPACK_RED_BUBBLE_TRANSIENT_KEY );
// Cache bypass for red bubbles should only happen on the My Jetpack page
if ( $stored_alerts !== false && ! ( $bypass_cache ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nitpick but are the parenthesis around $bypass_cache necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not, I chose that syntax since it was recommended by the phan static code analysis 😅

return $stored_alerts;
}

// go find the alerts
$red_bubble_alerts = apply_filters( 'my_jetpack_red_bubble_notification_slugs', $red_bubble_alerts );
// cache the alerts for one hour
set_transient( self::MY_JETPACK_RED_BUBBLE_TRANSIENT_KEY, $red_bubble_alerts, 3600 );

return $red_bubble_alerts;
}
Expand Down
15 changes: 15 additions & 0 deletions projects/packages/my-jetpack/src/products/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ abstract class Product {
*/
const EXPIRATION_CUTOFF_TIME = '+2 months';

/**
* Transient key for storing site features
*
* @var string;
*/
const MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY = 'my-jetpack-site-features';

/**
* Whether this module is a Jetpack feature
*
Expand Down Expand Up @@ -221,6 +228,12 @@ public static function get_site_features_from_wpcom() {
return $features;
}

// Check for a cached value before doing lookup
$stored_features = get_transient( self::MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY );
if ( $stored_features !== false ) {
return $stored_features;
}

$site_id = Jetpack_Options::get_option( 'id' );
$response = Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/features', $site_id ), '1.1' );

Expand All @@ -236,6 +249,8 @@ public static function get_site_features_from_wpcom() {
'active' => $feature_return->active,
'available' => $feature_return->available,
);
// set a short transient to help with multiple lookups on the same page load.
set_transient( self::MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY, $features, 15 );

return $features;
}
Expand Down
Loading