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);
     }
 }
 /**
  * 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'));
                     }
                 }
             }
         }
     }
 }
Esempio n. 4
0
 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 getBestAnswer()
 {
     if ($id = $this->getBestAnswerId()) {
         return CMA_Answer::getById($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;
 }
 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.');
     }
 }
 protected static function _processMarkBestAnswer()
 {
     if (self::$query->is_single()) {
         $post = self::$query->post;
         if (!empty($post)) {
             $response = array('success' => 0, 'message' => CMA::__('An error occurred.'));
             if (!is_user_logged_in()) {
                 $response['message'] = CMA::__('You have to be logged-in.');
             } else {
                 if (empty($_POST['nonce']) or !wp_verify_nonce($_POST['nonce'], 'cma_best_answer')) {
                     $response['message'] = CMA::__('Invalid nonce.');
                 } else {
                     $thread = CMA_Thread::getInstance($post->ID);
                     $answerId = self::_getParam('cma-answer-id');
                     if ($answer = CMA_Answer::getById($answerId)) {
                         if ($thread->getBestAnswerId() == $answerId) {
                             if ($thread->canUnmarkBestAnswer()) {
                                 $thread->unmarkBestAnswer();
                                 $response['message'] = CMA::__('The best answer has been unmarked.');
                                 $response['marked'] = 0;
                                 $response['success'] = 1;
                             }
                         } else {
                             if ($answer->canMarkBestAnswer()) {
                                 $thread->setBestAnswer($answerId);
                                 $removeOther = $_POST['remove-other'] === true || $_POST['remove-other'] === 'true';
                                 if ($removeOther and CMA_Settings::getOption(CMA_Settings::OPTION_BEST_ANSWER_REMOVE_OTHER)) {
                                     $thread->removeNotBestAnswers();
                                 }
                                 $response['message'] = apply_filters('cma_question_mark_best_answer_msg_success', CMA::__('The best answer has been marked.'), $thread);
                                 $response['marked'] = 1;
                                 $response['success'] = 1;
                             }
                         }
                     } else {
                         $response['message'] = CMA::__('Answer not found.');
                     }
                 }
             }
             header('Content-type: application/json');
             echo json_encode($response);
             exit;
         }
     }
 }