=== ABCode View As Role Switcher ===
Contributors: ashbrentnall, abcodeuk
Donate link: https://abcode.co.uk/
Tags: user roles, role switcher, view as user, switch role, frontend testing
Requires at least: 5.0
Tested up to: 7.0
Stable tag: 2.0.0
Requires PHP: 7.4
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

View your site as a subscriber, customer, editor or logged-out visitor from the toolbar. Real role switching, no test accounts needed.

== Description ==

**ABCode View As Role Switcher** lets an administrator see the front end of the site exactly as a chosen user role sees it, without logging out and without creating a test account for every role on the site.

Pick a role from the toolbar, and the front end reloads with that role's capabilities. Member-only content hides itself. Price rules, menus, shortcodes and blocks that check who is looking behave as they would for a real customer or subscriber. Pick **Switch back** and you are yourself again.

= Switch to a role, not to a user =

Most switching plugins log you in as a specific person. That is a different job, and it means keeping a spare account for every role you want to check, then keeping those accounts in step as the site changes.

This plugin switches to the *role*. Add a role today, test it in the next click. There is no dummy account to create, no password to store, no extra user cluttering the users list, and nothing to tidy up afterwards. It never touches anybody's real account.

= Capabilities actually change =

The switch is applied where WordPress decides what a visitor is allowed to see, so `current_user_can()` tells the truth for the length of the session. That is the function themes, WooCommerce, membership plugins and page builders all call, which is why the pages you test look like the pages your users get.

The view is always a subset of your own account. It can only ever take capabilities away, never add them, so it can never be used to reach something you could not already reach.

= Features =

* **Switch from the toolbar:** every role on the site is one click away, on the front end and inside wp-admin
* **View as a logged-out visitor:** check the guest experience without a private window or a second browser
* **Real capability switching:** `current_user_can()`, `user_can()` and role checks all agree with the role you picked
* **A visible switched state:** the toolbar item turns green and reads "Viewing as: Subscriber", so you always know what you are looking at
* **One-click way back:** the same toolbar item ends the session, and wp-admin is never switched, so you cannot lock yourself out
* **Automatic expiry:** a session ends by itself after 15 minutes, 1 hour or 8 hours, whichever you choose
* **Nothing is saved to the database:** no user's stored roles or capabilities are ever written to
* **Choose what appears:** tick the roles you actually test with, and leave the rest out of the menu
* **Secure by design:** a signed, HttpOnly cookie tied to your login session, plus a compulsory nonce on every switch
* **Free forever:** no premium tier, no feature locks, no upsells
* **Translation ready:** fully internationalised, with five languages included

= Who is this for? =

* **WooCommerce shops** checking what a customer, a shop manager or a guest sees on a product page
* **Membership and course sites** confirming that gated content really is gated
* **Agencies and freelancers** handing over a site and wanting proof the permissions are right
* **Anybody** who has ever asked "does this menu item show up for subscribers?"

= How it works =

1. Go to **View As** in the toolbar
2. Choose a role, or **View as a logged-out visitor**
3. Browse the site the way that role would
4. Choose **Switch back to your own account** when you are done

The switch applies to front-end page loads only. wp-admin, the REST API and admin AJAX always run as your real account, on purpose: it means there is always a way back, whatever the role you switched to is allowed to do.

== Installation ==

1. Upload the `simple-admin-role-switcher` folder to `/wp-content/plugins/`
2. Activate the plugin through the **Plugins** menu in WordPress
3. Use **View As** in the toolbar. Settings, if you want them, are at **Tools > View As Role**

== Frequently Asked Questions ==

= How do I preview my site as a customer? =

Hover **View As** in the toolbar and choose **View as Customer**. The page reloads with a customer's capabilities and the toolbar turns green to show you are in a switched view. Choose **Switch back to your own account** to end it.

= Do I need a test account for each role? =

No, and that is the point of this plugin. It switches to the role itself, so a role added by WooCommerce or a membership plugin this morning can be tested this afternoon with nothing to set up first.

= Does it change a user's real role? =

Never. Nothing is written to the database at any point. The switch is a filter applied while a page is being built, held in a signed cookie that expires on its own, and it only ever affects the person who started it.

= Does it work with WooCommerce? =

Yes. Switch to Customer, Shop Manager, or a logged-out visitor and browse the shop, a product page or the account pages. Anything that asks WordPress what the current user is allowed to do, which is how WooCommerce and its extensions gate content, gets the switched answer.

= Does it work with membership plugins and page builders? =

Yes, for anything that gates content on capabilities or roles, which is nearly all of them. Content restricted to a specific *user*, or to somebody's purchase history, is tied to an account rather than a role, so that will still resolve against your own account.

= Can I view the site as a logged-out visitor? =

Yes. **View as a logged-out visitor** treats the request as though nobody is signed in, so you see exactly what a first-time visitor sees. The toolbar stays on screen so you can get back.

= How do I switch back? =

Click the green **Viewing as** item in the toolbar, or pick **Switch back to your own account** from it. If the toolbar is hidden by your theme, go to **Tools > View As Role** in wp-admin and press **End the session now**. wp-admin is never switched, so that page always works.

= Can I get stuck in a role? =

No. There are three separate ways out: the toolbar, the button on the settings screen, and the session's own expiry. Logging out ends it too.

= Does the switch affect what other visitors see? =

No. It applies to your browser only, for the length of the session. Nobody else's experience of the site changes.

= Does it work in wp-admin? =

The switcher is *available* in wp-admin, and choosing a role from there opens the front end in a new tab. The switch itself is deliberately not applied to admin screens, because an administrator browsing wp-admin with a subscriber's capabilities would simply be locked out of it.

= Does it work with a page cache? =

Not reliably, no, and neither does any plugin of this kind. A full-page cache can serve a stored copy of the page before WordPress has a chance to decide who you are. Most caching plugins skip the cache for logged-in users, which is usually enough; if a switched page looks wrong, clear the cache and reload.

= Who can switch roles? =

Only users who can manage options, which in practice means administrators. Every switch is checked twice: once for the capability, and once for a valid nonce. The cookie holding the session is signed with a server-side key and tied to your login session, so it cannot be forged or handed to somebody else.

= Does it work on multisite? =

Yes, per site. Any administrator of a site can switch roles on that site, and the switch is confined to it. Network administrators are covered too: super admin status is set aside for the length of the view, otherwise the switch would have no effect at all.

= Can I hide roles I never test with? =

Yes. **Tools > View As Role** lists every role on the site, and only the ones you tick appear in the toolbar. Sites with a dozen roles from an eCommerce or LMS plugin are much easier to work with once the list is short.

== Screenshots ==

1. The View As menu in the toolbar, listing every role you have chosen to test with
2. A switched session, with the toolbar showing the role being viewed and the way back
3. Tools > View As Role, where you choose which roles appear and how long a session lasts

== Changelog ==

= 2.0.0 =
A full rewrite. Role switching now works.

* **Fixed the central bug: switching roles had no effect on capabilities.** Version 1.0 assigned to a property on the user object that WordPress never recalculates, so `current_user_can()` carried on answering as an administrator. The site behaved exactly as it always had while the toolbar claimed otherwise. Anything tested with 1.0 was, in effect, tested as an administrator and is worth checking again.
* Added: capability filtering, applied where WordPress resolves what the current user may do, so themes, WooCommerce, membership plugins and page builders all see the switched role
* Added: ownership-aware handling of meta capabilities, so content you wrote yourself is treated as somebody else's while you are viewing as another role
* Added: the switched view is always a subset of your own capabilities and can never grant one you do not have
* Added: a settings screen at **Tools > View As Role** for choosing which roles appear in the toolbar and how long a session lasts
* Added: automatic expiry, at 15 minutes, 1 hour or 8 hours
* Added: the switched state is now obvious in the toolbar, with a one-click way back from every page
* Added: multisite support, including network administrators
* Added: full internationalisation, with German, Spanish, French, Italian and Brazilian Portuguese translations
* Fixed: the nonce check could be skipped entirely by leaving the nonce out of the request, and the guest and reset links never carried one. Every switch now verifies a nonce, with no exceptions
* Fixed: the cookie was trusted without re-checking that the person presenting it was an administrator
* Fixed: the cookie is now signed, HttpOnly, Secure over HTTPS, SameSite Lax, and scoped to the site's own cookie path
* Fixed: the switch is now in place at the start of the request rather than at template load, so plugins that resolve capabilities early see it
* Fixed: redirects now use `wp_safe_redirect()`
* Fixed: role comparisons now use strict matching
* Changed: renamed to ABCode View As Role Switcher. The plugin slug, your settings and your update path are unchanged
* Changed: the switch applies to front-end page loads only. wp-admin always runs as your real account, which is what guarantees you can never be locked out
* Removed: `load_plugin_textdomain()`, which WordPress has not needed since 4.6

= 1.0 =
* Initial release

== Upgrade Notice ==

= 2.0.0 =
Important: in version 1.0 switching roles did not change capabilities, so the site behaved as an administrator throughout and any testing done with it should be repeated. 2.0.0 makes the switch real, and adds nonce checks, a signed cookie and automatic expiry.

== Privacy ==

The plugin stores one cookie, set only when you start a view session, and only for you. It holds the role being viewed, an expiry time and a signature, and is removed when the session ends, when you log out, or when it expires. Nothing is sent anywhere, no data is collected, and no third party is involved.
