Completed
Push — master ( 08da20...186893 )
by Sudar
02:13
created

EDDAPI::call_edd_api()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 24
rs 8.5125
1
<?php namespace EmailLog\Addon\API;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * Wrapper for EDD API
7
 *
8
 * @since 2.0.0
9
 */
10
class EDDAPI {
11
12
	protected $store_url = 'https://wpemaillog.com';
13
14
	/**
15
	 * Activate License.
16
	 *
17
	 * @param string $license_key License Key.
18
	 * @param string $addon_name  Add-on Name.
19
	 *
20
	 * @return object API Response JSON Object.
21
	 */
22
	public function activate_license( $license_key, $addon_name ) {
23
		$params = array(
24
			'edd_action' => 'activate_license',
25
			'license'    => $license_key,
26
			'item_name'  => urlencode( $addon_name ),
27
			'url'        => home_url(),
28
		);
29
30
		return $this->call_edd_api( $params );
31
	}
32
33
	/**
34
	 * Deactivate License.
35
	 *
36
	 * @param string $license_key License Key.
37
	 * @param string $addon_name  Add-on Name.
38
	 *
39
	 * @return object API Response JSON Object.
40
	 */
41
	public function deactivate_license( $license_key, $addon_name ) {
42
		$params = array(
43
			'edd_action' => 'deactivate_license',
44
			'license'    => $license_key,
45
			'item_name'  => urlencode( $addon_name ),
46
			'url'        => home_url(),
47
		);
48
49
		return $this->call_edd_api( $params );
50
	}
51
52
	/**
53
	 * Get version information.
54
	 *
55
	 * @param string $license_key License Key.
56
	 * @param string $addon_name  Add-on Name.
57
	 *
58
	 * @return object API Response JSON Object.
59
	 */
60
	public function get_version( $license_key, $addon_name ) {
61
		$params = array(
62
			'edd_action' => 'get_version',
63
			'license'    => $license_key,
64
			'item_name'  => $addon_name,
65
			'url'        => home_url(),
66
		);
67
68
		return $this->call_edd_api( $params );
69
	}
70
71
	/**
72
	 * Call the EDD API.
73
	 *
74
	 * @param array $params Parameters for request.
75
	 *
76
	 * @return object API Response in JSON.
77
	 * @throws \Exception If there is any error while making the request.
78
	 *
79
	 * TODO: Make the errors more user friendly and provide links to support.
80
	 */
81
	protected function call_edd_api( $params ) {
82
		$response = wp_remote_post( $this->store_url, array(
83
			'timeout' => 15,
84
			'body'    => $params,
85
		) );
86
87
		if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
88
89
			if ( is_wp_error( $response ) ) {
90
				throw new \Exception( $response->get_error_message() );
91
			}
92
93
			throw new \Exception( __( 'Unknown error occurred while trying to contact Email Log store. Please try again after sometime. If the problem persists contact support.', 'email-log' ) );
94
		}
95
96
		$body = wp_remote_retrieve_body( $response );
97
		$data = json_decode( $body );
98
99
		if ( empty( $data ) ) {
100
			throw new \Exception( __( 'Unable to parse the response Email Log store response. Please try again after sometime. If the problem persists contact support.', 'email-log' ) );
101
		}
102
103
		return $data;
104
	}
105
}
106