Exemplo n.º 1
0
 /**
  * Create new comment.
  * 
  * @param string $content
  * @param int $userId
  * @param int $threadId
  * @param int $answerId (optional)
  * @throws Exception
  * @return CMA_Comment
  */
 public static function create($content, $userId, $threadId, $answerId = null)
 {
     $user = get_userdata($userId);
     if (empty($userId) or empty($user)) {
         throw new Exception(CMA::__('Invalid user.'));
     }
     $thread = CMA_Thread::getInstance($threadId);
     if (!$thread or !$thread->isVisible()) {
         throw new Exception(CMA::__('You have no permission to post this comment.'));
     }
     if ($answerId) {
         $answer = CMA_Answer::getById($answerId);
         if (!$answer or !$answer->isVisible()) {
             throw new Exception(CMA::__('You have no permission to post this comment.'));
         }
     }
     $content = str_replace(';)', ':)', strip_tags($content));
     if (empty($content)) {
         throw new Exception(CMA::__('Content cannot be empty'));
     }
     if (($badWord = CMA_BadWords::filterIfEnabled($content)) !== false) {
         throw new Exception(sprintf(CMA_Labels::getLocalized('msg_content_includes_bad_word'), $badWord));
     }
     $approved = CMA_Settings::getOption(CMA_Settings::OPTION_COMMENTS_AUTO_APPROVE) || CMA_Thread::isAuthorAutoApproved($userId) ? 1 : 0;
     $comment = new self(array('comment_post_ID' => $threadId, 'comment_author' => $user->display_name, 'comment_author_email' => $user->user_email, 'comment_author_IP' => $_SERVER['REMOTE_ADDR'], 'comment_parent' => intval($answerId), 'comment_content' => apply_filters('comment_text', $content), 'comment_approved' => intval($approved), 'comment_date' => current_time('mysql'), 'comment_type' => self::COMMENT_TYPE, 'user_id' => $userId));
     do_action('cma_comment_post_before', $comment);
     if ($comment->save()) {
         do_action('cma_comment_post_after', $comment);
         if ($approved) {
             $comment->sendNotifications();
         } else {
             wp_notify_moderator($comment->getId());
         }
         return $comment;
     } else {
         throw new Exception(CMA::__('Failed to add comment.'));
     }
 }
 protected static function _processAddAnswerToThread()
 {
     $post = self::$query->post;
     $thread = CMA_Thread::getInstance($post->ID);
     $content = isset($_POST['content']) ? stripslashes($_POST['content']) : '';
     $author_id = CMA::getPostingUserId();
     $error = false;
     $messages = array();
     try {
         if (empty($_POST['nonce']) or !wp_verify_nonce($_POST['nonce'], 'cma_answer')) {
             throw new Exception(CMA::__('Invalid nonce.'));
         }
         if (!$thread->canPostAnswers($author_id)) {
             throw new Exception(CMA::__('You cannot post answers.'));
         }
         $answerId = $thread->addAnswer($content, $author_id, $follow = !empty($_POST['thread_notify']), $resolved = !empty($_POST['thread_resolved']), $private = !empty($_POST['private']));
         if ($answerId and $thread->getStatus() == 'draft' and current_user_can('manage_options')) {
             $thread->approve();
             $thread->notifyAboutNewQuestion();
         }
     } catch (Exception $e) {
         $messages = @unserialize($e->getMessage());
         if (!is_array($messages)) {
             $messages = array($e->getMessage());
         }
         $error = true;
     }
     if ($error) {
         foreach ((array) $messages as $message) {
             self::addMessage(self::MESSAGE_ERROR, $message);
         }
     } else {
         $autoApprove = CMA_Settings::getOption(CMA_Settings::OPTION_ANSWER_AUTO_APPROVE) || CMA_Thread::isAuthorAutoApproved(CMA::getPostingUserId());
         if ($autoApprove) {
             $msg = __('Your answer has been succesfully added.', 'cm-answers-pro');
         } else {
             $msg = __('Thank you for your answer, it has been held for moderation.', 'cm-answers-pro');
         }
         $msg = apply_filters('cma_answer_post_msg_success', $msg);
         self::addMessage(self::MESSAGE_SUCCESS, $msg);
         if (!empty($_POST['cma-referrer'])) {
             wp_redirect($_POST['cma-referrer'], 303);
         } else {
             wp_redirect($_SERVER['REQUEST_URI'] . ($autoApprove ? '#answer-' . $answerId : ''), 303);
         }
         exit;
     }
 }