PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / aweber / includes / functions.php

functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.3, at integrations/aweber/includes/functions.php

426 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package AutomatorWP\Integrations\AWeber\Functions
6 * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Get account from AWeber
14 *
15 * @since 1.0.0
16 *
17 * @param stdClass $field
18 *
19 * @return array
20 */
21 function automatorwp_aweber_options_cb_account( $field ) {
22
23 // Setup vars
24 $value = $field->escaped_value;
25 $none_value = 'any';
26 $none_label = __( 'any account', 'automatorwp-mailerlite' );
27 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
28
29 if( ! empty( $value ) ) {
30 if( ! is_array( $value ) ) {
31 $value = array( $value );
32 }
33
34 foreach( $value as $account_id ) {
35
36 // Skip option none
37 if( $account_id === $none_value ) {
38 continue;
39 }
40
41 $options[$account_id] = automatorwp_aweber_get_account_name( $account_id );
42 }
43 }
44
45 return $options;
46
47 }
48
49 /**
50 * Get accounts
51 *
52 * @since 1.0.0
53 *
54 * @return array
55 */
56 function automatorwp_aweber_get_accounts( ) {
57
58 $params = automatorwp_aweber_get_request_parameters( );
59
60 // Bail if the authorization has not been setup from settings
61 if( $params === false ) {
62 return;
63 }
64
65 $options = array();
66 $url = 'https://api.aweber.com/1.0/accounts';
67 $response = wp_remote_get( $url, $params );
68
69 $response = json_decode( wp_remote_retrieve_body( $response ), true );
70
71 foreach( $response['entries'] as $account ) {
72
73 $options[] = array(
74 'id' => $account['id'],
75 'company' => $account['company']
76 );
77
78 }
79
80 return $options;
81 }
82
83 /**
84 * Get the account name
85 *
86 * @since 1.0.0
87 *
88 * @param int $account_id
89 *
90 * @return string
91 */
92 function automatorwp_aweber_get_account_name( $account_id ) {
93
94 $params = automatorwp_aweber_get_request_parameters( );
95
96 // Bail if the authorization has not been setup from settings
97 if( $params === false ) {
98 return;
99 }
100
101 $url = 'https://api.aweber.com/1.0/accounts/' . $account_id;
102 $response = wp_remote_get( $url, $params );
103
104 $status_code = wp_remote_retrieve_response_code( $response );
105
106 if ( $status_code !== 200 ) {
107 return false;
108 }
109
110 $response = json_decode( wp_remote_retrieve_body( $response ), true );
111
112 return $response['company'];
113 }
114
115 /**
116 * Get account from AWeber
117 *
118 * @since 1.0.0
119 *
120 * @param stdClass $field
121 *
122 * @return array
123 */
124 function automatorwp_aweber_options_cb_list( $field ) {
125
126 // Setup vars
127 $value = $field->escaped_value;
128 $none_value = 'any';
129 $none_label = __( 'any account', 'automatorwp-aweber' );
130 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
131
132 $account_id = ct_get_object_meta( $field->object_id, 'account', true );
133
134 if( ! empty( $value ) ) {
135 if( ! is_array( $value ) ) {
136 $value = array( $value );
137 }
138
139 foreach( $value as $list_id ) {
140
141 // Skip option none
142 if( $list_id === $none_value ) {
143 continue;
144 }
145
146 $options[$list_id] = automatorwp_aweber_get_list_name( $account_id, $list_id );
147 }
148 }
149
150 return $options;
151
152 }
153
154 /**
155 * Get Lists
156 *
157 * @since 1.0.0
158 *
159 * @param int $account_id
160 *
161 * @return array
162 */
163 function automatorwp_aweber_get_lists( $account_id ) {
164
165 $params = automatorwp_aweber_get_request_parameters( );
166
167 // Bail if the authorization has not been setup from settings
168 if( $params === false ) {
169 return;
170 }
171
172 $options = array();
173 $url = 'https://api.aweber.com/1.0/accounts/' . $account_id . '/lists';
174 $response = wp_remote_get( $url, $params );
175
176 $response = json_decode( wp_remote_retrieve_body( $response ), true );
177
178 foreach( $response['entries'] as $list ) {
179
180 $options[] = array(
181 'id' => $list['id'],
182 'name' => $list['name']
183 );
184
185 }
186
187 return $options;
188 }
189
190 /**
191 * Get the list name
192 *
193 * @since 1.0.0
194 *
195 * @param int $account_id
196 * @param int $list_id
197 *
198 * @return string
199 */
200 function automatorwp_aweber_get_list_name( $account_id, $list_id ) {
201
202 $params = automatorwp_aweber_get_request_parameters( );
203
204 // Bail if the authorization has not been setup from settings
205 if( $params === false ) {
206 return;
207 }
208
209 $url = 'https://api.aweber.com/1.0/accounts/' . $account_id . '/lists/' . $list_id;
210 $response = wp_remote_get( $url, $params );
211
212 $status_code = wp_remote_retrieve_response_code( $response );
213
214 if ( $status_code !== 200 ) {
215 return false;
216 }
217
218 $response = json_decode( wp_remote_retrieve_body( $response ), true );
219
220 return $response['name'];
221 }
222
223 /**
224 * Add subscriber to AWeber list
225 *
226 * @since 1.0.0
227 *
228 * @param array $subscriber The subscriber data
229 * @param int $account_id
230 * @param int $list_id
231 *
232 * @return array
233 */
234 function automatorwp_aweber_add_subscriber( $subscriber, $account_id, $list_id ) {
235
236 $params = automatorwp_aweber_get_request_parameters( );
237
238 // Bail if the authorization has not been setup from settings
239 if( $params === false ) {
240 return;
241 }
242
243 $url = 'https://api.aweber.com/1.0/accounts/' . $account_id . '/lists/' . $list_id . '/subscribers';
244
245 $params['body'] = json_encode( $subscriber );
246
247 $response = wp_remote_post( $url, $params );
248
249 $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
250
251 return $response_body;
252
253 }
254
255 /**
256 * Add/Remove tags to subscriber
257 *
258 * @since 1.0.0
259 *
260 * @param array $subscriber_tags The tags to add
261 * @param int $account_id
262 * @param int $list_id
263 * @param string $email
264 *
265 * @return array
266 */
267 function automatorwp_aweber_handle_tag_subscriber( $subscriber_tags, $account_id, $list_id, $email ) {
268
269 $params = automatorwp_aweber_get_request_parameters( );
270
271 // Bail if the authorization has not been setup from settings
272 if( $params === false ) {
273 return;
274 }
275
276 $url = 'https://api.aweber.com/1.0/accounts/' . $account_id . '/lists/' . $list_id . '/subscribers?subscriber_email=' . $email;
277
278 $params['method'] = 'PATCH';
279 $params['body'] = json_encode( $subscriber_tags );
280
281 $response = wp_remote_request( $url, $params );
282
283 $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
284
285 return $response_body;
286
287 }
288
289 /**
290 * Get the request parameters
291 *
292 * @since 1.0.0
293 *
294 * @param string $platform
295 *
296 * @return array|false
297 */
298 function automatorwp_aweber_get_request_parameters( ) {
299
300 $auth = get_option( 'automatorwp_aweber_auth' );
301
302 if( ! is_array( $auth ) ) {
303 return false;
304 }
305
306 return array(
307 'user-agent' => 'AutomatorWP; ' . home_url(),
308 'timeout' => 120,
309 'httpversion' => '1.1',
310 'headers' => array(
311 'Authorization' => 'Bearer ' . $auth['access_token'],
312 'Content-Type' => 'application/json',
313 'Accept' => 'application/json'
314 )
315 );
316 }
317
318 /**
319 * Get the request parameters
320 *
321 * @since 1.0.0
322 *
323 * @param string $platform
324 *
325 * @return string|false|WP_Error
326 */
327 function automatorwp_aweber_refresh_token( ) {
328
329 $client_id = automatorwp_aweber_get_option( 'client_id', '' );
330 $client_secret = automatorwp_aweber_get_option( 'client_secret', '' );
331
332 if( empty( $client_id ) || empty( $client_secret ) ) {
333 return false;
334 }
335
336 $auth = get_option( 'automatorwp_aweber_auth', false );
337
338 if( ! is_array( $auth ) ) {
339 return false;
340 }
341
342 $params = array(
343 'headers' => array(
344 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
345 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $client_secret ),
346 'Accept' => 'application/json',
347 ),
348 'body' => array(
349 'grant_type' => 'refresh_token',
350 'client_id' => $client_id,
351 'client_secret' => $client_secret,
352 'refresh_token' => $auth['refresh_token'],
353 )
354 );
355
356 $response = wp_remote_post( 'https://auth.aweber.com/oauth2/token', $params );
357
358 if( is_wp_error( $response ) ) {
359 return false;
360 }
361
362 $response_code = wp_remote_retrieve_response_code( $response );
363
364 if ( $response_code !== 200 ) {
365 return false;
366 }
367
368 $body = json_decode( wp_remote_retrieve_body( $response ) );
369
370 $ref_token = $auth['refresh_token'];
371
372 $auth = array(
373 'access_token' => $body->access_token,
374 'refresh_token' => $ref_token,
375 'token_type' => $body->token_type,
376 'expires_in' => $body->expires_in,
377 //'scope' => $body->scope,
378 );
379
380 // Update the access and refresh tokens
381 update_option( 'automatorwp_aweber_auth', $auth );
382
383 return $body->access_token;
384
385 }
386
387 /**
388 * Filters the HTTP API response immediately before the response is returned.
389 *
390 * @since 1.0.0
391 *
392 * @param array $response HTTP response.
393 * @param array $parsed_args HTTP request arguments.
394 * @param string $url The request URL.
395 *
396 * @return array
397 */
398 function automatorwp_aweber_maybe_refresh_token( $response, $args, $url ) {
399
400 // Ensure to only run this check to on Aweber request
401 if( strpos( $url, 'api.aweber.com' ) !== false ) {
402
403 $code = wp_remote_retrieve_response_code( $response );
404
405 if( $code === 401 ) {
406
407 $access_token = automatorwp_aweber_refresh_token( );
408
409 // Send again the request if token gets refreshed successfully
410 if( $access_token ) {
411
412 $args['headers']['Authorization'] = 'Bearer ' . $access_token;
413
414 $response = wp_remote_request( $url, $args );
415
416 }
417
418 }
419
420 }
421
422 return $response;
423
424 }
425 add_filter( 'http_response', 'automatorwp_aweber_maybe_refresh_token', 10, 3 );
426