Skip to content

feat(autosuggest): enable autosuggest in the WordPress admin#4328

Open
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/3886-admin-autosuggest
Open

feat(autosuggest): enable autosuggest in the WordPress admin#4328
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/3886-admin-autosuggest

Conversation

@faisalahammad

Copy link
Copy Markdown

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.php

Before:
The feature only hooked wp_enqueue_scripts and 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:

  • A new enable_admin_autosuggest checkbox setting is added to the feature settings schema.
  • admin_enqueue_scripts is hooked to enqueue_admin_scripts(), which loads the same elasticpress-autosuggest script/style when the setting is enabled and the current user has the edit_posts capability (filterable via ep_admin_autosuggest_capability).
  • Shared enqueue and option-building logic is extracted into enqueue_autosuggest_assets(), get_autosuggest_endpoint_url(), and get_epas_options() so both frontend and admin use the same localization structure.
  • generate_search_query() now accepts an optional $is_admin flag. When true, it uses Indexables::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 filters ep_is_integrated_request to return true for the search and autosuggest contexts, 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.php

After:
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_request override callback, and the admin query template generation.

Testing

Test 1: Enable admin autosuggest and see suggestions on the post list

  1. Go to ElasticPress > Features > Autosuggest and check Enable autosuggest in the WordPress admin.
  2. Navigate to Posts > All Posts.
  3. Type in the search box (#post-search-input).
    Result: matching suggestions appear in a dropdown and clicking one navigates to the post.

Test 2: Capability check

  1. Log in as a user without edit_posts (e.g., Subscriber).
  2. Visit any admin screen.
    Result: the autosuggest script is not loaded.

Test 3: Frontend unchanged

  1. Visit a frontend search form.
  2. Type in the search field.
    Result: autosuggest continues to work exactly as before.

Test 4: PHPUnit

  1. Run composer run test -- --filter TestAutosuggest.
    Result: 24 tests, 67 assertions, all pass.

- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autosuggest for wp-admin section

1 participant