|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Wordlift_Autocomplete_Service class. |
|
4
|
|
|
* |
|
5
|
|
|
* The {@link Wordlift_Autocomplete_Service} class handle and process all autocomplete requests. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://wordlift.io |
|
8
|
|
|
* |
|
9
|
|
|
* @package Wordlift |
|
10
|
|
|
* @since 3.15.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Process WordLift's autocomplete requests. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 3.15.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Wordlift_Autocomplete_Service { |
|
23
|
|
|
/** |
|
24
|
|
|
* The {@link Wordlift_Configuration_Service} instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @since 3.15.0 |
|
27
|
|
|
* @access private |
|
28
|
|
|
* @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
29
|
|
|
*/ |
|
30
|
|
|
private $configuration_service; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @since 3.15.0 |
|
36
|
|
|
* @access private |
|
37
|
|
|
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
38
|
|
|
*/ |
|
39
|
|
|
private $log; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The {@link Class_Wordlift_Autocomplete_Service} instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 3.15.0 |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct( $configuration_service ) { |
|
49
|
|
|
$this->configuration_service = $configuration_service; |
|
50
|
|
|
$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Make request to external API and return the response. |
|
55
|
|
|
* |
|
56
|
|
|
* @since 3.15.0 |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $query The search string. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array $response The API response. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function make_request( $query ) { |
|
63
|
|
|
$url = $this->build_request_url( $query ); |
|
64
|
|
|
|
|
65
|
|
|
// Make request. |
|
66
|
|
|
$response = wp_remote_get( $url ); |
|
67
|
|
|
|
|
68
|
|
|
// Return the response. |
|
69
|
|
|
return $response; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Build the autocomplete url. |
|
74
|
|
|
* |
|
75
|
|
|
* @since 3.15.0 |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $query The search string. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string Built url. |
|
80
|
|
|
*/ |
|
81
|
|
|
private function build_request_url( $query ) { |
|
82
|
|
|
$args = array( |
|
83
|
|
|
'key' => $this->configuration_service->get_key(), |
|
84
|
|
|
'language' => $this->configuration_service->get_language_code(), |
|
85
|
|
|
'query' => $query, |
|
86
|
|
|
'limit' => 50, |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
// Add args to URL. |
|
90
|
|
|
$request_url = add_query_arg( |
|
91
|
|
|
$args, |
|
92
|
|
|
$this->configuration_service->get_autocomplete_url() |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
// return the built url. |
|
96
|
|
|
return $request_url; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|