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 / mailchimp.inc.php

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

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