-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathelasticpress-proxy.php
72 lines (60 loc) · 1.99 KB
/
elasticpress-proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Plugin Name: ElasticPress Proxy
* Description: A custom PHP Proxy to handle Instant Results requests.
* Version: 1.0.0
* Author: 10up | ElasticPress.io
* Author URI: https://elasticpress.io
* Text Domain: elasticpress-proxy
* Domain Path: /languages
* Update URI: https://github.com/10up/elasticpress-proxy
* Requires at least: 5.6
* Requires PHP: 7.0
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package ElasticPress_Custom_Proxy
*/
namespace ElasticPress_Custom_Proxy;
use ElasticPress\Indexables;
use ElasticPress\Utils;
defined( 'ABSPATH' ) || exit;
/**
* Enable the Instant Results feature.
*/
add_filter( 'ep_instant_results_available', '__return_true' );
/**
* 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.
*/
function save_template( $search_template ) {
global $wp_filesystem;
$post_index = Indexables::factory()->get( 'post' )->get_index_name();
$post_index_url = trailingslashit( Utils\get_host( true ) ) . $post_index;
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
$file_content = [
'<?php',
'// This file is automatically generated. DO NOT MODIFY IT.',
'$post_index_url = \'' . $post_index_url . '\';',
'$query_template = \'' . $search_template . '\';',
'',
];
$file_content = implode( "\n", $file_content );
$uploads_dir = wp_upload_dir();
$wp_filesystem->put_contents(
trailingslashit( $uploads_dir['basedir'] ) . 'ep-custom-proxy-credentials.php',
$file_content
);
}
add_action( 'ep_instant_results_template_saved', __NAMESPACE__ . '\save_template' );
/**
* Set the custom proxy as the search endpoint.
*
* @return string
*/
function set_proxy() {
return plugin_dir_url( __FILE__ ) . 'proxy.php';
}
add_filter( 'ep_instant_results_search_endpoint', __NAMESPACE__ . '\set_proxy' );