feat(autosuggest): enable autosuggest in the WordPress admin#4328
Open
faisalahammad wants to merge 1 commit into
Open
feat(autosuggest): enable autosuggest in the WordPress admin#4328faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
- Add enable_admin_autosuggest setting to the Autosuggest feature. - Enqueue existing autosuggest assets on admin_enqueue_scripts when the setting is enabled and the user has edit_posts (filterable via ep_admin_autosuggest_capability). - Generate an admin-specific query template using the post statuses that are actually indexed. - Force ElasticPress integration for the template-generation query only by filtering ep_is_integrated_request for search and autosuggest contexts, mirroring the pattern used by Instant Results and WooCommerce Orders Autosuggest. - Refactor shared enqueue/option logic into helper methods so both frontend and admin use the same localization structure. - Add PHPUnit tests for the new setting, admin enqueue gating, capability check, and admin query template. Fixes 10up#3886
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR adds an opt-in setting to extend the Autosuggest feature to the WordPress admin area, such as the post list table search box. The frontend autosuggest code and assets are reused; the only backend change is an admin-specific query template that uses the post statuses actually stored in the index.
Fixes #3886
Changes
includes/classes/Feature/Autosuggest/Autosuggest.phpBefore:
The feature only hooked
wp_enqueue_scriptsand generated a query template based on public, searchable post statuses. Admin requests were never integrated, so the template could not be built from an admin context.After:
enable_admin_autosuggestcheckbox setting is added to the feature settings schema.admin_enqueue_scriptsis hooked toenqueue_admin_scripts(), which loads the sameelasticpress-autosuggestscript/style when the setting is enabled and the current user has theedit_postscapability (filterable viaep_admin_autosuggest_capability).enqueue_autosuggest_assets(),get_autosuggest_endpoint_url(), andget_epas_options()so both frontend and admin use the same localization structure.generate_search_query()now accepts an optional$is_adminflag. When true, it usesIndexables::factory()->get( 'post' )->get_indexable_post_status()so the template matches what is actually indexed (including drafts/private/future when Protected Content is active).generate_admin_search_query()temporarily filtersep_is_integrated_requestto returntruefor thesearchandautosuggestcontexts, mirroring the pattern used by Instant Results and WooCommerce Orders Autosuggest. The filter is removed immediately after the template is captured, so regular admin queries are unaffected.tests/php/features/TestAutosuggest.phpAfter:
Seven new tests cover the setting schema, default-off behavior, enqueue gating when the setting is enabled, endpoint requirement, capability check, the
ep_is_integrated_requestoverride callback, and the admin query template generation.Testing
Test 1: Enable admin autosuggest and see suggestions on the post list
#post-search-input).Result: matching suggestions appear in a dropdown and clicking one navigates to the post.
Test 2: Capability check
edit_posts(e.g., Subscriber).Result: the autosuggest script is not loaded.
Test 3: Frontend unchanged
Result: autosuggest continues to work exactly as before.
Test 4: PHPUnit
composer run test -- --filter TestAutosuggest.Result: 24 tests, 67 assertions, all pass.