actions.php ➔ printcenter_ajax_search_vendors()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 3
nop 0
dl 0
loc 31
ccs 0
cts 17
cp 0
crap 20
rs 8.5806
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Vendor actions
4
 *
5
 * @package     PrintCenter\Vendor\Actions
6
 * @since       1.0.0
7
 */
8
9
10
// Exit if accessed directly
11
if( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
16
/**
17
 * Search vendors
18
 *
19
 * @since       1.0.0
20
 * @global      object $woo_vendors The vendors object
21
 * @return      void
22
 */
23
function printcenter_ajax_search_vendors() {
24
	global $woo_vendors;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
25
26
	check_ajax_referer( 'search-vendors', 'security' );
27
28
	header( 'Content-Type: application/json; charset=utf-8' );
29
30
	$term = urldecode( stripslashes( strip_tags( $_GET['term'] ) ) );
31
32
	if( empty( $term ) ) {
33
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function printcenter_ajax_search_vendors() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
34
	}
35
36
	$found_vendors = array();
37
38
	$args = array(
39
		'hide_empty' => false,
40
		'search'     => $term
41
	);
42
43
	$vendors = get_terms( $woo_vendors->token, $args );
44
45
	if( $vendors ) {
46
		foreach( $vendors as $vendor ) {
47
			$found_vendors[$vendor->term_id] = $vendor->name;
48
		}
49
	}
50
51
	echo json_encode( $found_vendors );
52
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function printcenter_ajax_search_vendors() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
53
}
54
add_action( 'wp_ajax_printcenter_ajax_search_vendors', 'printcenter_ajax_search_vendors' );