protected static function _processAddThread() { global $wp_query; $post = $wp_query->post; $title = $_POST['thread_title']; $content = $_POST['thread_comment']; $notify = (bool) $_POST['thread_notify']; $author_id = get_current_user_id(); $error = false; $messages = array(); $data = array('title' => $title, 'content' => $content, 'notify' => $notify, 'author_id' => $author_id); try { $thread = CMA_AnswerThread::newThread($data); $thread_id = $thread->getId(); } 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), 'thread_id' => $thread_id, 'message' => $messages)); exit; } else { if ($error) { foreach ((array) $messages as $message) { self::_addMessage(self::MESSAGE_ERROR, $message); } wp_redirect(get_post_type_archive_link(CMA_AnswerThread::POST_TYPE), 303); } else { $autoApprove = CMA_AnswerThread::isQuestionAutoApproved(); if ($autoApprove) { $msg = __('New question has been succesfully added.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); } else { $msg = __('Thank you for your question, it has been held for moderation.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); } wp_redirect(get_post_type_archive_link(CMA_AnswerThread::POST_TYPE), 303); } exit; } }
protected static function _processAddThread() { global $wp_query; $post = $wp_query->post; $title = $_POST['thread_title']; $content = $_POST['thread_comment']; $notify = (bool) $_POST['thread_notify']; $author_id = get_current_user_id(); $error = false; $messages = array(); $data = array('title' => $title, 'content' => $content, 'notify' => $notify, 'author_id' => $author_id); try { if (empty($_POST['nonce']) or !wp_verify_nonce($_POST['nonce'], 'cma_question')) { throw new Exception(serialize(array('Invalid nonce.'))); } $thread = CMA_AnswerThread::newThread($data); $thread_id = $thread->getId(); } 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), 'thread_id' => $thread_id, 'message' => $messages)); exit; } else { if ($error) { foreach ((array) $messages as $message) { self::_addMessage(self::MESSAGE_ERROR, $message); } wp_redirect(get_post_type_archive_link(CMA_AnswerThread::POST_TYPE), 303); } else { $autoApprove = CMA_AnswerThread::isQuestionAutoApproved(); if ($autoApprove) { $msg = __('Thêm câu hỏi mới thành công.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); } else { $msg = __('Cám ơn bạn đã đặt câu hỏi, câu hỏi đã được chuyển cho admin.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); } wp_redirect(get_post_type_archive_link(CMA_AnswerThread::POST_TYPE), 303); } exit; } }