|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Wordlift_Autocomplete_Adapter class. |
|
4
|
|
|
* |
|
5
|
|
|
* The {@link Wordlift_Autocomplete_Adapter} class create requests to external API's. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://wordlift.io |
|
8
|
|
|
* |
|
9
|
|
|
* @package Wordlift |
|
10
|
|
|
* @since 3.15.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
use Wordlift\Autocomplete\Autocomplete_Service; |
|
14
|
|
|
|
|
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
16
|
|
|
exit; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create autocomplete request to external API's and return the result if there is such. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 3.15.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Wordlift_Autocomplete_Adapter { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The {@link Autocomplete_Service} instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @since 3.15.0 |
|
30
|
|
|
* @access private |
|
31
|
|
|
* @var Autocomplete_Service $configuration_service The {@link Autocomplete_Service} instance. |
|
32
|
|
|
*/ |
|
33
|
|
|
private $autocomplete_service; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Wordlift_Autocomplete_Adapter constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Autocomplete_Service $autocomplete_service The {@link Autocomplete_Service} instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 3.14.2 |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( $autocomplete_service ) { |
|
45
|
|
|
$this->autocomplete_service = $autocomplete_service; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Handle the autocomplete ajax request. |
|
50
|
|
|
* |
|
51
|
|
|
* @since 3.15.0 |
|
52
|
|
|
*/ |
|
53
|
|
|
public function wl_autocomplete() { |
|
54
|
|
|
|
|
55
|
|
|
check_ajax_referer( 'wl_autocomplete' ); |
|
56
|
|
|
|
|
57
|
|
|
// Return error if the query param is empty. |
|
58
|
|
|
if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
59
|
|
|
$query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
60
|
|
|
} else { |
|
61
|
|
|
wp_send_json_error( array( |
|
62
|
|
|
'message' => __( 'The query param is empty.', 'wordlift' ), |
|
63
|
|
|
) ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Get the exclude parameter. |
|
67
|
|
|
$exclude = ! empty( $_REQUEST['exclude'] ) |
|
68
|
|
|
? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : ''; |
|
69
|
|
|
|
|
70
|
|
|
$scope = ! empty( $_REQUEST['scope'] ) |
|
71
|
|
|
? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE; |
|
72
|
|
|
|
|
73
|
|
|
// Make request. |
|
74
|
|
|
$results = $this->autocomplete_service->query( $query, $scope, $exclude ); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
// Clear any buffer. |
|
77
|
|
|
ob_clean(); |
|
78
|
|
|
|
|
79
|
|
|
wp_send_json_success( $results ); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: