protected static function _processAddCommentToThread() { global $wp_query; $post = $wp_query->post; $thread = CMA_AnswerThread::getInstance($post->ID); $content = $_POST['content']; $notify = !empty($_POST['thread_notify']); $resolved = !empty($_POST['thread_resolved']); $author_id = get_current_user_id(); $error = false; $messages = array(); try { $comment_id = $thread->addCommentToThread($content, $author_id, $notify, $resolved); } catch (Exception $e) { $messages = unserialize($e->getMessage()); $error = true; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { header('Content-type: application/json'); echo json_encode(array('success' => (int) (!$error), 'comment_id' => $comment_id, 'commentData' => CMA_AnswerThread::getCommentData($comment_id), 'message' => $messages)); exit; } else { if ($error) { foreach ((array) $messages as $message) { self::_addMessage(self::MESSAGE_ERROR, $message); } } else { do_action('cma_answer_post_after', $thread, CMA_Answer::getById($comment_id)); $autoApprove = CMA_AnswerThread::isAnswerAutoApproved(); if ($autoApprove) { $msg = __('Your answer has been succesfully added.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); wp_redirect(get_permalink($post->ID) . '/#comment-' . $comment_id, 303); } else { $msg = __('Thank you for your answer, it has been held for moderation.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); wp_redirect(get_permalink($post->ID), 303); } exit; } } }