From 81604ed87dbd3271d09d004e8ae0b9d4a15bf832 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 15 Jan 2024 10:21:27 +1100 Subject: [PATCH 1/2] For non-super-admins, use get_blogs_of_user() to determine authroized sites. --- .../NetworkSiteConnection.php | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index ac424d647..08b002472 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -866,12 +866,37 @@ public static function build_available_authorized_sites( $user_id = false, $cont if ( $force || false === $authorized_sites ) { $authorized_sites = array(); - $sites = get_sites( - array( - 'number' => 1000, - ) - ); - $current_blog_id = (int) get_current_blog_id(); + + if ( is_super_admin() ) { + $sites = get_sites( + array( + 'number' => 1000, + ) + ); + } else { + $user_sites = get_blogs_of_user( $user_id ); + $user_site_ids = wp_list_pluck( $user_sites, 'userblog_id' ); + $sites = array(); + + /* + * get_blogs_of_user() returns sites in a different shape to get_sites(). + * + * This gets each of the sites in the correct shape, get_site() is called for each + * site individually as the sites will each be cached so this is more performant + * than using get_sites(). + */ + foreach ( $user_site_ids as $user_site_id ) { + $user_site = get_site( $user_site_id ); + + if ( ! $user_site ) { + continue; + } + + $sites[] = $user_site; + } + } + + $current_blog_id = (int) get_current_blog_id(); foreach ( $sites as $site ) { $blog_id = (int) $site->blog_id; From 940162ac49eaaf26bb69c227184ff926e93ec319 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 15 Jan 2024 10:50:36 +1100 Subject: [PATCH 2/2] Ensure site caches are primed. --- .../classes/InternalConnections/NetworkSiteConnection.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index 08b002472..ddc994edc 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -881,10 +881,11 @@ public static function build_available_authorized_sites( $user_id = false, $cont /* * get_blogs_of_user() returns sites in a different shape to get_sites(). * - * This gets each of the sites in the correct shape, get_site() is called for each - * site individually as the sites will each be cached so this is more performant - * than using get_sites(). + * To avoid an additional query, the cache for the site IDs is primed and then + * get_site() is used to retrieve the site object. This prevents an unnecessary + * query in get_sites(). */ + _prime_site_caches( $user_site_ids ); foreach ( $user_site_ids as $user_site_id ) { $user_site = get_site( $user_site_id );