| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* List Server integrations. |
| 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 |
* @package s2Member\List_Servers |
| 16 |
* @since 3.5 |
| 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_list_servers')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* List Server integrations. |
| 25 |
* |
| 26 |
* @package s2Member\List_Servers |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_list_servers |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Processes list server integrations. |
| 33 |
* |
| 34 |
* @since 3.5 |
| 35 |
* @package s2Member\List_Servers |
| 36 |
* |
| 37 |
* @param string $role A WP role. |
| 38 |
* @param int|string $level A numeric level. |
| 39 |
* @param string $login Username for the user. |
| 40 |
* @param string $pass Plain text password for the User. |
| 41 |
* @param string $email Email address for the user. |
| 42 |
* @param string $fname First name for the user. |
| 43 |
* @param string $lname Last name for the user. |
| 44 |
* @param string $ip IP address for the user. |
| 45 |
* @param bool $opt_in Defaults to `FALSE`; must be set to `TRUE`. |
| 46 |
* @param bool $double_opt_in Defaults to `TRUE`. Use at your own risk. |
| 47 |
* @param int|string $user_id A WordPress User ID, numeric string or integer. |
| 48 |
* |
| 49 |
* @return bool True if at least one list server is processed successfully. |
| 50 |
*/ |
| 51 |
public static function process_list_servers($role = '', $level = '', |
| 52 |
$login = '', $pass = '', $email = '', $fname = '', $lname = '', $ip = '', |
| 53 |
$opt_in = FALSE, $double_opt_in = TRUE, |
| 54 |
$user_id = 0) |
| 55 |
{ |
| 56 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 57 |
do_action('ws_plugin__s2member_before_process_list_servers', get_defined_vars()); |
| 58 |
unset($__refs, $__v); // Allows vars to be modified by reference. |
| 59 |
|
| 60 |
if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_opt_in']) |
| 61 |
$opt_in = true; // Accept as true; the double opt-in box is null. |
| 62 |
|
| 63 |
if(c_ws_plugin__s2member_list_servers::list_servers_integrated()) |
| 64 |
{ |
| 65 |
$args = get_defined_vars(); // Function args. |
| 66 |
$mailchimp_success = c_ws_plugin__s2member_mailchimp::subscribe($args); |
| 67 |
$getresponse_success = c_ws_plugin__s2member_getresponse::subscribe($args); |
| 68 |
$aweber_success = c_ws_plugin__s2member_aweber::subscribe($args); |
| 69 |
$success = $mailchimp_success || $getresponse_success || $aweber_success; |
| 70 |
|
| 71 |
if($user_id) update_user_option($user_id, 's2member_opt_in', '1'); |
| 72 |
|
| 73 |
do_action('ws_plugin__s2member_during_process_list_servers', get_defined_vars()); |
| 74 |
} |
| 75 |
do_action('ws_plugin__s2member_after_process_list_servers', get_defined_vars()); |
| 76 |
|
| 77 |
return apply_filters('ws_plugin__s2member_process_list_servers', !empty($success), get_defined_vars()); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Process list servers against current user. |
| 82 |
* |
| 83 |
* @since 141004 |
| 84 |
* @package s2Member\List_Servers |
| 85 |
* |
| 86 |
* See {@link process_list_servers()} for further details. |
| 87 |
* |
| 88 |
* @param bool $opt_in Defaults to `FALSE`; must be set to `TRUE`. |
| 89 |
* @param bool $double_opt_in Defaults to `TRUE`. Use at your own risk. |
| 90 |
* @param bool $clean_user_cache Defaults to `TRUE`; i.e., we start from a fresh copy of the current user. |
| 91 |
* |
| 92 |
* @return bool True if at least one list server is processed successfully. |
| 93 |
*/ |
| 94 |
public static function process_list_servers_against_current_user($opt_in = FALSE, $double_opt_in = TRUE, $clean_user_cache = TRUE) |
| 95 |
{ |
| 96 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 97 |
do_action('ws_plugin__s2member_before_process_list_servers_against_current_user', get_defined_vars()); |
| 98 |
unset($__refs, $__v); // Allows vars to be modified by reference. |
| 99 |
|
| 100 |
if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_opt_in']) |
| 101 |
$opt_in = true; // Accept as true; the double opt-in box is null. |
| 102 |
|
| 103 |
if($clean_user_cache) // Start from a fresh user object here? |
| 104 |
{ |
| 105 |
clean_user_cache(get_current_user_id()); |
| 106 |
wp_cache_delete(get_current_user_id(), 'user_meta'); |
| 107 |
$user = new WP_User(get_current_user_id()); |
| 108 |
} |
| 109 |
else $user = wp_get_current_user(); |
| 110 |
|
| 111 |
return self::process_list_servers( |
| 112 |
($role = c_ws_plugin__s2member_user_access::user_access_role($user)), |
| 113 |
($level = c_ws_plugin__s2member_user_access::user_access_level($user)), |
| 114 |
($login = $user->user_login), |
| 115 |
($pass = $user->user_pass), |
| 116 |
($email = $user->user_email), |
| 117 |
($fname = $user->first_name), |
| 118 |
($lname = $user->last_name), |
| 119 |
($ip = c_ws_plugin__s2member_utils_ip::current()), |
| 120 |
($opt_in = $opt_in), |
| 121 |
($double_opt_in = $double_opt_in), |
| 122 |
($user_id = $user->ID) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Processes list server removals. |
| 128 |
* |
| 129 |
* @since 3.5 |
| 130 |
* @package s2Member\List_Servers |
| 131 |
* |
| 132 |
* @param string $role A WP role. |
| 133 |
* @param int|string $level A numeric level. |
| 134 |
* @param string $login Username for the user. |
| 135 |
* @param string $pass Plain text password for the User. |
| 136 |
* @param string $email Email address for the user. |
| 137 |
* @param string $fname First name for the user. |
| 138 |
* @param string $lname Last name for the user. |
| 139 |
* @param string $ip IP address for the user. |
| 140 |
* @param bool $opt_out Defaults to `FALSE`; must be set to `TRUE`. |
| 141 |
* @param int|string $user_id A WordPress User ID, numeric string or integer. |
| 142 |
* |
| 143 |
* @return bool True if at least one list server removal is processed successfully. |
| 144 |
*/ |
| 145 |
public static function process_list_server_removals($role = '', $level = '', |
| 146 |
$login = '', $pass = '', $email = '', $fname = '', $lname = '', $ip = '', |
| 147 |
$opt_out = FALSE, |
| 148 |
$user_id = 0) |
| 149 |
{ |
| 150 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 151 |
do_action('ws_plugin__s2member_before_process_list_server_removals', get_defined_vars()); |
| 152 |
unset($__refs, $__v); // Allows vars to be modified by reference. |
| 153 |
|
| 154 |
if(c_ws_plugin__s2member_list_servers::list_servers_integrated()) |
| 155 |
{ |
| 156 |
$args = get_defined_vars(); // Function args. |
| 157 |
$mailchimp_success = c_ws_plugin__s2member_mailchimp::unsubscribe($args); |
| 158 |
$getresponse_success = c_ws_plugin__s2member_getresponse::unsubscribe($args); |
| 159 |
$aweber_success = c_ws_plugin__s2member_aweber::unsubscribe($args); |
| 160 |
$success = $mailchimp_success || $getresponse_success || $aweber_success; |
| 161 |
|
| 162 |
do_action('ws_plugin__s2member_during_process_list_server_removals', get_defined_vars()); |
| 163 |
|
| 164 |
if($user_id) update_user_option($user_id, 's2member_opt_in', '0'); |
| 165 |
} |
| 166 |
do_action('ws_plugin__s2member_after_process_list_server_removals', get_defined_vars()); |
| 167 |
|
| 168 |
return apply_filters('ws_plugin__s2member_process_list_server_removals', !empty($success), get_defined_vars()); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Process list server removals against current user. |
| 173 |
* |
| 174 |
* See {@link process_list_server_removals()} for further details. |
| 175 |
* |
| 176 |
* @since 141004 |
| 177 |
* @package s2Member\List_Servers |
| 178 |
* |
| 179 |
* @param bool $opt_out Defaults to `FALSE`; must be set to `TRUE`. |
| 180 |
* @param bool $clean_user_cache Defaults to `TRUE`; i.e., we start from a fresh copy of the current user. |
| 181 |
* |
| 182 |
* @return bool True if at least one list server removal is processed successfully. |
| 183 |
*/ |
| 184 |
public static function process_list_server_removals_against_current_user($opt_out = FALSE, $clean_user_cache = TRUE) |
| 185 |
{ |
| 186 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 187 |
do_action('ws_plugin__s2member_before_process_list_server_removals_against_current_user', get_defined_vars()); |
| 188 |
unset($__refs, $__v); // Allows vars to be modified by reference. |
| 189 |
|
| 190 |
if($clean_user_cache) // Start from a fresh user object here? |
| 191 |
{ |
| 192 |
clean_user_cache(get_current_user_id()); |
| 193 |
wp_cache_delete(get_current_user_id(), 'user_meta'); |
| 194 |
$user = new WP_User(get_current_user_id()); |
| 195 |
} |
| 196 |
else $user = wp_get_current_user(); |
| 197 |
|
| 198 |
return self::process_list_server_removals( |
| 199 |
($role = c_ws_plugin__s2member_user_access::user_access_role($user)), |
| 200 |
($level = c_ws_plugin__s2member_user_access::user_access_level($user)), |
| 201 |
($login = $user->user_login), |
| 202 |
($pass = $user->user_pass), |
| 203 |
($email = $user->user_email), |
| 204 |
($fname = $user->first_name), |
| 205 |
($lname = $user->last_name), |
| 206 |
($ip = c_ws_plugin__s2member_utils_ip::current()), |
| 207 |
($opt_out = $opt_out), |
| 208 |
($user_id = $user->ID) |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Listens to Collective EOT/MOD events processed by s2Member. |
| 214 |
* |
| 215 |
* @since 3.5 |
| 216 |
* @package s2Member\List_Servers |
| 217 |
* |
| 218 |
* @attaches-to `add_action('ws_plugin__s2member_during_collective_mods');`. |
| 219 |
* @attaches-to `add_action('ws_plugin__s2member_during_collective_eots');`. |
| 220 |
* |
| 221 |
* @param int|string $user_id Required. A WordPress User ID, numeric string or integer. |
| 222 |
* @param array $vars Required. An array of defined variables passed by the calling hook. |
| 223 |
* @param string $event Required. A specific event that triggered this call from the action hook. |
| 224 |
* @param string $event_spec Required. A specific event specification *(a broader classification)*. |
| 225 |
* @param string $mod_new_role Required if `$event_spec === 'modification'`; but can be empty. User role. |
| 226 |
* @param string $mod_new_user Optional. If `$event_spec === 'modification'`, the new user object with current details. |
| 227 |
* @param string $mod_old_user Optional. If `$event_spec === 'modification'`, the old/previous user obj with old details. |
| 228 |
* |
| 229 |
* @note This is only applicable when `['custom_reg_auto_opt_outs']` contains related Event(s). |
| 230 |
*/ |
| 231 |
public static function auto_process_list_server_removals($user_id, $vars, $event, $event_spec, $mod_new_role = NULL, $mod_new_user = NULL, $mod_old_user = NULL) |
| 232 |
{ |
| 233 |
static $auto_processed = array(); // Static cache. |
| 234 |
|
| 235 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 236 |
do_action('ws_plugin__s2member_before_auto_process_list_server_removals', get_defined_vars()); |
| 237 |
unset($__refs, $__v); // Allows vars to be modified by reference. |
| 238 |
|
| 239 |
if(c_ws_plugin__s2member_list_servers::list_servers_integrated()) |
| 240 |
if($user_id && is_numeric($user_id) && !isset($auto_processed[$user_id])) |
| 241 |
if(is_array($vars) && is_string($event = (string)$event) && is_string($event_spec = (string)$event_spec)) |
| 242 |
if(($custom_reg_auto_op_outs = c_ws_plugin__s2member_utils_strings::wrap_deep($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_auto_opt_outs'], '/^', '$/i'))) |
| 243 |
if(c_ws_plugin__s2member_utils_arrays::in_regex_array($event, $custom_reg_auto_op_outs) || c_ws_plugin__s2member_utils_arrays::in_regex_array($event_spec, $custom_reg_auto_op_outs)) |
| 244 |
if(is_object($dynamic_user = $user_now = new WP_User($user_id)) && $dynamic_user->exists() && !empty($dynamic_user->ID)) |
| 245 |
{ |
| 246 |
$mod_new_role = $event_spec === 'modification' && is_string($mod_new_role) ? $mod_new_role : ''; // Might be empty. |
| 247 |
$mod_new_user = $event_spec === 'modification' && !empty($mod_new_user->ID) && $mod_new_user->ID === $dynamic_user->ID ? $mod_new_user : NULL; |
| 248 |
$mod_old_user = $event_spec === 'modification' && !empty($mod_old_user->ID) && $mod_old_user->ID === $dynamic_user->ID ? $mod_old_user : NULL; |
| 249 |
$dynamic_user = $event_spec === 'modification' && $mod_old_user ? $mod_old_user : $user_now; // Use old user when applicable. |
| 250 |
|
| 251 |
if( // Secondary conditionals. |
| 252 |
|
| 253 |
($event_spec !== 'modification' // Not a modification. |
| 254 |
|
| 255 |
|| ($event_spec === 'modification' // Or it is, with a role change! |
| 256 |
&& $mod_new_role !== c_ws_plugin__s2member_user_access::user_access_role($dynamic_user) |
| 257 |
&& strtotime($dynamic_user->user_registered) < strtotime('-10 seconds') // Hackety hack. |
| 258 |
&& ($event !== 'user-role-change' // Ignore this event, UNLESS it has confirmation. |
| 259 |
|| ($event === 'user-role-change' // An admin has specifically asked for this to occur? |
| 260 |
&& !empty($vars['_p']['ws_plugin__s2member_custom_reg_auto_opt_out_transitions'])))) |
| 261 |
) |
| 262 |
) // Let us proceed now; with list removals at the very least. |
| 263 |
{ |
| 264 |
$auto_processed[$dynamic_user->ID] = -1; // Flag as auto-processed! |
| 265 |
|
| 266 |
$auto_removal_success = c_ws_plugin__s2member_list_servers::process_list_server_removals |
| 267 |
( |
| 268 |
c_ws_plugin__s2member_user_access::user_access_role($dynamic_user), // Old role w/ modifications. |
| 269 |
c_ws_plugin__s2member_user_access::user_access_level($dynamic_user), // Old level w/ modifications. |
| 270 |
$dynamic_user->user_login, '', $dynamic_user->user_email, $dynamic_user->first_name, $dynamic_user->last_name, '', TRUE, $dynamic_user->ID |
| 271 |
); |
| 272 |
if( // Now let's determine if they should be subscribed to the new lists. |
| 273 |
|
| 274 |
$event_spec === 'modification' && $mod_new_role // And they have a role now? |
| 275 |
&& ($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_auto_opt_out_transitions'] === '2' |
| 276 |
|| ($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_auto_opt_out_transitions'] === '1' && $auto_removal_success)) |
| 277 |
|
| 278 |
) // Let us proceed now; we want to subscribe the user to the list they should be on now; based on role/level. |
| 279 |
{ |
| 280 |
$dynamic_user = $event_spec === 'modification' && $mod_new_user ? $mod_new_user : $user_now; // New user; when applicable. |
| 281 |
|
| 282 |
$auto_transition_success = c_ws_plugin__s2member_list_servers::process_list_servers |
| 283 |
( |
| 284 |
$mod_new_role, // Subscribe to lists associated w/ their new role/level. |
| 285 |
c_ws_plugin__s2member_user_access::user_access_role_to_level($mod_new_role), |
| 286 |
$dynamic_user->user_login, '', $dynamic_user->user_email, $dynamic_user->first_name, $dynamic_user->last_name, |
| 287 |
'', TRUE, ($auto_removal_success ? FALSE : TRUE), $dynamic_user->ID |
| 288 |
); |
| 289 |
do_action('ws_plugin__s2member_during_auto_process_list_server_removal_transitions', get_defined_vars()); |
| 290 |
} |
| 291 |
do_action('ws_plugin__s2member_during_auto_process_list_server_removals', get_defined_vars()); |
| 292 |
} |
| 293 |
} |
| 294 |
do_action('ws_plugin__s2member_after_auto_process_list_server_removals', get_defined_vars()); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* List servers have been integrated? |
| 299 |
* |
| 300 |
* @since 3.5 |
| 301 |
* @package s2Member\List_Servers |
| 302 |
* |
| 303 |
* @return bool True if list servers have been integrated. |
| 304 |
*/ |
| 305 |
public static function list_servers_integrated() |
| 306 |
{ |
| 307 |
do_action('ws_plugin__s2member_before_list_servers_integrated', get_defined_vars()); |
| 308 |
|
| 309 |
for($n = 0; $n <= $GLOBALS['WS_PLUGIN__']['s2member']['c']['levels']; $n++ /* Go through each Level; looking for a configured list. */) |
| 310 |
if(!empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_mailchimp_list_ids']) || !empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_getresponse_list_ids']) || !empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_aweber_list_ids'])) |
| 311 |
return apply_filters('ws_plugin__s2member_list_servers_integrated', TRUE, get_defined_vars()); |
| 312 |
|
| 313 |
return apply_filters('ws_plugin__s2member_list_servers_integrated', FALSE, get_defined_vars()); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|