Generate search template as anonymous user. #2672
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
As discussed on #2650, the query that generates the Elasticsearch search template for Instant Results runs in the context of the user being logged in as an admin. This means that the template generated will be for the query for an administrator, so things like excluding password protected posts will not be applied.
This change sets the current user as anonymous while generating the template to resolve this issue. It also includes a filter to allow developers to set a specific user for whom the template should be generated as.
Closes #2650
Alternate Designs
The original issue suggested using the
determine_current_user
filter to achieve this, by adding a filter that returns false for the current user during template generation. The problem with this is that it turns out that this filter does not actually run if the current user has already been set.Possible Drawbacks
None that I can think of.
Verification Process
pre_get_posts
that applies a change to the query, but only for logged in users. Save the weighting settings and check the search template. This change should not be apparent in the template.add_filter( 'ep_search_template_user_id', 'get_current_user_id' );
, save the weighting settings, and check the search template. The change should be apparent in the template.Checklist:
Changelog Entry
Fixed - An issue where Instant Results may return results intended for logged-in users only.
Credits
Props @JakePT