|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ElasticPress Client-Side Search Configuration |
| 4 | + * |
| 5 | + * Routes browser-originated search requests (Autosuggest and Instant Results) |
| 6 | + * directly to the public ElasticPress.io endpoint, bypassing the internal |
| 7 | + * mtlsproxy which is only accessible server-side. |
| 8 | + * |
| 9 | + * Server-side operations (indexing, admin queries, WP_Query integration) |
| 10 | + * continue to route through the mtlsproxy for authenticated access. |
| 11 | + * |
| 12 | + * @package pantheon |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Pantheon\ElasticPress; |
| 16 | + |
| 17 | +if ( ! defined( 'EP_DIRECT_HOST' ) ) { |
| 18 | + return; |
| 19 | +} |
| 20 | + |
| 21 | +add_filter( 'ep_autosuggest_options', __NAMESPACE__ . '\\filter_autosuggest_options' ); |
| 22 | +add_filter( 'ep_instant_results_search_endpoint', __NAMESPACE__ . '\\filter_instant_results_endpoint' ); |
| 23 | + |
| 24 | +/** |
| 25 | + * Override the Autosuggest endpoint URL to use the direct ElasticPress.io host. |
| 26 | + * |
| 27 | + * @param array $options The autosuggest options passed to the browser JS. |
| 28 | + * @return array Modified options with the direct endpoint URL. |
| 29 | + */ |
| 30 | +function filter_autosuggest_options( $options ) { |
| 31 | + $index = get_post_index_name(); |
| 32 | + if ( ! $index ) { |
| 33 | + return $options; |
| 34 | + } |
| 35 | + |
| 36 | + $options['endpointUrl'] = EP_DIRECT_HOST . '/' . $index . '/autosuggest'; |
| 37 | + return $options; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Filter the Instant Results search endpoint to use the direct ElasticPress.io URL. |
| 42 | + * |
| 43 | + * @param string $endpoint The default endpoint path. |
| 44 | + * @return string The full direct ElasticPress.io endpoint URL. |
| 45 | + */ |
| 46 | +function filter_instant_results_endpoint( $endpoint ) { |
| 47 | + $index = get_post_index_name(); |
| 48 | + if ( ! $index ) { |
| 49 | + return $endpoint; |
| 50 | + } |
| 51 | + |
| 52 | + return EP_DIRECT_HOST . '/api/v1/search/posts/' . $index; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Get the ElasticPress post index name. |
| 57 | + * |
| 58 | + * @return string|false The index name, or false if unavailable. |
| 59 | + */ |
| 60 | +function get_post_index_name() { |
| 61 | + $post_indexable = \ElasticPress\Indexables::factory()->get( 'post' ); |
| 62 | + if ( ! $post_indexable ) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + return $post_indexable->get_index_name(); |
| 67 | +} |
0 commit comments