JoryHogeveen / view-admin-as

View the WordPress admin as a different role, switch between users, temporarily change your capabilities, set default screen settings for roles, manage your roles and capabilities.

Home Page:https://wordpress.org/plugins/view-admin-as/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compatibility with WP Rocket logged-in user cache

JoryHogeveen opened this issue · comments

More info: https://wordpress.org/support/topic/compatibility-with-wp-rocket-11/

Basically, when cache is enabled for logged in users it prevents View Admin As from working correctly.
I'll have to check if there are some filters available to bypass cache or find some other solution.

Hi Jory,

Following up on the discussion on wp.org, may I ask how does View Admin As keeps track of the current role it is emulating?

So that I can see if there is any exclusion option in WP-Rocket that could work.

Hi @lucspe

I store the view in the user metadata and at a very early stage in the plugins_loaded hook I switch the user.
https://github.com/JoryHogeveen/view-admin-as/blob/master/includes/class-vaa.php#L163

The view class handles all switching:
https://github.com/JoryHogeveen/view-admin-as/blob/master/includes/class-view.php
For each view there is a module which sets the view data:
https://github.com/JoryHogeveen/view-admin-as/blob/master/modules/class-users.php#L132-L152

Hi @lucspe

I was just wondering is you might have found a solution. Let me know!
One option I could imagine is that I could disable cache entirely once a view is selected..

HI @JoryHogeveen

So this is definetely an issue that manifests if WP-Rocket is enabled and cache is enabled for logged in users.

As a temporary workaround, I simply add a bogus variable to the URL (i,e, "?whatever") before doing the switch. This way, when the page reloads with the new role it will bypass the cache.
To switch back to admin (reset to default) or change to another role, I then need to do this again adding the variable to the URL.

I was looking into WP-Rocket documentation and it seems that is possible to to exclude admins from cache even when it is enabled for logged in users:

https://docs.wp-rocket.me/article/919-disable-cache-for-logged-in-administrators

However I am not sure if this approach could be useful for compatibility with VAA.

Hi @lucspe

Thank you for this resource. I've created a patch that I think might help in your case:
https://github.com/JoryHogeveen/view-admin-as/blob/hotfix/126/includes/class-compat.php#L108-L116

Could you try this?

Hi @JoryHogeveen

Thanks.

It doesn't work unfortunately.

Did you clear your cache before testing?

Hi @JoryHogeveen,

I tried once again to be sure.

I have cleared the cache several times and it doesn't work unfortunately.

Specifically, clearing the cache makes it work once (just like adding a non existing variable to the URL).

I'm getting the same problem but with Litespeed cache, did you solve this for WP Rocket? Adding a query parameter while VAA is active may work as I can then exclude the view from being cached. I can also exclude via cookies.

hi @joncon62 , nope.

Currently in the only site where I need cache active also for logged in users I bypass the cache adding a query variable to the URL.

Hi @lucspe and @joncon62

Unfortunately, it seems cache plugins run their code even before plugins are loaded. This of course greatly enhances speed but results in a plugin like this not being able to function.

I could of course always add a refresh param when switching but that still won't work with browsing though the site.

Ideas on how to properly address this are very welcome!

Also having issues when using Litespeed. I don't have private cache enabled and caching is turned off for all roles. I did discover that it's only "broken" when I view as site visitor. If I view as subscriber, I can change the view back or reset to default properly. I'm happy to make my site available to Jory for testing if that would be helpful. Thank again Jory for a great plugin.

When viewing as a visitor the page cache would be enabled.
I'm still searching for a way to fix this as most caching plugins are enabled before WordPress is loaded, making my code quite useless.