Completed
Push — master ( 5db201...585add )
by David
08:20
created

Wordlift_Autocomplete_Service::make_request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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
	 * @param array|string $exclude The exclude parameter string.
60
	 *
61
	 * @return array $response The API response.
62
	 */
63
	public function make_request( $query, $exclude = '' ) {
64
		$url = $this->build_request_url( $query, $exclude );
65
66
		// Make request.
67
		$response = wp_remote_get( $url );
68
69
		// Return the response.
70
		return $response;
71
	}
72
73
	/**
74
	 * Build the autocomplete url.
75
	 *
76
	 * @since 3.15.0
77
	 *
78
	 * @param string       $query   The search string.
79
	 * @param array|string $exclude The exclude parameter.
80
	 *
81
	 * @return string Built url.
82
	 */
83
	private function build_request_url( $query, $exclude ) {
84
		$args = array(
85
			'key'      => $this->configuration_service->get_key(),
86
			'language' => $this->configuration_service->get_language_code(),
87
			'query'    => $query,
88
			'limit'    => 100,
89
		);
90
91
		// Add args to URL.
92
		$request_url = add_query_arg(
93
			urlencode_deep( $args ),
94
			$this->configuration_service->get_autocomplete_url()
95
		);
96
97
		// Add the exclude parameter.
98
		if ( ! empty( $exclude ) ) {
99
			foreach ( (array) $exclude as $item ) {
100
				$request_url .= "&exclude=" . urlencode( $item );
101
			}
102
		}
103
104
		// return the built url.
105
		return $request_url;
106
	}
107
}
108