Example #1
0
 protected function getLogDefaults()
 {
     $defaults = array(self::FIELD_LOG_TYPE => static::LOG_TYPE, self::FIELD_CREATED => Date('Y-m-d H:i:s'));
     // Add ip addr
     if (!empty($_SERVER['REMOTE_ADDR'])) {
         $defaults[self::FIELD_IP_ADDR] = $_SERVER['REMOTE_ADDR'];
     }
     // Add current user data
     if ($userId = CMA::getPostingUserId()) {
         $user = get_userdata($userId);
         $defaults[self::FIELD_USER_ID] = $userId;
     }
     return $defaults;
 }
 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;
 }
 public static function getCategoryAccessFilterSubquery($userId = null)
 {
     global $wpdb;
     if (is_null($userId)) {
         $userId = CMA::getPostingUserId();
     }
     if (empty($userId)) {
         $userId = 0;
     }
     if (user_can($userId, 'manage_options')) {
         // Admin can view all categories
         return $wpdb->prepare("SELECT tr.object_id\n\t    \t\tFROM {$wpdb->term_relationships} tr\n    \t\t\tINNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id\n\t    \t\tWHERE tt.taxonomy = %s", CMA_Category::TAXONOMY);
     } else {
         $sql = "SELECT tr.object_id\n\t    \t\tFROM {$wpdb->term_relationships} tr\n\t    \t\tJOIN {$wpdb->posts} p ON p.ID = tr.object_id\n\t    \t\tWHERE 1=1";
         if ($ids = CMA_Category::getVisibleTermTaxonomyIds($userId)) {
             // there are visible categories:
             $sql .= " AND tr.term_taxonomy_id IN (" . implode(',', $ids) . ")";
         } else {
             // there is no visible categories so reject all ids:
             $sql .= " AND 1=0 ";
         }
         if (CMA_Settings::getOption(CMA_Settings::OPTION_RESTRICT_UNANSWERED_QUESTIONS_TO_EXPERTS)) {
             $sql .= " AND (p.comment_count > 0";
             // question is unanswered
             if ($ids = CMA_Category::getExpertsTermTaxonomyIds($userId)) {
                 // or I'm the expert in question's category
                 $sql .= " OR tr.term_taxonomy_id IN (" . implode(',', $ids) . ")";
             }
             $sql .= ")";
         }
         return $sql;
     }
 }
 public function isVisible($userId = null)
 {
     if (is_null($userId)) {
         $userId = CMA::getPostingUserId();
     }
     if (user_can($userId, 'manage_options')) {
         return true;
     }
     return $this->isApproved() and $this->getThread()->isVisible($userId);
 }
 public static function getExpertsTermTaxonomyIds($userId = null)
 {
     global $wpdb;
     if (is_null($userId)) {
         $userId = CMA::getPostingUserId();
     }
     if (empty($userId)) {
         $userId = 0;
     }
     static $results = array();
     if (empty($results[$userId])) {
         $expertsConditions = $wpdb->prepare(" OR o.option_name = CONCAT(%s, tt.term_id, %s)", CMA_Category::OPTION_EXPERTS_PREFIX . '_', '_' . intval(get_current_user_id()));
         $sql = $wpdb->prepare("SELECT tt.term_taxonomy_id\n    \t\t\tFROM {$wpdb->term_taxonomy} tt\n    \t\t\tLEFT JOIN {$wpdb->options} o ON o.option_name LIKE CONCAT(%s, tt.term_id, '\\_%%')\n    \t\t\tWHERE tt.taxonomy = %s\n    \t\t\tAND (o.option_id IS NULL {$expertsConditions})\n    \t\t\t", self::OPTION_EXPERTS_PREFIX . '_', self::TAXONOMY);
         $results[$userId] = $wpdb->get_col($sql);
     }
     return $results[$userId];
 }
 public function canEdit($userId = null)
 {
     if (empty($userId)) {
         $userId = CMA::getPostingUserId();
     }
     if ($this->getAuthorId() == $userId) {
         $thread = CMA_Thread::getInstance($this->getThreadId());
         if (!$thread->isResolved() or CMA_Thread::canEditResolved()) {
             return CMA_Thread::checkEditMode(strtotime($this->getDate()));
         }
     }
     return false;
 }
 protected static function _processReportSpam()
 {
     if (self::$query->is_single()) {
         $post = self::$query->post;
         if (!empty($post)) {
             $response = array('success' => 0, 'message' => CMA::__('An error occurred.'));
             if (empty($_POST['nonce']) or !wp_verify_nonce($_POST['nonce'], 'cma_report_spam')) {
                 $response['message'] = CMA::__('Invalid nonce.');
             } else {
                 if (CMA_Settings::canReportSpam()) {
                     $thread = CMA_Thread::getInstance($post->ID);
                     $answerId = self::_getParam('answerId');
                     if ($userId = CMA::getPostingUserId()) {
                         $user = apply_filters('cma_filter_author', get_user_by('id', $userId), array('thread' => $thread));
                         $user = $user->display_name;
                     } else {
                         $user = CMA::__('Guest');
                     }
                     if ($answerId and $answer = CMA_Answer::getById($answerId)) {
                         $answer->markAsSpam(true);
                         $url = $answer->getPermalink();
                         $author = $answer->getAuthorLink(true);
                         $content = CMA_Thread::lightContent($answer->getContent());
                         $datetime = $answer->getDate();
                         $trashLink = get_admin_url(null, sprintf('comment.php?c=%d&action=trashcomment', $answerId));
                         $spamLink = get_admin_url(null, sprintf('comment.php?c=%d&action=spamcomment', $answerId));
                     } else {
                         $thread->markAsSpam(true);
                         $url = get_permalink($post->ID);
                         $author = $thread->getAuthorLink(true);
                         $content = $thread->getLightContent();
                         $datetime = $post->post_date;
                         $trashLink = get_admin_url(null, sprintf('post.php?post=%d&action=trash', $post->ID));
                         $spamLink = '--';
                     }
                     $replace = array('[blogname]' => get_bloginfo('name'), '[url]' => $url, '[title]' => strip_tags($thread->getTitle()), '[author]' => strip_tags($author), '[content]' => $content, '[user]' => strip_tags($user), '[datetime]' => $datetime, '[trash]' => $trashLink, '[spam]' => $spamLink);
                     $subject = strtr(CMA_Settings::getOption(CMA_Settings::OPTION_SPAM_REPORTING_EMAIL_SUBJECT), $replace);
                     $template = strtr(CMA_Settings::getOption(CMA_Settings::OPTION_SPAM_REPORTING_EMAIL_TEMPLATE), $replace);
                     $emails = explode(',', CMA_Settings::getOption(CMA_Settings::OPTION_SPAM_REPORTING_EMAIL_ADDR));
                     CMA_Email::send($emails, $subject, $template);
                     /* $headers = array();
                        foreach($emails as $email) {
                        	$email = trim($email);
                        	if (is_email($email)) {
                        		$headers[] = ' Bcc: '. $email;
                        	}
                        }
                        
                        if (!empty($headers)) wp_mail(null, $subject, $template, $headers); */
                     $response['success'] = 1;
                     $response['message'] = CMA_Labels::getLocalized('spam_report_sent');
                 }
             }
             header('Content-type: application/json');
             echo json_encode($response);
             exit;
         }
     }
 }