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

Clear out cache instead of rebuilding it when site data changes #829

Merged
merged 15 commits into from
Jan 26, 2022
Merged
30 changes: 18 additions & 12 deletions includes/classes/InternalConnections/NetworkSiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ public static function bootstrap() {
add_action( 'untrash_post', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'connect_syndicated_on_untrash' ) );
add_action( 'clean_site_cache', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'set_sites_last_changed_time' ) );
add_action( 'wp_insert_site', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'set_sites_last_changed_time' ) );
add_action( 'add_user_to_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'rebuild_user_authorized_sites_cache' ) );
add_action( 'remove_user_from_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'rebuild_user_authorized_sites_cache' ) );
add_action( 'add_user_to_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'clear_authorized_sites_cache' ) );
add_action( 'remove_user_from_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'clear_authorized_sites_cache' ) );
}

/**
Expand Down Expand Up @@ -822,8 +822,7 @@ public static function build_available_authorized_sites( $user_id = false, $cont
$last_changed = get_site_option( 'last_changed_sites' );

if ( ! $last_changed ) {
$last_changed = microtime();
self::set_sites_last_changed_time();
$last_changed = self::set_sites_last_changed_time();
}

$cache_key = "authorized_sites:$user_id:$context:$last_changed";
Expand Down Expand Up @@ -890,22 +889,29 @@ public static function build_available_authorized_sites( $user_id = false, $cont
* will have caching enabled, so we also store it
* in a site option.
*
* @return void
* @return string
*/
public static function set_sites_last_changed_time() {
update_site_option( 'last_changed_sites', microtime() );
$time = microtime();
update_site_option( 'last_changed_sites', $time );

return $time;
}

/**
* Rebuild the authorized sites cache for a specific user.
* Clear the authorized sites cache for a specific user.
*
* @param int $user_id Current user ID.
*
* @return void
*/
public static function rebuild_user_authorized_sites_cache( $user_id ) {
self::build_available_authorized_sites( $user_id, 'push', true );
self::build_available_authorized_sites( $user_id, 'pull', true );
public static function clear_authorized_sites_cache( $user_id = false ) {
$last_changed = get_site_option( 'last_changed_sites' );

if ( ! $last_changed ) {
$last_changed = self::set_sites_last_changed_time();
Copy link
Contributor

Choose a reason for hiding this comment

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

@dkotter In case of just-set $last_changed, both delete_transient functions will address non-existing transients, are we sure this is ok?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cadic I don't think it causes any issues to try deleting a transient that doesn't exist but I've modified this slightly so we only delete these transients if we have a proper last_changed_sites value. Otherwise the transients should either not exist (so they don't need deleted) or they exist with a different last_changed_sites value and will now be invalid since we've updated that value

}

delete_transient( "authorized_sites:$user_id:push:$last_changed" );
delete_transient( "authorized_sites:$user_id:pull:$last_changed" );
}

/**
Expand Down