Skip to content

Commit

Permalink
Support for EP instant results custom proxy
Browse files Browse the repository at this point in the history
Reimplements the custom proxy from https://github.com/10up/elasticpress-proxy

Currently WIP, still to do:

- [ ] Implement request signing for cloud environments
- [ ] Come up with a way to cache the db query results that can be cleared on index/reindex so query only occurs on each container once. Will need to delete cache on reindex if possible

Fixes #410

Feature was revealed for v11 so should be backported to v11-branch.
  • Loading branch information
roborourke committed May 17, 2022
1 parent 608084b commit ce9eaf2
Show file tree
Hide file tree
Showing 5 changed files with 731 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"files": [
"inc/namespace.php",
"inc/analysis/namespace.php",
"inc/packages/namespace.php"
"inc/packages/namespace.php",
"inc/instant_results/namespace.php"
]
},
"extra": {
Expand Down
51 changes: 51 additions & 0 deletions inc/instant_results/namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Altis Search Proxy for Instant Results.
*
* Based on the proxy plugin at https://github.com/10up/elasticpress-proxy
*
* @package altis/enhanced-search
*/

namespace Altis\Enhanced_Search\Instant_Results;

use ElasticPress\Indexables;
use ElasticPress\Utils;

/**
* Bootstrap Instant Results Proxy.
*
* @return void
*/
function boostrap() {
add_filter( 'ep_instant_results_available', '__return_true' );
add_action( 'ep_instant_results_template_saved', __NAMESPACE__ . '\\save_template' );
add_filter( 'ep_instant_results_search_endpoint', __NAMESPACE__ . '\\set_proxy' );
}

/**
* Save the a PHP file with the search query template and the post index URL into the uploads folder.
*
* @param string $search_template The search template.
* @return void
*/
function save_template( string $search_template ) : void {
$post_index_base = trailingslashit( Utils\get_host( true ) );
foreach( Utils\get_sites() as $site ) {
$post_index = Indexables::factory()->get( 'post' )->get_index_name( $site['blog_id'] );
update_site_option( "ep_{$site['blog_id']}_post_index_url", $post_index_base . $post_index, false );
update_site_option( "ep_{$site['blog_id']}_instant_results_template", $search_template, false );
}
}

/**
* Set the custom proxy as the search endpoint.
*
* @return string
*/
function set_proxy() : string {
return sprintf( '%sproxy.php?blog_id=%d',
plugin_dir_url( __FILE__ ),
get_current_blog_id()
);
}
Loading

0 comments on commit ce9eaf2

Please sign in to comment.