static function ajaxHandler()
 {
     $response = array('success' => 0, 'message' => 'An error occurred.');
     if (CMA_Settings::getOption(CMA_Settings::OPTION_NEW_QUESTION_NOTIFY_EVERYBODY_OPTINOUT)) {
         if (isset($_POST['nonce']) and wp_verify_nonce($_POST['nonce'], 'cma_follow')) {
             if (CMA_ThreadNewsletter::isNewsletterFollower()) {
                 if (CMA_Settings::getOption(CMA_Settings::OPTION_NEW_QUESTION_NOTIFY_EVERYBODY_ENABLED)) {
                     CMA_ThreadNewsletter::addUserToNewsletterBlacklist();
                 }
                 CMA_ThreadNewsletter::removeUserFromNewsletterWhitelist();
                 $response = array('success' => 1, 'message' => CMA_Labels::getLocalized('newsletter_new_therads_disabled_success'));
             } else {
                 CMA_ThreadNewsletter::removeUserFromNewsletterBlacklist();
                 CMA_ThreadNewsletter::addUserToNewsletterWhitelist();
                 $response = array('success' => 1, 'message' => CMA_Labels::getLocalized('newsletter_new_therads_enabled_success'));
             }
         }
     }
     header('content-type: application/json');
     echo json_encode($response);
     exit;
 }
 public function notifyAboutNewAnswer($lastAnswerId)
 {
     global $wpdb;
     $lastAnswer = CMA_Answer::getById($lastAnswerId);
     $receivers = array();
     // All users are receivers
     if (CMA_Settings::getOption(CMA_Settings::OPTION_NEW_ANSWER_NOTIFY_EVERYBODY_ENABLED) and !$lastAnswer->isPrivate()) {
         $users = $wpdb->get_results("SELECT ID, user_email FROM {$wpdb->users}", ARRAY_A);
         foreach ($users as $user) {
             if ($this->isVisible($user['ID'])) {
                 $receivers[] = $user['user_email'];
             }
         }
         unset($users);
     } else {
         // Users followers
         if (CMA_Settings::getOption(CMA_Settings::OPTION_ENABLE_THREAD_FOLLOWING)) {
             $followers = $this->getFollowersEngine()->getFollowers();
             if (!empty($followers)) {
                 foreach ($followers as $user_id) {
                     if ($user_id != $lastAnswer->getAuthorId() and $this->isVisible($user_id)) {
                         $user = get_userdata($user_id);
                         if (!empty($user->user_email)) {
                             $receivers[] = $user->user_email;
                         }
                     }
                 }
             }
             unset($followers);
         }
     }
     $receivers = apply_filters('cma_answer_notification_emails', $receivers, $lastAnswerId);
     if ($lastAnswer->isPrivate()) {
         if ($threadAuthor = $this->getAuthor()) {
             $receivers = array_intersect($receivers, array($threadAuthor->user_email));
         }
     }
     // Admin custom receivers' emails
     if (CMA_Settings::getOption(CMA_Settings::OPTION_NEW_ANSWER_ADMIN_NOTIFICATION_ENABLED)) {
         $receivers = array_merge($receivers, CMA_Settings::getOption(CMA_Settings::OPTION_POST_ADMIN_NOTIFICATION_EMAIL));
     }
     $receivers = array_filter(array_unique($receivers));
     if (!empty($receivers)) {
         $message = CMA_Settings::getOption(CMA_Settings::OPTION_THREAD_NOTIFICATION);
         $title = CMA_Settings::getOption(CMA_Settings::OPTION_THREAD_NOTIFICATION_TITLE);
         $replace = array('[blogname]' => get_bloginfo('name'), '[question_title]' => strip_tags($this->getTitle()), '[question_body]' => strip_tags($this->getContent()), '[answer]' => strip_tags($lastAnswer->getContent()), '[comment_link]' => $lastAnswer->getPermalink(), '[author]' => strip_tags($lastAnswer->getAuthor()->display_name), '[ip]' => $lastAnswer->getAuthorIP(), '[opt_out_url]' => CMA_ThreadNewsletter::getOptOutUrl($this, CMA_ThreadNewsletter::TYPE_THREAD));
         CMA_Email::send($receivers, $title, $message, $replace);
         /* $headers = array();
            foreach($receivers as $email) {
            	if (is_email($email)) {
            		$headers[] = ' Bcc: '. $email;
            	}
            }
            
            wp_mail(null, $title, $message, $headers); */
     }
 }
 public function sendNotifications()
 {
     $thread = $this->getThread();
     $receivers = array();
     // Admin notification emails
     if (CMA_Settings::getOption(CMA_Settings::OPTION_NEW_COMMENT_ADMIN_NOTIFICATION_ENABLED)) {
         $receivers = array_merge($receivers, CMA_Settings::getOption(CMA_Settings::OPTION_POST_ADMIN_NOTIFICATION_EMAIL));
     }
     // User subscribers email
     if (CMA_Settings::getOption(CMA_Settings::OPTION_ENABLE_THREAD_FOLLOWING) and CMA_Settings::getOption(CMA_Settings::OPTION_NEW_COMMENT_NOTIFICATION_ENABLED)) {
         $followers = $thread->getFollowersEngine()->getFollowers();
         if (!empty($followers)) {
             foreach ($followers as $user_id) {
                 if ($user_id != $this->getAuthorId() and $thread->isVisible($user_id)) {
                     $user = get_userdata($user_id);
                     if (!empty($user->user_email)) {
                         $receivers[] = $user->user_email;
                     }
                 }
             }
         }
     }
     $receivers = array_filter(array_unique($receivers));
     if (!empty($receivers)) {
         $replace = array('[blogname]' => get_bloginfo('name'), '[question_title]' => strip_tags($thread->getTitle()), '[question_body]' => strip_tags($thread->getContent()), '[comment_link]' => $this->getPermalink(), '[comment]' => strip_tags($this->getContent()), '[author]' => strip_tags($this->getAuthor()->display_name), '[opt_out_url]' => CMA_ThreadNewsletter::getOptOutUrl($this->getThread(), CMA_ThreadNewsletter::TYPE_THREAD));
         $subject = strtr(CMA_Settings::getOption(CMA_Settings::OPTION_NEW_COMMENT_NOTIFICATION_TITLE), $replace);
         $message = strtr(CMA_Settings::getOption(CMA_Settings::OPTION_NEW_COMMENT_NOTIFICATION_CONTENT), $replace);
         CMA_Email::send($receivers, $subject, $message);
         /* $headers = array();
            foreach($receivers as $email) {
            	$email = trim($email);
            	if (is_email($email)) {
            		$headers[] = ' Bcc: '. $email;
            	}
            }
            
            if (!empty($headers)) wp_mail(null, $subject, $message, $headers); */
     }
 }