1 | <?php |
||
13 | class BD_EDD_API_Wrapper { |
||
14 | /** |
||
15 | * Store url. |
||
16 | * |
||
17 | * @since 5.0 |
||
18 | */ |
||
19 | const STORE_URL = 'http://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 ) ) { |
||
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 ) ) { |
||
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 ) ) { |
||
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 ) { |
||
163 | } |
||
164 | } |
||
165 |