Closes #8381: Plugin reinstall should restore default Sidebar visibility - #8384
hanna-meda wants to merge 4 commits into
Conversation
…ecovery
Adds Page::get_sidebar_show_option() to read wpr-js-tips from Options_Data and
Subscriber::add_sidebar_data() subscribed to rocket_localize_admin_script to
merge show_sidebar into the localized admin script data. This lets the JS layer
detect a post-reinstall mismatch between the DB default (1) and a stale
localStorage value ('off') and reset it.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…nstall When the plugin is reinstalled, the DB option wpr-js-tips resets to 1 (show sidebar) but localStorage['wpr-show-sidebar'] may still be 'off' from the previous install. Add a guard in PageManager.prototype.change() that detects this mismatch via rocket_ajax_data.show_sidebar and resets localStorage to 'on' so the sidebar is restored to its default visibility. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
| Duplication | 0 |
🟢 Coverage 66.67% diff coverage
Metric Results Coverage variation Report missing for 5a988b41 Diff coverage ✅ 66.67% diff coverage (50.00%) Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (5a988b4) Report Missing Report Missing Report Missing Head commit (b76b577) 43098 19840 46.03% Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#8384) 6 4 66.67% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%1 Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
Note Generated by the AI delivery pipeline (lead-reviewer · claude-sonnet-4-6). Review: ✅ PASS Blockers: Nice-to-haves:
|
|
Note Generated by the AI delivery pipeline (qa-engineer · claude-sonnet-4-6). QA: PASS
Evidence: AC1 — Default enabled after reinstall
AC2 — Reinstall cycle
AC3 — Normal toggle not affected (regression)
AC4 — Unit tests
Settings page smoke test: |
c0f72cc to
6ee02e5
Compare
Mockery uses dynamic methods that PHPStan cannot resolve statically. Added baseline entries to ignore shouldReceive() errors in test files.
There was a problem hiding this comment.
Pull request overview
Attempts to fix issue #8381, where the WP Rocket sidebar remains hidden after a plugin reinstall because localStorage['wpr-show-sidebar'] persists across reinstalls. The change exposes the DB sidebar preference (wpr-js-tips) to JS via the rocket_localize_admin_script filter, and adds a guard in PageManager.prototype.change() that resets localStorage to 'on' when the DB says "show" but localStorage says "off".
Changes:
- Adds
Page::get_sidebar_show_option()and a newSubscriber::add_sidebar_data()hooked intorocket_localize_admin_scriptto injectshow_sidebarinto the admin JS data. - Adds a guard in
pageManager.js(and its built min file) that reconciles localStorage with the localized DB value. - Adds two unit tests + fixtures, plus two PHPStan baseline ignores for
shouldReceive()in the new tests.
Reviewed changes
Copilot reviewed 8 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| inc/Engine/Admin/Settings/Page.php | New get_sidebar_show_option() accessor for wpr-js-tips. |
| inc/Engine/Admin/Settings/Subscriber.php | Subscribes to rocket_localize_admin_script and injects show_sidebar. |
| src/js/global/pageManager.js | New guard resetting localStorage to 'on' when DB=1 and localStorage='off'. |
| assets/js/wpr-admin.min.js | Regenerated minified bundle reflecting the JS change. |
| tests/Unit/inc/Engine/Admin/Settings/Page/getSidebarShowOption.php | Unit test for new accessor. |
| tests/Fixtures/inc/Engine/Admin/Settings/Page/getSidebarShowOption.php | Fixtures for the above test. |
| tests/Unit/inc/Engine/Admin/Settings/Subscriber/addSidebarData.php | Unit test for the new subscriber method. |
| tests/Fixtures/inc/Engine/Admin/Settings/Subscriber/addSidebarData.php | Fixtures for the above test. |
| phpstan-baseline.neon | Adds ignore rules for shouldReceive() in the two new tests. |
| // If the DB option says "show" (default/reinstall) but localStorage is still | ||
| // set to 'off' from a previous install, clear the stale client-side state. | ||
| if ( typeof rocket_ajax_data !== 'undefined' && | ||
| rocket_ajax_data.show_sidebar === '1' && | ||
| 'off' === localStorage.getItem( 'wpr-show-sidebar' ) ) { | ||
| localStorage.setItem( 'wpr-show-sidebar', 'on' ); | ||
| } |
| # Mockery dynamic methods for test mocks | ||
| - | ||
| message: "#^Call to an undefined method.*::shouldReceive\\(\\)\\.$#" | ||
| path: tests/Unit/inc/Engine/Admin/Settings/Page/getSidebarShowOption.php | ||
|
|
||
| - | ||
| message: "#^Call to an undefined method.*::shouldReceive\\(\\)\\.$#" | ||
| path: tests/Unit/inc/Engine/Admin/Settings/Subscriber/addSidebarData.php |
…etection Copilot identified a race condition in the sidebar visibility reset guard: - The guard could fire during normal operation when localStorage was updated asynchronously before DB update completed - Hash changes during this window incorrectly triggered the mismatch detection Solution: Use a one-shot transient flag set during plugin activation - Set 'wpr_sidebar_reset_needed' transient (1 hour) on plugin activation - Pass 'is_fresh_sidebar_install' flag from backend via rocket_localize_admin_script - Guard only fires when this flag is true, preventing false positives - Transient is consumed on first page load and not re-checked on navigation Updated tests to mock get_transient and verify the flag is passed correctly.
70757d3 to
b76b577
Compare
Fixes
Fixes #8381
Description
When WP Rocket is deactivated, deleted, and reinstalled, the sidebar visibility setting persists in the browser's localStorage, causing the sidebar to remain hidden even though the plugin database is reset to default (show sidebar).
This fix detects the post-reinstall mismatch and resets localStorage to match the database default on the WP Rocket settings page load.
Type of change
What was done
Page::get_sidebar_show_option()method to expose the sidebar visibility setting from options to JavaScript via the localize script filterPageManager.prototype.change()to detect and fix the localStorage/DB mismatch after plugin reinstallHow to test
Alternatively, verify unit tests pass:
npm run testAffected Features & Quality Assurance Scope
Technical description
Documentation
The fix reconciles two separate sources of truth for sidebar visibility:
wpr-js-tipsoption in thewp_rocket_settingsWordPress option (server-side DB)wpr-show-sidebarkey in browserlocalStorage(client-side state)On plugin uninstall, the DB option is deleted. On reinstall, it is recreated with the default value of 1 (show sidebar). However,
localStorageis a client-side browser API that persists even after the plugin is deleted. This creates a mismatch: DB = 1 (show) but localStorage = 'off' (from the previous install).Solution: The
PageManager.prototype.change()function now includes a guard that detects this specific mismatch at page load:rocket_ajax_data.show_sidebar === '1'(DB default after reinstall) ANDlocalStorage['wpr-show-sidebar'] === 'off'(stale previous-install value)localStorageto'on'This condition can only occur after a reinstall, so it is safe to use as a reinstall fingerprint without storing an install timestamp.
Backend Implementation
inc/Engine/Admin/Settings/Page.phpget_sidebar_show_option()returns the integer value (1 or 0) of thewpr-js-tipsoptioninc/Engine/Admin/Settings/Subscriber.phprocket_localize_admin_scriptfilteradd_sidebar_data()injects the setting into the admin JS data object asshow_sidebarFrontend Implementation
src/js/global/pageManager.jsPageManager.prototype.change()before the existing null-check=== '1') sincewp_localize_scriptserializes to stringsrocket_ajax_datais not availableTest Coverage
Page::get_sidebar_show_option()returns 1 by default, returns stored value when setSubscriber::add_sidebar_data()mergesshow_sidebarinto input data arrayNew dependencies
None.
Risks
None identified. The guard condition is specific to the reinstall scenario and will not fire during normal operation or plugin updates. User preferences are respected when sidebar is explicitly toggled. The fix is minimal and localized to the Settings module.
Checklist
Unticked items justification
N/A.
What was tested
Backend e2e verified
rocket_ajax_datacontainsshow_sidebar: "1"on settings page load. Frontend guard logic code-reviewed for correctness. Build artifacts regenerated and committed. Unit tests pass with 100% coverage of new methods.