예제 #1
0
 public static function sendNotifications($post_id, $form_id, $notificationsToSent)
 {
     // custom action hooks here, for 3rd-party integration
     //do_action('cred_before_send_notifications_'.$form_id, $post_id, $form_id, $notificationsToSent);
     //do_action('cred_before_send_notifications', $post_id, $form_id, $notificationsToSent);
     // get Mailer
     $mailer = CRED_Loader::get('CLASS/Mail_Handler');
     // get current user
     $user = self::getCurrentUserData();
     $is_user_form = self::get_form_type($form_id) == CRED_USER_FORMS_CUSTOM_POST_NAME;
     // get Model
     $model = $is_user_form ? CRED_Loader::get('MODEL/UserForms') : CRED_Loader::get('MODEL/Forms');
     //user created/updated
     $the_user = $is_user_form ? get_userdata($post_id)->data : null;
     // get some data for placeholders
     $form_post = get_post($form_id);
     $form_title = $form_post ? $form_post->post_title : '';
     $link = get_permalink($post_id);
     $title = get_the_title($post_id);
     $admin_edit_link = CRED_CRED::getPostAdminEditLink($post_id);
     //get_edit_post_link( $post_id );
     //$date=date('d/m/Y H:i:s');
     $date = date('Y-m-d H:i:s', current_time('timestamp'));
     // placeholder codes, allow to add custom
     $data_subject = apply_filters('cred_subject_notification_codes', array('%%USER_USERID%%' => isset($the_user) && isset($the_user->ID) ? $the_user->ID : '', '%%USER_EMAIL%%' => isset($the_user) && isset($the_user->user_email) ? $the_user->user_email : '', '%%USER_USERNAME%%' => isset(StaticClass::$_username_generated) ? StaticClass::$_username_generated : '', '%%USER_PASSWORD%%' => isset(StaticClass::$_password_generated) ? StaticClass::$_password_generated : '', '%%USER_NICKNAME%%' => isset(StaticClass::$_nickname_generated) ? StaticClass::$_nickname_generated : '', '%%USER_LOGIN_NAME%%' => $user->login, '%%USER_DISPLAY_NAME%%' => $user->display_name, '%%POST_ID%%' => $post_id, '%%POST_TITLE%%' => $title, '%%FORM_NAME%%' => $form_title, '%%DATE_TIME%%' => $date), $form_id, $post_id);
     // placeholder codes, allow to add custom
     $data_body = apply_filters('cred_body_notification_codes', array('%%USER_USERID%%' => isset($the_user) && isset($the_user->ID) ? $the_user->ID : '', '%%USER_EMAIL%%' => isset($the_user) && isset($the_user->user_email) ? $the_user->user_email : '', '%%USER_USERNAME%%' => isset(StaticClass::$_username_generated) ? StaticClass::$_username_generated : '', '%%USER_PASSWORD%%' => isset(StaticClass::$_password_generated) ? StaticClass::$_password_generated : '', '%%USER_NICKNAME%%' => isset(StaticClass::$_nickname_generated) ? StaticClass::$_nickname_generated : '', '%%USER_LOGIN_NAME%%' => $user->login, '%%USER_DISPLAY_NAME%%' => $user->display_name, '%%POST_ID%%' => $post_id, '%%POST_TITLE%%' => $title, '%%POST_LINK%%' => $link, '%%POST_ADMIN_LINK%%' => $admin_edit_link, '%%FORM_NAME%%' => $form_title, '%%DATE_TIME%%' => $date), $form_id, $post_id);
     //cred_log(array($post_id, $form_id, $data_subject, $data_body, $notificationsToSent));
     foreach ($notificationsToSent as $notification) {
         // bypass if nothing
         if (!$notification || empty($notification) || !(isset($notification['to']['type']) || isset($notification['to']['author']))) {
             continue;
         }
         // reset mail handler
         $mailer->reset();
         $mailer->setHTML(true, false);
         $recipients = array();
         if (isset($notification['to']['author']) && 'author' == $notification['to']['author']) {
             $author_post_id = isset($_POST['form_' . $form_id . '_referrer_post_id']) ? $_POST['form_' . $form_id . '_referrer_post_id'] : 0;
             if (0 == $author_post_id && $post_id) {
                 $mypost = get_post($post_id);
                 $author_id = $mypost->post_author;
             } else {
                 $mypost = get_post($author_post_id);
                 $author_id = $user->ID;
                 if (!isset($author_id)) {
                     $author_id = $mypost->post_author;
                 }
             }
             if ($author_id) {
                 $_to_type = 'to';
                 $user_info = get_userdata($author_id);
                 $_addr_name = isset($user_info) && isset($user_info->user_firstname) && !empty($user_info->user_firstname) ? $user_info->user_firstname : false;
                 $_addr_lastname = isset($user_info) && isset($user_info->user_lasttname) && !empty($user_info->user_lasttname) ? $user_info->user_lastname : false;
                 $_addr = $user_info->user_email;
                 if (isset($_addr)) {
                     $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname);
                 }
             }
         }
         // parse Notification Fields
         if (!isset($notification['to']['type'])) {
             $notification['to']['type'] = array();
         }
         if (!is_array($notification['to']['type'])) {
             $notification['to']['type'] = (array) $notification['to']['type'];
         }
         // notification to a mail field (which is saved as post meta)
         if (in_array('mail_field', $notification['to']['type']) && isset($notification['to']['mail_field']['address_field']) && !empty($notification['to']['mail_field']['address_field'])) {
             $_to_type = 'to';
             $_addr = false;
             $_addr_name = false;
             $_addr_lastname = false;
             if ($is_user_form) {
                 $_addr = $the_user->user_email;
             } else {
                 $_addr = $model->getPostMeta($post_id, $notification['to']['mail_field']['address_field']);
             }
             if (isset($notification['to']['mail_field']['to_type']) && in_array($notification['to']['mail_field']['to_type'], array('to', 'cc', 'bcc'))) {
                 $_to_type = $notification['to']['mail_field']['to_type'];
             }
             if (isset($notification['to']['mail_field']['name_field']) && !empty($notification['to']['mail_field']['name_field']) && '###none###' != $notification['to']['mail_field']['name_field']) {
                 $_addr_name = $is_user_form ? $model->getUserMeta($post_id, $notification['to']['mail_field']['name_field']) : $model->getPostMeta($post_id, $notification['to']['mail_field']['name_field']);
             }
             if (isset($notification['to']['mail_field']['lastname_field']) && !empty($notification['to']['mail_field']['lastname_field']) && '###none###' != $notification['to']['mail_field']['lastname_field']) {
                 $_addr_lastname = $is_user_form ? $model->getUserMeta($post_id, $notification['to']['mail_field']['lastname_field']) : $model->getPostMeta($post_id, $notification['to']['mail_field']['lastname_field']);
             }
             // add to recipients
             $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname);
         }
         // notification to an exisiting wp user
         if (in_array('wp_user', $notification['to']['type'])) {
             $_to_type = 'to';
             $_addr = false;
             $_addr_name = false;
             $_addr_lastname = false;
             if (isset($notification['to']['wp_user']['to_type']) && in_array($notification['to']['wp_user']['to_type'], array('to', 'cc', 'bcc'))) {
                 $_to_type = $notification['to']['wp_user']['to_type'];
             }
             $_addr = $notification['to']['wp_user']['user'];
             $user_id = email_exists($_addr);
             if ($user_id) {
                 $user_info = get_userdata($user_id);
                 $_addr_name = isset($user_info->user_firstname) && !empty($user_info->user_firstname) ? $user_info->user_firstname : false;
                 $_addr_lastname = isset($user_info->user_lasttname) && !empty($user_info->user_lasttname) ? $user_info->user_lastname : false;
                 // add to recipients
                 $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname);
             }
         }
         // notification to an exisiting wp user
         if (in_array('user_id_field', $notification['to']['type'])) {
             $_to_type = 'to';
             $_addr = false;
             $_addr_name = false;
             $_addr_lastname = false;
             if (isset($notification['to']['user_id_field']['to_type']) && in_array($notification['to']['user_id_field']['to_type'], array('to', 'cc', 'bcc'))) {
                 $_to_type = $notification['to']['user_id_field']['to_type'];
             }
             //$user_id = $is_user_form ? @trim($model->getUserMeta($post_id, $notification['to']['user_id_field']['field_name'])) : @trim($model->getPostMeta($post_id, $notification['to']['user_id_field']['field_name']));
             $user_id = $is_user_form ? $post_id : @trim($model->getPostMeta($post_id, $notification['to']['user_id_field']['field_name']));
             if ($user_id) {
                 $user_info = get_userdata($user_id);
                 if ($user_info) {
                     $_addr = isset($user_info->user_email) && !empty($user_info->user_email) ? $user_info->user_email : false;
                     $_addr_name = isset($user_info->user_firstname) && !empty($user_info->user_firstname) ? $user_info->user_firstname : false;
                     $_addr_lastname = isset($user_info->user_lasttname) && !empty($user_info->user_lasttname) ? $user_info->user_lastname : false;
                     // add to recipients
                     $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname);
                 }
             }
         }
         // notification to specific recipients
         if (in_array('specific_mail', $notification['to']['type']) && isset($notification['to']['specific_mail']['address'])) {
             $tmp = explode(',', $notification['to']['specific_mail']['address']);
             foreach ($tmp as $aa) {
                 $recipients[] = array('address' => $aa, 'to' => false, 'name' => false, 'lastname' => false);
             }
             unset($tmp);
         }
         // add custom recipients by 3rd-party
         //cred_log(array('cred_notification_recipients', $recipients, $notification, $form_id, $post_id));
         //$recipients=apply_filters('cred_notification_recipients', $recipients, array('form_id'=>$form_id, 'post_id'=>$post_id, 'notification'=>$notification));
         $recipients = apply_filters('cred_notification_recipients', $recipients, $notification, $form_id, $post_id);
         if (!$recipients || empty($recipients)) {
             continue;
         }
         // build recipients
         foreach ($recipients as $ii => $recipient) {
             // nowhere to send, bypass
             if (!isset($recipient['address']) || !$recipient['address']) {
                 unset($recipients[$ii]);
                 continue;
             }
             if (false === $recipient['to']) {
                 // this is already formatted
                 $recipients[$ii] = $recipient['address'];
                 continue;
             }
             $tmp = '';
             $tmp .= $recipient['to'] . ': ';
             $tmp2 = array();
             if ($recipient['name']) {
                 $tmp2[] = $recipient['name'];
             }
             if ($recipient['lastname']) {
                 $tmp2[] = $recipient['lastname'];
             }
             if (!empty($tmp2)) {
                 $tmp .= implode(' ', $tmp2) . ' <' . $recipient['address'] . '>';
             } else {
                 $tmp .= $recipient['address'];
             }
             $recipients[$ii] = $tmp;
         }
         //cred_log($recipients);
         $mailer->addRecipients($recipients);
         if (isset($_POST[StaticClass::PREFIX . 'cred_container_id'])) {
             $notification['mail']['body'] = str_replace("[cred-container-id]", StaticClass::$_cred_container_id, $notification['mail']['body']);
         }
         global $current_user_id;
         $current_user_id = $user_id;
         if (!$user_id && $is_user_form) {
             $current_user_id = $post_id;
         }
         // build SUBJECT
         $_subj = '';
         if (isset($notification['mail']['subject'])) {
             $_subj = $notification['mail']['subject'];
         }
         // build BODY
         $_bod = '';
         if (isset($notification['mail']['body'])) {
             $_bod = $notification['mail']['body'];
         }
         // replace placeholders
         $_subj = self::replacePlaceholders($_subj, $data_subject);
         // replace placeholders
         $_bod = self::replacePlaceholders($_bod, $data_body);
         //fixing https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/188538611/comments
         if (defined('WPCF_EMBEDDED_ABSPATH') && WPCF_EMBEDDED_ABSPATH) {
             require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
         }
         // provide WPML localisation
         if (isset($notification['_cred_icl_string_id']['subject'])) {
             $notification_subject_string_translation_name = self::getNotification_translation_name($notification['_cred_icl_string_id']['subject']);
             if ($notification_subject_string_translation_name) {
                 $_subj = cred_translate($notification_subject_string_translation_name, $_subj, 'cred-form-' . $form_title . '-' . $form_id);
             }
         }
         // provide WPML localisation
         if (isset($notification['_cred_icl_string_id']['body'])) {
             $notification_body_string_translation_name = self::getNotification_translation_name($notification['_cred_icl_string_id']['body']);
             if ($notification_body_string_translation_name) {
                 $_bod = cred_translate($notification_body_string_translation_name, $_bod, 'cred-form-' . $form_title . '-' . $form_id);
             }
         }
         // parse shortcodes if necessary relative to $post_id
         $_subj = CRED_Helper::renderWithPost(stripslashes($_subj), $post_id, false);
         $mailer->setSubject($_subj);
         // parse shortcodes/rich text if necessary relative to $post_id
         $_bod = CRED_Helper::renderWithPost($_bod, $post_id);
         //https://icanlocalize.basecamphq.com/projects/11629195-toolset-peripheral-work/todo_items/195775787/comments#310779109
         $_bod = stripslashes($_bod);
         $mailer->setBody($_bod);
         // build FROM address / name, independantly
         $_from = array();
         if (isset($notification['from']['address']) && !empty($notification['from']['address'])) {
             $_from['address'] = $notification['from']['address'];
         }
         if (isset($notification['from']['name']) && !empty($notification['from']['name'])) {
             $_from['name'] = $notification['from']['name'];
         }
         if (!empty($_from)) {
             $mailer->setFrom($_from);
         }
         // send it
         $_send_result = $mailer->send();
         if ($_send_result !== true) {
             update_option('_' . $form_id . '_last_mail_error', $_send_result);
         }
     }
     // custom action hooks here, for 3rd-party integration
     //do_action('cred_after_send_notifications_'.$form_id, $post_id);
     //do_action('cred_after_send_notifications', $post_id);
 }