An exploration into the methods that set up User (WP_User), Blog, and Site meta.
Blog ID
\MySLP::getUserBlogId => (int) Blog ID
Returns the blog ID for the user WordPress considered already logged in.
Uses wp_get_current_user() to fetch the current WP_User.
Then uses get_user_meta( <id> , ‘primary_blog’, true) to fetch the Blog ID from the ‘primary_blog’ user meta.
If empty uses WP_User->get_site_id() => get_current_blog_id() => global $blog_id.
Blog ID
Primary Source: ‘primary_blog’ user meta
Fallback: global $blog_id
\MySLP_Dashboard_Controller::get_blog_id_by_user(<User ID>) => (int) Blog ID || WP_Error
Returns the blog ID for the user ID provided or WP_Error if no blogs can be found for this user.
Uses get_active_blog_for_user(<User ID>)
Returns the user’s primary blog (object) with get_user_meta( $user_id, ‘primary_blog’, true ) if the user has a primary blog.
If they do NOT have a primary blog, returns the blog for the first site they are connected to that they do not own (NOT in MySLP). Has the side effect of making that alternate blog their primary blog.
===> If the user is not attached to ANY blog, it makes them a subscriber on the main site and returns that.
This is very indeterminate and can return null, false, an object (blog), or WP_Site object.
User
\MySLP_User::get_primary_user(<Blog ID>) => WP_User || null
Return the primary user for a site based on the blog ID.
Calls get_users() to fetch a list of users filtered where blog_id = <Blog ID>, order by ID, Ascending, returns 1 value. If the main superadmin site must have the role ‘administrator’.
Set MySLP_User->primary_user to the first WP_User on the list
Sets MySLP_User->blog_id = <Blog ID>
Returns WP_User if the user is associated with the given blog.
wp_get_current_user() => WP_User
Calls _wp_get_current_user()
Which returns the global $current_user if set and type is WP_User
myslp_current_blog_settings()
Sets $user via wp_get_current_user().
$user->ID should be the <User ID> of the current logged in user.
Calls \MySLP_User::load(<User ID>) <== <User ID> from wp_get_current_user()
Then calls MySLP_Dashboard_Controller::get_blog_id_by_user(<User ID>) ==> <Blog ID> for that user
Then calls \MySLP_User::load_for_blog(<Blog ID>)
Which calls \MySLP_User::__get(‘wp_user)
Invoking \MySLP_User::get_primary_user(Blog ID>)