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

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

206 lines 8.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 * MailChimp (Old API)
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_o'))
22 {
23 /**
24 * MailChimp (Old API)
25 *
26 * @since 141004
27 * @package s2Member\List_Servers
28 */
29 class c_ws_plugin__s2member_mailchimp_o 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_o|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_o')) // Include the MailChimp API class here.
45 include_once dirname(dirname(__FILE__)).'/externals/mailchimp/Mailchimp-o.php';
46 return new Mailchimp_o($GLOBALS['WS_PLUGIN__']['s2member']['o']['mailchimp_api_key'], TRUE);
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' => implode(',', $_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 if($_mc['api_response'] = $mc_api->{$_mc['api_method']}($_mc['list_id'], $args->email, // See: `http://apidocs.mailchimp.com/` for full details.
108 ($_mc['api_merge_array'] = apply_filters('ws_plugin__s2member_mailchimp_merge_array', $_mc['merge_array'], get_defined_vars())), // Configured merge array above.
109 ($_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).
110 ($_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.
111 ($_mc['api_update_existing'] = apply_filters('ws_plugin__s2member_mailchimp_update_existing', TRUE, get_defined_vars())), // Existing subscribers should be updated with this?
112 ($_mc['api_replace_interests'] = apply_filters('ws_plugin__s2member_mailchimp_replace_interests', TRUE, get_defined_vars())), // Replace interest groups? (only if provided).
113 ($_mc['api_send_welcome'] = apply_filters('ws_plugin__s2member_mailchimp_send_welcome', FALSE, get_defined_vars())))
114 ) $_mc['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below.
115
116 c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', $_mc);
117 }
118 unset($_mc_list, $_mc); // Just a little housekeeping.
119
120 return !empty($success); // If one suceeds.
121 }
122
123 /**
124 * Unsubscribe.
125 *
126 * @since 141004
127 * @package s2Member\List_Servers
128 *
129 * @param array $args Input arguments.
130 *
131 * @return bool True if successful.
132 */
133 public static function unsubscribe($args)
134 {
135 if(!($args = self::validate_args($args)))
136 return FALSE; // Invalid args.
137
138 if(!$args->opt_out) // Double check.
139 return FALSE; // Must say explicitly.
140
141 if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['mailchimp_api_key'])
142 return FALSE; // Not possible.
143
144 if(empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_mailchimp_list_ids']))
145 return FALSE; // No list configured at this level.
146
147 if(!($mc_api = self::mc_api())) return FALSE; // Unable to acquire API instance.
148
149 $mc_level_list_ids = $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_mailchimp_list_ids'];
150
151 extract((array)$args); // Extract the arguments for back compat. w/ filters that relied upon them.
152
153 foreach(preg_split('/['."\r\n\t".';,]+/', $mc_level_list_ids, -1, PREG_SPLIT_NO_EMPTY) as $_mc_list)
154 {
155 $_mc = array(
156 'args' => $args,
157 'function' => __FUNCTION__,
158 'list' => trim($_mc_list),
159 'list_id' => trim($_mc_list),
160 'api_method' => 'listUnsubscribe',
161 'api_properties' => $mc_api
162 );
163 if(!$_mc['list'] || !$_mc['list_id'])
164 continue; // List missing.
165
166 if(strpos($_mc['list'], '::') !== FALSE) // Contains Interest Groups?
167 {
168 list($_mc['list_id'], $_mc['interest_groups_title'], $_mc['interest_groups']) = preg_split('/\:\:/', $_mc['list'], 3);
169
170 if(($_mc['interest_groups_title'] = trim($_mc['interest_groups_title'])))
171 if(($_mc['interest_groups'] = $_mc['interest_groups'] ? preg_split('/\|/', trim($_mc['interest_groups']), -1, PREG_SPLIT_NO_EMPTY) : array()))
172 $_mc['interest_groups'] = array('GROUPINGS' => array(array('name' => $_mc['interest_groups_title'], 'groups' => implode(',', $_mc['interest_groups']))));
173
174 if(!$_mc['list_id']) continue; // List ID is missing now; after parsing interest groups.
175 }
176 if($_mc['api_response'] = $mc_api->{$_mc['api_method']}($_mc['list_id'], $args->email, // See: `http://apidocs.mailchimp.com/`.
177 ($_mc['api_delete_member'] = apply_filters('ws_plugin__s2member_mailchimp_removal_delete_member', FALSE, get_defined_vars())), // Completely delete?
178 ($_mc['api_send_goodbye'] = apply_filters('ws_plugin__s2member_mailchimp_removal_send_goodbye', FALSE, get_defined_vars())), // Send goodbye letter?
179 ($_mc['api_send_notify'] = apply_filters('ws_plugin__s2member_mailchimp_removal_send_notify', FALSE, get_defined_vars())))
180 ) $_mc['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below.
181
182 c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', $_mc);
183 }
184 unset($_mc_list, $_mc); // Just a little housekeeping.
185
186 return !empty($success); // If one suceeds.
187 }
188
189 /**
190 * Transition.
191 *
192 * @since 141004
193 * @package s2Member\List_Servers
194 *
195 * @param array $old_args Input arguments.
196 * @param array $new_args Input arguments.
197 *
198 * @return bool True if successful.
199 */
200 public static function transition($old_args, $new_args)
201 {
202 return self::unsubscribe($old_args) && self::subscribe($new_args);
203 }
204 }
205 }
206