function save() { $datarequested = array(); $i = 0; foreach ($_REQUEST['data'] as $vals) { if ($vals['name'] == 'wysija[user_list][list_id][]') { $datarequested[str_replace('wysija[user_list][list_id][]', 'wysija[user_list][list_id][' . $i . ']', $vals['name'])] = $vals['value']; $i++; } else { $datarequested[$vals['name']] = $vals['value']; } } $data = $this->convertUserData($datarequested); $helperUser = WYSIJA::get('user', 'helper'); if (!$helperUser->checkData($data)) { return false; } $user_id = $helperUser->addSubscriber($data); if ((int) $user_id > 0) { // Handle custom fields if (isset($data['user_field'])) { WJ_FieldHandler::handle_all($data['user_field'], $user_id); } } return true; }
function save() { //get the user_id out of the params passed */ if ($this->_testKeyuser()) { //update the general details */ if (!is_array($_REQUEST['wysija']) || !is_array($_REQUEST['wysija']['user'])) { return false; } $userid = $this->userData['details']['user_id']; unset($_REQUEST['wysija']['user']['user_id']); $model_config = WYSIJA::get('config', 'model'); // we need to call the translation otherwise it will not be loaded and translated $model_config->add_translated_default(); $this->helperUser->uid = $userid; //if the status changed we might need to send notifications */ if ((int) $_REQUEST['wysija']['user']['status'] != (int) $this->userData['details']['status']) { if ($_REQUEST['wysija']['user']['status'] > 0) { if ($model_config->getValue('emails_notified_when_sub')) { $this->helperUser->_notify($this->userData['details']['email']); } } else { if ($model_config->getValue('emails_notified_when_unsub')) { $this->helperUser->_notify($this->userData['details']['email'], false); } } } //check whether the email address has changed if so then we should make sure that the new address doesnt exists already if (isset($_REQUEST['wysija']['user']['email'])) { $_REQUEST['wysija']['user']['email'] = trim($_REQUEST['wysija']['user']['email']); if ($this->userData['details']['email'] != $_REQUEST['wysija']['user']['email']) { $this->modelObj->reset(); $result = $this->modelObj->getOne(false, array('email' => $_REQUEST['wysija']['user']['email'])); if ($result) { $this->error(sprintf(__('Email %1$s already exists.', WYSIJA), $_REQUEST['wysija']['user']['email']), 1); unset($_REQUEST['wysija']['user']['email']); } } } $this->modelObj->update($_REQUEST['wysija']['user'], array('user_id' => $userid)); $id = $userid; $hUser = WYSIJA::get('user', 'helper'); //update the list subscriptions */ //run the unsubscribe process if needed if ((int) $_REQUEST['wysija']['user']['status'] == -1) { $hUser->unsubscribe($id); } //update subscriptions */ $modelUL = WYSIJA::get('user_list', 'model'); $modelUL->backSave = true; /* list of core list */ $modelLIST = WYSIJA::get('list', 'model'); // Using "like" condition in order to force sql query to OR (instead of AND). It'll be incorrct if status contains other values than 0/1. $results = $modelLIST->get(array('list_id'), array('like' => array('is_enabled' => 0, 'is_public' => 0))); $static_listids = array(); foreach ($results as $res) { $static_listids[] = $res['list_id']; } //0 - get current lists of the user $userlists = $modelUL->get(array('list_id', 'unsub_date'), array('user_id' => $id)); $oldlistids = $new_list_ids = array(); foreach ($userlists as $listdata) { $oldlistids[$listdata['list_id']] = $listdata['unsub_date']; } $config = WYSIJA::get('config', 'model'); $dbloptin = $config->getValue('confirm_dbleoptin'); //1 - insert new user_list if (!empty($_POST['wysija']['user_list']['list_id']) && is_array($_POST['wysija']['user_list']['list_id'])) { $modelUL->reset(); $modelUL->update(array('sub_date' => time()), array('user_id' => $id)); foreach ($_POST['wysija']['user_list']['list_id'] as $list_id) { //if the list is not already recorded for the user then we will need to insert it if (!isset($oldlistids[$list_id])) { $modelUL->reset(); $new_list_ids[] = $list_id; $dataul = array('user_id' => $id, 'list_id' => $list_id, 'sub_date' => time()); //if double optin is on then we want to send a confirmation email for newly added subscription if ($dbloptin) { unset($dataul['sub_date']); $modelUL->nohook = true; } $modelUL->insert($dataul); //if the list is recorded already then let's check the status, if it is an unsubed one then we update it } else { if ($oldlistids[$list_id] > 0) { $modelUL->reset(); $modelUL->update(array('unsub_date' => 0, 'sub_date' => time()), array('user_id' => $id, 'list_id' => $list_id)); } //$alreadysubscribelistids[]=$list_id; } } } //if a confirmation email needs to be sent then we send it if ($dbloptin && !empty($new_list_ids)) { $send_confirmation = true; $send_confirmation = apply_filters('mpoet_confirm_new_list_subscriptions_page', $send_confirmation); if ($send_confirmation === true) { $hUser->sendConfirmationEmail($id, true, $new_list_ids); } else { // this case has been made so that when subscribers add themselves to a // list through the edit your subscription form they don't receive a confirmation email they already confirmed. // so they receive also the autorespo,nders correspondign to that list immediately. $helper_user = WYSIJA::get('user', 'helper'); $_REQUEST['wysiconf'] = base64_encode(json_encode($new_list_ids)); $helper_user->confirm_user(); } } // list ids $list_ids = !empty($_POST['wysija']['user_list']['list_id']) ? $_POST['wysija']['user_list']['list_id'] : array(); if (is_array($list_ids) === false) { $list_ids = array(); } $notEqual = array_merge($static_listids, $list_ids); //delete the lists from which you've removed yourself $condiFirst = array('notequal' => array('list_id' => $notEqual), 'equal' => array('user_id' => $id, 'unsub_date' => 0)); $modelUL = WYSIJA::get('user_list', 'model'); $modelUL->update(array('unsub_date' => time()), $condiFirst); $modelUL->reset(); /* Custom Fields. */ if (isset($_POST['wysija']['field'])) { WJ_FieldHandler::handle_all($_POST['wysija']['field'], $id); } $this->notice(__('Newsletter profile has been updated.', WYSIJA)); $this->subscriptions(); //reset post otherwise wordpress will not recognise the post !!! $_POST = array(); } return true; }
/** * function to insert subscribers into wysija * data parameter is a multidimensional array * @param array $data=array( * 'user'=>array('email'=>$myuserEMAIL,'firstname'=>$myuserFIRSTNAME), * 'user_lists'=>array('list_ids'=>array($listid1,$listid2)) * ); * @param boolean $subscribing_from_backend * @return type */ function addSubscriber($data, $subscribing_from_backend = false) { $has_error = false; // 0 - action before any processing call third party services WangGuard for instance if (!$subscribing_from_backend) { $valid_email = apply_filters('wysija_beforeAddSubscriber', true, $data['user']['email']); if (!$valid_email) { $this->error(__('The email is not valid!', WYSIJA), true); $has_error = true; } } // 1-check if email is valid if (!$this->validEmail($data['user']['email'])) { $this->error(__('The email is not valid!', WYSIJA), true); $has_error = true; } // check if lists are specified? if (empty($data['user_list']['list_ids'])) { $this->error(__('You need to select at least one list.', WYSIJA), true); $has_error = true; } // make sure we get all the errors in one shot if ($has_error === true) { return false; } // 2-check if email doesn't exists already $model_user = WYSIJA::get('user', 'model'); $subscriber_exists_already = $model_user->getOne(false, array('email' => trim($data['user']['email']))); $model_config = WYSIJA::get('config', 'model'); $confirm_dbloptin = $model_config->getValue('confirm_dbleoptin'); // message success escaping, striping, setting $message_success = ''; if (isset($data['message_success'])) { $message_success = strip_tags($data['message_success'], '<p><em><span><b><strong><i><h1><h2><h3><a><ul><ol><li><br>'); } else { if (isset($data['success_message'])) { $message_success = strip_tags(nl2br(base64_decode($data['success_message'])), '<p><em><span><b><strong><i><h1><h2><h3><a><ul><ol><li><br>'); } else { if (isset($data['form_id'])) { // we have a form_id parameter so let's fetch the form success message $model_forms = WYSIJA::get('forms', 'model'); $form = $model_forms->getOne(array('data'), array('form_id' => (int) $data['form_id'])); $form_data = unserialize(base64_decode($form['data'])); // if the on_success action is 'message', display message if ($form_data['settings']['on_success'] === 'message') { $message_success = nl2br($form_data['settings']['success_message']); } } } } if ($subscriber_exists_already) { // show a message for that case scenario in the admin panel if ($subscribing_from_backend) { $this->error(str_replace(array('[link]', '[/link]'), array('<a href="admin.php?page=wysija_subscribers&action=edit&id=' . $subscriber_exists_already['user_id'] . '" >', "</a>"), __('Subscriber already exists. [link]Click to edit[/link].', WYSIJA)), true); return false; } $model_user_list = WYSIJA::get('user_list', 'model'); $subscribed_but_require_confirmation = false; if ($subscriber_exists_already['status'] == 1) { $unsubscribed_lists = $model_user_list->get(array('list_id'), array('greater' => array('unsub_date' => 0), 'equal' => array('user_id' => $subscriber_exists_already['user_id']))); $already_unsubscribed_list_ids_formatted = array(); foreach ($unsubscribed_lists as $user_list_detail) { $already_unsubscribed_list_ids_formatted[] = $user_list_detail['list_id']; } foreach ($data['user_list']['list_ids'] as $list_id) { if (in_array($list_id, $already_unsubscribed_list_ids_formatted)) { $subscribed_but_require_confirmation = true; } } } // if the status of the user is either unsubscribed or not confirmed // or he is unsubscribed from one of the list he is trying to subscribe again // we resend him the activation email if ((int) $subscriber_exists_already['status'] < 1 || $subscribed_but_require_confirmation) { $model_user->reset(); $model_user->update(array('status' => 0), array('user_id' => $subscriber_exists_already['user_id'])); $subscribe_to_list = 0; if (!$confirm_dbloptin) { $subscribe_to_list = time(); } $this->addToLists($data['user_list']['list_ids'], $subscriber_exists_already['user_id'], $subscribe_to_list); // this is the double optin case, where we simply send the signup confirmation if ($confirm_dbloptin) { $this->sendConfirmationEmail((object) $subscriber_exists_already, true, $data['user_list']['list_ids']); } else { // this is the single optin case, where we fire the autoresponders directly $lists = $this->getUserLists($subscriber_exists_already['user_id'], $data['user_list']['list_ids']); $this->sendAutoNl($subscriber_exists_already['user_id'], $lists); if ($model_config->getValue('emails_notified') && $model_config->getValue('emails_notified_when_sub')) { // notify the administrators of a new subscribption $this->uid = $subscriber_exists_already['user_id']; if (!$subscribing_from_backend) { $this->_notify($data['user']['email'], true, $data['user_list']['list_ids']); } } } if (!empty($message_success)) { $this->notice($message_success); } return true; } $model_user_list = WYSIJA::get('user_list', 'model'); $already_subscribed_list_ids = $model_user_list->get(array('list_id'), array('greater' => array('sub_date' => 0), 'equal' => array('user_id' => $subscriber_exists_already['user_id']))); $already_subscribed_list_ids_formatted = array(); foreach ($already_subscribed_list_ids as $user_list_detail) { $already_subscribed_list_ids_formatted[] = $user_list_detail['list_id']; } // a confirmation needs to be resend for those lists $list_ids_require_confirmation = array(); foreach ($data['user_list']['list_ids'] as $list_id) { // only the list for which the subscribe request is made and is not already subscribed too willr equire confirmation if (!in_array($list_id, $already_subscribed_list_ids_formatted)) { $list_ids_require_confirmation[] = $list_id; } } // this process require either a confirmation email to be sent or // the autoresponders to be triggerred if (!empty($list_ids_require_confirmation)) { $subscribe_to_list = $subscriber_status = 0; if (isset($data['user']['status'])) { $subscriber_status = $data['user']['status']; } // if double optin is activated and the subscriber status is 1 (subscribed) // or this is single optin, then we directly subscribe the user to the list if ($confirm_dbloptin && $subscriber_status || !$confirm_dbloptin) { $subscribe_to_list = time(); } // we can add the subscribers to the lists passed $this->addToLists($data['user_list']['list_ids'], $subscriber_exists_already['user_id'], $subscribe_to_list); // send a confirmation message when double optin is on if ($confirm_dbloptin) { //if we have lists that are going to be added we send a confirmation email for double optin $this->sendConfirmationEmail((object) $subscriber_exists_already, true, $list_ids_require_confirmation); } else { // send auto nl to single optin which have lists added if (!empty($list_ids_require_confirmation)) { $lists = $this->getUserLists($subscriber_exists_already['user_id'], $data['user_list']['list_ids']); $this->sendAutoNl($subscriber_exists_already['user_id'], $lists); } } if (!empty($message_success)) { $this->notice($message_success); } } else { //no lists need to be added so we can simply return the message $this->notice(__("Oops! You're already subscribed.", WYSIJA)); return true; } return true; } // 3-insert the subscriber with the right status based on optin status $subscriber_data = $data['user']; $subscriber_data['ip'] = $this->getIP(); $model_user->reset(); $user_id = $model_user->insert($subscriber_data); if ((int) $user_id > 0) { // if a form id is specified, let's increment its "subscribed count" if (isset($data['form_id']) && (int) $data['form_id'] > 0) { // check if the form exists $model_forms = WYSIJA::get('forms', 'model'); $form = $model_forms->getOne(array('form_id', 'subscribed'), array('form_id' => (int) $data['form_id'])); if (isset($form['form_id']) && (int) $form['form_id'] === (int) $data['form_id']) { // the form exists so let's increment the "subscribed" count $model_forms->update(array('subscribed' => $form['subscribed'] + 1), array('form_id' => (int) $form['form_id'])); } } // set user profile data if (!empty($data['user_field'])) { WJ_FieldHandler::handle_all($data['user_field'], $user_id); } // display success message if (!empty($message_success)) { $this->notice($message_success); } } else { if ($subscribing_from_backend) { $this->notice(__('Subscriber has not been saved.', WYSIJA)); } else { $this->notice(__('Oops! We could not add you!', WYSIJA)); } return false; } $subscribe_to_list = $subscriber_status = 0; if (isset($data['user']['status'])) { $subscriber_status = $data['user']['status']; } if ($confirm_dbloptin && $subscriber_status || !$confirm_dbloptin) { $subscribe_to_list = time(); } //4-we add the user to the lists $this->addToLists($data['user_list']['list_ids'], $user_id, $subscribe_to_list); // 5-send a confirmation email or add the user to the lists depending on the status $can_send_autoresponders = false; if ($subscriber_status > -1) { if ($confirm_dbloptin) { if ($subscriber_status == 0) { $model_user->reset(); $model_user->getFormat = OBJECT; $receiver = $model_user->getOne(false, array('email' => trim($data['user']['email']))); $this->sendConfirmationEmail($receiver, true, $data['user_list']['list_ids']); } else { //the subscriber status is set to subscribed so we send the auto nl straight away $can_send_autoresponders = true; } } else { // single optin - we send a notification to the admin if settings are set $can_send_autoresponders = true; if ($model_config->getValue('emails_notified') && $model_config->getValue('emails_notified_when_sub')) { $this->uid = $user_id; if (!$subscribing_from_backend) { $this->_notify($data['user']['email'], true, $data['user_list']['list_ids']); } } } // let's send the autoresponders if ($can_send_autoresponders) { $lists = $this->getUserLists($user_id, $data['user_list']['list_ids']); $this->sendAutoNl($user_id, $lists, 'subs-2-nl', $subscribing_from_backend); } } return $user_id; }
function save() { $this->redirectAfterSave = false; $this->requireSecurity(); $helper_user = WYSIJA::get('user', 'helper'); if (isset($_REQUEST['id'])) { $id = $_REQUEST['id']; parent::save(); //run the unsubscribe process if needed if ((int) $_REQUEST['wysija']['user']['status'] == -1) { $helper_user->unsubscribe($id); } /* update subscriptions */ $model_user_list = WYSIJA::get('user_list', 'model'); $model_user_list->backSave = true; /* list of core list */ $model_list = WYSIJA::get('list', 'model'); $results = $model_list->get(array('list_id'), array('is_enabled' => '0')); $core_listids = array(); foreach ($results as $res) { $core_listids[] = $res['list_id']; } //0 - get current lists of the user $userlists = $model_user_list->get(array('list_id', 'unsub_date'), array('user_id' => $id)); $oldlistids = $newlistids = array(); foreach ($userlists as $listdata) { $oldlistids[$listdata['list_id']] = $listdata['unsub_date']; } $model_config = WYSIJA::get('config', 'model'); $dbloptin = $model_config->getValue('confirm_dbleoptin'); //1 - insert new user_list if (isset($_POST['wysija']['user_list']) && $_POST['wysija']['user_list']) { $model_user_list->reset(); $model_user_list->update(array('sub_date' => time()), array('user_id' => $id)); if (!empty($_POST['wysija']['user_list']['list_id'])) { foreach ($_POST['wysija']['user_list']['list_id'] as $list_id) { //if the list is not already recorded for the user then we will need to insert it if (!isset($oldlistids[$list_id])) { $model_user_list->reset(); $newlistids[] = $list_id; $dataul = array('user_id' => $id, 'list_id' => $list_id, 'sub_date' => time()); //if double optin is on and user is unconfirmed or unsubscribed, then we need to set it as unconfirmed subscription if ($dbloptin && (int) $_POST['wysija']['user']['status'] < 1) { unset($dataul['sub_date']); } $model_user_list->insert($dataul); //if the list is recorded already then let's check the status, if it is an unsubed one then we update it } else { if ($oldlistids[$list_id] > 0) { $model_user_list->reset(); $model_user_list->update(array('unsub_date' => 0, 'sub_date' => time()), array('user_id' => $id, 'list_id' => $list_id)); } } } } } else { // if no list is selected we unsubscribe them all $model_user_list->reset(); $model_user_list->update(array('unsub_date' => time(), 'sub_date' => 0), array('user_id' => $id)); } //if a confirmation email needs to be sent then we send it if ($dbloptin && (int) $_POST['wysija']['user']['status'] == 0 && !empty($newlistids)) { $helper_user = WYSIJA::get('user', 'helper'); $helper_user->sendConfirmationEmail($id, true, $newlistids); } if ((int) $_POST['wysija']['user']['status'] == 0 || (int) $_POST['wysija']['user']['status'] == 1) { $model_user_list->reset(); $model_user_list->update(array('unsub_date' => 0, 'sub_date' => time()), array('user_id' => $id, 'list_id' => $core_listids)); } $arrayLists = array(); if (isset($_POST['wysija']['user_list']['list_id'])) { $arrayLists = $_POST['wysija']['user_list']['list_id']; } $notEqual = array_merge($core_listids, $arrayLists); //unsubscribe from lists which exist in the old list but does not exist in the new list $unsubsribe_list = array_diff(array_keys($oldlistids), $arrayLists); if (!empty($unsubsribe_list)) { $model_user_list->reset(); $model_user_list->update(array('unsub_date' => time()), array('user_id' => $id, 'list_id' => $unsubsribe_list)); } $model_user_list->reset(); /* Custom Fields. */ if (isset($_POST['wysija']['field'])) { WJ_FieldHandler::handle_all($_POST['wysija']['field'], $id); } } else { //instead of going through a classic save we should save through the helper $data = $_REQUEST['wysija']; $data['user_list']['list_ids'] = !empty($data['user_list']['list_id']) ? $data['user_list']['list_id'] : array(); unset($data['user_list']['list_id']); $data['message_success'] = __('Subscriber has been saved.', WYSIJA); $id = $helper_user->addSubscriber($data, true); //$id= parent::save(); if (!$id) { $this->viewShow = $this->action = 'add'; $data = array('details' => $_REQUEST['wysija']['user']); return $this->add($data); } } $this->redirect(); return true; }