public function addAnswer($content, $author_id, $follow = false, $resolved = false, $private = false) { $user = get_userdata($author_id); if (empty($author_id) or empty($user)) { throw new Exception(CMA::__('Invalid user.')); } if (!$this->isVisible()) { throw new Exception(CMA::__('You have no permission to post this answer.')); } $content = self::contentFilter($content, $author_id); if (empty($content)) { $errors[] = __('Content cannot be empty', 'cm-answers-pro'); } if (($badWord = CMA_BadWords::filterIfEnabled($content)) !== false) { $errors[] = sprintf(CMA_Labels::getLocalized('msg_content_includes_bad_word'), $badWord); } if (!empty($errors)) { throw new Exception(serialize($errors)); } $approved = CMA_Settings::getOption(CMA_Settings::OPTION_ANSWER_AUTO_APPROVE) || self::isAuthorAutoApproved($author_id) ? 1 : 0; $answer = new CMA_Answer(array('comment_post_ID' => $this->getId(), 'comment_author' => $user->display_name, 'comment_author_email' => $user->user_email, 'comment_author_IP' => $_SERVER['REMOTE_ADDR'], 'user_id' => $author_id, 'comment_parent' => 0, 'comment_content' => apply_filters('comment_text', str_replace(';)', ':)', $content)), 'comment_approved' => $approved, 'comment_date' => current_time('mysql'), 'comment_type' => CMA_Answer::COMMENT_TYPE)); do_action('cma_answer_post_before', $this, $answer); $answer->save(); $answerId = $answer->getId(); if (!$answerId) { throw new Exception('Failed to add answer.'); } $attachmentsIds = CMA_AnswerAttachment::handleUpload($this->getId()); if (!empty($_POST['attached']) && is_array($_POST['attached'])) { $attachmentsIds = array_merge($attachmentsIds, $_POST['attached']); } foreach ($attachmentsIds as $attachmentId) { $answer->addAttachment($attachmentId); } $answer->setPrivate($private); if (!$private) { $this->updateThreadMetadata(array('commentId' => $answerId, 'authorId' => $author_id, 'follow' => $follow, 'resolved' => $resolved, 'approved' => $approved, 'answerId' => $answerId), $notifyUsers = !$private); if ($approved) { $this->setUpdated(); } } if ($approved) { self::updateQA($author_id); $this->notifyAboutNewAnswer($answerId); } else { if (!$approved) { wp_notify_moderator($answerId); } } if (CMA_Settings::getOption(CMA_Settings::OPTION_LOGS_ENABLED)) { CMA_AnswerPostLog::instance()->log($answerId); } do_action('cma_answer_post_after', $this, $answer); return $answerId; }
public static function old_update() { global $wpdb; $oldVersion = get_option(CMA::OPTION_VERSION); if (empty($oldVersion)) { return; } $currentVersion = CMA::version(); if (empty($oldVersion) or version_compare($oldVersion, $currentVersion, '<')) { update_option(CMA::OPTION_VERSION, $currentVersion); if (version_compare($oldVersion, '2.1.6', '<')) { // Update comment_type $commentsIds = $wpdb->get_col($wpdb->prepare("SELECT `{$wpdb->comments}`.`comment_ID`\n\t\t\t\t\tFROM `{$wpdb->comments}`\n\t\t\t\t\tLEFT JOIN `{$wpdb->posts}` ON `{$wpdb->comments}`.`comment_post_ID` = `{$wpdb->posts}`.`ID`\n\t\t\t\t\tWHERE `{$wpdb->posts}`.`post_type` = %s AND `{$wpdb->comments}`.`comment_type` = ''", CMA_Thread::POST_TYPE)); $data = array('comment_type' => CMA_Answer::COMMENT_TYPE); foreach ($commentsIds as $comment_ID) { $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); } } // Update new post meta: question+answers votes count if (version_compare($oldVersion, '2.1.7', '<')) { $posts = get_posts(array('post_type' => CMA_Thread::POST_TYPE)); foreach ($posts as $post) { $thread = CMA_Thread::getInstance($post->ID); update_post_meta($post->ID, CMA_Thread::$_meta['votes_question'], 0); $thread->updateRatingCache(); } } // Update users counters if (version_compare($oldVersion, '2.1.9', '<=')) { CMA_Thread::updateAllQA(); } if (version_compare($oldVersion, '2.1.13', '<=')) { // Create logs records for old posts $posts = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_type = %s", CMA_Thread::POST_TYPE)); $postsLog = new CMA_QuestionPostLog(); foreach ($posts as $post) { $count = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $postsLog->getMetaTableName() . ' m JOIN ' . $postsLog->getTableName() . ' l ON l.id = m.log_id WHERE m.meta_name = %s AND m.meta_value = %s AND l.log_type = %s', CMA_QuestionPostLog::META_QUESTION_ID, (string) $post->ID, CMA_QuestionPostLog::LOG_TYPE)); if ($count == 0) { $postsLog->create(array(CMA_Log::FIELD_CREATED => $post->post_date, CMA_Log::FIELD_USER_ID => $post->post_author, CMA_Log::FIELD_IP_ADDR => NULL), array(CMA_QuestionPostLog::META_QUESTION_ID => $post->ID)); } } // Create logs records for old comments $answers = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_type = %s", CMA_Answer::COMMENT_TYPE)); $answersLog = new CMA_AnswerPostLog(); foreach ($answers as $answer) { $count = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $answersLog->getMetaTableName() . ' m JOIN ' . $answersLog->getTableName() . ' l ON l.id = m.log_id WHERE m.meta_name = %s AND m.meta_value = %s AND l.log_type = %s', CMA_QuestionPostLog::META_QUESTION_ID, (string) $answer->comment_ID, CMA_AnswerPostLog::LOG_TYPE)); if ($count == 0) { $postsLog->create(array(CMA_Log::FIELD_CREATED => $answer->comment_date, CMA_Log::FIELD_USER_ID => $answer->user_id, CMA_Log::FIELD_IP_ADDR => NULL), array(CMA_AnswerPostLog::META_QUESTION_ID => $answer->comment_post_ID, CMA_AnswerPostLog::META_ANSWER_ID => $answer->comment_ID)); } } } if (version_compare($oldVersion, '2.2.2', '<=')) { // Votes for questions $metaKeys = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s", CMA_Thread::$_meta['usersRated'])); foreach ($metaKeys as $mk) { $value = @unserialize($mk->meta_value); if (is_array($value)) { $value = array_filter($value); foreach ($value as $userId) { add_post_meta($mk->post_id, CMA_Thread::$_meta['usersRated'], $userId, $uniq = false); } $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_id = %d", $mk->meta_id)); } } // Votes for answers $metaKeys = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->commentmeta} WHERE meta_key = %s", CMA_Answer::META_USERS_RATED)); foreach ($metaKeys as $mk) { $value = @unserialize($mk->meta_value); if (is_array($value)) { $value = array_filter($value); foreach ($value as $userId) { add_comment_meta($mk->comment_id, CMA_Answer::META_USERS_RATED, $userId, $uniq = false); } $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->commentmeta} WHERE meta_id = %d", $mk->meta_id)); } } } } }