-
Notifications
You must be signed in to change notification settings - Fork 17
/
SwiftypePlugin.php
100 lines (89 loc) · 2.7 KB
/
SwiftypePlugin.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Swiftype\SiteSearch\Wordpress;
use Swiftype\SiteSearch\Wordpress\Config\Config as PluginConfig;
use Elastic\SiteSearch\Client\ClientBuilder;
use Elastic\OpenApi\Codegen\Exception\ClientException;
use Elastic\OpenApi\Codegen\Exception\AuthenticationException;
/**
* The Site Search Wordpress Plugin
*
* This class encapsulates all of the Swiftype Search plugin's functionality.
*
* @author Matt Riley <[email protected]>, Quin Hoxie <[email protected]>, Aurelien Foucret <[email protected]>
*/
class SwiftypePlugin
{
/**
* List of sub-components that will be initialized by the plugins.
* @var array
*/
private $components = [
\Swiftype\SiteSearch\Wordpress\Engine\Manager::class,
\Swiftype\SiteSearch\Wordpress\Search\PostSearch::class,
\Swiftype\SiteSearch\Wordpress\Search\Widget::class,
\Swiftype\SiteSearch\Wordpress\Document\Indexer::class,
\Swiftype\SiteSearch\Wordpress\Admin\Action::class,
\Swiftype\SiteSearch\Wordpress\Admin\Page::class,
];
/**
* Constructor.
*/
public function __construct()
{
$this->registerComponents();
\add_action('swiftype_config_loaded', [$this, 'initClient']);
\add_action('init', [$this, 'initConfig']);
}
/**
* Instantiate all sub-components required by the plugin.
*
*/
private function registerComponents()
{
foreach ($this->components as $component) {
new $component();
}
}
/**
* Instantiate plugin config.
*/
public function initConfig()
{
\do_action('swiftype_config_loaded', new PluginConfig());
}
/**
* Build a new client from the config.
*
* @param PluginConfig $config Configuration.
*/
public function initClient(PluginConfig $config)
{
$client = null;
$apiKey = $config->getApiKey();
if ($apiKey && strlen($apiKey) > 0) {
$client = $this->getClient($apiKey);
try {
$client->listEngines();
\do_action('swiftype_client_loaded', $client);
} catch (AuthenticationException $e) {
$client = null;
} catch (ClientException $e) {
$client = null;
\do_action('swiftype_admin_error', $e);
}
}
}
/**
* Get client by API key.
*
* @param string $apiKey API Key.
*
* @return Client
*
*/
private function getClient($apiKey)
{
$integrationName = sprintf("%s:%s", "wordpress-site-search", SWIFTYPE_VERSION);
return ClientBuilder::create($apiKey)->setIntegration($integrationName)->build();
}
}