PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260927
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260927
260927 260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 All 190 releases
s2member / src / includes / classes / mailchimp.inc.php

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

223 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * MailChimp
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_mailchimp'))
22 {
23 /**
24 * MailChimp
25 *
26 * @since 141004
27 * @package s2Member\List_Servers
28 */
29 class c_ws_plugin__s2member_mailchimp 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 Mailchimp|null MailChimp API instance.
38 */
39 public static function mc_api()
40 {
41 if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['mailchimp_api_key'])
42 return NULL; // Not possible.
43
44 if(!class_exists('Mailchimp')) // Include the MailChimp API class here.
45 include_once dirname(dirname(__FILE__)).'/externals/mailchimp/Mailchimp.php';
46
47 //260920.2015 MailchimpV3 expects a numeric timeout, which is passed directly to PHP's HTTP stream context.
48 return new Mailchimp($GLOBALS['WS_PLUGIN__']['s2member']['o']['mailchimp_api_key'], 30);
49 }
50
51 /**
52 * Subscribe.
53 *
54 * @since 141004
55 * @package s2Member\List_Servers
56 *
57 * @param array $args Input arguments.
58 *
59 * @return bool True if successful.
60 */
61 public static function subscribe($args)
62 {
63 if(!($args = self::validate_args($args)))
64 return FALSE; // Invalid args.
65
66 if(!$args->opt_in) // Double check.
67 return FALSE; // Must say explicitly.
68
69 if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['mailchimp_api_key'])
70 return FALSE; // Not possible.
71
72 if(empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_mailchimp_list_ids']))
73 return FALSE; // No list configured at this level.
74
75 if(!($mc_api = self::mc_api())) return FALSE; // Unable to acquire API instance.
76
77 $mc_level_list_ids = $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_mailchimp_list_ids'];
78
79 extract((array)$args); // Extract the arguments for back compat. w/ filters that relied upon them.
80
81 foreach(preg_split('/['."\r\n\t".';,]+/', $mc_level_list_ids, -1, PREG_SPLIT_NO_EMPTY) as $_mc_list)
82 {
83 $_mc = array(
84 'args' => $args,
85 'function' => __FUNCTION__,
86 'list' => trim($_mc_list),
87 'list_id' => trim($_mc_list),
88 'api_method' => 'listSubscribe',
89 'api_properties' => $mc_api
90 );
91 if(!$_mc['list'] || !$_mc['list_id'])
92 continue; // List missing.
93
94 if(strpos($_mc['list'], '::') !== FALSE) // Contains Interest Groups?
95 {
96 list($_mc['list_id'], $_mc['interest_groups_title'], $_mc['interest_groups']) = preg_split('/\:\:/', $_mc['list'], 3);
97
98 if(($_mc['interest_groups_title'] = trim($_mc['interest_groups_title'])))
99 if(($_mc['interest_groups'] = $_mc['interest_groups'] ? preg_split('/\|/', trim($_mc['interest_groups']), -1, PREG_SPLIT_NO_EMPTY) : array()))
100 $_mc['interest_groups'] = array('GROUPINGS' => array(array('name' => $_mc['interest_groups_title'], 'groups' => $_mc['interest_groups'])));
101
102 if(!$_mc['list_id']) continue; // List ID is missing now; after parsing interest groups.
103 }
104 $_mc['merge_array'] = array('MERGE1' => $args->fname, 'MERGE2' => $args->lname, 'OPTIN_IP' => $args->ip, 'OPTIN_TIME' => date('Y-m-d H:i:s'));
105 $_mc['merge_array'] = !empty($_mc['interest_groups']) ? array_merge($_mc['merge_array'], $_mc['interest_groups']) : $_mc['merge_array'];
106 $_mc['merge_array'] = apply_filters('ws_plugin__s2member_mailchimp_array', $_mc['merge_array'], get_defined_vars()); // Deprecated!
107 // Filter: `ws_plugin__s2member_mailchimp_array` deprecated in v110523. Please use Filter: `ws_plugin__s2member_mailchimp_merge_array`.
108
109 try // Catch any Mailchimp exceptions that occur here.
110 {
111 if(($_mc['api_response'] = $mc_api->lists->subscribe($_mc['list_id'], array('email' => $args->email), // See: `http://apidocs.mailchimp.com/` for full details.
112 ($_mc['api_merge_array'] = apply_filters('ws_plugin__s2member_mailchimp_merge_array', $_mc['merge_array'], get_defined_vars())), // Configured merge array above.
113 ($_mc['api_email_type'] = apply_filters('ws_plugin__s2member_mailchimp_email_type', 'html', get_defined_vars())), // Type of email to receive (i.e., html,text,mobile).
114 ($_mc['api_double_optin'] = apply_filters('ws_plugin__s2member_mailchimp_double_optin', $args->double_opt_in, get_defined_vars())), // Abuse of this may cause account suspension.
115 ($_mc['api_update_existing'] = apply_filters('ws_plugin__s2member_mailchimp_update_existing', TRUE, get_defined_vars())), // Existing subscribers should be updated with this?
116 ($_mc['api_replace_interests'] = apply_filters('ws_plugin__s2member_mailchimp_replace_interests', TRUE, get_defined_vars())), // Replace interest groups? (only if provided).
117 ($_mc['api_send_welcome'] = apply_filters('ws_plugin__s2member_mailchimp_send_welcome', FALSE, get_defined_vars()))))
118 && !empty($_mc['api_response']['email'])
119 ) $_mc['api_success'] = $success = TRUE;
120 }
121 catch(Exception $exception)
122 {
123 $_mc['exception'] = $exception;
124 }
125 c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', $_mc);
126 }
127 unset($_mc_list, $_mc); // Just a little housekeeping.
128
129 return !empty($success); // If one suceeds.
130 }
131
132 /**
133 * Unsubscribe.
134 *
135 * @since 141004
136 * @package s2Member\List_Servers
137 *
138 * @param array $args Input arguments.
139 *
140 * @return bool True if successful.
141 */
142 public static function unsubscribe($args)
143 {
144 if(!($args = self::validate_args($args)))
145 return FALSE; // Invalid args.
146
147 if(!$args->opt_out) // Double check.
148 return FALSE; // Must say explicitly.
149
150 if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['mailchimp_api_key'])
151 return FALSE; // Not possible.
152
153 if(empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_mailchimp_list_ids']))
154 return FALSE; // No list configured at this level.
155
156 if(!($mc_api = self::mc_api())) return FALSE; // Unable to acquire API instance.
157
158 $mc_level_list_ids = $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_mailchimp_list_ids'];
159
160 extract((array)$args); // Extract the arguments for back compat. w/ filters that relied upon them.
161
162 foreach(preg_split('/['."\r\n\t".';,]+/', $mc_level_list_ids, -1, PREG_SPLIT_NO_EMPTY) as $_mc_list)
163 {
164 $_mc = array(
165 'args' => $args,
166 'function' => __FUNCTION__,
167 'list' => trim($_mc_list),
168 'list_id' => trim($_mc_list),
169 'api_method' => 'listUnsubscribe',
170 'api_properties' => $mc_api
171 );
172 if(!$_mc['list'] || !$_mc['list_id'])
173 continue; // List missing.
174
175 if(strpos($_mc['list'], '::') !== FALSE) // Contains Interest Groups?
176 {
177 list($_mc['list_id'], $_mc['interest_groups_title'], $_mc['interest_groups']) = preg_split('/\:\:/', $_mc['list'], 3);
178
179 if(($_mc['interest_groups_title'] = trim($_mc['interest_groups_title'])))
180 if(($_mc['interest_groups'] = $_mc['interest_groups'] ? preg_split('/\|/', trim($_mc['interest_groups']), -1, PREG_SPLIT_NO_EMPTY) : array()))
181 $_mc['interest_groups'] = array('GROUPINGS' => array(array('name' => $_mc['interest_groups_title'], 'groups' => $_mc['interest_groups'])));
182
183 if(!$_mc['list_id']) continue; // List ID is missing now; after parsing interest groups.
184 }
185 try // Catch any Mailchimp exceptions that occur here.
186 {
187 if(($_mc['api_response'] = $mc_api->lists->unsubscribe($_mc['list_id'], array('email' => $args->email), // See: `http://apidocs.mailchimp.com/`.
188 ($_mc['api_delete_member'] = apply_filters('ws_plugin__s2member_mailchimp_removal_delete_member', FALSE, get_defined_vars())), // Completely delete?
189 ($_mc['api_send_goodbye'] = apply_filters('ws_plugin__s2member_mailchimp_removal_send_goodbye', FALSE, get_defined_vars())), // Send goodbye letter?
190 ($_mc['api_send_notify'] = apply_filters('ws_plugin__s2member_mailchimp_removal_send_notify', FALSE, get_defined_vars())), // Send notification?
191 (!empty($_mc['interest_groups']) ? $_mc['interest_groups'] : []) //230714 Interest groups
192 )) && !empty($_mc['api_response']['complete'])
193 ) $_mc['api_success'] = $success = TRUE;
194 }
195 catch(Exception $exception)
196 {
197 $_mc['exception'] = $exception;
198 }
199 c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', $_mc);
200 }
201 unset($_mc_list, $_mc); // Just a little housekeeping.
202
203 return !empty($success); // If one suceeds.
204 }
205
206 /**
207 * Transition.
208 *
209 * @since 141004
210 * @package s2Member\List_Servers
211 *
212 * @param array $old_args Input arguments.
213 * @param array $new_args Input arguments.
214 *
215 * @return bool True if successful.
216 */
217 public static function transition($old_args, $new_args)
218 {
219 return self::unsubscribe($old_args) && self::subscribe($new_args);
220 }
221 }
222 }
223