Skip to main content

Current

Current class manages the state of the "current" user, role, post, and site context at runtime. It is an internal utility wired into the Context to provide request-scoped information for WordPress-like environments.


Overview

  • Tracks the currently active user, role, post, and site/blog in single-site or multisite mode.
  • Supports switching blogs and sites dynamically.
  • Automatically handles time zone configuration.
  • Maintains a stack of previous context states for rollback (e.g. after impersonation or switching site/blog).

Example

Assume a user by ID or username:

await context.current.assumeUser(1);

Switch multisite context:

await context.current.switchSite(2, 5); // siteId = 2, blogId = 5

Properties

  • user: The current User object (or undefined).
  • role: The current Role assigned to the user.
  • post: The current Post in context.
  • site: The active Site (only in multisite mode).
  • blogId: Returns current blog ID (0 if single-site).
  • siteId: Returns current site ID (0 if single-site).
  • tables: The current active Tables instance (multisite-aware).

Key Methods

assumeUser(userRefOrUser?: number | string | User)

Sets the current user and role. Accepts a user ID, username, or User object.

setPost(id: number)

Sets the current Post instance based on the post ID.

setUserRoles()

Loads blog-specific user roles and stores them in global vars.

setDefaultUserRoles()

Sets a fallback list of user roles using default roles.

setTimezone()

Configures the application's time offset (vars.TIME_OFFSET_MINUTES) based on WordPress settings (timezone_string and gmt_offset).

switchSite(siteId, blogId?)

Switches the active site and blog in multisite. If no blog ID is provided, it infers the main blog using site meta.

await current.switchSite(2); // siteId = 2, auto-detects blogId
switchBlog(blogRef: number | string)

Switches to a new blog context and updates tables.index for multisite-aware querying.

restorePrevious()

Restores the previous context (user, site, blog) from the stack. Useful for undoing impersonation or temporary switching.