public static function getByUser($user_id, $approved = null, $limit = -1, $page = 1, $onlyVisible = true)
    {
        global $wpdb;
        if (!$user_id) {
            return array();
        }
        $limitPart = '';
        if ($limit > 1) {
            $limitPart = 'LIMIT ' . intval($limit) . ' OFFSET ' . intval($limit * ($page - 1));
        }
        $where = '';
        if (!is_null($approved)) {
            $where .= ' AND c.comment_approved = ' . intval($approved);
        }
        if ($onlyVisible) {
            if ($user_id != get_current_user_id()) {
                $where .= ' AND (cmpa.meta_value IS NULL OR cmpa.meta_value <> 1)';
            }
            if (CMA_Category::isAnyCategoryResticted()) {
                $where .= ' AND (c.comment_post_id IN (' . CMA_Thread::getCategoryAccessFilterSubquery() . ')
	    					OR c.comment_post_id NOT IN (' . CMA_Thread::getCategorizedThreadIdsSubquery() . '))';
            }
        }
        $query = $wpdb->prepare("SELECT c.* FROM {$wpdb->comments} c\n\t\t\t\tINNER JOIN {$wpdb->posts} p ON p.ID = c.comment_post_ID\n\t\t\t\tLEFT JOIN {$wpdb->commentmeta} cmpa ON c.comment_id = cmpa.comment_id AND cmpa.meta_key = %s\n\t\t\t\tWHERE user_id = %d\n\t\t\t\tAND c.comment_type = %s", self::META_PRIVATE, $user_id, self::COMMENT_TYPE) . " {$where} ORDER BY comment_id DESC {$limitPart}";
        $comments = $wpdb->get_results($query);
        $result = array();
        foreach ($comments as $comment) {
            $result[] = new self($comment);
        }
        return $result;
    }
 public function getLastActivity()
 {
     global $wpdb;
     if (CMA_Category::isAnyCategoryResticted()) {
         $accessFilter = ' AND (ID IN (' . CMA_Thread::getCategoryAccessFilterSubquery() . ')
 					OR ID NOT IN (' . CMA_Thread::getCategorizedThreadIdsSubquery() . ')
 					OR post_author = ' . intval(get_current_user_id()) . '
 				)';
     } else {
         $accessFilter = '';
     }
     return $wpdb->get_var($wpdb->prepare("SELECT MAX(p.post_modified) AS pm\n\t\t\tFROM {$wpdb->posts} p\n\t\t\tJOIN {$wpdb->term_relationships} tr ON tr.object_id = p.ID\n\t\t\tJOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = %s\n\t\t\tWHERE p.post_type = %s\n\t\t\tAND p.post_status = 'publish'\n\t\t\tAND tt.term_id = %d\n\t\t\t" . $accessFilter, CMA_Category::TAXONOMY, CMA_Thread::POST_TYPE, $this->getId()));
 }
 public static function categoryAccessFilter($val)
 {
     // Don't add a subquery if no category restricted (query time optimization):
     if (!CMA_Category::isAnyCategoryResticted()) {
         return $val;
     }
     $val .= ' AND (ID IN (' . CMA_Thread::getCategoryAccessFilterSubquery() . ')
 					OR ID NOT IN (' . CMA_Thread::getCategorizedThreadIdsSubquery() . ')
 					OR post_author = ' . intval(get_current_user_id()) . '
 				)';
     return $val;
 }