/**
  * Returns a list of all sitewide comments.
  *
  * If desired, this function can be passed a boolean indicating whether or
  * not to return only approved comments, which is the action performed when
  * `$approved_only` is true.  By default, this value is true.
  *
  * @param  bool  $approved_only only return approved comments
  * @return array                all sitewide comments
  *
  * @since 0.1
  */
 public function get_sitewide_comments($approved_only = true)
 {
     // Set a proper cache key based upon which sort of comments are allowed
     $cache_key = 'comments_';
     $cache_key .= $approved_only ? 'approved' : 'all';
     // Return the cached comments if possible
     $cached = $this->get_site_cache($cache_key);
     if ($cached !== null) {
         return $cached;
     }
     // Filter out any unapproved comments if we're only allowing approved ones
     $approved_filter = "";
     if ($approved_only) {
         $approved_filter = "AND c.comment_approved = '1'";
     }
     global $nxtdb;
     $comments = $nxtdb->get_results("\n\t\t\tSELECT c.*, p.post_title\n\t\t\tFROM {$this->sw_tables->comments} AS c, {$this->sw_tables->posts} AS p\n\t\t\tWHERE p.ID = c.comment_post_ID AND c.cb_sw_blog_id = p.cb_sw_blog_id {$approved_filter}\n\t\t\tORDER BY c.comment_date DESC");
     // Even if all comments are allowed, don't display spam comments
     if (!$approved_only) {
         global $blog_id;
         $current_blog_id = $blog_id;
         $no_spam = array();
         foreach ($comments as $comment) {
             ClassBlogs_NXTClass::switch_to_blog($comment->cb_sw_blog_id);
             if (nxt_get_comment_status($comment->comment_ID) != 'spam') {
                 $no_spam[] = $comment;
             }
         }
         ClassBlogs_Utils::restore_blog($current_blog_id);
         $comments = $no_spam;
     }
     $this->set_site_cache($cache_key, $comments);
     return $comments;
 }
Example #2
0
 /**
  * Returns a random image from one of the blogs on the site.
  *
  * The returned image object, if not null, will have the following properties:
  *
  *     blog_id - the ID of the blog on which the image was posted
  *     title   - the image's title
  *     url     - the absolute URL to the image
  *
  * If the image is associated with a particular post, it will also have
  * the following properties on it:
  *
  *     post_id - the ID of the post that uses the image
  *     user_id - the ID of the user who created a post using the image
  *
  * @return mixed the random image object, or null if none can be found
  *
  * @since 0.1
  */
 public function get_random_image()
 {
     global $blog_id, $nxtdb;
     $current_blog_id = $blog_id;
     $image = null;
     $urls = array();
     // Search through every blog for a usable image.  If an image is found, build
     // the link to it and add a possible caption.
     $blogs = ClassBlogs_Utils::get_all_blog_ids();
     shuffle($blogs);
     foreach ($blogs as $blog) {
         ClassBlogs_NXTClass::switch_to_blog($blog);
         $images = $nxtdb->get_results("\n\t\t\t\tSELECT ID, post_title, GUID FROM {$nxtdb->posts}\n\t\t\t\tWHERE post_mime_type LIKE 'image/%%'\n\t\t\t\tAND post_content <> guid");
         if ($images) {
             $image = $images[array_rand($images)];
             $urls[] = $image->GUID;
             $info = nxt_get_attachment_image_src($image->ID);
             if (!empty($info)) {
                 $image = array('blog_id' => $blog, 'title' => $image->post_title, 'url' => $info[0]);
                 $urls[] = $info[0];
             }
             break;
         }
     }
     ClassBlogs_Utils::restore_blog($current_blog_id);
     // If we have a valid image, try to find the first post on which it was
     // used and add its ID to the image data
     if ($image) {
         $info = array();
         $post_id = null;
         $user_id = null;
         foreach ($urls as $url) {
             $post = $this->_find_first_post_to_use_image($image['blog_id'], $url);
             if (!empty($post)) {
                 break;
             }
         }
         if (!empty($post)) {
             $post_id = $post->ID;
             $user_id = $post->post_author;
         }
         $image['post_id'] = $post_id;
         $image['user_id'] = $user_id;
         $image = (object) $image;
     }
     return $image;
 }