Completed
Pull Request — develop (#1082)
by Naveen
04:01
created

Wordlift_Autocomplete_Adapter::wl_autocomplete()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 16
nop 0
dl 0
loc 45
rs 8.5777
c 0
b 0
f 0
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
		/**
74
		 * @since 3.26.1
75
		 * Providing a way for term pages to show and save local entities.
76
		 */
77
		$show_local_entities = false;
78
79
		if ( isset( $_REQUEST['show_local_entities'] )
80
		     && ! empty( $_REQUEST['show_local_entities'] ) ) // Make request.
81
		{
82
			$show_local_entities = $_REQUEST['show_local_entities'] === 'true';
83
		}
84
85
		// Add the filter to check if we need to show local entities or not.
86
		add_filter( 'wl_show_local_entities', function ( $state ) use ( $show_local_entities ) {
0 ignored issues
show
Unused Code introduced by
The parameter $state is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
			return $show_local_entities;
88
		} );
89
90
		$results = $this->autocomplete_service->query( $query, $scope, $exclude );
0 ignored issues
show
Bug introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
91
92
		// Clear any buffer.
93
		ob_clean();
94
95
		wp_send_json_success( $results );
96
97
	}
98
}
99