public static function newThread($data = array()) { $userId = CMA::getPostingUserId(); $user = get_userdata($userId); if (empty($userId) or empty($user)) { throw new Exception(CMA::__('Invalid user.')); } $title = self::titleFilter($data['title']); $content = self::contentFilter($data['content'], $userId); self::validateTitle($title, $editId = false, $errors); if (!CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_DESCRIPTION_OPTIONAL) && 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($_FILES) and !self::areQuestionAttachmentsAllowed()) { $errors[] = __('Upload is not allowed.', 'cm-answers-pro'); } elseif (!self::validateUploadSize()) { $errors[] = __('The file you uploaded is too big', 'cm-answers-pro'); } elseif (!self::validateUploadNames()) { $errors[] = __('The file you uploaded is not allowed', 'cm-answers-pro'); } if (!empty($data['category']) && $data['category'] > 0) { if ($category = CMA_Category::getInstance($data['category'])) { if (!$category->isVisible()) { $errors[] = CMA::__('You have no permission to post this question.'); } } else { $errors[] = CMA::__('Choose a valid category.'); } } else { if (CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_REQUIRE_CATEGORY)) { $errors[] = CMA::__('Choose a category.'); } } if (!empty($errors)) { throw new Exception(serialize($errors)); } if (CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_AUTO_APPROVE) || self::isAuthorAutoApproved($userId)) { $status = 'publish'; } else { $status = 'draft'; if (self::getSpamFilter() || CMA_Settings::getOption(CMA_Settings::OPTION_SIMULATE_COMMENT)) { /** Hack, simulate comment adding to trigger spam filters * */ $commentdata = array('comment_post_ID' => 0, 'comment_author' => $user->first_name, 'comment_author_email' => $user->user_email, 'comment_author_url' => '', 'comment_content' => $title . ' ' . $content, 'comment_type' => self::POST_TYPE, 'user_ID' => $userId, 'comment_parent' => 0, 'comment_author_IP' => preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']), 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1), 'comment_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? substr($_SERVER['HTTP_USER_AGENT'], 0, 254) : ''); if (CMA_Settings::getOption(CMA_Settings::OPTION_SIMULATE_COMMENT)) { // Simulate comment to detect flood and so on. if (wp_allow_comment($commentdata) == 'spam') { $status = 'draft'; } } } } $postData = array('post_status' => $status, 'post_type' => self::POST_TYPE, 'post_title' => $title, 'post_content' => $content, 'post_name' => urldecode(sanitize_title_with_dashes(remove_accents($title))), 'post_author' => $userId); do_action('cma_question_post_before', $postData); $id = wp_insert_post($postData); if ($id instanceof WP_Error) { return $id->get_error_message(); } else { $instance = self::getInstance($id); $instance->setUpdated()->setResolved(false)->setAuthorIP()->checkGeolocation(); if (!empty($data['notify']) and $data['notify'] == 1) { $instance->getFollowersEngine()->addFollower(); } $instance->savePostMeta(array(self::$_meta['views'] => 0)); $instance->savePostMeta(array(self::$_meta['votes_answers'] => 0)); $instance->savePostMeta(array(self::$_meta['votes_question'] => 0)); $instance->savePostMeta(array(self::$_meta['votes_question_answers'] => 0)); $instance->savePostMeta(array(self::$_meta['highestRatedAnswer'] => 0)); $instance->savePostMeta(array(self::$_meta['stickyPost'] => 0)); if (!empty($data['category'])) { $r = wp_set_post_terms($id, array($data['category']), CMA_Category::TAXONOMY, true); } if (isset($data['tags'])) { $r = wp_set_post_tags($id, $data["tags"], true); } if (CMA_Settings::getOption(CMA_Settings::OPTION_USER_RELATED_QUESTIONS_ENABLE) and !empty($data['userRelatedQuestions'])) { $instance->setUserRelatedQuestions(CMA_UserRelatedQuestions::getIdsFromRaw($data['userRelatedQuestions'])); } $instance->savePost(); $attachmentsIds = CMA_QuestionAttachment::handleUpload($instance->getId()); if (!empty($_POST['attached']) && is_array($_POST['attached'])) { $attachmentsIds = array_merge($attachmentsIds, $_POST['attached']); } foreach ($attachmentsIds as $attachmentId) { if (!empty($attachmentId)) { $instance->addAttachment($attachmentId); } } if (CMA_Settings::getOption(CMA_Settings::OPTION_NEW_QUESTION_EVERYBODY_FOLLOW_ENABLED)) { $instance->makeEverybodyFollowers(); } if ($status == 'draft') { $instance->notifyModerator(); } else { self::updateQA($userId); $instance->notifyAboutNewQuestion(); } if (CMA_Settings::getOption(CMA_Settings::OPTION_LOGS_ENABLED)) { CMA_QuestionPostLog::instance()->log($id); } do_action('cma_question_post_after', $instance, $data); return $instance; } }
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)); } } } } }