/** * test the various return types of go_syncuser()->config() */ public function test_config() { $triggers = go_syncuser()->config('triggers'); $this->assertFalse(empty($triggers)); $this->assertTrue(isset($triggers['wp_login'])); $this->assertEquals(NULL, go_syncuser()->config('nada')); global $wp_filter; $this->assertTrue(isset($wp_filter['wp_login'])); $this->assertGreaterThan(0, count($wp_filter['wp_login'])); }
/** * Unsubscribe a user from the list(s) passed in. Optionally, this can * be delayed into the cron as well as trigger a full delete of the * user in MailChimp. * * @param object $user A WP_User object. * @param mixed Either a string list id, or an array of list id's * @param boolean $delete Optional delete flag set to FALSE * @param boolean $wait Optional flag to cron action * @param string $action What is calling the unsub * @return boolean Returns the success or failure */ public function unsubscribe($user, $list = NULL, $delete = FALSE, $wait = FALSE, $action = 'unsubscribe') { if (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', 'unsubscribe()'); } // validate the user input $user_in = $user; $user = $this->sanitize_user($user); // make sure we found a user if (empty($user) || is_wp_error($user)) { do_action('go_slog', 'go-mailchimp', __FUNCTION__ . ': No user found for input value ' . var_export($user_in, TRUE)); return FALSE; } $list = $list ? $list : $this->lists(); if (is_array($list)) { /** * We want to unsubscribe the user from multiple lists. Call this * method recursively for each list that was passed in accounting * for ones the user is unsubscribed from. */ $success = FALSE; foreach ($list as $list_id => $list) { // success = FALSE if any of the subscribe() calls fail $success &= $this->unsubscribe($user, $list_id, $delete, $wait); } //END foreach return $success; } //END if if ($wait) { if (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', 'cronified unsubscribe request for user ' . $user->ID); } return $this->cronify($user->ID); } if ($this->unsubscribed($user, $list)) { if (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', __FUNCTION__ . ": The user's email appears to be already unsubscribed from the list '{$list}'"); } $this->save_status($user, $action, __FUNCTION__); return TRUE; } //END if try { $email_struct = array('email' => $user->user_email); $api_result = $this->mc->lists->unsubscribe($list, $email_struct, $delete); } catch (Exception $e) { do_action('go_slog', 'go-mailchimp', __FUNCTION__ . ': An Exception was thrown: ' . $e->getMessage()); return FALSE; } //END catch if (!$api_result) { do_action('go_slog', 'go-mailchimp', __FUNCTION__ . ': Unable to call $list->unsubscribe'); return FALSE; } if (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', 'unsubscribed user ' . $user->ID . ' from list ' . $list); } $this->save_status($user, $action, __FUNCTION__); // sync some subscriber info from MC $this->sync_subscriber_info($user, $list); do_action('go_mailchimp_sync', $user, $list); return $api_result; }
/** * this callback gets invoked when events configured in go-syncuser * are fired. * * @param int $user_id ID of the user who triggered an event * @param string $action the type action triggered. we're only * processing 'update' and 'delete'. */ public function go_syncuser_user($user_id, $action) { if (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', 'go_syncuser_user action invoked for user ' . $user_id); } // get all lists from mailchimp $lists = $this->api()->lists(); if ('update' == $action || 'add' == $action) { // call subscribe() on lists the user is subscribed to to sync // the mailchimp merge vars foreach ($lists as $list) { $membership_info = $this->api()->member($user_id, $list['id']); if ('subscribed' == $membership_info['status']) { $subscribed[] = $list['id']; } } //END foreach $this->api()->subscribe($user_id, $subscribed); } elseif ('delete' == $action) { // unsubscribe from all lists $this->api()->unsubscribe($user_id, $lists); } }
/** * Function used to catch hooks being fired from MailChimp * Goes through on a case basis and assigns to necessary actions */ public function webhook_ajax() { if (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', 'webhook_ajax called'); } // Mailchimp's documentation: http://apidocs.mailchimp.com/webhooks/ // example URL: http://site.org/wp-admin/admin-ajax.php?action=go-mailchimp-webhook&mailchimpwhs=$webhook_secret // $webhook_secret is set in the config for each list independently $list_id = preg_replace('/[^a-zA-Z0-9\\-_]/', '', $_POST['data']['list_id']); // get the info we need to check the secret // from MC: "our best suggestion is to simply include a secret key in the URL your provide and check that GET parameter in your scripts" $lists = $this->core->config('lists'); if (!isset($lists[$list_id]['webhook_secret']) || !$_GET['mailchimpwhs'] == $lists[$list_id]['webhook_secret']) { if (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', 'missing or invalid webhook_secret'); } wp_die(); } switch ($_POST['type']) { case 'unsubscribe': //The api will fail as the user is not subscribed, but will //update the user's status in WP accordingly if ($user = get_user_by('email', sanitize_email($_POST['data']['email']))) { $this->core->api()->unsubscribe($user->ID, $list_id); } elseif (go_syncuser()->debug()) { do_action('go_slog', 'go-mailchimp', 'user with email ' . sanitize_email($_POST['data']['email']) . ' not found'); } break; /* Commenting these out for now, because they not immediately required and I don't want to test them. We'll revisit them in time. case 'subscribe': //The api will fail as the user is subscribed, but will //update the user's status in WP accordingly $this->core->api()->subscribe( sanitize_email( $_POST[ 'data' ][ 'new_email' ] ), $list_id ); break; case 'upemail': if ( $user = get_user_by( 'email', sanitize_email( $_POST[ 'data' ][ 'old_email' ] ) ) ) { wp_update_user( array( 'ID' => $user->ID, 'user_email' => sanitize_email( $_POST[ 'data' ][ 'new_email' ] ) ) ); }//end if break; */ } //END switch wp_die(); }
<?php /* Plugin Name: Gigaom Sync User Version: 1.0 Plugin URI: http://gigaom.com Description: Author: Gigaom Author URI: http://gigaom.com Contributors: wluo, misterbisson Tags: user, action Tested up to: 3.9.1 Stable tag: 3.9.1 License: GNU GPL v2 License URI: http://www.gnu.org/licenses/gpl-2.0.html */ require_once __DIR__ . '/components/class-go-syncuser.php'; require_once __DIR__ . '/components/class-go-syncuser-map.php'; go_syncuser();