Skip to content

Commit 52f11b3

Browse files
authored
PRT-3011 add elastic environment variable (#109)
* PRT-3011 add elastic environment variable * PRT-3011 use defined variable from prepend file * PRT-3011 refactor
1 parent 780a9e3 commit 52f11b3

5 files changed

Lines changed: 109 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ composer.lock
66
/bin/install-local-tests.sh
77
/bin/install-wp-tests.sh
88
/bin/phpunit-test.sh
9-
/bin/helpers.sh
9+
/bin/helpers.sh
10+
.idea
11+
.ds_store

inc/elasticpress-client-side.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

pantheon.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
if ( defined( 'WP_CLI' ) && WP_CLI ) {
2929
require_once 'inc/cli.php';
3030
}
31+
require_once 'inc/elasticpress-client-side.php';
3132
if ( ! defined( 'FS_METHOD' ) ) {
3233
/**
3334
* When this constant is not set, WordPress writes and then deletes a

tests/phpunit/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
function _manually_load_plugin() {
2020
// Set the Pantheon environment variable.
2121
$_ENV['PANTHEON_ENVIRONMENT'] = 'dev';
22+
define( 'EP_DIRECT_HOST', 'https://test.hosted-elasticpress.io' );
2223

2324
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
2425
putenv( 'FRAMEWORK=wordpress_network' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Tests for ElasticPress client-side search configuration.
4+
*
5+
* @package pantheon
6+
*/
7+
8+
/**
9+
* Test class for ElasticPress client-side endpoint overrides.
10+
*/
11+
class Test_ElasticPress_Client_Side extends WP_UnitTestCase {
12+
/**
13+
* Test that EP_DIRECT_HOST is defined.
14+
*/
15+
public function test_ep_direct_host_defined() {
16+
$this->assertTrue( defined( 'EP_DIRECT_HOST' ) );
17+
$this->assertSame( 'https://test.hosted-elasticpress.io', EP_DIRECT_HOST );
18+
}
19+
20+
/**
21+
* Test that the autosuggest options filter is registered.
22+
*/
23+
public function test_autosuggest_filter_registered() {
24+
$this->assertNotFalse(
25+
has_filter( 'ep_autosuggest_options', 'Pantheon\\ElasticPress\\filter_autosuggest_options' )
26+
);
27+
}
28+
29+
/**
30+
* Test that the instant results filter is registered.
31+
*/
32+
public function test_instant_results_filter_registered() {
33+
$this->assertNotFalse(
34+
has_filter( 'ep_instant_results_search_endpoint', 'Pantheon\\ElasticPress\\filter_instant_results_endpoint' )
35+
);
36+
}
37+
}

0 commit comments

Comments
 (0)