BD_EDD_API_Wrapper::call_edd_api()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Wrapper for EDD API.
4
 *
5
 * @since      5.0
6
 *
7
 * @author     Sudar
8
 *
9
 * @package    BulkDelete\License
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
class BD_EDD_API_Wrapper {
14
	/**
15
	 * Store url.
16
	 *
17
	 * @since 5.0
18
	 */
19
	const STORE_URL = 'https://bulkwp.com';
20
21
	/**
22
	 * Check license.
23
	 *
24
	 * @since  5.0
25
	 * @static
26
	 *
27
	 * @param string $addon   Addon name
28
	 * @param string $license The license code
29
	 *
30
	 * @return array|false False if request fails, API response otherwise
31
	 */
32
	public static function check_license( $addon, $license ) {
33
		$api_params = array(
34
			'edd_action' => 'check_license',
35
			'license'    => trim( $license ),
36
			'item_name'  => urlencode( $addon ),
37
			'url'        => home_url(),
38
		);
39
40
		$license_data = array(
41
			'license'   => $license,
42
			'item_name' => $addon,
43
			'validity'  => 'invalid',
44
		);
45
46
		$response = self::call_edd_api( $api_params );
47
48
		if ( $response && isset( $response->license ) ) {
0 ignored issues
show
Bug introduced by
The property license does not exist on true.
Loading history...
49
			if ( 'valid' == $response->license ) {
50
				$license_data['license']    = $license;
51
				$license_data['validity']   = 'valid';
52
				$license_data['expires']    = $response->expires;
53
				$license_data['addon-name'] = $response->item_name;
54
			} elseif ( 'invalid' == $response->license ) {
55
				$license_data['validity']   = 'invalid';
56
			} elseif ( 'site_inactive' == $response->license ) {
57
				$license_data['validity']   = 'invalid';
58
			}
59
60
			return $license_data;
61
		}
62
63
		return false;
64
	}
65
66
	/**
67
	 * Activate license.
68
	 *
69
	 * @since  5.0
70
	 * @static
71
	 *
72
	 * @param string $addon   The addon that needs to be activated
73
	 * @param string $license The license code
74
	 *
75
	 * @return array|false False if request fails, License info otherwise
76
	 */
77
	public static function activate_license( $addon, $license ) {
78
		$api_params = array(
79
			'edd_action' => 'activate_license',
80
			'license'    => trim( $license ),
81
			'item_name'  => urlencode( $addon ),
82
			'url'        => home_url(),
83
		);
84
85
		$response = self::call_edd_api( $api_params );
86
87
		if ( $response && isset( $response->success ) ) {
0 ignored issues
show
Bug introduced by
The property success does not exist on true.
Loading history...
88
			if ( 'true' == $response->success ) {
89
				return array(
90
					'license'    => $license,
91
					'validity'   => 'valid',
92
					'expires'    => $response->expires,
93
					'addon-name' => $response->item_name,
94
				);
95
			} else {
96
				$err_response = array(
97
					'validity'   => 'invalid',
98
				);
99
100
				if ( isset( $response->error ) ) {
101
					$err_response['error'] = $response->error;
102
				}
103
104
				return $err_response;
105
			}
106
		}
107
108
		return false;
109
	}
110
111
	/**
112
	 * Deactivate License.
113
	 *
114
	 * @since  5.0
115
	 * @static
116
	 *
117
	 * @param string $addon   The addon that needs to be deactivated
118
	 * @param string $license The license code
119
	 *
120
	 * @return bool True if deactivated, False otherwise
121
	 */
122
	public static function deactivate_license( $addon, $license ) {
123
		$api_params = array(
124
			'edd_action' => 'deactivate_license',
125
			'license'    => trim( $license ),
126
			'item_name'  => urlencode( $addon ),
127
			'url'        => home_url(),
128
		);
129
130
		$response = self::call_edd_api( $api_params );
131
132
		if ( $response && isset( $response->license ) ) {
0 ignored issues
show
Bug introduced by
The property license does not exist on true.
Loading history...
133
			if ( 'deactivated' == $response->license ) {
134
				return true;
135
			}
136
		}
137
138
		return false;
139
	}
140
141
	/**
142
	 * Call the EDD API.
143
	 *
144
	 * @since  5.0
145
	 * @static
146
	 * @access private
147
	 *
148
	 * @param array $api_params Parameters for API
149
	 *
150
	 * @return array|bool $license_data False if request fails, API response otherwise
151
	 */
152
	private static function call_edd_api( $api_params ) {
153
		$response = wp_remote_get( add_query_arg( $api_params, self::STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );
154
155
		// make sure the response came back okay
156
		if ( is_wp_error( $response ) ) {
157
			return false;
158
		}
159
160
		$license_object = json_decode( wp_remote_retrieve_body( $response ) );
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_body() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
		$license_object = json_decode( wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response ) );
Loading history...
161
162
		return $license_object;
163
	}
164
}
165