Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for EP instant results custom proxy #411

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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