public static function save_postdata($post_id) { $postType = isset($_POST['post_type']) ? $_POST['post_type'] : ''; if ('cma_thread' != $postType) { return; } $answerThread = CMA_Thread::getInstance($post_id); if (empty($answerThread)) { return; } $sticky = 0; if (isset($_POST["cma_sticky_box"]) and isset($_POST["cma_sticky_box"])) { $sticky = 1; } update_post_meta($post_id, '_sticky_post', $sticky); /* * Update views count */ $viewsAmount = isset($_POST['cma_views_box']) ? $_POST['cma_views_box'] : 0; $answerThread->setViews($viewsAmount); if (isset($_POST['cma_post_rating_box']) and is_numeric($_POST['cma_post_rating_box'])) { $answerThread->setRatingHandicap($_POST['cma_post_rating_box']); } if (isset($_POST['cma_post_votes_handicap_box']) and is_numeric($_POST['cma_post_votes_handicap_box'])) { $answerThread->setVotesHandicap($_POST['cma_post_votes_handicap_box']); } }
protected static function optoutThread($threadId) { $token = self::_getParam('token'); if ($thread = CMA_Thread::getInstance($threadId) and CMA_ThreadNewsletter::getOptOutToken($thread) == $token) { $thread->getFollowersEngine()->removeFollower(); self::$contentMessage = CMA_Labels::getLocalized('unfollow_success'); } else { self::$contentMessage = 'An error occurred.'; } }
static function save_post($postId) { if ($thread = CMA_Thread::getInstance($postId) and self::save_post_verify_nonce()) { $related = array(); if (!empty($_POST['cma_related_questions'])) { $related = $_POST['cma_related_questions']; } if (!empty($_POST['cma_related_questions_add'])) { $related = array_merge($related, self::getIdsFromRaw($_POST['cma_related_questions_add'])); } $thread->setUserRelatedQuestions($related); } }
protected function getRelatedQuestions($limit, $matchCategory, $matchTags) { $wp_query = CMA_AnswerController::$query; if (empty($this->questions) and $wp_query->get('post_type') == CMA_Thread::POST_TYPE and $wp_query->is_single()) { $post = $wp_query->get_posts(); $post = reset($post); if (!empty($post) and $post->post_type == CMA_Thread::POST_TYPE) { $thread = CMA_Thread::getInstance($post->ID); $questions = $thread->getRelated($limit, $matchCategory, $matchTags); $this->questions = array_map(array('CMA_Thread', 'getInstance'), $questions); } } return $this->questions; }
protected static function _processCommentAdd() { $wp_query = self::$query; $response = array('success' => 0, 'msg' => CMA::__('An error occured.')); $post = $wp_query->post; $thread = CMA_Thread::getInstance($post->ID); $content = self::_getParam('content'); $answerId = self::_getParam('cma-answer-id'); if (empty($_POST['nonce']) or !wp_verify_nonce($_POST['nonce'], 'cma_comment')) { $error = CMA::__('Invalid nonce.'); } else { if (!CMA_Comment::canCreate()) { $error = CMA::__('You have to be logged-in.'); } else { if (empty($content)) { $error = CMA::__('Content cannot be empty.'); } else { if ($answerId) { $answer = CMA_Answer::getById($answerId); if (empty($answer)) { $error = CMA::__('Unknown answer.'); } } } } } header('content-type: application/json'); if (empty($error)) { try { $comment = CMA_Comment::create($content, CMA::getPostingUserId(), $thread->getId(), $answerId); if (!$comment) { throw new Exception(CMA::__('Failed to add comment.')); } if ($comment->isApproved()) { $thread->setUpdated(); } $msg = $comment->isApproved() ? CMA::__('Comment has been added.') : CMA::__('Thank you for your comment, it has been held for moderation.'); $html = $comment->isApproved() ? self::_loadView('answer/comments/comment-single', compact('comment')) : null; $response = array('success' => 1, 'msg' => $msg, 'html' => $html); } catch (Exception $e) { $error = $e->getMessage(); } } if (!empty($error)) { $response['msg'] = $error; } echo json_encode(apply_filters('cma_comment_add_ajax_response', $response)); exit; }
protected function importQuestion(array $question) { global $wpdb; $question['post_status'] = 'publish'; $question['post_type'] = 'cma_thread'; $question['post_author'] = $wpdb->get_var("SELECT user_id FROM {$wpdb->usermeta}\n\t\t\tWHERE meta_key = 'cma_import_old_id' AND meta_value = " . $question['import_old_author_id']); if (empty($question['post_author'])) { throw new Exception(sprintf('Failed to import question with old ID %d. Unknown user with old ID %d.', $question['import_old_id'], $question['import_old_author_id'])); } $id = wp_insert_post($question, true); if ($id and is_numeric($id)) { update_post_meta($id, 'cma_import_old_id', $question['import_old_id']); $instance = CMA_Thread::getInstance($id); $instance->savePostMeta(array(CMA_Thread::$_meta['votes_answers'] => 0)); $instance->savePostMeta(array(CMA_Thread::$_meta['votes_question'] => 0)); $instance->savePostMeta(array(CMA_Thread::$_meta['votes_question_answers'] => 0)); $instance->savePostMeta(array(CMA_Thread::$_meta['highestRatedAnswer'] => 0)); $instance->savePostMeta(array(CMA_Thread::$_meta['stickyPost'] => 0)); // $votes = $this->getQuestionVotes($instance->getId()); return $instance; } else { throw new Exception('Error: ' . json_encode($id)); } }
public static function notifyAllUsers() { global $wpdb; if (!empty($_GET['nonce']) and wp_verify_nonce($_GET['nonce'], CMA_BaseController::ADMIN_BP_NOTIFY)) { $usersIds = $wpdb->get_col("SELECT ID FROM {$wpdb->users}"); if (!empty($_GET['post_id']) and $thread = CMA_Thread::getInstance($_GET['post_id'])) { $notification = array('item_id' => $thread->getId(), 'secondary_item_id' => 0, 'component_name' => 'cma_notifier', 'component_action' => self::ACTION_NOTIFICATION_THREAD, 'date_notified' => bp_core_current_time(), 'is_new' => 1, 'allow_duplicate' => true); foreach ($usersIds as $userId) { if ($thread->isVisible($userId)) { $notification['user_id'] = $userId; bp_notifications_add_notification($notification); } } } if (!empty($_GET['comment_id']) and $answer = CMA_Answer::getById($_GET['comment_id'])) { $notification = array('item_id' => $answer->getId(), 'secondary_item_id' => 0, 'component_name' => 'cma_notifier', 'component_action' => self::ACTION_NOTIFICATION_ANSWER, 'date_notified' => bp_core_current_time(), 'is_new' => 1, 'allow_duplicate' => true); foreach ($usersIds as $userId) { if ($answer->isVisible($userId)) { $notification['user_id'] = $userId; bp_notifications_add_notification($notification); } } } } wp_safe_redirect(CMA::getReferer()); exit; }
protected function importQuestion(array $question) { global $wpdb; $thread = false; $existing = $wpdb->get_var("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'cma_import_old_id' AND meta_value = " . $question['import_old_id']); if ($existing) { $this->showWarning(sprintf('Question with old ID %d already imported.', $question['import_old_id'])); $id = $existing; $thread = CMA_Thread::getInstance($id); } else { try { if ($thread = parent::importQuestion($question) and $thread instanceof CMA_Thread) { $thread->setResolved($question['state_type'] == 'closed'); if (!empty($question["tagnames"])) { $this->showSuccess(sprintf('Question with old ID %d imported.', $question['import_old_id'])); wp_set_post_tags($thread->getId(), str_replace(" ", ',', $question["tagnames"]), true); $this->importedQuestions++; } } else { $this->showError(sprintf('Failed to import question with old ID %d', $question['import_old_id'])); } } catch (Exception $e) { $this->showError($e); } } return $thread; }
public function getThread() { return CMA_Thread::getInstance($this->getThreadId()); }
public static function update_2_4_13() { global $wpdb; // Comment rating $commentsIds = array(); if (CMA_Settings::getOption(CMA_Settings::OPTION_NEGATIVE_RATING_ALLOWED)) { // Copy comments' ratings to the rating handicap fields. $ratings = $wpdb->get_results($wpdb->prepare("SELECT m.comment_id, m.meta_value, c.comment_post_ID\n\t\t\t\tFROM {$wpdb->commentmeta} m\n\t\t\t\tINNER JOIN {$wpdb->comments} c ON m.comment_id = c.comment_ID\n\t\t\t\tWHERE m.meta_key = %s", CMA_Thread::$_meta['rating']), ARRAY_A); foreach ($ratings as $record) { add_comment_meta($record['comment_id'], CMA_Answer::META_USER_RATING_HANDICAP, $record['meta_value'], $unique = true); $commentsIds[$record['comment_id']] = $record['comment_id']; } } else { // Only positive ratings - move voters to the positive voters meta $voters = $wpdb->get_results($wpdb->prepare("SELECT m.comment_id, m.meta_value, c.comment_post_ID\n\t\t\t\tFROM {$wpdb->commentmeta} m\n\t\t\t\tINNER JOIN {$wpdb->comments} c ON m.comment_id = c.comment_ID\n\t\t\t\tWHERE m.meta_key = %s", CMA_Answer::META_USERS_RATED), ARRAY_A); foreach ($voters as $record) { $commentRatingPositive = get_comment_meta($record['comment_id'], CMA_Answer::META_USER_RATING_POSITIVE, $single = false); if (!in_array($record['meta_value'], $commentRatingPositive)) { add_comment_meta($record['comment_id'], CMA_Answer::META_USER_RATING_POSITIVE, $record['meta_value'], $unique = false); } $commentsIds[$record['comment_id']] = $record['comment_id']; } } // ---------------------------------------------------- // Post rating $postsIds = array(); if (CMA_Settings::getOption(CMA_Settings::OPTION_NEGATIVE_RATING_ALLOWED)) { // Copy posts' ratings to the rating handicap fields. $ratings = $wpdb->get_results($wpdb->prepare("SELECT post_id, meta_value FROM {$wpdb->postmeta}\n\t\t\t\tWHERE meta_key = %s", CMA_Thread::$_meta['rating']), ARRAY_A); foreach ($ratings as $record) { add_post_meta($record['post_id'], CMA_Thread::$_meta['userRatingHandicap'], $record['meta_value'], $unique = true); $postsIds[$record['post_id']] = $record['post_id']; } } else { // Only positive ratings - copy voters to the positive voters meta $voters = $wpdb->get_results($wpdb->prepare("SELECT post_id, meta_value FROM {$wpdb->postmeta}\n\t\t\t\tWHERE meta_key = %s", CMA_Thread::$_meta['usersRated']), ARRAY_A); foreach ($voters as $record) { $postRatingPositive = get_post_meta($record['post_id'], CMA_Thread::$_meta['userRatingPositive'], $single = false); if (!in_array($record['meta_value'], $postRatingPositive)) { add_post_meta($record['post_id'], CMA_Thread::$_meta['userRatingPositive'], $record['meta_value'], $unique = false); } $postsIds[$record['post_id']] = $record['post_id']; } } // ---------------------------------------------------- // Update comments counter cache foreach ($commentsIds as $commentId) { if ($answer = CMA_Answer::getById($commentId)) { $answer->updateRatingCache(); } } // Update posts counter cache foreach ($postsIds as $postId) { if ($thread = CMA_Thread::getInstance($postId)) { $thread->updateRatingCache(); } } }
static function questionForm($categoryId, $threadId = null) { if ($category = CMA_Category::getInstance($categoryId)) { $fields = $category->getCustomFields(); if ($threadId and $thread = CMA_Thread::getInstance($threadId)) { $values = $thread->getCategoryCustomFields(); } else { $values = array_fill(0, count($fields), ''); } echo self::_loadView('answer/meta/question-form-category-custom-fields', compact('fields', 'values')); } }
public static function displayBreadcrumbs($threadId = null, $basedOnSettings = true) { if ($basedOnSettings and !CMA_Settings::getOption(CMA_Settings::OPTION_BREADCRUMBS_ENABLED)) { return; } global $post; $queriedObject = self::$query->get_queried_object(); $indexLink = sprintf('<a href="%s">%s</a>', esc_attr(get_post_type_archive_link(CMA_Thread::POST_TYPE)), esc_html(CMA_Labels::getLocalized('index_page_title'))); $categoryLink = null; $paretnCategoryLink = null; $threadLink = null; if (empty($threadId) and self::$query->is_single() and !empty($post) and $post->post_type = CMA_Thread::POST_TYPE) { $threadId = $post->ID; } if (!empty($threadId) and $thread = CMA_Thread::getInstance($threadId)) { if ($category = $thread->getCategory()) { $categoryLink = $category->getLink(); if ($parentCategory = $category->getParentInstance()) { $parentCategoryLink = $parentCategory->getLink(); } } $threadLink = sprintf('<a href="%s">%s</a>', esc_attr(!empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $thread->getPermalink()), esc_html($thread->getTitle())); } else { if (isset($queriedObject->term_id) and $category = get_term($queriedObject->term_id, CMA_Category::TAXONOMY)) { $categoryLink = sprintf('<a href="%s">%s</a>', esc_attr(get_term_link($category->term_id, CMA_Category::TAXONOMY)), esc_html($category->name)); if (!empty($category->parent) and $parent = get_term($category->parent, CMA_Category::TAXONOMY)) { $parentCategoryLink = sprintf('<a href="%s">%s</a>', esc_attr(get_term_link($parent->term_id, CMA_Category::TAXONOMY)), esc_html($parent->name)); } } } if (!empty($categoryLink) or !empty($threadLink)) { if (empty($threadLink)) { $categoryLink = '<span>' . strip_tags($categoryLink) . '</span>'; } else { $threadLink = '<span>' . strip_tags($threadLink) . '</span>'; } echo self::_loadView('answer/nav/breadcrumbs', compact('indexLink', 'categoryLink', 'parentCategoryLink', 'threadLink')); } }