Skip to content

Commit

Permalink
Merge pull request #829 from 10up/fix/site-creation-timeouts
Browse files Browse the repository at this point in the history
Clear out cache instead of rebuilding it when site data changes
  • Loading branch information
jeffpaul authored Jan 26, 2022
2 parents 0cc8a5b + d031263 commit c3690a1
Showing 1 changed file with 18 additions and 12 deletions.
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 @@ -827,8 +827,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 @@ -895,22 +894,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 ) {
self::set_sites_last_changed_time();
} else {
delete_transient( "authorized_sites:$user_id:push:$last_changed" );
delete_transient( "authorized_sites:$user_id:pull:$last_changed" );
}
}

/**
Expand Down

0 comments on commit c3690a1

Please sign in to comment.