/** * 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.')); } }
public function updateQuestionContent($userId, $title, $content) { global $wpdb; $errors = array(); $title = self::titleFilter($title); $content = self::contentFilter($content, $userId); self::validateTitle($title, $editId = $this->getId(), $errors); if (!CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_DESCRIPTION_OPTIONAL) && empty($content)) { $errors[] = CMA::__('Content cannot be empty'); } if (($badWord = CMA_BadWords::filterIfEnabled($content)) !== false) { $errors[] = sprintf(CMA_Labels::getLocalized('msg_content_includes_bad_word'), $badWord); } if (empty($errors)) { if ($this->getAuthorId() == $userId) { $update = array('ID' => $this->post->ID, 'post_content' => $content, 'post_title' => $title); if (!wp_update_post($update)) { $errors[] = 'Failed to update the question.'; } } else { $errors[] = 'Cannot edit question of another author.'; } } if (!empty($errors)) { throw new Exception(serialize($errors)); } else { do_action('cma_question_update_after', $this); return true; } }