/** * * TODO: Force re-sending double opt-in email by deleting pending subscribers from list first. * * Sends a subscription request to the MailChimp API * * @param string $list_id The list id to subscribe to * @param string $email_address The email address to subscribe * @param array $args * @param boolean $update_existing Update information if this email is already on list? * @param boolean $replace_interests Replace interest groupings, only if update_existing is true. * * @return object */ public function list_subscribe($list_id, $email_address, array $args = array(), $update_existing = false, $replace_interests = true) { $this->reset_error(); $default_args = array('status' => 'pending', 'email_address' => $email_address, 'interests' => array(), 'merge_fields' => array()); $already_on_list = false; // setup default args $args = $args + $default_args; // first, check if subscriber is already on the given list try { $existing_member_data = $this->api->get_list_member($list_id, $email_address); if ($existing_member_data->status === 'subscribed') { $already_on_list = true; // if we're not supposed to update, bail. if (!$update_existing) { $this->error_code = 214; $this->error_message = 'That subscriber already exists.'; return null; } $args['status'] = 'subscribed'; // this key only exists if list actually has interests if (isset($existing_member_data->interests)) { $existing_interests = (array) $existing_member_data->interests; // if replace, assume all existing interests disabled if ($replace_interests) { $existing_interests = array_fill_keys(array_keys($existing_interests), false); } $args['interests'] = $args['interests'] + $existing_interests; } } else { // delete list member so we can re-add it... $this->api->delete_list_member($list_id, $email_address); } } catch (MC4WP_API_Resource_Not_Found_Exception $e) { // subscriber does not exist (not an issue in this case) } catch (MC4WP_API_Exception $e) { // other errors. $this->error_code = $e->getCode(); $this->error_message = $e; return null; } try { $data = $this->api->add_list_member($list_id, $args); } catch (MC4WP_API_Exception $e) { $this->error_code = $e->getCode(); $this->error_message = $e; return null; } $data->was_already_on_list = $already_on_list; return $data; }