PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / trunk
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions vtrunk
260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 120219 120301 All 187 releases
s2member / src / includes / classes / aweber.inc.php

aweber.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions trunk, at src/includes/classes/aweber.inc.php

319 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * AWeber
5 *
6 * Copyright: © 2009-2011
7 * {@link http://websharks-inc.com/ WebSharks, Inc.}
8 * (coded in the USA)
9 *
10 * Released under the terms of the GNU General Public License.
11 * You should have received a copy of the GNU General Public License,
12 * along with this software. In the main directory, see: /licensing/
13 * If not, see: {@link http://www.gnu.org/licenses/}.
14 *
15 * @since 141004
16 * @package s2Member\List_Servers
17 */
18 if(!defined('WPINC')) // MUST have WordPress.
19 exit('Do not access this file directly.');
20
21 if(!class_exists('c_ws_plugin__s2member_aweber'))
22 {
23 /**
24 * AWeber
25 *
26 * @since 141004
27 * @package s2Member\List_Servers
28 */
29 class c_ws_plugin__s2member_aweber extends c_ws_plugin__s2member_list_server_base
30 {
31 /**
32 * API instance.
33 *
34 * @since 141004
35 * @package s2Member\List_Servers
36 *
37 * @return AWeberAPI|null AWeber API instance.
38 */
39 public static function aw_api()
40 {
41 if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_api_key'])
42 return NULL; // Not possible.
43
44 if(!class_exists('AWeberAPI')) // Include the AWeber API class here.
45 include_once dirname(dirname(__FILE__)).'/externals/aweber/aweber_api.php';
46
47 if(count($key_parts = explode('|', $GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_api_key'])) < 5)
48 return NULL; // It's an invalid API key; i.e., authorization code.
49
50 list($consumerKey, $consumerSecret, $requestToken, $tokenSecret, $verifier) = $key_parts;
51 $internal_api_key_checksum = md5($consumerKey.$consumerSecret.$requestToken.$tokenSecret.$verifier);
52
53 if(count($internal_key_parts = explode('|', $GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_internal_api_key'])) >= 5)
54 list(, , , , $checksum) = $internal_key_parts; // Only need checksum for now.
55
56 if(empty($checksum) || $checksum !== $internal_api_key_checksum)
57 {
58 try // Catch any AWeber exceptions that occur here.
59 {
60 $aw_api = new AWeberAPI($consumerKey, $consumerSecret);
61 $aw_api->user->requestToken = $requestToken;
62 $aw_api->user->tokenSecret = $tokenSecret;
63 $aw_api->user->verifier = $verifier;
64
65 if(!is_array($accessToken = $aw_api->getAccessToken()) || count($accessToken) < 2)
66 return NULL; // Not possible.
67
68 list($accessTokenKey, $accessTokenSecret) = $accessToken;
69 if(!$accessTokenKey || !$accessTokenSecret)
70 return NULL; // Not possible.
71
72 $GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_internal_api_key'] = $internal_api_key
73 = $consumerKey.'|'.$consumerSecret.'|'.$accessTokenKey.'|'.$accessTokenSecret.'|'.$internal_api_key_checksum;
74
75 c_ws_plugin__s2member_menu_pages::update_all_options
76 (array('ws_plugin__s2member_aweber_internal_api_key' => $internal_api_key),
77 TRUE, FALSE, FALSE, FALSE, FALSE);
78 }
79 catch(Exception $exception)
80 {
81 return NULL; // API initialization failure.
82 }
83 }
84 if(count($internal_key_parts = explode('|', $GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_internal_api_key'])) < 5)
85 return NULL; // It's an invalid internal API key. Cannot continue.
86
87 list($consumerKey, $consumerSecret, $accessTokenKey, $accessTokenSecret, $checksum) = $internal_key_parts;
88
89 try // Catch any AWeber exceptions that occur here.
90 {
91 $aw_api = new AWeberAPI($consumerKey, $consumerSecret);
92 $aw_api->___account = $aw_api->getAccount($accessTokenKey, $accessTokenSecret);
93
94 return $aw_api; // AWeberAPI instance.
95 }
96 catch(Exception $exception)
97 {
98 return NULL; // API initialization failure.
99 }
100 }
101
102 /**
103 * Checks a countable obj.
104 *
105 * @since 141004
106 * @package s2Member\List_Servers
107 *
108 * @param Countable $countable Countable obj.
109 *
110 * @return bool True if has `count()` > `0`.
111 */
112 public static function count($countable)
113 {
114 return $countable instanceof Countable && $countable->count();
115 }
116
117 /**
118 * Subscribe.
119 *
120 * @since 141004
121 * @package s2Member\List_Servers
122 *
123 * @param array $args Input arguments.
124 *
125 * @return bool True if successful.
126 */
127 public static function subscribe($args)
128 {
129 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_api_type'] === 'email')
130 return c_ws_plugin__s2member_aweber_e::subscribe($args);
131
132 if(!($args = self::validate_args($args)))
133 return FALSE; // Invalid args.
134
135 if(!$args->opt_in) // Double check.
136 return FALSE; // Must say explicitly.
137
138 if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_api_key'])
139 return FALSE; // Not possible.
140
141 if(empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_aweber_list_ids']))
142 return FALSE; // No list configured at this level.
143
144 if(!($aw_api = self::aw_api()) || !@$aw_api->___account->id)
145 return FALSE; // Unable to acquire API instance.
146
147 $aw_level_list_ids = $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_aweber_list_ids'];
148
149 foreach(preg_split('/['."\r\n\t".'\s;,]+/', $aw_level_list_ids, -1, PREG_SPLIT_NO_EMPTY) as $_aw_list)
150 {
151 $_aw = array(
152 'args' => $args,
153 'function' => __FUNCTION__,
154 'list' => trim($_aw_list),
155 'list_id' => trim($_aw_list),
156 'api_method' => 'listSubscribe'
157 );
158 if(!$_aw['list'] || !$_aw['list_id'])
159 continue; // List missing.
160
161 try // Catch any AWeber exceptions that occur here.
162 {
163 if(self::count($_aw['foundLists'] = $aw_api->___account->lists->find(array('name' => $_aw['list_id']))))
164 if(($_aw['listUrl'] = '/accounts/'.$aw_api->___account->id.'/lists/'.$_aw['foundLists'][0]->id))
165 if(($_aw['list'] = $aw_api->___account->loadFromUrl($_aw['listUrl'])))
166 {
167 $_aw['subscriber_props'] = array(
168 'name' => $args->name,
169 'email' => $args->email,
170 'ip_address' => $args->ip,
171 'ad_tracking' => 's2-'.(is_multisite() && !is_main_site()
172 ? $GLOBALS['current_blog']->domain.$GLOBALS['current_blog']->path
173 : $_SERVER['HTTP_HOST']),
174 'custom_fields' => apply_filters('ws_plugin__s2member_aweber_custom_fields_array', array(), get_defined_vars()),
175 'status' => !$args->double_opt_in ? 'subscribed' : '', // Try to bypass confirmation?
176 );
177 $_aw['subscriber_props']['name'] = substr($_aw['subscriber_props']['name'], 0, 60);
178 $_aw['subscriber_props']['email'] = substr($_aw['subscriber_props']['email'], 0, 50);
179 $_aw['subscriber_props']['ip_address'] = substr($_aw['subscriber_props']['ip_address'], 0, 60);
180 $_aw['subscriber_props']['ad_tracking'] = substr($_aw['subscriber_props']['ad_tracking'], 0, 20);
181
182 if(!filter_var($_aw['subscriber_props']['ip_address'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
183 $_aw['subscriber_props']['ip_address'] = ''; // IPv4 addresses only.
184
185 foreach($_aw['subscriber_props'] as $_key => $_value)
186 if(!$_value && $_value !== FALSE) // Empty?
187 unset($_aw['subscriber_props'][$_key]);
188 unset($_key, $_value); // Housekeeping.
189
190 $_aw['findSubscriber'] = array('email' => $args->email);
191 if(self::count($_aw['foundSubscribers'] = $_aw['list']->subscribers->find($_aw['findSubscriber']))
192 && $_aw['foundSubscribers'][0]->status !== 'unconfirmed' // i.e., `subscribed|unsubscribed`.
193 ) // Cannot modify an `unconfirmed` subscriber.
194 {
195 /** @var AWeberEntry $_existing_subscriber */
196 $_existing_subscriber = $_aw['foundSubscribers'][0];
197 $_existing_subscriber->status = 'subscribed'; // Subscribe.
198
199 foreach($_aw['subscriber_props'] as $_key => $_value)
200 if(in_array($_key, array('name', 'ad_tracking', 'custom_fields'), TRUE))
201 $_existing_subscriber->{$_key} = $_value;
202 unset($_key, $_value); // Housekeeping.
203
204 if($_existing_subscriber->save() && ($_aw['subscriber'] = $_existing_subscriber))
205 $_aw['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below.
206
207 unset($_existing_subscriber); // Housekeeping.
208 }
209 else if(($_aw['subscriber'] = $_aw['list']->subscribers->create($_aw['subscriber_props'])) && @$_aw['subscriber']->id)
210 $_aw['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below.
211 }
212 }
213 catch(Exception $exception)
214 {
215 $_aw['exception'] = $exception;
216 }
217 c_ws_plugin__s2member_utils_logs::log_entry('aweber-api', $_aw);
218 }
219 unset($_aw_list, $_aw); // Just a little housekeeping.
220
221 return !empty($success); // If one suceeds.
222 }
223
224 /**
225 * Unsubscribe.
226 *
227 * @since 141004
228 * @package s2Member\List_Servers
229 *
230 * @param array $args Input arguments.
231 *
232 * @return bool True if successful.
233 */
234 public static function unsubscribe($args)
235 {
236 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_api_type'] === 'email')
237 return c_ws_plugin__s2member_aweber_e::unsubscribe($args);
238
239 if(!($args = self::validate_args($args)))
240 return FALSE; // Invalid args.
241
242 if(!$args->opt_out) // Double check.
243 return FALSE; // Must say explicitly.
244
245 if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_api_key'])
246 return FALSE; // Not possible.
247
248 if(empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_aweber_list_ids']))
249 return FALSE; // No list configured at this level.
250
251 if(!($aw_api = self::aw_api()) || !@$aw_api->___account->id)
252 return FALSE; // Unable to acquire API instance.
253
254 $aw_level_list_ids = $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_aweber_list_ids'];
255
256 foreach(preg_split('/['."\r\n\t".'\s;,]+/', $aw_level_list_ids, -1, PREG_SPLIT_NO_EMPTY) as $_aw_list)
257 {
258 $_aw = array(
259 'args' => $args,
260 'function' => __FUNCTION__,
261 'list' => trim($_aw_list),
262 'list_id' => trim($_aw_list),
263 'api_method' => 'listUnsubscribe'
264 );
265 if(!$_aw['list'] || !$_aw['list_id'])
266 continue; // List missing.
267
268 try // Catch any AWeber exceptions that occur here.
269 {
270 if(self::count($_aw['foundLists'] = $aw_api->___account->lists->find(array('name' => $_aw['list_id']))))
271 if(($_aw['listUrl'] = '/accounts/'.$aw_api->___account->id.'/lists/'.$_aw['foundLists'][0]->id))
272 if(($_aw['list'] = $aw_api->___account->loadFromUrl($_aw['listUrl'])))
273 {
274 $_aw['findSubscriber'] = array('email' => $args->email, 'status' => 'subscribed');
275 if(self::count($_aw['foundSubscribers'] = $_aw['list']->subscribers->find($_aw['findSubscriber'])))
276 {
277 /** @var AWeberEntry $_existing_subscriber */
278 $_existing_subscriber = $_aw['foundSubscribers'][0];
279 $_existing_subscriber->status = 'unsubscribed'; // Unsubscribe.
280
281 if($_existing_subscriber->save() && ($_aw['subscriber'] = $_existing_subscriber))
282 $_aw['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below.
283
284 unset($_existing_subscriber); // Housekeeping.
285 }
286 }
287 }
288 catch(Exception $exception)
289 {
290 $_aw['exception'] = $exception;
291 }
292 c_ws_plugin__s2member_utils_logs::log_entry('aweber-api', $_aw);
293 }
294 unset($_aw_list, $_aw); // Just a little housekeeping.
295
296 return !empty($success); // If one suceeds.
297 }
298
299 /**
300 * Transition.
301 *
302 * @since 141004
303 * @package s2Member\List_Servers
304 *
305 * @param array $old_args Input arguments.
306 * @param array $new_args Input arguments.
307 *
308 * @return bool True if successful.
309 */
310 public static function transition($old_args, $new_args)
311 {
312 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['aweber_api_type'] === 'email')
313 return c_ws_plugin__s2member_aweber_e::transition($old_args, $new_args);
314
315 return self::unsubscribe($old_args) && self::subscribe($new_args);
316 }
317 }
318 }
319