Idea Hub Drafts are containing trashed posts.
Bug Description
When we create draft idea hub posts, if we subsequently delete them, they are still shown on the dashboard.
Steps to reproduce
We are caching draft posts.
- Create two draft posts based on idea hub using
/wp-admin/admin.php?page=googlesitekit-dashboard#new-ideas - Delete one of the idea hub posts via
/wp-admin/edit.php - Open the sitekit dashboard to the drafts view
/wp-admin/admin.php?page=googlesitekit-dashboard#draft-ideasand see there are still two posts there, even though one is deleted. - Create another draft post, it will show three draft posts.
- Refresh the page, and you will see two posts as the deleted post is no longer shown.
Screenshots
https://user-images.githubusercontent.com/373931/126131941-83a67286-a52c-4f90-946b-f29e18b4ed6c.mp4
Additional Context
- PHP Version:
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Plugin Version [e.g. 22]
- Device: [e.g. iPhone6]
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Deleted or trashed posts created from Idea Hub ideas should no longer appear as draft posts in the Idea Hub widget.
- Specifically, the client-side cache needs to be invalidated once the status of an Idea Hub idea post is changed - this is strictly not limited to trashing/deleting posts, it would also make sense e.g. once a draft post is published. Potentially worth relying on the WordPress action hook that changes the post status.
Implementation Brief
- In
assets/js/modules/idea-hub/datastore/draft-post-ideas.js:- Refactor the call to
API.getinsidefetchGetDraftPostIdeasStoreto pass a fourth and 5th parameter:undefinedand{ useCache: false }:
controlCallback: () => { return API.get( 'modules', 'idea-hub', 'draft-post-ideas', undefined, { useCache: false } ); }, - Refactor the call to
- Merge https://github.com/google/site-kit-wp/pull/3749
Test Coverage
- No new tests required
Visual Regression Changes
- None anticipated.
QA Brief
- Follow the steps to recreate above.
- Issue should be fixed.
Changelog entry
- Fix bug in Idea Hub dashboard widget that caused drafts not to refresh.
@ivankruchkoff I went through this with @tofumatt and we couldn't see a straightforward way to detect that a post had changed status and then clear the client side cache. I don't know if you had anything specific in mind?
I'm suggesting that we simply don't cache the GET request to fetch drafts - it felt to us that these requests probably shouldn't be cached anyway.
I think this is the best approach here; simply not caching these requests.
Caching these requests doesn't help that much, and creating a system to clear the client-side cache from the server would require a lot of infrastructure and wouldn't be great in my mind.
IB ✅
QA Update: ✅
Followed the steps to recreate the issue. Then, verified if you create a draft and then go into the posts and delete it, that it no longer appears in the draft tab.
Approval ❌
@johnPhillips @tofumatt With the new implementation, the request to get draft post ideas now runs on every page load, which is something we should avoid - we're not doing that anywhere in the dashboard for a reason. Looking back at https://github.com/google/site-kit-wp/issues/3733#issuecomment-884281505, it looks like this decision was already made in the IB stage, however I'd challenge it - we shouldn't get rid of our "cache everything in the dashboard" best practice only because it's more complex to handle in this case. The implementation therefore doesn't satisfy the second point of the ACs - we can definitely discuss and potentially modify, but please consult first when diverging from the ACs.
There are two problems here that need to be fixed:
- The draft post ideas request now happens on every page load.
- The new ideas and saved ideas requests remain unaffected, although their responses also consider draft posts (since they are excluded from those results).
We could solve it for example in the following way:
- In PHP, keep track of when any Idea Hub post was last changed, e.g. store timestamp in a transient for one hour (same as our client-side cache expiration).
- In PHP, if the transient is set, set the timestamp value in PHP-to-JS data.
- In JS, if the flag variable is set and contains a timestamp, invalidate the Idea Hub cache if its data is older than the timestamp.
- Either by a specific routine that checks the Idea Hub cache and clears everything that's older than the timestamp.
- Or per datastore selector that checks only the specific cache for that selector is older than the timestamp, and if so clears it.
- This needs to apply to all Idea Hub endpoints to get ideas.
Thoughts @johnPhillips @tofumatt? Since this is not yet user-facing, we shouldn't rush a quick fix into the current release with this issue, but I think the current behavior is undesirable and we should look into making it more reliable while not ignoring cache benefits.