protected static function _processAddThread() { $title = $_POST['thread_title']; $content = $_POST['thread_content']; $tags = isset($_POST['thread_tags']) ? $_POST['thread_tags'] : null; $notify = isset($_POST['thread_notify']) ? (bool) $_POST['thread_notify'] : false; $thread_id = null; if (!empty($_POST['thread_subcategory'])) { $cat = $_POST['thread_subcategory']; } else { if (!empty($_POST['thread_category'])) { $cat = $_POST['thread_category']; } else { $cat = null; } } $author_id = CMA::getPostingUserId(); $error = false; $messages = array(); $data = array('title' => $title, 'content' => $content, 'notify' => $notify, 'author_id' => $author_id, 'category' => $cat, 'tags' => $tags, 'userRelatedQuestions' => isset($_POST['cma_related_questions']) ? $_POST['cma_related_questions'] : array()); try { if (empty($_POST['nonce']) or !wp_verify_nonce($_POST['nonce'], 'cma_question')) { throw new Exception(CMA::__('Invalid nonce.')); } if (!CMA_Thread::canPostQuestions()) { throw new Exception(CMA::__('You cannot post questions.')); } $thread = CMA_Thread::newThread($data); $thread_id = $thread->getId(); } catch (Exception $e) { $messages = @unserialize($e->getMessage()); if (!is_array($messages)) { $messages = array($e->getMessage()); } $error = true; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { if (!$error) { $heldForModeration = $thread->wasHeldForModeration(); if (!$heldForModeration) { $messages[] = CMA_Labels::getLocalized('msg_post_question_success'); } else { $messages[] = CMA_Labels::getLocalized('msg_post_question_moderation'); } } header('Content-type: application/json'); echo json_encode(array('success' => (int) (!$error), 'thread_id' => $thread_id, 'messages' => $messages)); exit; } else { if ($error) { foreach ((array) $messages as $message) { self::addMessage(self::MESSAGE_ERROR, $message); } self::_populate($_POST); if (!empty($_POST['cma-referrer'])) { wp_redirect($_POST['cma-referrer'], 303); } else { wp_redirect(get_post_type_archive_link(CMA_Thread::POST_TYPE), 303); } } else { $heldForModeration = $thread->wasHeldForModeration(); if (!$heldForModeration) { $msg = CMA_Labels::getLocalized('msg_post_question_success'); } else { $msg = CMA_Labels::getLocalized('msg_post_question_moderation'); } self::addMessage(self::MESSAGE_SUCCESS, apply_filters('cma_question_post_msg_success', $msg)); if (!empty($_POST['cma-redirect'])) { if ($_POST['cma-redirect'] == '_thread') { if ($heldForModeration) { wp_redirect(get_post_type_archive_link(CMA_Thread::POST_TYPE), 303); } else { wp_redirect(get_permalink($thread_id), 303); } } else { wp_redirect($_POST['cma-redirect'], 303); } } else { if (!empty($_POST['cma-referrer'])) { wp_redirect($_POST['cma-referrer'], 303); } else { wp_redirect(get_post_type_archive_link(CMA_Thread::POST_TYPE), 303); } } } exit; } }