/** * Gets a list of recent comments formatted for display in a widget. * * The array of returned comments contains custom object instances with the * following properties that can be used by the widget: * * author_name - the display name of the comment's author * content - the content of the comment * meta - a string describing the comment's meta, constructed from * the meta formatting string passed to this method * permalink - the permalink URL for the comment * post_title - the name of the post on which the comment was made * * @param int $max_comments the maximum number of comments to return * @param int $max_comments_per_blog the most comments allowed per blog * @param string $meta_format the formatting string for the comment meta * @return array an array of formatted comments * * @since 0.2 */ public function get_comments_for_widget($max_comments, $max_comments_per_blog, $meta_format) { // Use cached values if possible $cached = $this->get_site_cache('widget'); if ($cached !== null) { return $cached; } $comments = array(); $raw_comments = $this->limit_sitewide_resources($this->get_sitewide_comments(), $max_comments, $max_comments_per_blog); $student_ids = ClassBlogs_Students::get_student_user_ids(); foreach ($raw_comments as $comment) { // Ignore the comment if it's not by a student if (!in_array($comment->user_id, $student_ids)) { continue; } // Create a string for the comment metadata $meta = ""; if ($meta_format) { $blog = sprintf('<a href="%s">%s</a>', ClassBlogs_NXTClass::get_blogaddress_by_id($comment->cb_sw_blog_id), ClassBlogs_NXTClass::get_blog_option($comment->cb_sw_blog_id, 'blogname')); $meta = ClassBlogs_Utils::format_user_string($meta_format, array('blog' => $blog, 'date' => mysql2date(get_option('date_format'), $comment->comment_date), 'time' => mysql2date(get_option('time_format'), $comment->comment_date)), 'cb-sitewide-comment'); } // Build the permalink to the comment using the post URL and an anchor $permalink = sprintf('%s#comment-%d', ClassBlogs_NXTClass::get_blog_permalink($comment->cb_sw_blog_id, $comment->comment_post_ID), $comment->comment_ID); $comments[] = (object) array('author_name' => $comment->comment_author, 'content' => $comment->comment_content, 'meta' => $meta, 'permalink' => $permalink, 'post_title' => $comment->post_title); } $this->set_site_cache('widget', $comments); return $comments; }
/** * Gets a list of recent posts formatted for display in a widget. * * The array of returned posts contains custom object instances with the * following properties that can be used by the widget: * * content - the content of the post * meta - a string describing the post's meta, constructed from * the meta formatting string passed to this method * permalink - the permalink URL for the post * title - the title of the post * * @param int $max_posts the maximum number of posts to return * @param int $max_posts_per_blog the most posts allowed per blog * @param string $meta_format the formatting string for the post meta * @return array an array of formatted posts * * @since 0.2 */ public function get_posts_for_widget($max_posts, $max_posts_per_blog, $meta_format) { // Use cache values if possible $cached = $this->get_site_cache('widget'); if ($cached !== null) { return $cached; } $posts = array(); $raw_posts = $this->limit_sitewide_resources($this->get_sitewide_posts(), $max_posts, $max_posts_per_blog); $student_ids = ClassBlogs_Students::get_student_user_ids(); foreach ($raw_posts as $post) { // Ignore the post if it's not by a student if (!in_array($post->post_author, $student_ids)) { continue; } // Create a string for the post metadata $meta = ""; if ($meta_format) { $user_data = get_userdata($post->post_author); $blog = sprintf('<a href="%s" class="cb-sitewide-post-blog">%s</a>', ClassBlogs_NXTClass::get_blogaddress_by_id($post->cb_sw_blog_id), ClassBlogs_NXTClass::get_blog_option($post->cb_sw_blog_id, 'blogname')); $meta = ClassBlogs_Utils::format_user_string($meta_format, array('author' => $user_data->display_name, 'blog' => $blog, 'date' => mysql2date(get_option('date_format'), $post->post_date), 'time' => mysql2date(get_option('time_format'), $post->post_date)), 'cb-sitewide-post'); } $posts[] = (object) array('content' => $post->post_content, 'meta' => $meta, 'permalink' => ClassBlogs_NXTClass::get_blog_permalink($post->cb_sw_blog_id, $post->ID), 'title' => $post->post_title); } $this->set_site_cache('widget', $posts); return $posts; }