-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-subscription-genius-api.php
323 lines (299 loc) · 8.82 KB
/
wp-subscription-genius-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
class wp_subscription_genius_api {
const ENDPOINT = "https://api.subscriptiongenius.com/";
protected $api_version = "2";
protected $api_key = "";
protected $api_password = "";
protected $prevent_future = false;
public $errors = array();
public $headers = array();
protected $cache = false; //
protected $customfields = null;
//TODO get subscribers with X issues left
function __construct( $api_key = false, $api_password = false ){
if($api_key !== false){
$this->set_api_key( $api_key );
}
if($api_password !== false){
$this->set_api_password( $api_password );
}
}
function get_api_url($resource = ""){;
return self::ENDPOINT . $this->api_version .'/'. $resource .'/';
}
function setup_auth_header(){
$this->headers = array( 'Authorization' => 'Basic ' . base64_encode($this->get_api_key() .":". $this->get_api_password()) );
}
function get_headers(){
return $this->headers;
}
public function set_api_key( $key ){
$this->api_key = $key;
}
public function get_api_key(){
return $this->api_key;
}
public function set_api_password( $password ){
$this->api_password = $password;
}
public function get_api_password(){
return $this->api_password;
}
function set_cache( $cache_length = 3600){
//valid options are int or bool. True should be assumed to be 60
if(is_int($cache_length) || is_bool($cache_length)){
$this->cache = $cache_length;
return true;
} else {
return false;
}
}
public static function maybe_is_subscription_genius_id( $value ){
if( (!empty( $value ) || !is_null( $value ) || ( false !== $value ) ) && ( intval( $value ) == $value ) ){
return true;
} else {
return false;
}
}
public function maybe_is_subscriber_id( $subscriber_id = ""){
if( self::maybe_is_subscription_genius_id( $subscriber_id ) ){
return $subscriber_id;
} else {
return false;
}
}
public function send_get_request( $resource = "", array $data = array() ){
$this->setup_auth_header();
$data_out = array(
'method' => 'GET',
'compress' => true,
'decompress' => true,
'headers' => $this->get_headers(),
);
$url = add_query_arg( $data, $this->get_api_url( $resource ) );
//TODO: validate url?
if($url !== false){
$result = wp_safe_remote_get( $url, $data_out );
$json = wp_remote_retrieve_body( $result );
unset($this->headers);
return array(
'code' => wp_remote_retrieve_response_code( $result ),
'data' => json_decode( $json, true ),
'json' => $json
);
} else {
unset($this->headers);
//what to do if the url is false
}
}
public function send_post_request( $resource = "", array $data = array(), $query = array() ){
$this->setup_auth_header();
$data_out = array(
'method' => 'POST',
'compress' => true,
'decompress' => true,
'headers' => $this->get_headers(),
'body' => http_build_query( $data,'', '&' ),
);
$url = add_query_arg( $query, $this->get_api_url( $resource ) );
//$url = ( $data, $this->get_api_url( $resource ) );
//TODO: validate url?
if($url !== false){
$result = wp_safe_remote_post( $url, $data_out );
$json = wp_remote_retrieve_body( $result );
unset($this->headers);
return array(
'code' => wp_remote_retrieve_response_code( $result ),
'data' => json_decode( $json, true ),
'json' => $json
);
} else {
unset($this->headers);
//what to do if the url is false
}
}
public function send_put_request( $resource ="", array $data = array() ){
$this->setup_auth_header();
$data_out = array(
'method' => 'PUT',
'compress' => true,
'decompress' => true,
'headers' => $this->get_headers(),
'body' => http_build_query( $data,'', '&' ),
);
$url = $this->get_api_url($resource);
//TODO: validate url?
if($url !== false){
$result = wp_safe_remote_request( $url, $data_out );
$json = wp_remote_retrieve_body( $result );
unset($this->headers);
//TODO Check for wp_error
return array(
'code' => wp_remote_retrieve_response_code( $result ),
'data' => json_decode( $json, true ),
'json' => $json
);
} else {
unset($this->headers);
//what to do if the url is false
}
}
public function send_delete_request( $resource = "" ){
$this->setup_auth_header();
$data_out = array(
'method' => 'DELETE',
'compress' => true,
'decompress' => true,
'headers' => $this->get_headers(),
//'body' => http_build_query( $data,'', '&' ),
);
$url = $this->get_api_url($resource);
if($url !== false){
$result = wp_safe_remote_request( $url, $data_out );
$json = wp_remote_retrieve_body( $result );
return array(
'code' => wp_remote_retrieve_response_code( $result ),
'data' => json_decode( $json, true ),
'json' => $json
);
} else {
unset($this->headers);
}
}
function report_error( $title = "", $message = "", $retry = false, $prevent_future = false, $severity = 'normal'){
/*
severity: array allowed values: low, normal, high, fatal
title: string any
message: string any
retry: bool //tell wordpress cron to retry this whole stack in a few minutes
prevent_future: bool //prevents future api calls on this page, because they depend on this call's success
*/
$this->latest_error = array();
$this->latest_error[$severity][] = array(
'title' => $title,
'message' => $message,
'retry' => (bool) $retry,
'prevent_future' => (bool) $prevent_future,
);
$this->errors[$severity][] = array(
'title' => $title,
'message' => $message,
'retry' => (bool) $retry,
'prevent_future' => (bool) $prevent_future,
);
if($prevent_future === true){
$this->prevent_future = true;
}
}
//@API Documented at:
//http://developer.subscriptiongenius.com/2/index.php?objectID=15&callID=45
function get_subscriber_history( $subscriber_id ){
$subscriber_id = intval( $subscriber_id );
$result = $this->send_get_request( "history", array( 'subscriber_id' => $subscriber_id ) );
if( $this->proceed_if_200( $result, 'create_history' ) ){
return $result['data']['history'];
} else {
return false;
}
}
//@API Documented at:
//http://developer.subscriptiongenius.com/2/index.php?objectID=15&callID=46
function update_history( $history_id, $notes ){
$history_id = intval( $history_id );
$args = array(
'notes' => $notes,
);
$result = $this->send_delete_request( "history/{$history_id}", $args );
if( $this->proceed_if_204( $result, 'update_history' ) ){
return true;
} else {
return false;
}
}
//@API Documented at:
//http://developer.subscriptiongenius.com/2/index.php?objectID=15&callID=47
/*
NOTE: SG API is buggy and returning a 500 error but this should work when their end is fixed.
*/
function delete_history( $history_id ){
$history_id = intval( $history_id );
$result = $this->send_delete_request( "history/{$history_id}" );
print_r($result);
if( $this->proceed_if_204( $result, 'delete_history' ) ){
return true;
} else {
return false;
}
}
//@API Documented at:
//http://developer.subscriptiongenius.com/2/index.php?objectID=3&callID=8
public function get_customfields( $load_fresh = false ){
if( !$load_fresh ){
if(!is_null($this->customfields)){
return $this->customfields;
}
if($this->cache){
$result = get_transient( "wp_sg_api_customfields" );
if($result){
return $result;
}
}
}
$result = $this->send_get_request( "customfields" );
if( $this->proceed_if_200( $result, 'get_customfields' ) ){
if($this->cache){
set_transient( "wp_sg_api_customfields", $result['data'], $this->cache );
}
$this->customfields = $result['data'];
return $result['data'];
} else {
return false;
}
}
//@API Documented at:
//http://developer.subscriptiongenius.com/2/index.php?objectID=3&callID=48
function create_field( $name, $display, $type, $allow_other = false){
$allowed_types = array( 'select', 'checkbox', 'text', 'textrea', 'radio' );
if(!in_array($type, $allowed_types)){
return false;
}
$args = array(
'allow_other' => $allow_other,
'display_as' => $display,
'fieldType' => $type,
'name' => $name,
);
$result = $this->send_post_request( "customfields", $args );
if( $this->proceed_if_200( $result, 'create_field' ) ){
return $result['data']['field_id'];
} else {
return false;
}
}
//Custom Helper Function
function get_custom_field_type( $field, $value ){
$customfield = $this->get_custom_field_by( $field, $value );
if($customfield){
return $customfield['type'];
}
return false;
}
//Custom Helper Function
function get_custom_field_by( $field, $value ){
$fields = array( 'field_id', 'name', 'display_as' );
if(!in_array($field, $fields)){
return false;
}
$customfields = $this->get_customfields();
if($customfields){
foreach($customfields as $customfield){
if($customfield[$field] == $value){
return $customfield;
}
}
return false; //nothing found
} else {
return false;
}
}
?>