public static function selectForAnswer(CMA_Answer $answer) { $ids = get_comment_meta($answer->getId(), CMA_Answer::META_ATTACHMENT, false); if (!empty($ids)) { $result = parent::select($answer->getThreadId(), get_comment_meta($answer->getId(), CMA_Answer::META_ATTACHMENT, false)); foreach ($result as $attachment) { $attachment->setAnswerId($answer->getId()); } return $result; } else { return array(); } }
public function log($answerId, $data = array(), $meta = array()) { if ($answer = CMA_Answer::getById($answerId)) { $meta[self::META_ANSWER_ID] = $answerId; $meta[self::META_QUESTION_ID] = $answer->getThreadId(); if (!empty($_SERVER['HTTP_USER_AGENT'])) { $meta[self::META_USER_AGENT] = $_SERVER['HTTP_USER_AGENT']; } if (!empty($_SERVER['REMOTE_ADDR'])) { $meta = $this->appendMetaGeolocation($meta, $_SERVER['REMOTE_ADDR']); } return $this->create($data, $meta); } }
public static function indexAction() { $userId = self::getContributorAndId(); $user = $userId['user']; $id = $userId['id']; $socialLinks = array(); $providers = array('google', 'linkedin', 'facebook'); foreach ($providers as $provider) { $link = get_user_meta($id, '_cma_social_' . $provider . '_url', true); if (!empty($link)) { $socialLinks[$provider] = $link; } } return array('name' => $user->display_name, 'user_id' => $id, 'link' => get_user_meta($id, '_cma_social_url', true), 'socialLinks' => $socialLinks, 'questions' => CMA_Thread::getQuestionsByUser($id, -1, $onlyVisible = true), 'answers' => CMA_Answer::getByUser($id, $approved = true, $limit = -1, $page = 1, $onlyVisible = true)); }
public static function shortcode_answers($atts) { if (empty($atts['author'])) { return; } $limit = isset($atts['limit']) ? $atts['limit'] : 5; $atts['pagination'] = isset($atts['pagination']) ? $atts['pagination'] : 1; // $answersCount = CMA_Answer::countForUser($atts['author'], $approved = true, $onlyVisible = true); $answers = CMA_Answer::getByUser($atts['author'], $approved = true, $limit, $page = 1, $onlyVisible = true); $totalPages = ceil(CMA_Answer::countForUser($atts['author'], $approved = true, $onlyVisible = true) / $limit); $authorSlug = ''; $user = get_user_by(is_numeric($atts['author']) ? 'id' : 'slug', $atts['author']); if (!empty($user)) { $authorSlug = $user->user_nicename; } $public = false; $currentPage = 1; $ajax = (!isset($atts['ajax']) or $atts['ajax']) ? true : false; return CMA_BaseController::_loadView('answer/widget/answers-list', compact('answers', 'atts', 'public', 'authorSlug', 'currentPage', 'totalPages', 'limit', 'ajax')); }
/** * 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.')); } }
/** * Action then the thread has been resolved. * * @param CMA_Thread $thread */ public function threadResolved(CMA_Thread $thread) { if (CMA_Settings::getOption(CMA_Settings::OPTION_MP_REWARD_BEST_ANSWER_ENABLE)) { if ($points = CMA_Settings::getOption(CMA_Settings::OPTION_MP_REWARD_BEST_ANSWER_POINTS)) { if ($thread->isResolved() and $bestAnswer = CMA_Answer::getById($thread->getBestAnswerId())) { if ($userId = $bestAnswer->getAuthorId() and $user = get_userdata($userId)) { if ($this->chargeUserWallet($userId, $points)) { add_filter('cma_question_resolved_msg_success', array($this, 'questionResolvedMessageSuccess')); add_filter('cma_question_mark_best_answer_msg_success', array($this, 'questionResolvedMessageSuccess')); } } } } } }
protected static function _processAddCommentToThread() { global $wp_query; $post = $wp_query->post; $thread = CMA_AnswerThread::getInstance($post->ID); $content = $_POST['content']; $notify = !empty($_POST['thread_notify']); $resolved = !empty($_POST['thread_resolved']); $author_id = get_current_user_id(); $error = false; $messages = array(); try { $comment_id = $thread->addCommentToThread($content, $author_id, $notify, $resolved); } catch (Exception $e) { $messages = unserialize($e->getMessage()); $error = true; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { header('Content-type: application/json'); echo json_encode(array('success' => (int) (!$error), 'comment_id' => $comment_id, 'commentData' => CMA_AnswerThread::getCommentData($comment_id), 'message' => $messages)); exit; } else { if ($error) { foreach ((array) $messages as $message) { self::_addMessage(self::MESSAGE_ERROR, $message); } } else { do_action('cma_answer_post_after', $thread, CMA_Answer::getById($comment_id)); $autoApprove = CMA_AnswerThread::isAnswerAutoApproved(); if ($autoApprove) { $msg = __('Your answer has been succesfully added.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); wp_redirect(get_permalink($post->ID) . '/#comment-' . $comment_id, 303); } else { $msg = __('Thank you for your answer, it has been held for moderation.', 'cm-answers'); self::_addMessage(self::MESSAGE_SUCCESS, $msg); wp_redirect(get_permalink($post->ID), 303); } exit; } } }
public function removeNotBestAnswers() { $answers = CMA_Answer::getAnswersByThread($this->getId(), $approved = null); foreach ($answers as $answer) { if (!$answer->isBestAnswer()) { $answer->trash(); } } }
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; }
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(); } } }
public static function hookSetStatus($commentId, $status) { // Update rating cache of the thread if ($answer = CMA_Answer::getById($commentId) and $thread = $answer->getThread()) { $thread->updateRatingCache(); } }
protected static function processPrivateAnswer() { if (!empty($_GET['nonce']) and wp_verify_nonce($_GET['nonce'], self::ADMIN_PRIVATE_ANSWER)) { // Answer if (!empty($_GET['answer_id'])) { if ($answer = CMA_Answer::getById($_GET['answer_id'])) { $answer->setPrivate(!empty($_GET['private'])); } else { die('Unknown answer.'); } } if (!empty($_GET['backlink'])) { wp_safe_redirect(base64_decode(urldecode($_GET['backlink']))); exit; } } else { die('Invalid nonce.'); } }
public static function answersAction() { $limit = intval(self::_getParam('limit')); if (empty($limit)) { $limit = 5; } $currentPage = max(1, intval(self::_getParam('page'))); $totalPages = 1; $answers = array(); $authorSlug = ''; $ajax = false; if ($authorSlug = self::_getParam('author')) { if ($user = get_user_by('slug', $authorSlug)) { $authorSlug = $user->user_nicename; $answers = CMA_Answer::getByUser($user->ID, $approved = true, $limit, $currentPage, $onlyVisible = true); $totalPages = ceil(CMA_Answer::countForUser($user->ID, $approved = true, $limit, $currentPage, $onlyVisible = true) / $limit); } } $public = false; return array('content' => self::_loadView('answer/widget/answers-list', compact('answers', 'currentPage', 'totalPages', 'authorSlug', 'limit', 'ajax', 'public'))); }